@@ -48,198 +48,198 @@ |
||
48 | 48 | * @package OC\Mail |
49 | 49 | */ |
50 | 50 | class Mailer implements IMailer { |
51 | - /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
|
52 | - private $instance = null; |
|
53 | - /** @var IConfig */ |
|
54 | - private $config; |
|
55 | - /** @var ILogger */ |
|
56 | - private $logger; |
|
57 | - /** @var Defaults */ |
|
58 | - private $defaults; |
|
59 | - /** @var IURLGenerator */ |
|
60 | - private $urlGenerator; |
|
61 | - /** @var IL10N */ |
|
62 | - private $l10n; |
|
63 | - |
|
64 | - /** |
|
65 | - * @param IConfig $config |
|
66 | - * @param ILogger $logger |
|
67 | - * @param Defaults $defaults |
|
68 | - * @param IURLGenerator $urlGenerator |
|
69 | - * @param IL10N $l10n |
|
70 | - */ |
|
71 | - public function __construct(IConfig $config, |
|
72 | - ILogger $logger, |
|
73 | - Defaults $defaults, |
|
74 | - IURLGenerator $urlGenerator, |
|
75 | - IL10N $l10n) { |
|
76 | - $this->config = $config; |
|
77 | - $this->logger = $logger; |
|
78 | - $this->defaults = $defaults; |
|
79 | - $this->urlGenerator = $urlGenerator; |
|
80 | - $this->l10n = $l10n; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Creates a new message object that can be passed to send() |
|
85 | - * |
|
86 | - * @return Message |
|
87 | - */ |
|
88 | - public function createMessage() { |
|
89 | - return new Message(new \Swift_Message()); |
|
90 | - } |
|
91 | - |
|
92 | - public function createEMailTemplate() { |
|
93 | - return new EMailTemplate( |
|
94 | - $this->defaults, |
|
95 | - $this->urlGenerator, |
|
96 | - $this->l10n |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Send the specified message. Also sets the from address to the value defined in config.php |
|
102 | - * if no-one has been passed. |
|
103 | - * |
|
104 | - * @param Message $message Message to send |
|
105 | - * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
106 | - * therefore should be considered |
|
107 | - * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
108 | - * has been supplied.) |
|
109 | - */ |
|
110 | - public function send(Message $message) { |
|
111 | - $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
112 | - |
|
113 | - if (sizeof($message->getFrom()) === 0) { |
|
114 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]); |
|
115 | - } |
|
116 | - |
|
117 | - $failedRecipients = []; |
|
118 | - |
|
119 | - $mailer = $this->getInstance(); |
|
120 | - |
|
121 | - // Enable logger if debug mode is enabled |
|
122 | - if($debugMode) { |
|
123 | - $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
124 | - $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
125 | - } |
|
126 | - |
|
127 | - $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
128 | - |
|
129 | - // Debugging logging |
|
130 | - $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
131 | - $this->logger->debug($logMessage, ['app' => 'core']); |
|
132 | - if($debugMode && isset($mailLogger)) { |
|
133 | - $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
134 | - } |
|
135 | - |
|
136 | - return $failedRecipients; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Checks if an e-mail address is valid |
|
141 | - * |
|
142 | - * @param string $email Email address to be validated |
|
143 | - * @return bool True if the mail address is valid, false otherwise |
|
144 | - */ |
|
145 | - public function validateMailAddress($email) { |
|
146 | - return \Swift_Validate::email($this->convertEmail($email)); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
151 | - * |
|
152 | - * FIXME: Remove this once SwiftMailer supports IDN |
|
153 | - * |
|
154 | - * @param string $email |
|
155 | - * @return string Converted mail address if `idn_to_ascii` exists |
|
156 | - */ |
|
157 | - protected function convertEmail($email) { |
|
158 | - if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) { |
|
159 | - return $email; |
|
160 | - } |
|
161 | - |
|
162 | - list($name, $domain) = explode('@', $email, 2); |
|
163 | - $domain = idn_to_ascii($domain); |
|
164 | - return $name.'@'.$domain; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Returns whatever transport is configured within the config |
|
169 | - * |
|
170 | - * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
|
171 | - */ |
|
172 | - protected function getInstance() { |
|
173 | - if (!is_null($this->instance)) { |
|
174 | - return $this->instance; |
|
175 | - } |
|
176 | - |
|
177 | - switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
178 | - case 'smtp': |
|
179 | - $this->instance = $this->getSMTPInstance(); |
|
180 | - break; |
|
181 | - case 'sendmail': |
|
182 | - // FIXME: Move into the return statement but requires proper testing |
|
183 | - // for SMTP and mail as well. Thus not really doable for a |
|
184 | - // minor release. |
|
185 | - $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance()); |
|
186 | - break; |
|
187 | - default: |
|
188 | - $this->instance = $this->getMailInstance(); |
|
189 | - break; |
|
190 | - } |
|
191 | - |
|
192 | - return $this->instance; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Returns the SMTP transport |
|
197 | - * |
|
198 | - * @return \Swift_SmtpTransport |
|
199 | - */ |
|
200 | - protected function getSmtpInstance() { |
|
201 | - $transport = \Swift_SmtpTransport::newInstance(); |
|
202 | - $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
203 | - $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
204 | - $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
205 | - if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
206 | - $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
207 | - $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
208 | - $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
209 | - } |
|
210 | - $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
211 | - if (!empty($smtpSecurity)) { |
|
212 | - $transport->setEncryption($smtpSecurity); |
|
213 | - } |
|
214 | - $transport->start(); |
|
215 | - return $transport; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Returns the sendmail transport |
|
220 | - * |
|
221 | - * @return \Swift_SendmailTransport |
|
222 | - */ |
|
223 | - protected function getSendMailInstance() { |
|
224 | - switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
225 | - case 'qmail': |
|
226 | - $binaryPath = '/var/qmail/bin/sendmail'; |
|
227 | - break; |
|
228 | - default: |
|
229 | - $binaryPath = '/usr/sbin/sendmail'; |
|
230 | - break; |
|
231 | - } |
|
232 | - |
|
233 | - return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs'); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Returns the mail transport |
|
238 | - * |
|
239 | - * @return \Swift_MailTransport |
|
240 | - */ |
|
241 | - protected function getMailInstance() { |
|
242 | - return \Swift_MailTransport::newInstance(); |
|
243 | - } |
|
51 | + /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */ |
|
52 | + private $instance = null; |
|
53 | + /** @var IConfig */ |
|
54 | + private $config; |
|
55 | + /** @var ILogger */ |
|
56 | + private $logger; |
|
57 | + /** @var Defaults */ |
|
58 | + private $defaults; |
|
59 | + /** @var IURLGenerator */ |
|
60 | + private $urlGenerator; |
|
61 | + /** @var IL10N */ |
|
62 | + private $l10n; |
|
63 | + |
|
64 | + /** |
|
65 | + * @param IConfig $config |
|
66 | + * @param ILogger $logger |
|
67 | + * @param Defaults $defaults |
|
68 | + * @param IURLGenerator $urlGenerator |
|
69 | + * @param IL10N $l10n |
|
70 | + */ |
|
71 | + public function __construct(IConfig $config, |
|
72 | + ILogger $logger, |
|
73 | + Defaults $defaults, |
|
74 | + IURLGenerator $urlGenerator, |
|
75 | + IL10N $l10n) { |
|
76 | + $this->config = $config; |
|
77 | + $this->logger = $logger; |
|
78 | + $this->defaults = $defaults; |
|
79 | + $this->urlGenerator = $urlGenerator; |
|
80 | + $this->l10n = $l10n; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Creates a new message object that can be passed to send() |
|
85 | + * |
|
86 | + * @return Message |
|
87 | + */ |
|
88 | + public function createMessage() { |
|
89 | + return new Message(new \Swift_Message()); |
|
90 | + } |
|
91 | + |
|
92 | + public function createEMailTemplate() { |
|
93 | + return new EMailTemplate( |
|
94 | + $this->defaults, |
|
95 | + $this->urlGenerator, |
|
96 | + $this->l10n |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Send the specified message. Also sets the from address to the value defined in config.php |
|
102 | + * if no-one has been passed. |
|
103 | + * |
|
104 | + * @param Message $message Message to send |
|
105 | + * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and |
|
106 | + * therefore should be considered |
|
107 | + * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address |
|
108 | + * has been supplied.) |
|
109 | + */ |
|
110 | + public function send(Message $message) { |
|
111 | + $debugMode = $this->config->getSystemValue('mail_smtpdebug', false); |
|
112 | + |
|
113 | + if (sizeof($message->getFrom()) === 0) { |
|
114 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]); |
|
115 | + } |
|
116 | + |
|
117 | + $failedRecipients = []; |
|
118 | + |
|
119 | + $mailer = $this->getInstance(); |
|
120 | + |
|
121 | + // Enable logger if debug mode is enabled |
|
122 | + if($debugMode) { |
|
123 | + $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger(); |
|
124 | + $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); |
|
125 | + } |
|
126 | + |
|
127 | + $mailer->send($message->getSwiftMessage(), $failedRecipients); |
|
128 | + |
|
129 | + // Debugging logging |
|
130 | + $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject()); |
|
131 | + $this->logger->debug($logMessage, ['app' => 'core']); |
|
132 | + if($debugMode && isset($mailLogger)) { |
|
133 | + $this->logger->debug($mailLogger->dump(), ['app' => 'core']); |
|
134 | + } |
|
135 | + |
|
136 | + return $failedRecipients; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Checks if an e-mail address is valid |
|
141 | + * |
|
142 | + * @param string $email Email address to be validated |
|
143 | + * @return bool True if the mail address is valid, false otherwise |
|
144 | + */ |
|
145 | + public function validateMailAddress($email) { |
|
146 | + return \Swift_Validate::email($this->convertEmail($email)); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
151 | + * |
|
152 | + * FIXME: Remove this once SwiftMailer supports IDN |
|
153 | + * |
|
154 | + * @param string $email |
|
155 | + * @return string Converted mail address if `idn_to_ascii` exists |
|
156 | + */ |
|
157 | + protected function convertEmail($email) { |
|
158 | + if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) { |
|
159 | + return $email; |
|
160 | + } |
|
161 | + |
|
162 | + list($name, $domain) = explode('@', $email, 2); |
|
163 | + $domain = idn_to_ascii($domain); |
|
164 | + return $name.'@'.$domain; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Returns whatever transport is configured within the config |
|
169 | + * |
|
170 | + * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport |
|
171 | + */ |
|
172 | + protected function getInstance() { |
|
173 | + if (!is_null($this->instance)) { |
|
174 | + return $this->instance; |
|
175 | + } |
|
176 | + |
|
177 | + switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
178 | + case 'smtp': |
|
179 | + $this->instance = $this->getSMTPInstance(); |
|
180 | + break; |
|
181 | + case 'sendmail': |
|
182 | + // FIXME: Move into the return statement but requires proper testing |
|
183 | + // for SMTP and mail as well. Thus not really doable for a |
|
184 | + // minor release. |
|
185 | + $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance()); |
|
186 | + break; |
|
187 | + default: |
|
188 | + $this->instance = $this->getMailInstance(); |
|
189 | + break; |
|
190 | + } |
|
191 | + |
|
192 | + return $this->instance; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Returns the SMTP transport |
|
197 | + * |
|
198 | + * @return \Swift_SmtpTransport |
|
199 | + */ |
|
200 | + protected function getSmtpInstance() { |
|
201 | + $transport = \Swift_SmtpTransport::newInstance(); |
|
202 | + $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10)); |
|
203 | + $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1')); |
|
204 | + $transport->setPort($this->config->getSystemValue('mail_smtpport', 25)); |
|
205 | + if ($this->config->getSystemValue('mail_smtpauth', false)) { |
|
206 | + $transport->setUsername($this->config->getSystemValue('mail_smtpname', '')); |
|
207 | + $transport->setPassword($this->config->getSystemValue('mail_smtppassword', '')); |
|
208 | + $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN')); |
|
209 | + } |
|
210 | + $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', ''); |
|
211 | + if (!empty($smtpSecurity)) { |
|
212 | + $transport->setEncryption($smtpSecurity); |
|
213 | + } |
|
214 | + $transport->start(); |
|
215 | + return $transport; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Returns the sendmail transport |
|
220 | + * |
|
221 | + * @return \Swift_SendmailTransport |
|
222 | + */ |
|
223 | + protected function getSendMailInstance() { |
|
224 | + switch ($this->config->getSystemValue('mail_smtpmode', 'php')) { |
|
225 | + case 'qmail': |
|
226 | + $binaryPath = '/var/qmail/bin/sendmail'; |
|
227 | + break; |
|
228 | + default: |
|
229 | + $binaryPath = '/usr/sbin/sendmail'; |
|
230 | + break; |
|
231 | + } |
|
232 | + |
|
233 | + return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs'); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Returns the mail transport |
|
238 | + * |
|
239 | + * @return \Swift_MailTransport |
|
240 | + */ |
|
241 | + protected function getMailInstance() { |
|
242 | + return \Swift_MailTransport::newInstance(); |
|
243 | + } |
|
244 | 244 | |
245 | 245 | } |