Completed
Pull Request — master (#5268)
by Joas
44:27 queued 16:59
created
lib/private/Mail/Mailer.php 1 patch
Indentation   +203 added lines, -203 removed lines patch added patch discarded remove patch
@@ -48,208 +48,208 @@
 block discarded – undo
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
-		$class = $this->config->getSystemValue('mail_template_class', '');
94
-
95
-		if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) {
96
-			return new $class(
97
-				$this->defaults,
98
-				$this->urlGenerator,
99
-				$this->l10n
100
-			);
101
-		}
102
-
103
-		return new EMailTemplate(
104
-			$this->defaults,
105
-			$this->urlGenerator,
106
-			$this->l10n
107
-		);
108
-	}
109
-
110
-	/**
111
-	 * Send the specified message. Also sets the from address to the value defined in config.php
112
-	 * if no-one has been passed.
113
-	 *
114
-	 * @param Message $message Message to send
115
-	 * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
116
-	 * therefore should be considered
117
-	 * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
118
-	 * has been supplied.)
119
-	 */
120
-	public function send(Message $message) {
121
-		$debugMode = $this->config->getSystemValue('mail_smtpdebug', false);
122
-
123
-		if (sizeof($message->getFrom()) === 0) {
124
-			$message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]);
125
-		}
126
-
127
-		$failedRecipients = [];
128
-
129
-		$mailer = $this->getInstance();
130
-
131
-		// Enable logger if debug mode is enabled
132
-		if($debugMode) {
133
-			$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
134
-			$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
135
-		}
136
-
137
-		$mailer->send($message->getSwiftMessage(), $failedRecipients);
138
-
139
-		// Debugging logging
140
-		$logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject());
141
-		$this->logger->debug($logMessage, ['app' => 'core']);
142
-		if($debugMode && isset($mailLogger)) {
143
-			$this->logger->debug($mailLogger->dump(), ['app' => 'core']);
144
-		}
145
-
146
-		return $failedRecipients;
147
-	}
148
-
149
-	/**
150
-	 * Checks if an e-mail address is valid
151
-	 *
152
-	 * @param string $email Email address to be validated
153
-	 * @return bool True if the mail address is valid, false otherwise
154
-	 */
155
-	public function validateMailAddress($email) {
156
-		return \Swift_Validate::email($this->convertEmail($email));
157
-	}
158
-
159
-	/**
160
-	 * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
161
-	 *
162
-	 * FIXME: Remove this once SwiftMailer supports IDN
163
-	 *
164
-	 * @param string $email
165
-	 * @return string Converted mail address if `idn_to_ascii` exists
166
-	 */
167
-	protected function convertEmail($email) {
168
-		if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) {
169
-			return $email;
170
-		}
171
-
172
-		list($name, $domain) = explode('@', $email, 2);
173
-		$domain = idn_to_ascii($domain);
174
-		return $name.'@'.$domain;
175
-	}
176
-
177
-	/**
178
-	 * Returns whatever transport is configured within the config
179
-	 *
180
-	 * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport
181
-	 */
182
-	protected function getInstance() {
183
-		if (!is_null($this->instance)) {
184
-			return $this->instance;
185
-		}
186
-
187
-		switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
188
-			case 'smtp':
189
-				$this->instance = $this->getSMTPInstance();
190
-				break;
191
-			case 'sendmail':
192
-				// FIXME: Move into the return statement but requires proper testing
193
-				//       for SMTP and mail as well. Thus not really doable for a
194
-				//       minor release.
195
-				$this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance());
196
-				break;
197
-			default:
198
-				$this->instance = $this->getMailInstance();
199
-				break;
200
-		}
201
-
202
-		return $this->instance;
203
-	}
204
-
205
-	/**
206
-	 * Returns the SMTP transport
207
-	 *
208
-	 * @return \Swift_SmtpTransport
209
-	 */
210
-	protected function getSmtpInstance() {
211
-		$transport = \Swift_SmtpTransport::newInstance();
212
-		$transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10));
213
-		$transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1'));
214
-		$transport->setPort($this->config->getSystemValue('mail_smtpport', 25));
215
-		if ($this->config->getSystemValue('mail_smtpauth', false)) {
216
-			$transport->setUsername($this->config->getSystemValue('mail_smtpname', ''));
217
-			$transport->setPassword($this->config->getSystemValue('mail_smtppassword', ''));
218
-			$transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN'));
219
-		}
220
-		$smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', '');
221
-		if (!empty($smtpSecurity)) {
222
-			$transport->setEncryption($smtpSecurity);
223
-		}
224
-		$transport->start();
225
-		return $transport;
226
-	}
227
-
228
-	/**
229
-	 * Returns the sendmail transport
230
-	 *
231
-	 * @return \Swift_SendmailTransport
232
-	 */
233
-	protected function getSendMailInstance() {
234
-		switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
235
-			case 'qmail':
236
-				$binaryPath = '/var/qmail/bin/sendmail';
237
-				break;
238
-			default:
239
-				$binaryPath = '/usr/sbin/sendmail';
240
-				break;
241
-		}
242
-
243
-		return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs');
244
-	}
245
-
246
-	/**
247
-	 * Returns the mail transport
248
-	 *
249
-	 * @return \Swift_MailTransport
250
-	 */
251
-	protected function getMailInstance() {
252
-		return \Swift_MailTransport::newInstance();
253
-	}
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
+        $class = $this->config->getSystemValue('mail_template_class', '');
94
+
95
+        if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) {
96
+            return new $class(
97
+                $this->defaults,
98
+                $this->urlGenerator,
99
+                $this->l10n
100
+            );
101
+        }
102
+
103
+        return new EMailTemplate(
104
+            $this->defaults,
105
+            $this->urlGenerator,
106
+            $this->l10n
107
+        );
108
+    }
109
+
110
+    /**
111
+     * Send the specified message. Also sets the from address to the value defined in config.php
112
+     * if no-one has been passed.
113
+     *
114
+     * @param Message $message Message to send
115
+     * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
116
+     * therefore should be considered
117
+     * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
118
+     * has been supplied.)
119
+     */
120
+    public function send(Message $message) {
121
+        $debugMode = $this->config->getSystemValue('mail_smtpdebug', false);
122
+
123
+        if (sizeof($message->getFrom()) === 0) {
124
+            $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName()) => $this->defaults->getName()]);
125
+        }
126
+
127
+        $failedRecipients = [];
128
+
129
+        $mailer = $this->getInstance();
130
+
131
+        // Enable logger if debug mode is enabled
132
+        if($debugMode) {
133
+            $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
134
+            $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
135
+        }
136
+
137
+        $mailer->send($message->getSwiftMessage(), $failedRecipients);
138
+
139
+        // Debugging logging
140
+        $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject());
141
+        $this->logger->debug($logMessage, ['app' => 'core']);
142
+        if($debugMode && isset($mailLogger)) {
143
+            $this->logger->debug($mailLogger->dump(), ['app' => 'core']);
144
+        }
145
+
146
+        return $failedRecipients;
147
+    }
148
+
149
+    /**
150
+     * Checks if an e-mail address is valid
151
+     *
152
+     * @param string $email Email address to be validated
153
+     * @return bool True if the mail address is valid, false otherwise
154
+     */
155
+    public function validateMailAddress($email) {
156
+        return \Swift_Validate::email($this->convertEmail($email));
157
+    }
158
+
159
+    /**
160
+     * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
161
+     *
162
+     * FIXME: Remove this once SwiftMailer supports IDN
163
+     *
164
+     * @param string $email
165
+     * @return string Converted mail address if `idn_to_ascii` exists
166
+     */
167
+    protected function convertEmail($email) {
168
+        if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) {
169
+            return $email;
170
+        }
171
+
172
+        list($name, $domain) = explode('@', $email, 2);
173
+        $domain = idn_to_ascii($domain);
174
+        return $name.'@'.$domain;
175
+    }
176
+
177
+    /**
178
+     * Returns whatever transport is configured within the config
179
+     *
180
+     * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport
181
+     */
182
+    protected function getInstance() {
183
+        if (!is_null($this->instance)) {
184
+            return $this->instance;
185
+        }
186
+
187
+        switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
188
+            case 'smtp':
189
+                $this->instance = $this->getSMTPInstance();
190
+                break;
191
+            case 'sendmail':
192
+                // FIXME: Move into the return statement but requires proper testing
193
+                //       for SMTP and mail as well. Thus not really doable for a
194
+                //       minor release.
195
+                $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance());
196
+                break;
197
+            default:
198
+                $this->instance = $this->getMailInstance();
199
+                break;
200
+        }
201
+
202
+        return $this->instance;
203
+    }
204
+
205
+    /**
206
+     * Returns the SMTP transport
207
+     *
208
+     * @return \Swift_SmtpTransport
209
+     */
210
+    protected function getSmtpInstance() {
211
+        $transport = \Swift_SmtpTransport::newInstance();
212
+        $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10));
213
+        $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1'));
214
+        $transport->setPort($this->config->getSystemValue('mail_smtpport', 25));
215
+        if ($this->config->getSystemValue('mail_smtpauth', false)) {
216
+            $transport->setUsername($this->config->getSystemValue('mail_smtpname', ''));
217
+            $transport->setPassword($this->config->getSystemValue('mail_smtppassword', ''));
218
+            $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN'));
219
+        }
220
+        $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', '');
221
+        if (!empty($smtpSecurity)) {
222
+            $transport->setEncryption($smtpSecurity);
223
+        }
224
+        $transport->start();
225
+        return $transport;
226
+    }
227
+
228
+    /**
229
+     * Returns the sendmail transport
230
+     *
231
+     * @return \Swift_SendmailTransport
232
+     */
233
+    protected function getSendMailInstance() {
234
+        switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
235
+            case 'qmail':
236
+                $binaryPath = '/var/qmail/bin/sendmail';
237
+                break;
238
+            default:
239
+                $binaryPath = '/usr/sbin/sendmail';
240
+                break;
241
+        }
242
+
243
+        return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs');
244
+    }
245
+
246
+    /**
247
+     * Returns the mail transport
248
+     *
249
+     * @return \Swift_MailTransport
250
+     */
251
+    protected function getMailInstance() {
252
+        return \Swift_MailTransport::newInstance();
253
+    }
254 254
 
255 255
 }
Please login to merge, or discard this patch.