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