Passed
Push — master ( aa3be1...5c4a66 )
by Julius
11:35 queued 11s
created

Message::useTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
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 Arne Hamann <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Jared Boone <[email protected]>
11
 * @author Joas Schilling <[email protected]>
12
 * @author Lukas Reschke <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
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
			$parsableEmail = is_numeric($email) ? $readableName : $email;
82
			if (strpos($parsableEmail, '@') === false) {
83
				$convertedAddresses[$parsableEmail] = $readableName;
84
				continue;
85
			}
86
87
			[$name, $domain] = explode('@', $parsableEmail, 2);
88
			$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
89
			if (is_numeric($email)) {
90
				$convertedAddresses[] = $name . '@' . $domain;
91
			} else {
92
				$convertedAddresses[$name . '@' . $domain] = $readableName;
93
			}
94
		}
95
96
		return $convertedAddresses;
97
	}
98
99
	/**
100
	 * Set the from address of this message.
101
	 *
102
	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
103
	 *
104
	 * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
105
	 * @return $this
106
	 */
107
	public function setFrom(array $addresses): IMessage {
108
		$addresses = $this->convertAddresses($addresses);
109
110
		$this->swiftMessage->setFrom($addresses);
111
		return $this;
112
	}
113
114
	/**
115
	 * Get the from address of this message.
116
	 *
117
	 * @return array
118
	 */
119
	public function getFrom(): array {
120
		return $this->swiftMessage->getFrom() ?? [];
121
	}
122
123
	/**
124
	 * Set the Reply-To address of this message
125
	 *
126
	 * @param array $addresses
127
	 * @return $this
128
	 */
129
	public function setReplyTo(array $addresses): IMessage {
130
		$addresses = $this->convertAddresses($addresses);
131
132
		$this->swiftMessage->setReplyTo($addresses);
133
		return $this;
134
	}
135
136
	/**
137
	 * Returns the Reply-To address of this message
138
	 *
139
	 * @return string
140
	 */
141
	public function getReplyTo(): string {
142
		return $this->swiftMessage->getReplyTo();
143
	}
144
145
	/**
146
	 * Set the to addresses of this message.
147
	 *
148
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
149
	 * @return $this
150
	 */
151
	public function setTo(array $recipients): IMessage {
152
		$recipients = $this->convertAddresses($recipients);
153
154
		$this->swiftMessage->setTo($recipients);
155
		return $this;
156
	}
157
158
	/**
159
	 * Get the to address of this message.
160
	 *
161
	 * @return array
162
	 */
163
	public function getTo(): array {
164
		return $this->swiftMessage->getTo() ?? [];
165
	}
166
167
	/**
168
	 * Set the CC recipients of this message.
169
	 *
170
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
171
	 * @return $this
172
	 */
173
	public function setCc(array $recipients): IMessage {
174
		$recipients = $this->convertAddresses($recipients);
175
176
		$this->swiftMessage->setCc($recipients);
177
		return $this;
178
	}
179
180
	/**
181
	 * Get the cc address of this message.
182
	 *
183
	 * @return array
184
	 */
185
	public function getCc(): array {
186
		return $this->swiftMessage->getCc() ?? [];
187
	}
188
189
	/**
190
	 * Set the BCC recipients of this message.
191
	 *
192
	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
193
	 * @return $this
194
	 */
195
	public function setBcc(array $recipients): IMessage {
196
		$recipients = $this->convertAddresses($recipients);
197
198
		$this->swiftMessage->setBcc($recipients);
199
		return $this;
200
	}
201
202
	/**
203
	 * Get the Bcc address of this message.
204
	 *
205
	 * @return array
206
	 */
207
	public function getBcc(): array {
208
		return $this->swiftMessage->getBcc() ?? [];
209
	}
210
211
	/**
212
	 * Set the subject of this message.
213
	 *
214
	 * @param string $subject
215
	 * @return IMessage
216
	 */
217
	public function setSubject(string $subject): IMessage {
218
		$this->swiftMessage->setSubject($subject);
219
		return $this;
220
	}
221
222
	/**
223
	 * Get the from subject of this message.
224
	 *
225
	 * @return string
226
	 */
227
	public function getSubject(): string {
228
		return $this->swiftMessage->getSubject();
229
	}
230
231
	/**
232
	 * Set the plain-text body of this message.
233
	 *
234
	 * @param string $body
235
	 * @return $this
236
	 */
237
	public function setPlainBody(string $body): IMessage {
238
		$this->swiftMessage->setBody($body);
239
		return $this;
240
	}
241
242
	/**
243
	 * Get the plain body of this message.
244
	 *
245
	 * @return string
246
	 */
247
	public function getPlainBody(): string {
248
		return $this->swiftMessage->getBody();
249
	}
250
251
	/**
252
	 * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
253
	 *
254
	 * @param string $body
255
	 * @return $this
256
	 */
257
	public function setHtmlBody($body) {
258
		if (!$this->plainTextOnly) {
259
			$this->swiftMessage->addPart($body, 'text/html');
260
		}
261
		return $this;
262
	}
263
264
	/**
265
	 * Get's the underlying SwiftMessage
266
	 * @param Swift_Message $swiftMessage
267
	 */
268
	public function setSwiftMessage(Swift_Message $swiftMessage): void {
269
		$this->swiftMessage = $swiftMessage;
270
	}
271
272
	/**
273
	 * Get's the underlying SwiftMessage
274
	 * @return Swift_Message
275
	 */
276
	public function getSwiftMessage(): Swift_Message {
277
		return $this->swiftMessage;
278
	}
279
280
	/**
281
	 * @param string $body
282
	 * @param string $contentType
283
	 * @return $this
284
	 */
285
	public function setBody($body, $contentType) {
286
		if (!$this->plainTextOnly || $contentType !== 'text/html') {
287
			$this->swiftMessage->setBody($body, $contentType);
288
		}
289
		return $this;
290
	}
291
292
	/**
293
	 * @param IEMailTemplate $emailTemplate
294
	 * @return $this
295
	 */
296
	public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
297
		$this->setSubject($emailTemplate->renderSubject());
298
		$this->setPlainBody($emailTemplate->renderText());
299
		if (!$this->plainTextOnly) {
300
			$this->setHtmlBody($emailTemplate->renderHtml());
301
		}
302
		return $this;
303
	}
304
}
305