Completed
Push — master ( 8175ac...6bd1c5 )
by Morris
28:19 queued 16:38
created

Mailer::getSendMailInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Mail;
24
25
use OCP\Defaults;
26
use OCP\IConfig;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\Mail\IMailer;
30
use OCP\ILogger;
31
32
/**
33
 * Class Mailer provides some basic functions to create a mail message that can be used in combination with
34
 * \OC\Mail\Message.
35
 *
36
 * Example usage:
37
 *
38
 * 	$mailer = \OC::$server->getMailer();
39
 * 	$message = $mailer->createMessage();
40
 * 	$message->setSubject('Your Subject');
41
 * 	$message->setFrom(array('[email protected]' => 'ownCloud Notifier');
42
 * 	$message->setTo(array('[email protected]' => 'Recipient');
43
 * 	$message->setBody('The message text');
44
 * 	$mailer->send($message);
45
 *
46
 * This message can then be passed to send() of \OC\Mail\Mailer
47
 *
48
 * @package OC\Mail
49
 */
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 View Code Duplication
	public function __construct(IConfig $config,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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())]);
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
245
}
246