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