1 | <?php |
||
28 | abstract class BaseMailer extends Component implements MailerInterface |
||
29 | { |
||
30 | /** |
||
31 | * @event MailEvent an event raised right before send. |
||
32 | * You may set [[MailEvent::isValid]] to be false to cancel the send. |
||
33 | */ |
||
34 | const EVENT_BEFORE_SEND = 'beforeSend'; |
||
35 | /** |
||
36 | * @event MailEvent an event raised right after send. |
||
37 | */ |
||
38 | const EVENT_AFTER_SEND = 'afterSend'; |
||
39 | |||
40 | /** |
||
41 | * @var array the configuration that should be applied to any newly created |
||
42 | * email message instance by [[createMessage()]] or [[compose()]]. Any valid property defined |
||
43 | * by [[MessageInterface]] can be configured, such as `from`, `to`, `subject`, `textBody`, `htmlBody`, etc. |
||
44 | * |
||
45 | * For example: |
||
46 | * |
||
47 | * ```php |
||
48 | * [ |
||
49 | * 'charset' => 'UTF-8', |
||
50 | * 'from' => '[email protected]', |
||
51 | * 'bcc' => '[email protected]', |
||
52 | * ] |
||
53 | * ``` |
||
54 | */ |
||
55 | public $messageConfig = []; |
||
56 | /** |
||
57 | * @var string the default class name of the new message instances created by [[createMessage()]] |
||
58 | */ |
||
59 | public $messageClass = BaseMessage::class; |
||
60 | /** |
||
61 | * @var bool whether to save email messages as files under [[fileTransportPath]] instead of sending them |
||
62 | * to the actual recipients. This is usually used during development for debugging purpose. |
||
63 | * @see fileTransportPath |
||
64 | */ |
||
65 | public $useFileTransport = false; |
||
66 | /** |
||
67 | * @var string the directory where the email messages are saved when [[useFileTransport]] is true. |
||
68 | */ |
||
69 | public $fileTransportPath = '@runtime/mail'; |
||
70 | /** |
||
71 | * @var callable a PHP callback that will be called by [[send()]] when [[useFileTransport]] is true. |
||
72 | * The callback should return a file name which will be used to save the email message. |
||
73 | * If not set, the file name will be generated based on the current timestamp. |
||
74 | * |
||
75 | * The signature of the callback is: |
||
76 | * |
||
77 | * ```php |
||
78 | * function ($mailer, $message) |
||
79 | * ``` |
||
80 | */ |
||
81 | public $fileTransportCallback; |
||
82 | |||
83 | /** |
||
84 | * @var Composer|array|string|callable message composer. |
||
85 | * @since 2.1 |
||
86 | */ |
||
87 | private $_composer; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @return Composer message composer instance. |
||
92 | * @since 2.1 |
||
93 | */ |
||
94 | 1 | public function getComposer() |
|
104 | |||
105 | /** |
||
106 | * @param Composer|array|string|callable $composer message composer instance or DI compatible configuration. |
||
107 | * @since 2.1 |
||
108 | */ |
||
109 | 5 | public function setComposer($composer) |
|
113 | |||
114 | /** |
||
115 | * Creates a new message instance and optionally composes its body content via view rendering. |
||
116 | * |
||
117 | * @param string|array|null $view the view to be used for rendering the message body. This can be: |
||
118 | * |
||
119 | * - a string, which represents the view name or path alias for rendering the HTML body of the email. |
||
120 | * In this case, the text body will be generated by applying `strip_tags()` to the HTML body. |
||
121 | * - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name or path alias |
||
122 | * for rendering the HTML body, while 'text' element is for rendering the text body. For example, |
||
123 | * `['html' => 'contact-html', 'text' => 'contact-text']`. |
||
124 | * - null, meaning the message instance will be returned without body content. |
||
125 | * |
||
126 | * The view to be rendered can be specified in one of the following formats: |
||
127 | * |
||
128 | * - path alias (e.g. "@app/mail/contact"); |
||
129 | * - a relative view name (e.g. "contact") located under [[viewPath]]. |
||
130 | * |
||
131 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
132 | * @return MessageInterface message instance. |
||
133 | */ |
||
134 | 6 | public function compose($view = null, array $params = []) |
|
145 | |||
146 | /** |
||
147 | * Creates a new message instance. |
||
148 | * The newly created instance will be initialized with the configuration specified by [[messageConfig]]. |
||
149 | * If the configuration does not specify a 'class', the [[messageClass]] will be used as the class |
||
150 | * of the new message instance. |
||
151 | * @return MessageInterface message instance. |
||
152 | */ |
||
153 | 6 | protected function createMessage() |
|
162 | |||
163 | /** |
||
164 | * Sends the given email message. |
||
165 | * This method will log a message about the email being sent. |
||
166 | * If [[useFileTransport]] is true, it will save the email as a file under [[fileTransportPath]]. |
||
167 | * Otherwise, it will call [[sendMessage()]] to send the email to its recipient(s). |
||
168 | * Child classes should implement [[sendMessage()]] with the actual email sending logic. |
||
169 | * @param MessageInterface $message email message instance to be sent |
||
170 | * @return bool whether the message has been sent successfully |
||
171 | */ |
||
172 | 3 | public function send($message) |
|
193 | |||
194 | /** |
||
195 | * Sends multiple messages at once. |
||
196 | * |
||
197 | * The default implementation simply calls [[send()]] multiple times. |
||
198 | * Child classes may override this method to implement more efficient way of |
||
199 | * sending multiple messages. |
||
200 | * |
||
201 | * @param array $messages list of email messages, which should be sent. |
||
202 | * @return int number of messages that are successfully sent. |
||
203 | */ |
||
204 | public function sendMultiple(array $messages) |
||
215 | |||
216 | /** |
||
217 | * Renders the specified view with optional parameters and layout. |
||
218 | * The view will be rendered using the [[view]] component. |
||
219 | * @param string $view the view name or the [path alias](guide:concept-aliases) of the view file. |
||
220 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
221 | * @param string|bool $layout layout view name or [path alias](guide:concept-aliases). If false, no layout will be applied. |
||
222 | * @return string the rendering result. |
||
223 | */ |
||
224 | public function render($view, $params = [], $layout = false) |
||
233 | |||
234 | /** |
||
235 | * Sends the specified message. |
||
236 | * This method should be implemented by child classes with the actual email sending logic. |
||
237 | * @param MessageInterface $message the message to be sent |
||
238 | * @return bool whether the message is sent successfully |
||
239 | */ |
||
240 | abstract protected function sendMessage($message); |
||
241 | |||
242 | /** |
||
243 | * Saves the message as a file under [[fileTransportPath]]. |
||
244 | * @param MessageInterface $message |
||
245 | * @return bool whether the message is saved successfully |
||
246 | */ |
||
247 | 1 | protected function saveMessage($message) |
|
262 | |||
263 | /** |
||
264 | * @return string the file name for saving the message when [[useFileTransport]] is true. |
||
265 | */ |
||
266 | public function generateMessageFileName() |
||
272 | |||
273 | /** |
||
274 | * This method is invoked right before mail send. |
||
275 | * You may override this method to do last-minute preparation for the message. |
||
276 | * If you override this method, please make sure you call the parent implementation first. |
||
277 | * @param MessageInterface $message |
||
278 | * @return bool whether to continue sending an email. |
||
279 | */ |
||
280 | 2 | public function beforeSend($message) |
|
287 | |||
288 | /** |
||
289 | * This method is invoked right after mail was send. |
||
290 | * You may override this method to do some postprocessing or logging based on mail send status. |
||
291 | * If you override this method, please make sure you call the parent implementation first. |
||
292 | * @param MessageInterface $message |
||
293 | * @param bool $isSuccessful |
||
294 | */ |
||
295 | 2 | public function afterSend($message, $isSuccessful) |
|
300 | } |
||
301 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: