1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Utils\Mailer; |
3
|
|
|
|
4
|
|
|
use Html2Text\Html2Text; |
5
|
|
|
use Pelago\Emogrifier; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class represents a mail to be sent using a Mailer class extending the MailerInterface. |
9
|
|
|
* + it has special features to add a text mail for any HTML mail that has not been provided the text mail. |
10
|
|
|
* |
11
|
|
|
* Note: default encoding for the mail is UTF-8 if not specified. |
12
|
|
|
* |
13
|
|
|
* @Component |
14
|
|
|
*/ |
15
|
|
|
class Mail implements MailInterface { |
|
|
|
|
16
|
|
|
|
17
|
|
|
protected $bodyText; |
18
|
|
|
protected $bodyHtml; |
19
|
|
|
protected $title; |
20
|
|
|
protected $from; |
21
|
|
|
protected $toRecipients = array(); |
22
|
|
|
protected $ccRecipients = array(); |
23
|
|
|
protected $bccRecipients = array(); |
24
|
|
|
protected $attachements = array(); |
25
|
|
|
protected $encoding = "utf-8"; |
26
|
|
|
protected $autocreateMissingText = true; |
27
|
|
|
protected $css; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns the mail text body. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getBodyText() { |
35
|
|
|
if ($this->bodyText != null) { |
36
|
|
|
return $this->bodyText; |
37
|
|
|
} elseif ($this->autocreateMissingText == true) { |
|
|
|
|
38
|
|
|
return Html2Text::convert($this->getBodyHtml()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The mail text body. |
44
|
|
|
* |
45
|
|
|
* @Property |
46
|
|
|
* @param string $bodyText |
47
|
|
|
*/ |
48
|
|
|
public function setBodyText($bodyText) { |
49
|
|
|
$this->bodyText = $bodyText; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the HTML text before "emogrification". |
54
|
|
|
* This method can be overwritten by subclasses to overwrite the mail body and still applying "emogrification". |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function getBodyHtmlBeforeEmogrify() { |
59
|
|
|
return $this->bodyHtml; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the mail html body. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getBodyHtml() { |
68
|
|
|
if ($this->css) { |
69
|
|
|
$emogrifier = new Emogrifier($this->getBodyHtmlBeforeEmogrify(), $this->css); |
|
|
|
|
70
|
|
|
$finalHtml = $emogrifier->emogrify(); |
71
|
|
|
} else { |
72
|
|
|
$finalHtml = $this->getBodyHtmlBeforeEmogrify(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $finalHtml; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The mail html body. |
80
|
|
|
* |
81
|
|
|
* @Property |
82
|
|
|
* @param string $bodyHtml |
83
|
|
|
*/ |
84
|
|
|
public function setBodyHtml($bodyHtml) { |
85
|
|
|
$this->bodyHtml = $bodyHtml; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the mail title. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getTitle() { |
94
|
|
|
return $this->title; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The mail title. |
99
|
|
|
* |
100
|
|
|
* @Property |
101
|
|
|
* @param string $title |
102
|
|
|
*/ |
103
|
|
|
public function setTitle($title) { |
104
|
|
|
$this->title = $title; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the "From" email address |
109
|
|
|
* |
110
|
|
|
* @return MailAddressInterface The first element is the email address, the second the name to display. |
111
|
|
|
*/ |
112
|
|
|
public function getFrom() { |
113
|
|
|
return $this->from; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The mail from address. |
118
|
|
|
* |
119
|
|
|
* @Property |
120
|
|
|
* @param MailAddressInterface $from |
121
|
|
|
*/ |
122
|
|
|
public function setFrom(MailAddressInterface $from) { |
123
|
|
|
$this->from = $from; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns an array containing the recipients. |
128
|
|
|
* |
129
|
|
|
* @return MailAddressInterface[] |
130
|
|
|
*/ |
131
|
|
|
public function getToRecipients() { |
132
|
|
|
return $this->toRecipients; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* An array containing the recipients. |
137
|
|
|
* |
138
|
|
|
* @Property |
139
|
|
|
* @param MailAddressInterface[] $toRecipients |
140
|
|
|
*/ |
141
|
|
|
public function setToRecipients(array $toRecipients) { |
142
|
|
|
$this->toRecipients = $toRecipients; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adss a recipient. |
147
|
|
|
* |
148
|
|
|
* @param MailAddressInterface $toRecipient |
149
|
|
|
*/ |
150
|
|
|
public function addToRecipient(MailAddressInterface $toRecipient) { |
151
|
|
|
$this->toRecipients[] = $toRecipient; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns an array containing the recipients in Cc. |
156
|
|
|
* |
157
|
|
|
* @return MailAddressInterface[] |
158
|
|
|
*/ |
159
|
|
|
public function getCcRecipients() { |
160
|
|
|
return $this->ccRecipients; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* An array containing the recipients. |
165
|
|
|
* |
166
|
|
|
* @Property |
167
|
|
|
* @param MailAddressInterface[] $ccRecipients |
168
|
|
|
*/ |
169
|
|
|
public function setCcRecipients(array $ccRecipients) { |
170
|
|
|
$this->ccRecipients = $ccRecipients; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Adds a recipient. |
175
|
|
|
* |
176
|
|
|
* @param MailAddressInterface $ccRecipient |
177
|
|
|
*/ |
178
|
|
|
public function addCcRecipient(MailAddressInterface $ccRecipient) { |
179
|
|
|
$this->ccRecipients[] = $ccRecipient; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns an array containing the recipients in Bcc. |
184
|
|
|
* |
185
|
|
|
* @return MailAddressInterface[] |
186
|
|
|
*/ |
187
|
|
|
public function getBccRecipients() { |
188
|
|
|
return $this->bccRecipients; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* An array containing the recipients. |
193
|
|
|
* |
194
|
|
|
* @Property |
195
|
|
|
* @param MailAddressInterface[] $bccRecipients |
196
|
|
|
*/ |
197
|
|
|
public function setBccRecipients(array $bccRecipients) { |
198
|
|
|
$this->bccRecipients = $bccRecipients; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Adds a recipient. |
203
|
|
|
* |
204
|
|
|
* @param MailAddressInterface $bccRecipient |
205
|
|
|
*/ |
206
|
|
|
public function addBccRecipient(MailAddressInterface $bccRecipient) { |
207
|
|
|
$this->bccRecipients[] = $bccRecipient; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns an array of attachements for that mail. |
212
|
|
|
* |
213
|
|
|
* @return array<MailAttachmentInterface> |
214
|
|
|
*/ |
215
|
|
|
public function getAttachements() { |
216
|
|
|
return $this->attachements; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* An array containing the attachments. |
221
|
|
|
* |
222
|
|
|
* @Property |
223
|
|
|
* @param array<MailAttachmentInterface> $attachements |
224
|
|
|
*/ |
225
|
|
|
public function setAttachements(array $attachements) { |
226
|
|
|
$this->attachements = $attachements; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Adds an attachment. |
231
|
|
|
* |
232
|
|
|
* @param MailAttachmentInterface attachement |
233
|
|
|
*/ |
234
|
|
|
public function addAttachement(MailAttachmentInterface $attachement) { |
235
|
|
|
$this->attachements[] = $attachement; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the encoding of the mail. |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function getEncoding() { |
244
|
|
|
return $this->encoding; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* The mail encoding. Defaults to utf-8. |
249
|
|
|
* |
250
|
|
|
* @Property |
251
|
|
|
* @param string $encoding |
252
|
|
|
*/ |
253
|
|
|
public function setEncoding($encoding) { |
254
|
|
|
$this->encoding = $encoding; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* If no body text is set for that mail, and if autoCreateBodyText is set to true, this object will create the body text from the body HTML text, |
259
|
|
|
* by removing any tags. |
260
|
|
|
* |
261
|
|
|
* @param boolean $autoCreate |
262
|
|
|
*/ |
263
|
|
|
public function autoCreateBodyText($autoCreate) { |
264
|
|
|
$this->autocreateMissingText = $autoCreate; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Registers some CSS to be applied to the HTML. |
269
|
|
|
* When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS. |
270
|
|
|
* |
271
|
|
|
* CSS is inlined using the Emogrifier library. |
272
|
|
|
* |
273
|
|
|
* @param string $css The CSS to apply. |
274
|
|
|
*/ |
275
|
|
|
public function addCssText($css) { |
276
|
|
|
$this->css .= $css; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Registers a CSS file to be applied to the HTML. |
281
|
|
|
* When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS. |
282
|
|
|
* |
283
|
|
|
* CSS is inlined using the Emogrifier library. |
284
|
|
|
* |
285
|
|
|
* @param string $file The CSS file to apply. |
286
|
|
|
*/ |
287
|
|
|
public function addCssFile($file) { |
288
|
|
|
$this->css .= file_get_contents($file); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.