|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Mailer | Mailer.php |
|
4
|
|
|
* @package Faulancer\Mail |
|
5
|
|
|
* @author Florian Knapp <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Faulancer\Mail; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Mailer |
|
11
|
|
|
* |
|
12
|
|
|
* +--Example usage: |
|
13
|
|
|
* | |
|
14
|
|
|
* | $mail = new Mailer(); |
|
15
|
|
|
* | $mail->addTo('[email protected]'); |
|
16
|
|
|
* | $mail->addCc('[email protected]'); |
|
17
|
|
|
* | $mail->addBcc('[email protected]'); |
|
18
|
|
|
* | $mail->setFrom('Test User <[email protected]>'); |
|
19
|
|
|
* | $mail->setSubject('Testsubject'); |
|
20
|
|
|
* | $mail->addAttachment('shownFileName.png', '/absolute/path/to/file.png'); |
|
21
|
|
|
* | $mail->addAttachment('logo.png', '/absolute/path/to/logo.png', true); // We want to show it inline |
|
22
|
|
|
* | $mail->setContent('<img src="cid:logo.png"><br /><h3>Welcome to our newsletter</h3>... ... ...'); |
|
23
|
|
|
* | $mail->send(); // Html is on per default; give this method 'false' to send in plain text |
|
24
|
|
|
* | |
|
25
|
|
|
*/ |
|
26
|
|
|
class Mailer |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $recipients = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $carbonCopies = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $blindCarbonCopies = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $from = ''; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $replyTo = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $subject = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $content = ''; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $isHtml = false; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $attachment = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $boundary = ''; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
*/ |
|
82
|
|
|
public function addTo(string $name) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->recipients[] = $name; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addCc(string $name) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->carbonCopies[] = $name; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
*/ |
|
98
|
|
|
public function addBcc(string $name) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->blindCarbonCopies[] = $name; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setFrom(string $name) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->from = $name; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setReplyTo(string $name) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->replyTo = $name; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $subject |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setSubject(string $subject) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->subject = $subject; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function setContent(string $content) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->content = $content; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $name |
|
131
|
|
|
* @param string $path |
|
132
|
|
|
* @param bool $inline |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addAttachment(string $name = '', string $path, bool $inline = false) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->attachment[] = [ |
|
137
|
|
|
'name' => $name, |
|
138
|
|
|
'path' => $path, |
|
139
|
|
|
'encoded' => chunk_split(base64_encode(file_get_contents($path))), |
|
140
|
|
|
'mimetype' => mime_content_type($path), |
|
141
|
|
|
'inline' => $inline |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param bool $html |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
public function send($html = true) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->isHtml = $html; |
|
152
|
|
|
return $this->sendMail($this->getHeaders(), $this->getMessage()); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $headers |
|
157
|
|
|
* @param string $message |
|
158
|
|
|
* @return bool |
|
159
|
|
|
* @codeCoverageIgnore |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function sendMail($headers, $message) |
|
162
|
|
|
{ |
|
163
|
|
|
if (mail(implode(',', $this->recipients), $this->subject,$headers, $message)) { |
|
164
|
|
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getBoundary() |
|
173
|
|
|
{ |
|
174
|
|
|
if (empty($this->boundary)) { |
|
175
|
|
|
$this->boundary = md5('faulancer'); |
|
176
|
|
|
} |
|
177
|
|
|
return $this->boundary; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function getHeaders() |
|
184
|
|
|
{ |
|
185
|
|
|
$boundary = $this->getBoundary(); |
|
186
|
|
|
$headers = 'MIME-Version: 1.0' . PHP_EOL; |
|
187
|
|
|
|
|
188
|
|
|
if (!empty($this->from)) { |
|
189
|
|
|
$headers .= 'From: ' .$this->from . PHP_EOL; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if (!empty($this->replyTo)) { |
|
193
|
|
|
$headers .= 'Reply-To: ' . $this->replyTo . PHP_EOL; |
|
194
|
|
|
} else if (!empty($this->from)) { |
|
195
|
|
|
$headers .= 'Reply-To: ' . $this->from . PHP_EOL; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
View Code Duplication |
if (!empty($this->carbonCopies)) { |
|
|
|
|
|
|
199
|
|
|
$headers .= 'CC: ' . implode(',', $this->carbonCopies) . PHP_EOL; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
View Code Duplication |
if (!empty($this->blindCarbonCopies)) { |
|
|
|
|
|
|
203
|
|
|
$headers .= 'BCC: ' . implode(',', $this->blindCarbonCopies) . PHP_EOL; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$headers .= 'Content-Type: multipart/mixed; boundary = ' . $boundary . PHP_EOL; |
|
207
|
|
|
|
|
208
|
|
|
return $headers; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function getMessage() |
|
215
|
|
|
{ |
|
216
|
|
|
$boundary = $this->getBoundary(); |
|
217
|
|
|
$body = '--' . $boundary . PHP_EOL; |
|
218
|
|
|
|
|
219
|
|
|
if ($this->isHtml) { |
|
220
|
|
|
$body .= 'Content-Type: text/html; charset=utf-8' . PHP_EOL; |
|
221
|
|
|
} else { |
|
222
|
|
|
$body .= 'Content-Type: text/plain; charset=utf-8' . PHP_EOL; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$body .= 'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL; |
|
226
|
|
|
$body .= chunk_split(base64_encode($this->content)); |
|
227
|
|
|
|
|
228
|
|
|
if (!empty($this->attachment)) { |
|
229
|
|
|
|
|
230
|
|
|
foreach ($this->attachment as $attachment) { |
|
231
|
|
|
|
|
232
|
|
|
$encodedContent = $attachment['encoded']; |
|
233
|
|
|
$fileType = $attachment['mimetype']; |
|
234
|
|
|
$fileName = $attachment['name']; |
|
235
|
|
|
|
|
236
|
|
|
$body .= '--' . $boundary . PHP_EOL; |
|
237
|
|
|
$body .= 'Content-Type: ' . $fileType . '; name=' . $fileName . PHP_EOL; |
|
238
|
|
|
|
|
239
|
|
|
if ($attachment['inline']) { |
|
240
|
|
|
$body .= 'Content-ID: <' . $fileName . '>' . PHP_EOL; |
|
241
|
|
|
$body .= 'Content-Disposition: inline; filename=' . $fileName . PHP_EOL; |
|
242
|
|
|
} else { |
|
243
|
|
|
$body .= 'Content-Disposition: attachment; filename=' . $fileName . PHP_EOL; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$body .= 'Content-Transfer-Encoding: base64' . PHP_EOL; |
|
247
|
|
|
$body .= 'X-Attachment-Id: ' . rand(1000,99999) . PHP_EOL . PHP_EOL; |
|
248
|
|
|
$body .= $encodedContent; |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$body .= '--' . $boundary . '--' . PHP_EOL; |
|
255
|
|
|
|
|
256
|
|
|
return $body; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
} |
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.