Passed
Push — master ( 52957d...b93e1e )
by Roeland
24:45 queued 12:33
created

Message::setSwiftMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Jared Boone <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Arne Hamann <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC\Mail;
33
34
use OCP\Mail\IAttachment;
35
use OCP\Mail\IEMailTemplate;
36
use OCP\Mail\IMessage;
37
use Swift_Message;
0 ignored issues
show
Bug introduced by
The type Swift_Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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