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\Headers\AutoSubmitted; |
35
|
|
|
use OCP\Mail\IAttachment; |
36
|
|
|
use OCP\Mail\IEMailTemplate; |
37
|
|
|
use OCP\Mail\IMessage; |
38
|
|
|
use Swift_Message; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class Message provides a wrapper around SwiftMail |
42
|
|
|
* |
43
|
|
|
* @package OC\Mail |
44
|
|
|
*/ |
45
|
|
|
class Message implements IMessage { |
46
|
|
|
/** @var Swift_Message */ |
47
|
|
|
private $swiftMessage; |
48
|
|
|
/** @var bool */ |
49
|
|
|
private $plainTextOnly; |
50
|
|
|
|
51
|
|
|
public function __construct(Swift_Message $swiftMessage, bool $plainTextOnly) { |
52
|
|
|
$this->swiftMessage = $swiftMessage; |
53
|
|
|
$this->plainTextOnly = $plainTextOnly; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param IAttachment $attachment |
58
|
|
|
* @return $this |
59
|
|
|
* @since 13.0.0 |
60
|
|
|
*/ |
61
|
|
|
public function attach(IAttachment $attachment): IMessage { |
62
|
|
|
/** @var Attachment $attachment */ |
63
|
|
|
$this->swiftMessage->attach($attachment->getSwiftAttachment()); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
69
|
|
|
* FIXME: Remove this once SwiftMailer supports IDN |
70
|
|
|
* |
71
|
|
|
* @param array $addresses Array of mail addresses, key will get converted |
72
|
|
|
* @return array Converted addresses if `idn_to_ascii` exists |
73
|
|
|
*/ |
74
|
|
|
protected function convertAddresses(array $addresses): array { |
75
|
|
|
if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) { |
76
|
|
|
return $addresses; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$convertedAddresses = []; |
80
|
|
|
|
81
|
|
|
foreach ($addresses as $email => $readableName) { |
82
|
|
|
$parsableEmail = is_numeric($email) ? $readableName : $email; |
83
|
|
|
if (strpos($parsableEmail, '@') === false) { |
84
|
|
|
$convertedAddresses[$parsableEmail] = $readableName; |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
[$name, $domain] = explode('@', $parsableEmail, 2); |
89
|
|
|
$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); |
90
|
|
|
if (is_numeric($email)) { |
91
|
|
|
$convertedAddresses[] = $name . '@' . $domain; |
92
|
|
|
} else { |
93
|
|
|
$convertedAddresses[$name . '@' . $domain] = $readableName; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $convertedAddresses; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the from address of this message. |
102
|
|
|
* |
103
|
|
|
* If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
104
|
|
|
* |
105
|
|
|
* @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setFrom(array $addresses): IMessage { |
109
|
|
|
$addresses = $this->convertAddresses($addresses); |
110
|
|
|
|
111
|
|
|
$this->swiftMessage->setFrom($addresses); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the from address of this message. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getFrom(): array { |
121
|
|
|
return $this->swiftMessage->getFrom() ?? []; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the Reply-To address of this message |
126
|
|
|
* |
127
|
|
|
* @param array $addresses |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function setReplyTo(array $addresses): IMessage { |
131
|
|
|
$addresses = $this->convertAddresses($addresses); |
132
|
|
|
|
133
|
|
|
$this->swiftMessage->setReplyTo($addresses); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns the Reply-To address of this message |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getReplyTo(): string { |
143
|
|
|
return $this->swiftMessage->getReplyTo(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the to addresses of this message. |
148
|
|
|
* |
149
|
|
|
* @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function setTo(array $recipients): IMessage { |
153
|
|
|
$recipients = $this->convertAddresses($recipients); |
154
|
|
|
|
155
|
|
|
$this->swiftMessage->setTo($recipients); |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the to address of this message. |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getTo(): array { |
165
|
|
|
return $this->swiftMessage->getTo() ?? []; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the CC recipients of this message. |
170
|
|
|
* |
171
|
|
|
* @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setCc(array $recipients): IMessage { |
175
|
|
|
$recipients = $this->convertAddresses($recipients); |
176
|
|
|
|
177
|
|
|
$this->swiftMessage->setCc($recipients); |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the cc address of this message. |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getCc(): array { |
187
|
|
|
return $this->swiftMessage->getCc() ?? []; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set the BCC recipients of this message. |
192
|
|
|
* |
193
|
|
|
* @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function setBcc(array $recipients): IMessage { |
197
|
|
|
$recipients = $this->convertAddresses($recipients); |
198
|
|
|
|
199
|
|
|
$this->swiftMessage->setBcc($recipients); |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the Bcc address of this message. |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function getBcc(): array { |
209
|
|
|
return $this->swiftMessage->getBcc() ?? []; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set the subject of this message. |
214
|
|
|
* |
215
|
|
|
* @param string $subject |
216
|
|
|
* @return IMessage |
217
|
|
|
*/ |
218
|
|
|
public function setSubject(string $subject): IMessage { |
219
|
|
|
$this->swiftMessage->setSubject($subject); |
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get the from subject of this message. |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getSubject(): string { |
229
|
|
|
return $this->swiftMessage->getSubject(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set the plain-text body of this message. |
234
|
|
|
* |
235
|
|
|
* @param string $body |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function setPlainBody(string $body): IMessage { |
239
|
|
|
$this->swiftMessage->setBody($body); |
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get the plain body of this message. |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function getPlainBody(): string { |
249
|
|
|
return $this->swiftMessage->getBody(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
254
|
|
|
* |
255
|
|
|
* @param string $body |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setHtmlBody($body) { |
259
|
|
|
if (!$this->plainTextOnly) { |
260
|
|
|
$this->swiftMessage->addPart($body, 'text/html'); |
261
|
|
|
} |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get's the underlying SwiftMessage |
267
|
|
|
* @param Swift_Message $swiftMessage |
268
|
|
|
*/ |
269
|
|
|
public function setSwiftMessage(Swift_Message $swiftMessage): void { |
270
|
|
|
$this->swiftMessage = $swiftMessage; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get's the underlying SwiftMessage |
275
|
|
|
* @return Swift_Message |
276
|
|
|
*/ |
277
|
|
|
public function getSwiftMessage(): Swift_Message { |
278
|
|
|
return $this->swiftMessage; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $body |
283
|
|
|
* @param string $contentType |
284
|
|
|
* @return $this |
285
|
|
|
*/ |
286
|
|
|
public function setBody($body, $contentType) { |
287
|
|
|
if (!$this->plainTextOnly || $contentType !== 'text/html') { |
288
|
|
|
$this->swiftMessage->setBody($body, $contentType); |
289
|
|
|
} |
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param IEMailTemplate $emailTemplate |
295
|
|
|
* @return $this |
296
|
|
|
*/ |
297
|
|
|
public function useTemplate(IEMailTemplate $emailTemplate): IMessage { |
298
|
|
|
$this->setSubject($emailTemplate->renderSubject()); |
299
|
|
|
$this->setPlainBody($emailTemplate->renderText()); |
300
|
|
|
if (!$this->plainTextOnly) { |
301
|
|
|
$this->setHtmlBody($emailTemplate->renderHtml()); |
302
|
|
|
} |
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Add the Auto-Submitted header to the email, preventing most automated |
308
|
|
|
* responses to automated messages. |
309
|
|
|
* |
310
|
|
|
* @param AutoSubmitted::VALUE_* $value (one of AutoSubmitted::VALUE_NO, AutoSubmitted::VALUE_AUTO_GENERATED, AutoSubmitted::VALUE_AUTO_REPLIED) |
311
|
|
|
* @return $this |
312
|
|
|
*/ |
313
|
|
|
public function setAutoSubmitted(string $value): IMessage { |
314
|
|
|
$headers = $this->swiftMessage->getHeaders(); |
315
|
|
|
|
316
|
|
|
if ($headers->has(AutoSubmitted::HEADER)) { |
317
|
|
|
// if the header already exsists, remove it. |
318
|
|
|
// the value can be modified with some implementations |
319
|
|
|
// of the interface \Swift_Mime_Header, however the |
320
|
|
|
// interface doesn't, and this makes the static-code |
321
|
|
|
// analysis unhappy. |
322
|
|
|
$headers->remove(AutoSubmitted::HEADER); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$headers->addTextHeader(AutoSubmitted::HEADER, $value); |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Get the current value of the Auto-Submitted header. Defaults to "no" |
332
|
|
|
* which is equivalent to the header not existing at all |
333
|
|
|
* |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function getAutoSubmitted(): string { |
337
|
|
|
$headers = $this->swiftMessage->getHeaders(); |
338
|
|
|
|
339
|
|
|
return $headers->has(AutoSubmitted::HEADER) ? |
340
|
|
|
$headers->get(AutoSubmitted::HEADER)->toString() : AutoSubmitted::VALUE_NO; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths