Completed
Pull Request — master (#5791)
by Lukas
18:29 queued 03:50
created
lib/private/Mail/Message.php 2 patches
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -31,223 +31,223 @@
 block discarded – undo
31 31
  * @package OC\Mail
32 32
  */
33 33
 class Message {
34
-	/** @var Swift_Message */
35
-	private $swiftMessage;
36
-
37
-	/**
38
-	 * @param Swift_Message $swiftMessage
39
-	 */
40
-	public function __construct(Swift_Message $swiftMessage) {
41
-		$this->swiftMessage = $swiftMessage;
42
-	}
43
-
44
-	/**
45
-	 * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
46
-	 * FIXME: Remove this once SwiftMailer supports IDN
47
-	 *
48
-	 * @param array $addresses Array of mail addresses, key will get converted
49
-	 * @return array Converted addresses if `idn_to_ascii` exists
50
-	 */
51
-	protected function convertAddresses($addresses) {
52
-		if (!function_exists('idn_to_ascii')) {
53
-			return $addresses;
54
-		}
55
-
56
-		$convertedAddresses = array();
57
-
58
-		foreach($addresses as $email => $readableName) {
59
-			if(!is_numeric($email)) {
60
-				list($name, $domain) = explode('@', $email, 2);
61
-				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
62
-				$convertedAddresses[$name.'@'.$domain] = $readableName;
63
-			} else {
64
-				list($name, $domain) = explode('@', $readableName, 2);
65
-				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
66
-				$convertedAddresses[$email] = $name.'@'.$domain;
67
-			}
68
-		}
69
-
70
-		return $convertedAddresses;
71
-	}
72
-
73
-	/**
74
-	 * Set the from address of this message.
75
-	 *
76
-	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
77
-	 *
78
-	 * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
79
-	 * @return $this
80
-	 */
81
-	public function setFrom(array $addresses) {
82
-		$addresses = $this->convertAddresses($addresses);
83
-
84
-		$this->swiftMessage->setFrom($addresses);
85
-		return $this;
86
-	}
87
-
88
-	/**
89
-	 * Get the from address of this message.
90
-	 *
91
-	 * @return array
92
-	 */
93
-	public function getFrom() {
94
-		return $this->swiftMessage->getFrom();
95
-	}
96
-
97
-	/**
98
-	 * Set the Reply-To address of this message
99
-	 *
100
-	 * @param array $addresses
101
-	 * @return $this
102
-	 */
103
-	public function setReplyTo(array $addresses) {
104
-		$addresses = $this->convertAddresses($addresses);
105
-
106
-		$this->swiftMessage->setReplyTo($addresses);
107
-		return $this;
108
-	}
109
-
110
-	/**
111
-	 * Returns the Reply-To address of this message
112
-	 *
113
-	 * @return array
114
-	 */
115
-	public function getReplyTo() {
116
-		return $this->swiftMessage->getReplyTo();
117
-	}
118
-
119
-	/**
120
-	 * Set the to addresses of this message.
121
-	 *
122
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
123
-	 * @return $this
124
-	 */
125
-	public function setTo(array $recipients) {
126
-		$recipients = $this->convertAddresses($recipients);
127
-
128
-		$this->swiftMessage->setTo($recipients);
129
-		return $this;
130
-	}
131
-
132
-	/**
133
-	 * Get the to address of this message.
134
-	 *
135
-	 * @return array
136
-	 */
137
-	public function getTo() {
138
-		return $this->swiftMessage->getTo();
139
-	}
140
-
141
-	/**
142
-	 * Set the CC recipients of this message.
143
-	 *
144
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
145
-	 * @return $this
146
-	 */
147
-	public function setCc(array $recipients) {
148
-		$recipients = $this->convertAddresses($recipients);
149
-
150
-		$this->swiftMessage->setCc($recipients);
151
-		return $this;
152
-	}
153
-
154
-	/**
155
-	 * Get the cc address of this message.
156
-	 *
157
-	 * @return array
158
-	 */
159
-	public function getCc() {
160
-		return $this->swiftMessage->getCc();
161
-	}
162
-
163
-	/**
164
-	 * Set the BCC recipients of this message.
165
-	 *
166
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
167
-	 * @return $this
168
-	 */
169
-	public function setBcc(array $recipients) {
170
-		$recipients = $this->convertAddresses($recipients);
171
-
172
-		$this->swiftMessage->setBcc($recipients);
173
-		return $this;
174
-	}
175
-
176
-	/**
177
-	 * Get the Bcc address of this message.
178
-	 *
179
-	 * @return array
180
-	 */
181
-	public function getBcc() {
182
-		return $this->swiftMessage->getBcc();
183
-	}
184
-
185
-	/**
186
-	 * Set the subject of this message.
187
-	 *
188
-	 * @param $subject
189
-	 * @return $this
190
-	 */
191
-	public function setSubject($subject) {
192
-		$this->swiftMessage->setSubject($subject);
193
-		return $this;
194
-	}
195
-
196
-	/**
197
-	 * Get the from subject of this message.
198
-	 *
199
-	 * @return string
200
-	 */
201
-	public function getSubject() {
202
-		return $this->swiftMessage->getSubject();
203
-	}
204
-
205
-	/**
206
-	 * Set the plain-text body of this message.
207
-	 *
208
-	 * @param string $body
209
-	 * @return $this
210
-	 */
211
-	public function setPlainBody($body) {
212
-		$this->swiftMessage->setBody($body);
213
-		return $this;
214
-	}
215
-
216
-	/**
217
-	 * Get the plain body of this message.
218
-	 *
219
-	 * @return string
220
-	 */
221
-	public function getPlainBody() {
222
-		return $this->swiftMessage->getBody();
223
-	}
224
-
225
-	/**
226
-	 * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
227
-	 *
228
-	 * @param string $body
229
-	 * @return $this
230
-	 */
231
-	public function setHtmlBody($body) {
232
-		$this->swiftMessage->addPart($body, 'text/html');
233
-		return $this;
234
-	}
235
-
236
-	/**
237
-	 * Get's the underlying SwiftMessage
238
-	 * @return Swift_Message
239
-	 */
240
-	public function getSwiftMessage() {
241
-		return $this->swiftMessage;
242
-	}
243
-
244
-	/**
245
-	 * @param string $body
246
-	 * @param string $contentType
247
-	 * @return $this
248
-	 */
249
-	public function setBody($body, $contentType) {
250
-		$this->swiftMessage->setBody($body, $contentType);
251
-		return $this;
252
-	}
34
+    /** @var Swift_Message */
35
+    private $swiftMessage;
36
+
37
+    /**
38
+     * @param Swift_Message $swiftMessage
39
+     */
40
+    public function __construct(Swift_Message $swiftMessage) {
41
+        $this->swiftMessage = $swiftMessage;
42
+    }
43
+
44
+    /**
45
+     * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
46
+     * FIXME: Remove this once SwiftMailer supports IDN
47
+     *
48
+     * @param array $addresses Array of mail addresses, key will get converted
49
+     * @return array Converted addresses if `idn_to_ascii` exists
50
+     */
51
+    protected function convertAddresses($addresses) {
52
+        if (!function_exists('idn_to_ascii')) {
53
+            return $addresses;
54
+        }
55
+
56
+        $convertedAddresses = array();
57
+
58
+        foreach($addresses as $email => $readableName) {
59
+            if(!is_numeric($email)) {
60
+                list($name, $domain) = explode('@', $email, 2);
61
+                $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
62
+                $convertedAddresses[$name.'@'.$domain] = $readableName;
63
+            } else {
64
+                list($name, $domain) = explode('@', $readableName, 2);
65
+                $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
66
+                $convertedAddresses[$email] = $name.'@'.$domain;
67
+            }
68
+        }
69
+
70
+        return $convertedAddresses;
71
+    }
72
+
73
+    /**
74
+     * Set the from address of this message.
75
+     *
76
+     * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
77
+     *
78
+     * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
79
+     * @return $this
80
+     */
81
+    public function setFrom(array $addresses) {
82
+        $addresses = $this->convertAddresses($addresses);
83
+
84
+        $this->swiftMessage->setFrom($addresses);
85
+        return $this;
86
+    }
87
+
88
+    /**
89
+     * Get the from address of this message.
90
+     *
91
+     * @return array
92
+     */
93
+    public function getFrom() {
94
+        return $this->swiftMessage->getFrom();
95
+    }
96
+
97
+    /**
98
+     * Set the Reply-To address of this message
99
+     *
100
+     * @param array $addresses
101
+     * @return $this
102
+     */
103
+    public function setReplyTo(array $addresses) {
104
+        $addresses = $this->convertAddresses($addresses);
105
+
106
+        $this->swiftMessage->setReplyTo($addresses);
107
+        return $this;
108
+    }
109
+
110
+    /**
111
+     * Returns the Reply-To address of this message
112
+     *
113
+     * @return array
114
+     */
115
+    public function getReplyTo() {
116
+        return $this->swiftMessage->getReplyTo();
117
+    }
118
+
119
+    /**
120
+     * Set the to addresses of this message.
121
+     *
122
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
123
+     * @return $this
124
+     */
125
+    public function setTo(array $recipients) {
126
+        $recipients = $this->convertAddresses($recipients);
127
+
128
+        $this->swiftMessage->setTo($recipients);
129
+        return $this;
130
+    }
131
+
132
+    /**
133
+     * Get the to address of this message.
134
+     *
135
+     * @return array
136
+     */
137
+    public function getTo() {
138
+        return $this->swiftMessage->getTo();
139
+    }
140
+
141
+    /**
142
+     * Set the CC recipients of this message.
143
+     *
144
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
145
+     * @return $this
146
+     */
147
+    public function setCc(array $recipients) {
148
+        $recipients = $this->convertAddresses($recipients);
149
+
150
+        $this->swiftMessage->setCc($recipients);
151
+        return $this;
152
+    }
153
+
154
+    /**
155
+     * Get the cc address of this message.
156
+     *
157
+     * @return array
158
+     */
159
+    public function getCc() {
160
+        return $this->swiftMessage->getCc();
161
+    }
162
+
163
+    /**
164
+     * Set the BCC recipients of this message.
165
+     *
166
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
167
+     * @return $this
168
+     */
169
+    public function setBcc(array $recipients) {
170
+        $recipients = $this->convertAddresses($recipients);
171
+
172
+        $this->swiftMessage->setBcc($recipients);
173
+        return $this;
174
+    }
175
+
176
+    /**
177
+     * Get the Bcc address of this message.
178
+     *
179
+     * @return array
180
+     */
181
+    public function getBcc() {
182
+        return $this->swiftMessage->getBcc();
183
+    }
184
+
185
+    /**
186
+     * Set the subject of this message.
187
+     *
188
+     * @param $subject
189
+     * @return $this
190
+     */
191
+    public function setSubject($subject) {
192
+        $this->swiftMessage->setSubject($subject);
193
+        return $this;
194
+    }
195
+
196
+    /**
197
+     * Get the from subject of this message.
198
+     *
199
+     * @return string
200
+     */
201
+    public function getSubject() {
202
+        return $this->swiftMessage->getSubject();
203
+    }
204
+
205
+    /**
206
+     * Set the plain-text body of this message.
207
+     *
208
+     * @param string $body
209
+     * @return $this
210
+     */
211
+    public function setPlainBody($body) {
212
+        $this->swiftMessage->setBody($body);
213
+        return $this;
214
+    }
215
+
216
+    /**
217
+     * Get the plain body of this message.
218
+     *
219
+     * @return string
220
+     */
221
+    public function getPlainBody() {
222
+        return $this->swiftMessage->getBody();
223
+    }
224
+
225
+    /**
226
+     * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
227
+     *
228
+     * @param string $body
229
+     * @return $this
230
+     */
231
+    public function setHtmlBody($body) {
232
+        $this->swiftMessage->addPart($body, 'text/html');
233
+        return $this;
234
+    }
235
+
236
+    /**
237
+     * Get's the underlying SwiftMessage
238
+     * @return Swift_Message
239
+     */
240
+    public function getSwiftMessage() {
241
+        return $this->swiftMessage;
242
+    }
243
+
244
+    /**
245
+     * @param string $body
246
+     * @param string $contentType
247
+     * @return $this
248
+     */
249
+    public function setBody($body, $contentType) {
250
+        $this->swiftMessage->setBody($body, $contentType);
251
+        return $this;
252
+    }
253 253
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -55,8 +55,8 @@
 block discarded – undo
55 55
 
56 56
 		$convertedAddresses = array();
57 57
 
58
-		foreach($addresses as $email => $readableName) {
59
-			if(!is_numeric($email)) {
58
+		foreach ($addresses as $email => $readableName) {
59
+			if (!is_numeric($email)) {
60 60
 				list($name, $domain) = explode('@', $email, 2);
61 61
 				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
62 62
 				$convertedAddresses[$name.'@'.$domain] = $readableName;
Please login to merge, or discard this patch.
lib/private/Mail/Mailer.php 2 patches
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 (empty($message->getFrom())) {
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, 0,INTL_IDNA_VARIANT_UTS46);
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 (empty($message->getFrom())) {
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, 0,INTL_IDNA_VARIANT_UTS46);
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.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 		$mailer = $this->getInstance();
130 130
 
131 131
 		// Enable logger if debug mode is enabled
132
-		if($debugMode) {
132
+		if ($debugMode) {
133 133
 			$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
134 134
 			$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
135 135
 		}
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 		// Debugging logging
140 140
 		$logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject());
141 141
 		$this->logger->debug($logMessage, ['app' => 'core']);
142
-		if($debugMode && isset($mailLogger)) {
142
+		if ($debugMode && isset($mailLogger)) {
143 143
 			$this->logger->debug($mailLogger->dump(), ['app' => 'core']);
144 144
 		}
145 145
 
@@ -170,7 +170,7 @@  discard block
 block discarded – undo
170 170
 		}
171 171
 
172 172
 		list($name, $domain) = explode('@', $email, 2);
173
-		$domain = idn_to_ascii($domain, 0,INTL_IDNA_VARIANT_UTS46);
173
+		$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
174 174
 		return $name.'@'.$domain;
175 175
 	}
176 176
 
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 				break;
241 241
 		}
242 242
 
243
-		return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs');
243
+		return \Swift_SendmailTransport::newInstance($binaryPath.' -bs');
244 244
 	}
245 245
 
246 246
 	/**
Please login to merge, or discard this patch.