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