Completed
Push — master ( b19735...50442b )
by Blizzz
51:50 queued 34:49
created

Message::convertAddresses()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Mail;
28
29
use OCP\Mail\IAttachment;
30
use OCP\Mail\IEMailTemplate;
31
use OCP\Mail\IMessage;
32
use Swift_Message;
33
34
/**
35
 * Class Message provides a wrapper around SwiftMail
36
 *
37
 * @package OC\Mail
38
 */
39
class Message implements IMessage {
40
	/** @var Swift_Message */
41
	private $swiftMessage;
42
43
	/**
44
	 * @param Swift_Message $swiftMessage
45
	 */
46
	public function __construct(Swift_Message $swiftMessage) {
47
		$this->swiftMessage = $swiftMessage;
48
	}
49
50
	/**
51
	 * @param IAttachment $attachment
52
	 * @return $this
53
	 * @since 13.0.0
54
	 */
55
	public function attach(IAttachment $attachment): IMessage {
56
		/** @var Attachment $attachment */
57
		$this->swiftMessage->attach($attachment->getSwiftAttachment());
58
		return $this;
59
	}
60
61
	/**
62
	 * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
63
	 * FIXME: Remove this once SwiftMailer supports IDN
64
	 *
65
	 * @param array $addresses Array of mail addresses, key will get converted
66
	 * @return array Converted addresses if `idn_to_ascii` exists
67
	 */
68
	protected function convertAddresses(array $addresses): array {
69
		if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) {
70
			return $addresses;
71
		}
72
73
		$convertedAddresses = [];
74
75
		foreach($addresses as $email => $readableName) {
76
			if(!is_numeric($email)) {
77
				list($name, $domain) = explode('@', $email, 2);
78
				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
79
				$convertedAddresses[$name.'@'.$domain] = $readableName;
80
			} else {
81
				list($name, $domain) = explode('@', $readableName, 2);
82
				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
83
				$convertedAddresses[$email] = $name.'@'.$domain;
84
			}
85
		}
86
87
		return $convertedAddresses;
88
	}
89
90
	/**
91
	 * Set the from address of this message.
92
	 *
93
	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
94
	 *
95
	 * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
96
	 * @return $this
97
	 */
98
	public function setFrom(array $addresses): IMessage {
99
		$addresses = $this->convertAddresses($addresses);
100
101
		$this->swiftMessage->setFrom($addresses);
102
		return $this;
103
	}
104
105
	/**
106
	 * Get the from address of this message.
107
	 *
108
	 * @return array
109
	 */
110
	public function getFrom(): array {
111
		return $this->swiftMessage->getFrom();
112
	}
113
114
	/**
115
	 * Set the Reply-To address of this message
116
	 *
117
	 * @param array $addresses
118
	 * @return $this
119
	 */
120
	public function setReplyTo(array $addresses): IMessage {
121
		$addresses = $this->convertAddresses($addresses);
122
123
		$this->swiftMessage->setReplyTo($addresses);
124
		return $this;
125
	}
126
127
	/**
128
	 * Returns the Reply-To address of this message
129
	 *
130
	 * @return string
131
	 */
132
	public function getReplyTo(): string {
133
		return $this->swiftMessage->getReplyTo();
134
	}
135
136
	/**
137
	 * Set the to addresses of this message.
138
	 *
139
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
140
	 * @return $this
141
	 */
142
	public function setTo(array $recipients): IMessage {
143
		$recipients = $this->convertAddresses($recipients);
144
145
		$this->swiftMessage->setTo($recipients);
146
		return $this;
147
	}
148
149
	/**
150
	 * Get the to address of this message.
151
	 *
152
	 * @return array
153
	 */
154
	public function getTo(): array {
155
		return $this->swiftMessage->getTo();
156
	}
157
158
	/**
159
	 * Set the CC recipients of this message.
160
	 *
161
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
162
	 * @return $this
163
	 */
164
	public function setCc(array $recipients): IMessage {
165
		$recipients = $this->convertAddresses($recipients);
166
167
		$this->swiftMessage->setCc($recipients);
168
		return $this;
169
	}
170
171
	/**
172
	 * Get the cc address of this message.
173
	 *
174
	 * @return array
175
	 */
176
	public function getCc(): array {
177
		return $this->swiftMessage->getCc();
178
	}
179
180
	/**
181
	 * Set the BCC recipients of this message.
182
	 *
183
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
184
	 * @return $this
185
	 */
186
	public function setBcc(array $recipients): IMessage {
187
		$recipients = $this->convertAddresses($recipients);
188
189
		$this->swiftMessage->setBcc($recipients);
190
		return $this;
191
	}
192
193
	/**
194
	 * Get the Bcc address of this message.
195
	 *
196
	 * @return array
197
	 */
198
	public function getBcc(): array {
199
		return $this->swiftMessage->getBcc();
200
	}
201
202
	/**
203
	 * Set the subject of this message.
204
	 *
205
	 * @param string $subject
206
	 * @return IMessage
207
	 */
208
	public function setSubject(string $subject): IMessage {
209
		$this->swiftMessage->setSubject($subject);
210
		return $this;
211
	}
212
213
	/**
214
	 * Get the from subject of this message.
215
	 *
216
	 * @return string
217
	 */
218
	public function getSubject(): string {
219
		return $this->swiftMessage->getSubject();
220
	}
221
222
	/**
223
	 * Set the plain-text body of this message.
224
	 *
225
	 * @param string $body
226
	 * @return $this
227
	 */
228
	public function setPlainBody(string $body): IMessage {
229
		$this->swiftMessage->setBody($body);
230
		return $this;
231
	}
232
233
	/**
234
	 * Get the plain body of this message.
235
	 *
236
	 * @return string
237
	 */
238
	public function getPlainBody(): string {
239
		return $this->swiftMessage->getBody();
240
	}
241
242
	/**
243
	 * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
244
	 *
245
	 * @param string $body
246
	 * @return $this
247
	 */
248
	public function setHtmlBody($body) {
249
		$this->swiftMessage->addPart($body, 'text/html');
250
		return $this;
251
	}
252
253
	/**
254
	 * Get's the underlying SwiftMessage
255
	 * @return Swift_Message
256
	 */
257
	public function getSwiftMessage(): Swift_Message {
258
		return $this->swiftMessage;
259
	}
260
261
	/**
262
	 * @param string $body
263
	 * @param string $contentType
264
	 * @return $this
265
	 */
266
	public function setBody($body, $contentType) {
267
		$this->swiftMessage->setBody($body, $contentType);
268
		return $this;
269
	}
270
271
	/**
272
	 * @param IEMailTemplate $emailTemplate
273
	 * @return $this
274
	 */
275
	public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
276
		$this->setSubject($emailTemplate->renderSubject());
277
		$this->setPlainBody($emailTemplate->renderText());
278
		$this->setHtmlBody($emailTemplate->renderHtml());
279
		return $this;
280
	}
281
}
282