Completed
Push — master ( 8e884b...bea04d )
by Joas
12:13 queued 11:33
created

Message::useTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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