Completed
Push — master ( 73aef9...c8323f )
by Georg
60:20 queued 46:52
created

Message::getBcc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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\IAttachment;
27
use OCP\Mail\IEMailTemplate;
28
use OCP\Mail\IMessage;
29
use Swift_Message;
30
31
/**
32
 * Class Message provides a wrapper around SwiftMail
33
 *
34
 * @package OC\Mail
35
 */
36
class Message implements IMessage {
37
	/** @var Swift_Message */
38
	private $swiftMessage;
39
40
	/**
41
	 * @param Swift_Message $swiftMessage
42
	 */
43
	public function __construct(Swift_Message $swiftMessage) {
44
		$this->swiftMessage = $swiftMessage;
45
	}
46
47
	/**
48
	 * @param IAttachment $attachment
49
	 * @return $this
50
	 * @since 13.0.0
51
	 */
52
	public function attach(IAttachment $attachment) {
53
		/** @var Attachment $attachment */
54
		$this->swiftMessage->attach($attachment->getSwiftAttachment());
55
		return $this;
56
	}
57
58
	/**
59
	 * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
60
	 * FIXME: Remove this once SwiftMailer supports IDN
61
	 *
62
	 * @param array $addresses Array of mail addresses, key will get converted
63
	 * @return array Converted addresses if `idn_to_ascii` exists
64
	 */
65
	protected function convertAddresses($addresses) {
66
		if (!function_exists('idn_to_ascii')) {
67
			return $addresses;
68
		}
69
70
		$convertedAddresses = array();
71
72
		foreach($addresses as $email => $readableName) {
73
			if(!is_numeric($email)) {
74
				list($name, $domain) = explode('@', $email, 2);
75
				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
76
				$convertedAddresses[$name.'@'.$domain] = $readableName;
77
			} else {
78
				list($name, $domain) = explode('@', $readableName, 2);
79
				$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
80
				$convertedAddresses[$email] = $name.'@'.$domain;
81
			}
82
		}
83
84
		return $convertedAddresses;
85
	}
86
87
	/**
88
	 * Set the from address of this message.
89
	 *
90
	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
91
	 *
92
	 * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
93
	 * @return $this
94
	 */
95
	public function setFrom(array $addresses) {
96
		$addresses = $this->convertAddresses($addresses);
97
98
		$this->swiftMessage->setFrom($addresses);
99
		return $this;
100
	}
101
102
	/**
103
	 * Get the from address of this message.
104
	 *
105
	 * @return array
106
	 */
107
	public function getFrom() {
108
		return $this->swiftMessage->getFrom();
109
	}
110
111
	/**
112
	 * Set the Reply-To address of this message
113
	 *
114
	 * @param array $addresses
115
	 * @return $this
116
	 */
117
	public function setReplyTo(array $addresses) {
118
		$addresses = $this->convertAddresses($addresses);
119
120
		$this->swiftMessage->setReplyTo($addresses);
121
		return $this;
122
	}
123
124
	/**
125
	 * Returns the Reply-To address of this message
126
	 *
127
	 * @return array
128
	 */
129
	public function getReplyTo() {
130
		return $this->swiftMessage->getReplyTo();
131
	}
132
133
	/**
134
	 * Set the to addresses of this message.
135
	 *
136
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
137
	 * @return $this
138
	 */
139
	public function setTo(array $recipients) {
140
		$recipients = $this->convertAddresses($recipients);
141
142
		$this->swiftMessage->setTo($recipients);
143
		return $this;
144
	}
145
146
	/**
147
	 * Get the to address of this message.
148
	 *
149
	 * @return array
150
	 */
151
	public function getTo() {
152
		return $this->swiftMessage->getTo();
153
	}
154
155
	/**
156
	 * Set the CC recipients of this message.
157
	 *
158
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
159
	 * @return $this
160
	 */
161
	public function setCc(array $recipients) {
162
		$recipients = $this->convertAddresses($recipients);
163
164
		$this->swiftMessage->setCc($recipients);
165
		return $this;
166
	}
167
168
	/**
169
	 * Get the cc address of this message.
170
	 *
171
	 * @return array
172
	 */
173
	public function getCc() {
174
		return $this->swiftMessage->getCc();
175
	}
176
177
	/**
178
	 * Set the BCC recipients of this message.
179
	 *
180
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
181
	 * @return $this
182
	 */
183
	public function setBcc(array $recipients) {
184
		$recipients = $this->convertAddresses($recipients);
185
186
		$this->swiftMessage->setBcc($recipients);
187
		return $this;
188
	}
189
190
	/**
191
	 * Get the Bcc address of this message.
192
	 *
193
	 * @return array
194
	 */
195
	public function getBcc() {
196
		return $this->swiftMessage->getBcc();
197
	}
198
199
	/**
200
	 * Set the subject of this message.
201
	 *
202
	 * @param $subject
203
	 * @return $this
204
	 */
205
	public function setSubject($subject) {
206
		$this->swiftMessage->setSubject($subject);
207
		return $this;
208
	}
209
210
	/**
211
	 * Get the from subject of this message.
212
	 *
213
	 * @return string
214
	 */
215
	public function getSubject() {
216
		return $this->swiftMessage->getSubject();
217
	}
218
219
	/**
220
	 * Set the plain-text body of this message.
221
	 *
222
	 * @param string $body
223
	 * @return $this
224
	 */
225
	public function setPlainBody($body) {
226
		$this->swiftMessage->setBody($body);
227
		return $this;
228
	}
229
230
	/**
231
	 * Get the plain body of this message.
232
	 *
233
	 * @return string
234
	 */
235
	public function getPlainBody() {
236
		return $this->swiftMessage->getBody();
237
	}
238
239
	/**
240
	 * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
241
	 *
242
	 * @param string $body
243
	 * @return $this
244
	 */
245
	public function setHtmlBody($body) {
246
		$this->swiftMessage->addPart($body, 'text/html');
247
		return $this;
248
	}
249
250
	/**
251
	 * Get's the underlying SwiftMessage
252
	 * @return Swift_Message
253
	 */
254
	public function getSwiftMessage() {
255
		return $this->swiftMessage;
256
	}
257
258
	/**
259
	 * @param string $body
260
	 * @param string $contentType
261
	 * @return $this
262
	 */
263
	public function setBody($body, $contentType) {
264
		$this->swiftMessage->setBody($body, $contentType);
265
		return $this;
266
	}
267
268
	/**
269
	 * @param IEMailTemplate $emailTemplate
270
	 * @return $this
271
	 */
272
	public function useTemplate(IEMailTemplate $emailTemplate) {
273
		$this->setSubject($emailTemplate->renderSubject());
274
		$this->setPlainBody($emailTemplate->renderText());
275
		$this->setHtmlBody($emailTemplate->renderHtml());
276
		return $this;
277
	}
278
}
279