@@ -70,260 +70,260 @@ |
||
70 | 70 | * @package OC\Mail |
71 | 71 | */ |
72 | 72 | class Mailer implements IMailer { |
73 | - /** @var \Swift_Mailer Cached mailer */ |
|
74 | - private $instance = null; |
|
75 | - /** @var IConfig */ |
|
76 | - private $config; |
|
77 | - /** @var ILogger */ |
|
78 | - private $logger; |
|
79 | - /** @var Defaults */ |
|
80 | - private $defaults; |
|
81 | - /** @var IURLGenerator */ |
|
82 | - private $urlGenerator; |
|
83 | - /** @var IL10N */ |
|
84 | - private $l10n; |
|
85 | - /** @var IEventDispatcher */ |
|
86 | - private $dispatcher; |
|
87 | - /** @var IFactory */ |
|
88 | - private $l10nFactory; |
|
89 | - |
|
90 | - /** |
|
91 | - * @param IConfig $config |
|
92 | - * @param ILogger $logger |
|
93 | - * @param Defaults $defaults |
|
94 | - * @param IURLGenerator $urlGenerator |
|
95 | - * @param IL10N $l10n |
|
96 | - * @param IEventDispatcher $dispatcher |
|
97 | - */ |
|
98 | - public function __construct(IConfig $config, |
|
99 | - ILogger $logger, |
|
100 | - Defaults $defaults, |
|
101 | - IURLGenerator $urlGenerator, |
|
102 | - IL10N $l10n, |
|
103 | - IEventDispatcher $dispatcher, |
|
104 | - IFactory $l10nFactory) { |
|
105 | - $this->config = $config; |
|
106 | - $this->logger = $logger; |
|
107 | - $this->defaults = $defaults; |
|
108 | - $this->urlGenerator = $urlGenerator; |
|
109 | - $this->l10n = $l10n; |
|
110 | - $this->dispatcher = $dispatcher; |
|
111 | - $this->l10nFactory = $l10nFactory; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Creates a new message object that can be passed to send() |
|
116 | - * |
|
117 | - * @return IMessage |
|
118 | - */ |
|
119 | - public function createMessage(): IMessage { |
|
120 | - $plainTextOnly = $this->config->getSystemValue('mail_send_plaintext_only', false); |
|
121 | - return new Message(new \Swift_Message(), $plainTextOnly); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string|null $data |
|
126 | - * @param string|null $filename |
|
127 | - * @param string|null $contentType |
|
128 | - * @return IAttachment |
|
129 | - * @since 13.0.0 |
|
130 | - */ |
|
131 | - public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment { |
|
132 | - return new Attachment(new \Swift_Attachment($data, $filename, $contentType)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @param string $path |
|
137 | - * @param string|null $contentType |
|
138 | - * @return IAttachment |
|
139 | - * @since 13.0.0 |
|
140 | - */ |
|
141 | - public function createAttachmentFromPath(string $path, $contentType = null): IAttachment { |
|
142 | - return new Attachment(\Swift_Attachment::fromPath($path, $contentType)); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Creates a new email template object |
|
147 | - * |
|
148 | - * @param string $emailId |
|
149 | - * @param array $data |
|
150 | - * @return IEMailTemplate |
|
151 | - * @since 12.0.0 |
|
152 | - */ |
|
153 | - public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate { |
|
154 | - $class = $this->config->getSystemValue('mail_template_class', ''); |
|
155 | - |
|
156 | - if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) { |
|
157 | - return new $class( |
|
158 | - $this->defaults, |
|
159 | - $this->urlGenerator, |
|
160 | - $this->l10nFactory, |
|
161 | - $emailId, |
|
162 | - $data |
|
163 | - ); |
|
164 | - } |
|
165 | - |
|
166 | - return new EMailTemplate( |
|
167 | - $this->defaults, |
|
168 | - $this->urlGenerator, |
|
169 | - $this->l10nFactory, |
|
170 | - $emailId, |
|
171 | - $data |
|
172 | - ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Send the specified message. Also sets the from address to the value defined in config.php |
|
177 | - * if no-one has been passed. |
|
178 | - * |
|
179 | - * @param IMessage|Message $message Message to send |
|
180 | - * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
181 | - * therefore should be considered |
|
182 | - * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
183 | - * has been supplied.) |
|
184 | - */ |
|
185 | - public function send(IMessage $message): array { |
|
186 | - $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
187 | - |
|
188 | - if (empty($message->getFrom())) { |
|
189 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress('no-reply') => $this->defaults->getName()]); |
|
190 | - } |
|
191 | - |
|
192 | - $failedRecipients = []; |
|
193 | - |
|
194 | - $mailer = $this->getInstance(); |
|
195 | - |
|
196 | - // Enable logger if debug mode is enabled |
|
197 | - if ($debugMode) { |
|
198 | - $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
199 | - $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
200 | - } |
|
201 | - |
|
202 | - |
|
203 | - $this->dispatcher->dispatchTyped(new BeforeMessageSent($message)); |
|
204 | - |
|
205 | - $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
206 | - |
|
207 | - // Debugging logging |
|
208 | - $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
209 | - $this->logger->debug($logMessage, ['app' => 'core']); |
|
210 | - if ($debugMode && isset($mailLogger)) { |
|
211 | - $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
212 | - } |
|
213 | - |
|
214 | - return $failedRecipients; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Checks if an e-mail address is valid |
|
219 | - * |
|
220 | - * @param string $email Email address to be validated |
|
221 | - * @return bool True if the mail address is valid, false otherwise |
|
222 | - */ |
|
223 | - public function validateMailAddress(string $email): bool { |
|
224 | - if ($email === '') { |
|
225 | - // Shortcut: empty addresses are never valid |
|
226 | - return false; |
|
227 | - } |
|
228 | - $validator = new EmailValidator(); |
|
229 | - $validation = new RFCValidation(); |
|
230 | - |
|
231 | - return $validator->isValid($this->convertEmail($email), $validation); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
236 | - * |
|
237 | - * FIXME: Remove this once SwiftMailer supports IDN |
|
238 | - * |
|
239 | - * @param string $email |
|
240 | - * @return string Converted mail address if `idn_to_ascii` exists |
|
241 | - */ |
|
242 | - protected function convertEmail(string $email): string { |
|
243 | - if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46') || strpos($email, '@') === false) { |
|
244 | - return $email; |
|
245 | - } |
|
246 | - |
|
247 | - list($name, $domain) = explode('@', $email, 2); |
|
248 | - $domain = idn_to_ascii($domain, 0,INTL_IDNA_VARIANT_UTS46); |
|
249 | - return $name.'@'.$domain; |
|
250 | - } |
|
251 | - |
|
252 | - protected function getInstance(): \Swift_Mailer { |
|
253 | - if (!is_null($this->instance)) { |
|
254 | - return $this->instance; |
|
255 | - } |
|
256 | - |
|
257 | - $transport = null; |
|
258 | - |
|
259 | - switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) { |
|
260 | - case 'sendmail': |
|
261 | - $transport = $this->getSendMailInstance(); |
|
262 | - break; |
|
263 | - case 'smtp': |
|
264 | - default: |
|
265 | - $transport = $this->getSmtpInstance(); |
|
266 | - break; |
|
267 | - } |
|
268 | - |
|
269 | - return new \Swift_Mailer($transport); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Returns the SMTP transport |
|
274 | - * |
|
275 | - * @return \Swift_SmtpTransport |
|
276 | - */ |
|
277 | - protected function getSmtpInstance(): \Swift_SmtpTransport { |
|
278 | - $transport = new \Swift_SmtpTransport(); |
|
279 | - $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
280 | - $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
281 | - $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
282 | - if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
283 | - $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
284 | - $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
285 | - $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
286 | - } |
|
287 | - $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
288 | - if (!empty($smtpSecurity)) { |
|
289 | - $transport->setEncryption($smtpSecurity); |
|
290 | - } |
|
291 | - $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', []); |
|
292 | - if (is_array($streamingOptions) && !empty($streamingOptions)) { |
|
293 | - $transport->setStreamOptions($streamingOptions); |
|
294 | - } |
|
295 | - |
|
296 | - return $transport; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Returns the sendmail transport |
|
301 | - * |
|
302 | - * @return \Swift_SendmailTransport |
|
303 | - */ |
|
304 | - protected function getSendMailInstance(): \Swift_SendmailTransport { |
|
305 | - switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) { |
|
306 | - case 'qmail': |
|
307 | - $binaryPath = '/var/qmail/bin/sendmail'; |
|
308 | - break; |
|
309 | - default: |
|
310 | - $sendmail = \OC_Helper::findBinaryPath('sendmail'); |
|
311 | - if ($sendmail === null) { |
|
312 | - $sendmail = '/usr/sbin/sendmail'; |
|
313 | - } |
|
314 | - $binaryPath = $sendmail; |
|
315 | - break; |
|
316 | - } |
|
317 | - |
|
318 | - switch ($this->config->getSystemValue('mail_sendmailmode', 'smtp')) { |
|
319 | - case 'pipe': |
|
320 | - $binaryParam = ' -t'; |
|
321 | - break; |
|
322 | - default: |
|
323 | - $binaryParam = ' -bs'; |
|
324 | - break; |
|
325 | - } |
|
326 | - |
|
327 | - return new \Swift_SendmailTransport($binaryPath . $binaryParam); |
|
328 | - } |
|
73 | + /** @var \Swift_Mailer Cached mailer */ |
|
74 | + private $instance = null; |
|
75 | + /** @var IConfig */ |
|
76 | + private $config; |
|
77 | + /** @var ILogger */ |
|
78 | + private $logger; |
|
79 | + /** @var Defaults */ |
|
80 | + private $defaults; |
|
81 | + /** @var IURLGenerator */ |
|
82 | + private $urlGenerator; |
|
83 | + /** @var IL10N */ |
|
84 | + private $l10n; |
|
85 | + /** @var IEventDispatcher */ |
|
86 | + private $dispatcher; |
|
87 | + /** @var IFactory */ |
|
88 | + private $l10nFactory; |
|
89 | + |
|
90 | + /** |
|
91 | + * @param IConfig $config |
|
92 | + * @param ILogger $logger |
|
93 | + * @param Defaults $defaults |
|
94 | + * @param IURLGenerator $urlGenerator |
|
95 | + * @param IL10N $l10n |
|
96 | + * @param IEventDispatcher $dispatcher |
|
97 | + */ |
|
98 | + public function __construct(IConfig $config, |
|
99 | + ILogger $logger, |
|
100 | + Defaults $defaults, |
|
101 | + IURLGenerator $urlGenerator, |
|
102 | + IL10N $l10n, |
|
103 | + IEventDispatcher $dispatcher, |
|
104 | + IFactory $l10nFactory) { |
|
105 | + $this->config = $config; |
|
106 | + $this->logger = $logger; |
|
107 | + $this->defaults = $defaults; |
|
108 | + $this->urlGenerator = $urlGenerator; |
|
109 | + $this->l10n = $l10n; |
|
110 | + $this->dispatcher = $dispatcher; |
|
111 | + $this->l10nFactory = $l10nFactory; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Creates a new message object that can be passed to send() |
|
116 | + * |
|
117 | + * @return IMessage |
|
118 | + */ |
|
119 | + public function createMessage(): IMessage { |
|
120 | + $plainTextOnly = $this->config->getSystemValue('mail_send_plaintext_only', false); |
|
121 | + return new Message(new \Swift_Message(), $plainTextOnly); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string|null $data |
|
126 | + * @param string|null $filename |
|
127 | + * @param string|null $contentType |
|
128 | + * @return IAttachment |
|
129 | + * @since 13.0.0 |
|
130 | + */ |
|
131 | + public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment { |
|
132 | + return new Attachment(new \Swift_Attachment($data, $filename, $contentType)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @param string $path |
|
137 | + * @param string|null $contentType |
|
138 | + * @return IAttachment |
|
139 | + * @since 13.0.0 |
|
140 | + */ |
|
141 | + public function createAttachmentFromPath(string $path, $contentType = null): IAttachment { |
|
142 | + return new Attachment(\Swift_Attachment::fromPath($path, $contentType)); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Creates a new email template object |
|
147 | + * |
|
148 | + * @param string $emailId |
|
149 | + * @param array $data |
|
150 | + * @return IEMailTemplate |
|
151 | + * @since 12.0.0 |
|
152 | + */ |
|
153 | + public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate { |
|
154 | + $class = $this->config->getSystemValue('mail_template_class', ''); |
|
155 | + |
|
156 | + if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) { |
|
157 | + return new $class( |
|
158 | + $this->defaults, |
|
159 | + $this->urlGenerator, |
|
160 | + $this->l10nFactory, |
|
161 | + $emailId, |
|
162 | + $data |
|
163 | + ); |
|
164 | + } |
|
165 | + |
|
166 | + return new EMailTemplate( |
|
167 | + $this->defaults, |
|
168 | + $this->urlGenerator, |
|
169 | + $this->l10nFactory, |
|
170 | + $emailId, |
|
171 | + $data |
|
172 | + ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Send the specified message. Also sets the from address to the value defined in config.php |
|
177 | + * if no-one has been passed. |
|
178 | + * |
|
179 | + * @param IMessage|Message $message Message to send |
|
180 | + * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
181 | + * therefore should be considered |
|
182 | + * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
183 | + * has been supplied.) |
|
184 | + */ |
|
185 | + public function send(IMessage $message): array { |
|
186 | + $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
187 | + |
|
188 | + if (empty($message->getFrom())) { |
|
189 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress('no-reply') => $this->defaults->getName()]); |
|
190 | + } |
|
191 | + |
|
192 | + $failedRecipients = []; |
|
193 | + |
|
194 | + $mailer = $this->getInstance(); |
|
195 | + |
|
196 | + // Enable logger if debug mode is enabled |
|
197 | + if ($debugMode) { |
|
198 | + $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
199 | + $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
200 | + } |
|
201 | + |
|
202 | + |
|
203 | + $this->dispatcher->dispatchTyped(new BeforeMessageSent($message)); |
|
204 | + |
|
205 | + $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
206 | + |
|
207 | + // Debugging logging |
|
208 | + $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
209 | + $this->logger->debug($logMessage, ['app' => 'core']); |
|
210 | + if ($debugMode && isset($mailLogger)) { |
|
211 | + $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
212 | + } |
|
213 | + |
|
214 | + return $failedRecipients; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Checks if an e-mail address is valid |
|
219 | + * |
|
220 | + * @param string $email Email address to be validated |
|
221 | + * @return bool True if the mail address is valid, false otherwise |
|
222 | + */ |
|
223 | + public function validateMailAddress(string $email): bool { |
|
224 | + if ($email === '') { |
|
225 | + // Shortcut: empty addresses are never valid |
|
226 | + return false; |
|
227 | + } |
|
228 | + $validator = new EmailValidator(); |
|
229 | + $validation = new RFCValidation(); |
|
230 | + |
|
231 | + return $validator->isValid($this->convertEmail($email), $validation); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
236 | + * |
|
237 | + * FIXME: Remove this once SwiftMailer supports IDN |
|
238 | + * |
|
239 | + * @param string $email |
|
240 | + * @return string Converted mail address if `idn_to_ascii` exists |
|
241 | + */ |
|
242 | + protected function convertEmail(string $email): string { |
|
243 | + if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46') || strpos($email, '@') === false) { |
|
244 | + return $email; |
|
245 | + } |
|
246 | + |
|
247 | + list($name, $domain) = explode('@', $email, 2); |
|
248 | + $domain = idn_to_ascii($domain, 0,INTL_IDNA_VARIANT_UTS46); |
|
249 | + return $name.'@'.$domain; |
|
250 | + } |
|
251 | + |
|
252 | + protected function getInstance(): \Swift_Mailer { |
|
253 | + if (!is_null($this->instance)) { |
|
254 | + return $this->instance; |
|
255 | + } |
|
256 | + |
|
257 | + $transport = null; |
|
258 | + |
|
259 | + switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) { |
|
260 | + case 'sendmail': |
|
261 | + $transport = $this->getSendMailInstance(); |
|
262 | + break; |
|
263 | + case 'smtp': |
|
264 | + default: |
|
265 | + $transport = $this->getSmtpInstance(); |
|
266 | + break; |
|
267 | + } |
|
268 | + |
|
269 | + return new \Swift_Mailer($transport); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Returns the SMTP transport |
|
274 | + * |
|
275 | + * @return \Swift_SmtpTransport |
|
276 | + */ |
|
277 | + protected function getSmtpInstance(): \Swift_SmtpTransport { |
|
278 | + $transport = new \Swift_SmtpTransport(); |
|
279 | + $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
280 | + $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
281 | + $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
282 | + if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
283 | + $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
284 | + $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
285 | + $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
286 | + } |
|
287 | + $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
288 | + if (!empty($smtpSecurity)) { |
|
289 | + $transport->setEncryption($smtpSecurity); |
|
290 | + } |
|
291 | + $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', []); |
|
292 | + if (is_array($streamingOptions) && !empty($streamingOptions)) { |
|
293 | + $transport->setStreamOptions($streamingOptions); |
|
294 | + } |
|
295 | + |
|
296 | + return $transport; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Returns the sendmail transport |
|
301 | + * |
|
302 | + * @return \Swift_SendmailTransport |
|
303 | + */ |
|
304 | + protected function getSendMailInstance(): \Swift_SendmailTransport { |
|
305 | + switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) { |
|
306 | + case 'qmail': |
|
307 | + $binaryPath = '/var/qmail/bin/sendmail'; |
|
308 | + break; |
|
309 | + default: |
|
310 | + $sendmail = \OC_Helper::findBinaryPath('sendmail'); |
|
311 | + if ($sendmail === null) { |
|
312 | + $sendmail = '/usr/sbin/sendmail'; |
|
313 | + } |
|
314 | + $binaryPath = $sendmail; |
|
315 | + break; |
|
316 | + } |
|
317 | + |
|
318 | + switch ($this->config->getSystemValue('mail_sendmailmode', 'smtp')) { |
|
319 | + case 'pipe': |
|
320 | + $binaryParam = ' -t'; |
|
321 | + break; |
|
322 | + default: |
|
323 | + $binaryParam = ' -bs'; |
|
324 | + break; |
|
325 | + } |
|
326 | + |
|
327 | + return new \Swift_SendmailTransport($binaryPath . $binaryParam); |
|
328 | + } |
|
329 | 329 | } |