@@ -55,241 +55,241 @@ |
||
| 55 | 55 | * @package OC\Mail |
| 56 | 56 | */ |
| 57 | 57 | class Mailer implements IMailer { |
| 58 | - /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
|
| 59 | - private $instance = null; |
|
| 60 | - /** @var IConfig */ |
|
| 61 | - private $config; |
|
| 62 | - /** @var ILogger */ |
|
| 63 | - private $logger; |
|
| 64 | - /** @var Defaults */ |
|
| 65 | - private $defaults; |
|
| 66 | - /** @var IURLGenerator */ |
|
| 67 | - private $urlGenerator; |
|
| 68 | - /** @var IL10N */ |
|
| 69 | - private $l10n; |
|
| 58 | + /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
|
| 59 | + private $instance = null; |
|
| 60 | + /** @var IConfig */ |
|
| 61 | + private $config; |
|
| 62 | + /** @var ILogger */ |
|
| 63 | + private $logger; |
|
| 64 | + /** @var Defaults */ |
|
| 65 | + private $defaults; |
|
| 66 | + /** @var IURLGenerator */ |
|
| 67 | + private $urlGenerator; |
|
| 68 | + /** @var IL10N */ |
|
| 69 | + private $l10n; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param IConfig $config |
|
| 73 | - * @param ILogger $logger |
|
| 74 | - * @param Defaults $defaults |
|
| 75 | - * @param IURLGenerator $urlGenerator |
|
| 76 | - * @param IL10N $l10n |
|
| 77 | - */ |
|
| 78 | - public function __construct(IConfig $config, |
|
| 79 | - ILogger $logger, |
|
| 80 | - Defaults $defaults, |
|
| 81 | - IURLGenerator $urlGenerator, |
|
| 82 | - IL10N $l10n) { |
|
| 83 | - $this->config = $config; |
|
| 84 | - $this->logger = $logger; |
|
| 85 | - $this->defaults = $defaults; |
|
| 86 | - $this->urlGenerator = $urlGenerator; |
|
| 87 | - $this->l10n = $l10n; |
|
| 88 | - } |
|
| 71 | + /** |
|
| 72 | + * @param IConfig $config |
|
| 73 | + * @param ILogger $logger |
|
| 74 | + * @param Defaults $defaults |
|
| 75 | + * @param IURLGenerator $urlGenerator |
|
| 76 | + * @param IL10N $l10n |
|
| 77 | + */ |
|
| 78 | + public function __construct(IConfig $config, |
|
| 79 | + ILogger $logger, |
|
| 80 | + Defaults $defaults, |
|
| 81 | + IURLGenerator $urlGenerator, |
|
| 82 | + IL10N $l10n) { |
|
| 83 | + $this->config = $config; |
|
| 84 | + $this->logger = $logger; |
|
| 85 | + $this->defaults = $defaults; |
|
| 86 | + $this->urlGenerator = $urlGenerator; |
|
| 87 | + $this->l10n = $l10n; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Creates a new message object that can be passed to send() |
|
| 92 | - * |
|
| 93 | - * @return IMessage |
|
| 94 | - */ |
|
| 95 | - public function createMessage(): IMessage { |
|
| 96 | - return new Message(new \Swift_Message()); |
|
| 97 | - } |
|
| 90 | + /** |
|
| 91 | + * Creates a new message object that can be passed to send() |
|
| 92 | + * |
|
| 93 | + * @return IMessage |
|
| 94 | + */ |
|
| 95 | + public function createMessage(): IMessage { |
|
| 96 | + return new Message(new \Swift_Message()); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * @param string|null $data |
|
| 101 | - * @param string|null $filename |
|
| 102 | - * @param string|null $contentType |
|
| 103 | - * @return IAttachment |
|
| 104 | - * @since 13.0.0 |
|
| 105 | - */ |
|
| 106 | - public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment { |
|
| 107 | - return new Attachment(\Swift_Attachment::newInstance($data, $filename, $contentType)); |
|
| 108 | - } |
|
| 99 | + /** |
|
| 100 | + * @param string|null $data |
|
| 101 | + * @param string|null $filename |
|
| 102 | + * @param string|null $contentType |
|
| 103 | + * @return IAttachment |
|
| 104 | + * @since 13.0.0 |
|
| 105 | + */ |
|
| 106 | + public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment { |
|
| 107 | + return new Attachment(\Swift_Attachment::newInstance($data, $filename, $contentType)); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * @param string $path |
|
| 112 | - * @param string|null $contentType |
|
| 113 | - * @return IAttachment |
|
| 114 | - * @since 13.0.0 |
|
| 115 | - */ |
|
| 116 | - public function createAttachmentFromPath(string $path, $contentType = null): IAttachment { |
|
| 117 | - return new Attachment(\Swift_Attachment::fromPath($path, $contentType)); |
|
| 118 | - } |
|
| 110 | + /** |
|
| 111 | + * @param string $path |
|
| 112 | + * @param string|null $contentType |
|
| 113 | + * @return IAttachment |
|
| 114 | + * @since 13.0.0 |
|
| 115 | + */ |
|
| 116 | + public function createAttachmentFromPath(string $path, $contentType = null): IAttachment { |
|
| 117 | + return new Attachment(\Swift_Attachment::fromPath($path, $contentType)); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Creates a new email template object |
|
| 122 | - * |
|
| 123 | - * @param string $emailId |
|
| 124 | - * @param array $data |
|
| 125 | - * @return IEMailTemplate |
|
| 126 | - * @since 12.0.0 |
|
| 127 | - */ |
|
| 128 | - public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate { |
|
| 129 | - $class = $this->config->getSystemValue('mail_template_class', ''); |
|
| 120 | + /** |
|
| 121 | + * Creates a new email template object |
|
| 122 | + * |
|
| 123 | + * @param string $emailId |
|
| 124 | + * @param array $data |
|
| 125 | + * @return IEMailTemplate |
|
| 126 | + * @since 12.0.0 |
|
| 127 | + */ |
|
| 128 | + public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate { |
|
| 129 | + $class = $this->config->getSystemValue('mail_template_class', ''); |
|
| 130 | 130 | |
| 131 | - if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) { |
|
| 132 | - return new $class( |
|
| 133 | - $this->defaults, |
|
| 134 | - $this->urlGenerator, |
|
| 135 | - $this->l10n, |
|
| 136 | - $emailId, |
|
| 137 | - $data |
|
| 138 | - ); |
|
| 139 | - } |
|
| 131 | + if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) { |
|
| 132 | + return new $class( |
|
| 133 | + $this->defaults, |
|
| 134 | + $this->urlGenerator, |
|
| 135 | + $this->l10n, |
|
| 136 | + $emailId, |
|
| 137 | + $data |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - return new EMailTemplate( |
|
| 142 | - $this->defaults, |
|
| 143 | - $this->urlGenerator, |
|
| 144 | - $this->l10n, |
|
| 145 | - $emailId, |
|
| 146 | - $data |
|
| 147 | - ); |
|
| 148 | - } |
|
| 141 | + return new EMailTemplate( |
|
| 142 | + $this->defaults, |
|
| 143 | + $this->urlGenerator, |
|
| 144 | + $this->l10n, |
|
| 145 | + $emailId, |
|
| 146 | + $data |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Send the specified message. Also sets the from address to the value defined in config.php |
|
| 152 | - * if no-one has been passed. |
|
| 153 | - * |
|
| 154 | - * @param IMessage|Message $message Message to send |
|
| 155 | - * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
| 156 | - * therefore should be considered |
|
| 157 | - * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
| 158 | - * has been supplied.) |
|
| 159 | - */ |
|
| 160 | - public function send(IMessage $message): array { |
|
| 161 | - $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
| 150 | + /** |
|
| 151 | + * Send the specified message. Also sets the from address to the value defined in config.php |
|
| 152 | + * if no-one has been passed. |
|
| 153 | + * |
|
| 154 | + * @param IMessage|Message $message Message to send |
|
| 155 | + * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
| 156 | + * therefore should be considered |
|
| 157 | + * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
| 158 | + * has been supplied.) |
|
| 159 | + */ |
|
| 160 | + public function send(IMessage $message): array { |
|
| 161 | + $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
| 162 | 162 | |
| 163 | - if (empty($message->getFrom())) { |
|
| 164 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]); |
|
| 165 | - } |
|
| 163 | + if (empty($message->getFrom())) { |
|
| 164 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - $failedRecipients = []; |
|
| 167 | + $failedRecipients = []; |
|
| 168 | 168 | |
| 169 | - $mailer = $this->getInstance(); |
|
| 169 | + $mailer = $this->getInstance(); |
|
| 170 | 170 | |
| 171 | - // Enable logger if debug mode is enabled |
|
| 172 | - if($debugMode) { |
|
| 173 | - $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
| 174 | - $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
| 175 | - } |
|
| 171 | + // Enable logger if debug mode is enabled |
|
| 172 | + if($debugMode) { |
|
| 173 | + $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
| 174 | + $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
| 177 | + $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
| 178 | 178 | |
| 179 | - // Debugging logging |
|
| 180 | - $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
| 181 | - $this->logger->debug($logMessage, ['app' => 'core']); |
|
| 182 | - if($debugMode && isset($mailLogger)) { |
|
| 183 | - $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
| 184 | - } |
|
| 179 | + // Debugging logging |
|
| 180 | + $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
| 181 | + $this->logger->debug($logMessage, ['app' => 'core']); |
|
| 182 | + if($debugMode && isset($mailLogger)) { |
|
| 183 | + $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - return $failedRecipients; |
|
| 187 | - } |
|
| 186 | + return $failedRecipients; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Checks if an e-mail address is valid |
|
| 191 | - * |
|
| 192 | - * @param string $email Email address to be validated |
|
| 193 | - * @return bool True if the mail address is valid, false otherwise |
|
| 194 | - */ |
|
| 195 | - public function validateMailAddress(string $email): bool { |
|
| 196 | - return \Swift_Validate::email($this->convertEmail($email)); |
|
| 197 | - } |
|
| 189 | + /** |
|
| 190 | + * Checks if an e-mail address is valid |
|
| 191 | + * |
|
| 192 | + * @param string $email Email address to be validated |
|
| 193 | + * @return bool True if the mail address is valid, false otherwise |
|
| 194 | + */ |
|
| 195 | + public function validateMailAddress(string $email): bool { |
|
| 196 | + return \Swift_Validate::email($this->convertEmail($email)); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
| 201 | - * |
|
| 202 | - * FIXME: Remove this once SwiftMailer supports IDN |
|
| 203 | - * |
|
| 204 | - * @param string $email |
|
| 205 | - * @return string Converted mail address if `idn_to_ascii` exists |
|
| 206 | - */ |
|
| 207 | - protected function convertEmail(string $email): string { |
|
| 208 | - if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46') || strpos($email, '@') === false) { |
|
| 209 | - return $email; |
|
| 210 | - } |
|
| 199 | + /** |
|
| 200 | + * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
| 201 | + * |
|
| 202 | + * FIXME: Remove this once SwiftMailer supports IDN |
|
| 203 | + * |
|
| 204 | + * @param string $email |
|
| 205 | + * @return string Converted mail address if `idn_to_ascii` exists |
|
| 206 | + */ |
|
| 207 | + protected function convertEmail(string $email): string { |
|
| 208 | + if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46') || strpos($email, '@') === false) { |
|
| 209 | + return $email; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - list($name, $domain) = explode('@', $email, 2); |
|
| 213 | - $domain = idn_to_ascii($domain, 0,INTL_IDNA_VARIANT_UTS46); |
|
| 214 | - return $name.'@'.$domain; |
|
| 215 | - } |
|
| 212 | + list($name, $domain) = explode('@', $email, 2); |
|
| 213 | + $domain = idn_to_ascii($domain, 0,INTL_IDNA_VARIANT_UTS46); |
|
| 214 | + return $name.'@'.$domain; |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Returns whatever transport is configured within the config |
|
| 219 | - * |
|
| 220 | - * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
|
| 221 | - */ |
|
| 222 | - protected function getInstance() { |
|
| 223 | - if (!is_null($this->instance)) { |
|
| 224 | - return $this->instance; |
|
| 225 | - } |
|
| 217 | + /** |
|
| 218 | + * Returns whatever transport is configured within the config |
|
| 219 | + * |
|
| 220 | + * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
|
| 221 | + */ |
|
| 222 | + protected function getInstance() { |
|
| 223 | + if (!is_null($this->instance)) { |
|
| 224 | + return $this->instance; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
| 228 | - case 'smtp': |
|
| 229 | - $this->instance = $this->getSmtpInstance(); |
|
| 230 | - break; |
|
| 231 | - case 'sendmail': |
|
| 232 | - // FIXME: Move into the return statement but requires proper testing |
|
| 233 | - // for SMTP and mail as well. Thus not really doable for a |
|
| 234 | - // minor release. |
|
| 235 | - $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance()); |
|
| 236 | - break; |
|
| 237 | - default: |
|
| 238 | - $this->instance = $this->getMailInstance(); |
|
| 239 | - break; |
|
| 240 | - } |
|
| 227 | + switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
| 228 | + case 'smtp': |
|
| 229 | + $this->instance = $this->getSmtpInstance(); |
|
| 230 | + break; |
|
| 231 | + case 'sendmail': |
|
| 232 | + // FIXME: Move into the return statement but requires proper testing |
|
| 233 | + // for SMTP and mail as well. Thus not really doable for a |
|
| 234 | + // minor release. |
|
| 235 | + $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance()); |
|
| 236 | + break; |
|
| 237 | + default: |
|
| 238 | + $this->instance = $this->getMailInstance(); |
|
| 239 | + break; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - return $this->instance; |
|
| 243 | - } |
|
| 242 | + return $this->instance; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Returns the SMTP transport |
|
| 247 | - * |
|
| 248 | - * @return \Swift_SmtpTransport |
|
| 249 | - */ |
|
| 250 | - protected function getSmtpInstance(): \Swift_SmtpTransport { |
|
| 251 | - $transport = \Swift_SmtpTransport::newInstance(); |
|
| 252 | - $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
| 253 | - $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
| 254 | - $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
| 255 | - if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
| 256 | - $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
| 257 | - $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
| 258 | - $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
| 259 | - } |
|
| 260 | - $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
| 261 | - if (!empty($smtpSecurity)) { |
|
| 262 | - $transport->setEncryption($smtpSecurity); |
|
| 263 | - } |
|
| 264 | - $transport->start(); |
|
| 265 | - return $transport; |
|
| 266 | - } |
|
| 245 | + /** |
|
| 246 | + * Returns the SMTP transport |
|
| 247 | + * |
|
| 248 | + * @return \Swift_SmtpTransport |
|
| 249 | + */ |
|
| 250 | + protected function getSmtpInstance(): \Swift_SmtpTransport { |
|
| 251 | + $transport = \Swift_SmtpTransport::newInstance(); |
|
| 252 | + $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
| 253 | + $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
| 254 | + $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
| 255 | + if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
| 256 | + $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
| 257 | + $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
| 258 | + $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
| 259 | + } |
|
| 260 | + $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
| 261 | + if (!empty($smtpSecurity)) { |
|
| 262 | + $transport->setEncryption($smtpSecurity); |
|
| 263 | + } |
|
| 264 | + $transport->start(); |
|
| 265 | + return $transport; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Returns the sendmail transport |
|
| 270 | - * |
|
| 271 | - * @return \Swift_SendmailTransport |
|
| 272 | - */ |
|
| 273 | - protected function getSendMailInstance(): \Swift_SendmailTransport { |
|
| 274 | - switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
| 275 | - case 'qmail': |
|
| 276 | - $binaryPath = '/var/qmail/bin/sendmail'; |
|
| 277 | - break; |
|
| 278 | - default: |
|
| 279 | - $binaryPath = '/usr/sbin/sendmail'; |
|
| 280 | - break; |
|
| 281 | - } |
|
| 268 | + /** |
|
| 269 | + * Returns the sendmail transport |
|
| 270 | + * |
|
| 271 | + * @return \Swift_SendmailTransport |
|
| 272 | + */ |
|
| 273 | + protected function getSendMailInstance(): \Swift_SendmailTransport { |
|
| 274 | + switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
| 275 | + case 'qmail': |
|
| 276 | + $binaryPath = '/var/qmail/bin/sendmail'; |
|
| 277 | + break; |
|
| 278 | + default: |
|
| 279 | + $binaryPath = '/usr/sbin/sendmail'; |
|
| 280 | + break; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs'); |
|
| 284 | - } |
|
| 283 | + return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs'); |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Returns the mail transport |
|
| 288 | - * |
|
| 289 | - * @return \Swift_MailTransport |
|
| 290 | - */ |
|
| 291 | - protected function getMailInstance(): \Swift_MailTransport { |
|
| 292 | - return \Swift_MailTransport::newInstance(); |
|
| 293 | - } |
|
| 286 | + /** |
|
| 287 | + * Returns the mail transport |
|
| 288 | + * |
|
| 289 | + * @return \Swift_MailTransport |
|
| 290 | + */ |
|
| 291 | + protected function getMailInstance(): \Swift_MailTransport { |
|
| 292 | + return \Swift_MailTransport::newInstance(); |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | 295 | } |
@@ -37,245 +37,245 @@ |
||
| 37 | 37 | * @package OC\Mail |
| 38 | 38 | */ |
| 39 | 39 | class Message implements IMessage { |
| 40 | - /** @var Swift_Message */ |
|
| 41 | - private $swiftMessage; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param Swift_Message $swiftMessage |
|
| 45 | - */ |
|
| 46 | - public function __construct(Swift_Message $swiftMessage) { |
|
| 47 | - $this->swiftMessage = $swiftMessage; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param IAttachment $attachment |
|
| 52 | - * @return $this |
|
| 53 | - * @since 13.0.0 |
|
| 54 | - */ |
|
| 55 | - public function attach(IAttachment $attachment): IMessage { |
|
| 56 | - /** @var Attachment $attachment */ |
|
| 57 | - $this->swiftMessage->attach($attachment->getSwiftAttachment()); |
|
| 58 | - return $this; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
| 63 | - * FIXME: Remove this once SwiftMailer supports IDN |
|
| 64 | - * |
|
| 65 | - * @param array $addresses Array of mail addresses, key will get converted |
|
| 66 | - * @return array Converted addresses if `idn_to_ascii` exists |
|
| 67 | - */ |
|
| 68 | - protected function convertAddresses(array $addresses): array { |
|
| 69 | - if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 70 | - return $addresses; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $convertedAddresses = []; |
|
| 74 | - |
|
| 75 | - foreach($addresses as $email => $readableName) { |
|
| 76 | - if(!is_numeric($email)) { |
|
| 77 | - list($name, $domain) = explode('@', $email, 2); |
|
| 78 | - $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); |
|
| 79 | - $convertedAddresses[$name.'@'.$domain] = $readableName; |
|
| 80 | - } else { |
|
| 81 | - list($name, $domain) = explode('@', $readableName, 2); |
|
| 82 | - $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); |
|
| 83 | - $convertedAddresses[$email] = $name.'@'.$domain; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $convertedAddresses; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Set the from address of this message. |
|
| 92 | - * |
|
| 93 | - * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
|
| 94 | - * |
|
| 95 | - * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 96 | - * @return $this |
|
| 97 | - */ |
|
| 98 | - public function setFrom(array $addresses): IMessage { |
|
| 99 | - $addresses = $this->convertAddresses($addresses); |
|
| 100 | - |
|
| 101 | - $this->swiftMessage->setFrom($addresses); |
|
| 102 | - return $this; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get the from address of this message. |
|
| 107 | - * |
|
| 108 | - * @return array |
|
| 109 | - */ |
|
| 110 | - public function getFrom(): array { |
|
| 111 | - return $this->swiftMessage->getFrom(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Set the Reply-To address of this message |
|
| 116 | - * |
|
| 117 | - * @param array $addresses |
|
| 118 | - * @return $this |
|
| 119 | - */ |
|
| 120 | - public function setReplyTo(array $addresses): IMessage { |
|
| 121 | - $addresses = $this->convertAddresses($addresses); |
|
| 122 | - |
|
| 123 | - $this->swiftMessage->setReplyTo($addresses); |
|
| 124 | - return $this; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns the Reply-To address of this message |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public function getReplyTo(): string { |
|
| 133 | - return $this->swiftMessage->getReplyTo(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Set the to addresses of this message. |
|
| 138 | - * |
|
| 139 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 140 | - * @return $this |
|
| 141 | - */ |
|
| 142 | - public function setTo(array $recipients): IMessage { |
|
| 143 | - $recipients = $this->convertAddresses($recipients); |
|
| 144 | - |
|
| 145 | - $this->swiftMessage->setTo($recipients); |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get the to address of this message. |
|
| 151 | - * |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 154 | - public function getTo(): array { |
|
| 155 | - return $this->swiftMessage->getTo(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set the CC recipients of this message. |
|
| 160 | - * |
|
| 161 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 162 | - * @return $this |
|
| 163 | - */ |
|
| 164 | - public function setCc(array $recipients): IMessage { |
|
| 165 | - $recipients = $this->convertAddresses($recipients); |
|
| 166 | - |
|
| 167 | - $this->swiftMessage->setCc($recipients); |
|
| 168 | - return $this; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get the cc address of this message. |
|
| 173 | - * |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function getCc(): array { |
|
| 177 | - return $this->swiftMessage->getCc(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Set the BCC recipients of this message. |
|
| 182 | - * |
|
| 183 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 184 | - * @return $this |
|
| 185 | - */ |
|
| 186 | - public function setBcc(array $recipients): IMessage { |
|
| 187 | - $recipients = $this->convertAddresses($recipients); |
|
| 188 | - |
|
| 189 | - $this->swiftMessage->setBcc($recipients); |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get the Bcc address of this message. |
|
| 195 | - * |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - public function getBcc(): array { |
|
| 199 | - return $this->swiftMessage->getBcc(); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Set the subject of this message. |
|
| 204 | - * |
|
| 205 | - * @param string $subject |
|
| 206 | - * @return IMessage |
|
| 207 | - */ |
|
| 208 | - public function setSubject(string $subject): IMessage { |
|
| 209 | - $this->swiftMessage->setSubject($subject); |
|
| 210 | - return $this; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Get the from subject of this message. |
|
| 215 | - * |
|
| 216 | - * @return string |
|
| 217 | - */ |
|
| 218 | - public function getSubject(): string { |
|
| 219 | - return $this->swiftMessage->getSubject(); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Set the plain-text body of this message. |
|
| 224 | - * |
|
| 225 | - * @param string $body |
|
| 226 | - * @return $this |
|
| 227 | - */ |
|
| 228 | - public function setPlainBody(string $body): IMessage { |
|
| 229 | - $this->swiftMessage->setBody($body); |
|
| 230 | - return $this; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Get the plain body of this message. |
|
| 235 | - * |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - public function getPlainBody(): string { |
|
| 239 | - return $this->swiftMessage->getBody(); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
|
| 244 | - * |
|
| 245 | - * @param string $body |
|
| 246 | - * @return $this |
|
| 247 | - */ |
|
| 248 | - public function setHtmlBody($body) { |
|
| 249 | - $this->swiftMessage->addPart($body, 'text/html'); |
|
| 250 | - return $this; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Get's the underlying SwiftMessage |
|
| 255 | - * @return Swift_Message |
|
| 256 | - */ |
|
| 257 | - public function getSwiftMessage(): Swift_Message { |
|
| 258 | - return $this->swiftMessage; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * @param string $body |
|
| 263 | - * @param string $contentType |
|
| 264 | - * @return $this |
|
| 265 | - */ |
|
| 266 | - public function setBody($body, $contentType) { |
|
| 267 | - $this->swiftMessage->setBody($body, $contentType); |
|
| 268 | - return $this; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @param IEMailTemplate $emailTemplate |
|
| 273 | - * @return $this |
|
| 274 | - */ |
|
| 275 | - public function useTemplate(IEMailTemplate $emailTemplate): IMessage { |
|
| 276 | - $this->setSubject($emailTemplate->renderSubject()); |
|
| 277 | - $this->setPlainBody($emailTemplate->renderText()); |
|
| 278 | - $this->setHtmlBody($emailTemplate->renderHtml()); |
|
| 279 | - return $this; |
|
| 280 | - } |
|
| 40 | + /** @var Swift_Message */ |
|
| 41 | + private $swiftMessage; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param Swift_Message $swiftMessage |
|
| 45 | + */ |
|
| 46 | + public function __construct(Swift_Message $swiftMessage) { |
|
| 47 | + $this->swiftMessage = $swiftMessage; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param IAttachment $attachment |
|
| 52 | + * @return $this |
|
| 53 | + * @since 13.0.0 |
|
| 54 | + */ |
|
| 55 | + public function attach(IAttachment $attachment): IMessage { |
|
| 56 | + /** @var Attachment $attachment */ |
|
| 57 | + $this->swiftMessage->attach($attachment->getSwiftAttachment()); |
|
| 58 | + return $this; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
| 63 | + * FIXME: Remove this once SwiftMailer supports IDN |
|
| 64 | + * |
|
| 65 | + * @param array $addresses Array of mail addresses, key will get converted |
|
| 66 | + * @return array Converted addresses if `idn_to_ascii` exists |
|
| 67 | + */ |
|
| 68 | + protected function convertAddresses(array $addresses): array { |
|
| 69 | + if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 70 | + return $addresses; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $convertedAddresses = []; |
|
| 74 | + |
|
| 75 | + foreach($addresses as $email => $readableName) { |
|
| 76 | + if(!is_numeric($email)) { |
|
| 77 | + list($name, $domain) = explode('@', $email, 2); |
|
| 78 | + $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); |
|
| 79 | + $convertedAddresses[$name.'@'.$domain] = $readableName; |
|
| 80 | + } else { |
|
| 81 | + list($name, $domain) = explode('@', $readableName, 2); |
|
| 82 | + $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); |
|
| 83 | + $convertedAddresses[$email] = $name.'@'.$domain; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $convertedAddresses; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Set the from address of this message. |
|
| 92 | + * |
|
| 93 | + * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
|
| 94 | + * |
|
| 95 | + * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 96 | + * @return $this |
|
| 97 | + */ |
|
| 98 | + public function setFrom(array $addresses): IMessage { |
|
| 99 | + $addresses = $this->convertAddresses($addresses); |
|
| 100 | + |
|
| 101 | + $this->swiftMessage->setFrom($addresses); |
|
| 102 | + return $this; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get the from address of this message. |
|
| 107 | + * |
|
| 108 | + * @return array |
|
| 109 | + */ |
|
| 110 | + public function getFrom(): array { |
|
| 111 | + return $this->swiftMessage->getFrom(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Set the Reply-To address of this message |
|
| 116 | + * |
|
| 117 | + * @param array $addresses |
|
| 118 | + * @return $this |
|
| 119 | + */ |
|
| 120 | + public function setReplyTo(array $addresses): IMessage { |
|
| 121 | + $addresses = $this->convertAddresses($addresses); |
|
| 122 | + |
|
| 123 | + $this->swiftMessage->setReplyTo($addresses); |
|
| 124 | + return $this; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns the Reply-To address of this message |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public function getReplyTo(): string { |
|
| 133 | + return $this->swiftMessage->getReplyTo(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Set the to addresses of this message. |
|
| 138 | + * |
|
| 139 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 140 | + * @return $this |
|
| 141 | + */ |
|
| 142 | + public function setTo(array $recipients): IMessage { |
|
| 143 | + $recipients = $this->convertAddresses($recipients); |
|
| 144 | + |
|
| 145 | + $this->swiftMessage->setTo($recipients); |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get the to address of this message. |
|
| 151 | + * |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | + public function getTo(): array { |
|
| 155 | + return $this->swiftMessage->getTo(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set the CC recipients of this message. |
|
| 160 | + * |
|
| 161 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 162 | + * @return $this |
|
| 163 | + */ |
|
| 164 | + public function setCc(array $recipients): IMessage { |
|
| 165 | + $recipients = $this->convertAddresses($recipients); |
|
| 166 | + |
|
| 167 | + $this->swiftMessage->setCc($recipients); |
|
| 168 | + return $this; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get the cc address of this message. |
|
| 173 | + * |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function getCc(): array { |
|
| 177 | + return $this->swiftMessage->getCc(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Set the BCC recipients of this message. |
|
| 182 | + * |
|
| 183 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
| 184 | + * @return $this |
|
| 185 | + */ |
|
| 186 | + public function setBcc(array $recipients): IMessage { |
|
| 187 | + $recipients = $this->convertAddresses($recipients); |
|
| 188 | + |
|
| 189 | + $this->swiftMessage->setBcc($recipients); |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get the Bcc address of this message. |
|
| 195 | + * |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + public function getBcc(): array { |
|
| 199 | + return $this->swiftMessage->getBcc(); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Set the subject of this message. |
|
| 204 | + * |
|
| 205 | + * @param string $subject |
|
| 206 | + * @return IMessage |
|
| 207 | + */ |
|
| 208 | + public function setSubject(string $subject): IMessage { |
|
| 209 | + $this->swiftMessage->setSubject($subject); |
|
| 210 | + return $this; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Get the from subject of this message. |
|
| 215 | + * |
|
| 216 | + * @return string |
|
| 217 | + */ |
|
| 218 | + public function getSubject(): string { |
|
| 219 | + return $this->swiftMessage->getSubject(); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Set the plain-text body of this message. |
|
| 224 | + * |
|
| 225 | + * @param string $body |
|
| 226 | + * @return $this |
|
| 227 | + */ |
|
| 228 | + public function setPlainBody(string $body): IMessage { |
|
| 229 | + $this->swiftMessage->setBody($body); |
|
| 230 | + return $this; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Get the plain body of this message. |
|
| 235 | + * |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + public function getPlainBody(): string { |
|
| 239 | + return $this->swiftMessage->getBody(); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
|
| 244 | + * |
|
| 245 | + * @param string $body |
|
| 246 | + * @return $this |
|
| 247 | + */ |
|
| 248 | + public function setHtmlBody($body) { |
|
| 249 | + $this->swiftMessage->addPart($body, 'text/html'); |
|
| 250 | + return $this; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Get's the underlying SwiftMessage |
|
| 255 | + * @return Swift_Message |
|
| 256 | + */ |
|
| 257 | + public function getSwiftMessage(): Swift_Message { |
|
| 258 | + return $this->swiftMessage; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * @param string $body |
|
| 263 | + * @param string $contentType |
|
| 264 | + * @return $this |
|
| 265 | + */ |
|
| 266 | + public function setBody($body, $contentType) { |
|
| 267 | + $this->swiftMessage->setBody($body, $contentType); |
|
| 268 | + return $this; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @param IEMailTemplate $emailTemplate |
|
| 273 | + * @return $this |
|
| 274 | + */ |
|
| 275 | + public function useTemplate(IEMailTemplate $emailTemplate): IMessage { |
|
| 276 | + $this->setSubject($emailTemplate->renderSubject()); |
|
| 277 | + $this->setPlainBody($emailTemplate->renderText()); |
|
| 278 | + $this->setHtmlBody($emailTemplate->renderHtml()); |
|
| 279 | + return $this; |
|
| 280 | + } |
|
| 281 | 281 | } |