-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipartEmail.php
425 lines (378 loc) · 11.1 KB
/
MultipartEmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/**
* PHP Multipart Mailer
* @author Aleksandr.ru
* @url http://aleksandr.ru
* @version 1.0 18.04.2012
* @version 1.0.1 01.10.2013 ('\r\n' replaced with '\n' when building message)
* @version 1.0.2 06.03.2015 ($add_cid parameter added, html parsing for cid src, 'multipart/related' header in case of embedded images)
* @version 1.0.3 22.09.2015 (added $charset parameter to constructor for non utf8 usage)
* @version 1.0.4 25.12.2017 (added CSS "url('image.ext')" replacement with attachment CID)
* @version 1.0.5 20.03.2020 (added backtrace and optional exception for send errors)
* @version 1.0.6 23.07.2020 (image CID fix when send multiple times)
* @version 1.0.7 02.10.2020 (composer support)
* @version 1.1 09.09.2022 (named headers and reply-to feature)
* @version 1.1.1 29.02.2023 (email regexp allows absence of space between name and address)
* @version 1.1.2 02.11.2024 (php8 hotfix: message must starts with '\n')
* @version 1.1.3 05.11.2024 (fixed trim nulls deprecation)
*/
class MultipartEmail
{
const EMAIL_REGEXP = "/^(.+)\s*<(.+\@.+)>$/i";
protected $charset = 'UTF-8';
protected $text = '';
protected $html = '';
protected $from = '';
protected $to = '';
protected $reply_to = '';
protected $subject = '';
protected $attachments = array();
protected $headers = array();
private $att_counter = 0;
/**
* init. set charset for all data (from, to, subject, text and html)
* @param string $charset for non utf8 usage
* @see iconv
*
* @return void
*/
function __construct($charset = 'UTF-8')
{
$this->charset = $charset;
}
/**
* set the plain text of email
* @param string $val
*
* @return void
*/
function setText($val)
{
$this->text = $val;
}
/**
* get plain text part of email
*
* @return string
*/
function getText()
{
return $this->text;
}
/**
* set html part of email
* @param string $val
*
* @return void
*/
function setHtml($val)
{
$this->html = $val;
}
/**
* get html part of email
*
* @return string
*/
function getHtml()
{
return $this->html;
}
/**
* set TO address ('[email protected]' or 'some name <[email protected]>')
* multi addresses can be divided by , or ;
* @param string $val
*
* @return void
*/
function setTo($val)
{
$this->to = $val;
}
/**
* get TO address
*
* @return string
*/
function getTo()
{
return $this->to;
}
/**
* set REPLY-TO address ('[email protected]' or 'some name <[email protected]>')
* @param string $val
*
* @return void
*/
function setReplyTo($val)
{
$this->reply_to = $val;
}
/**
* get REPLY-TO address
*
* @return string
*/
function getReplyTo()
{
return $this->reply_to;
}
/**
* set FROM address ('[email protected]' or 'some name <[email protected]>')
* @param string $val
*
* @return void
*/
function setFrom($val)
{
$this->from = $val;
}
/**
* get FROM address
*
* @return string
*/
function getFrom()
{
return $this->from;
}
/**
* set email SUBJECT
* @param string $val
*
* @return void
*/
function setSubject($val)
{
$this->subject = $val;
}
/**
* get email SUBJECT
*
* @return string
*/
function getSubject()
{
return $this->subject;
}
/**
* add attachment to email
* @param string $file filename or data of attachment
* @param string $mime_type
* @param string $name filename of attachment
* @param bool $file_is_data if TRUE use $file as data, if FALSE load data from $file
* @param bool $add_cid add content-id to embed in html part by parsing src='...' attribute
*
* @return integer attachment id (index)
*/
function addAttachment($file, $mime_type, $name, $file_is_data = false, $add_cid = false)
{
$this->att_counter++;
$this->attachments[$this->att_counter]['data'] = $file_is_data ? $file : file_get_contents($file);
$this->attachments[$this->att_counter]['mime_type'] = $mime_type;
$this->attachments[$this->att_counter]['name'] = $name;
$this->attachments[$this->att_counter]['cid'] = $add_cid ? uniqid('MPM-cid-') : null;
if(!$this->attachments[$this->att_counter]['data'] || !$this->attachments[$this->att_counter]['mime_type'] || !$this->attachments[$this->att_counter]['name']) {
unset($this->attachments[$this->att_counter]);
trigger_error("Failed to add an attachment!", E_USER_WARNING);
return false;
}
return $this->att_counter;
}
/**
* remove attachment
* @param integer $id index of the attachment
*
* @return bool
*/
function removeAttachment($id)
{
if(isset($this->attachments[$id])) {
unset($this->attachments[$id]);
return true;
} else {
trigger_error("Attachment not found");
return false;
}
}
/**
* @deprecated backwards compatibility
*/
function addAttachement($file, $mime_type, $name, $file_is_data = false, $add_cid = false)
{
return $this->addAttachment($file, $mime_type, $name, $file_is_data, $add_cid);
}
/**
* @deprecated backwards compatibility
*/
function removeAttachement($id)
{
return $this->removeAttachment($id);
}
/**
* add extra header to email ('Header-name: header-value')
* caution! any message header can be redefined with this
* @param string $header
*
* @return string header-name
*/
function addHeader($header)
{
if(!preg_match('/^(.+):.+/', $header, $matches)) {
trigger_error(sprintf('Invalid header "%s"', $header), E_USER_WARNING);
return false;
}
$key = strtolower($matches[1]);
$this->headers[$key] = $header;
return $key;
}
/**
* get extra header value
* @param string $key header-name
*
* @return string
*/
function getHeader($key)
{
$key = strtolower($key);
if(isset($this->headers[$key])) {
return $this->headers[$key];
} else {
trigger_error(sprintf('Header "%s" not found', $key));
return false;
}
}
/**
* remove an extra header
* @param string $key header-name
*
* @return bool
*/
function removeHeader($key)
{
$key = strtolower($key);
if(isset($this->headers[$key])) {
unset($this->headers[$key]);
return true;
} else {
trigger_error(sprintf('Header "%s" not found', $key));
return false;
}
}
/**
* send mail
* @param bool $throw_exception in case of empty TO
*
* @return bool
* @throws Exception
*/
function send($throw_exception = false)
{
if(!$this->to) {
$e = new Exception("Send failed, TO is empty!");
if($throw_exception) {
throw $e;
}
trigger_error($e->getMessage() . " Trace:\n" . $e->getTraceAsString(), E_USER_WARNING);
return false;
}
$html = $this->html;
$multipart = ($html || sizeof($this->attachments));
$related = false; // default i.e. no embedded images in html
$boundary_part = uniqid('MPM-part-');
$boundary_alt = uniqid('MPM-alt-');
if($html && sizeof($this->attachments)) {
foreach($this->attachments as $i => $a) if($a['cid'] && preg_match("/^image/", $a['mime_type'])) {
$html = preg_replace("/src=(['\"]?)".addslashes($this->attachments[$i]['name'])."(['\"]?)/i",
"src=$1cid:{$this->attachments[$i]['cid']}$2", $html);
$html = preg_replace("/url\((['\"]?)".addslashes($this->attachments[$i]['name'])."(['\"]?)\)/i",
"url(cid:{$this->attachments[$i]['cid']})", $html);
$related = true;
}
}
/* headers */
if(preg_match(self::EMAIL_REGEXP, trim($this->from), $arr)) {
$from = "=?UTF-8?B?".base64_encode($this->toUTF8($arr[1]))."?= <{$arr[2]}>";
} else {
$from = $this->from;
}
if(preg_match(self::EMAIL_REGEXP, trim($this->reply_to), $arr)) {
$reply_to = "=?UTF-8?B?".base64_encode($this->toUTF8($arr[1]))."?= <{$arr[2]}>";
} elseif($this->reply_to) {
$reply_to = $this->reply_to;
}
else {
$reply_to = $from;
}
$to = preg_split("/[,;]/", $this->to);
foreach($to as $i=>$t) if(preg_match(self::EMAIL_REGEXP, trim($t), $arr)) {
$to[$i] = "=?UTF-8?B?".base64_encode($this->toUTF8($arr[1]))."?= <{$arr[2]}>";
} else {
$to[$i] = $t;
}
$to = implode(", ", $to);
$subject = "=?UTF-8?B?".base64_encode($this->toUTF8($this->subject))."?=";
$headers['from'] = "From: {$from}";
$headers['reply-to'] = "Reply-To: {$reply_to}";
$headers['mime-version'] = "MIME-Version: 1.0";
$headers['x-mailer'] = "X-Mailer: PHP/MPM 1.1 by aleksandr.ru";
if($multipart && !$related) {
$headers['content-type'] = "Content-Type: multipart/mixed; boundary=\"$boundary_part\"";
} elseif($multipart && $related) {
$headers['content-type'] = "Content-Type: multipart/related; boundary=\"$boundary_part\"";
} else {
$headers['content-type'] = "Content-Type: text/plain; charset={$this->charset}\nContent-Transfer-Encoding: base64";
}
if(sizeof($this->headers)) $headers = array_merge($headers, $this->headers);
$headers = implode("\n", $headers);
$message = "";
/* text/alternative part */
if(!$multipart) {
$message .= chunk_split(base64_encode($this->text))."\n";
} elseif($this->text && $html) {
$message .= "--$boundary_part\n";
$message .= "Content-Type: multipart/alternative; boundary=\"$boundary_alt\"\n\n";
$message .= "--$boundary_alt\n";
$message .= "Content-Type: text/plain; charset={$this->charset}\nContent-Transfer-Encoding: base64\n\n";
$message .= chunk_split(base64_encode($this->text))."\n";
$message .= "--$boundary_alt\n";
$message .= "Content-Type: text/html; charset={$this->charset}\nContent-Transfer-Encoding: base64\n\n";
$message .= chunk_split(base64_encode($html))."\n";
$message .= "--$boundary_alt--\n\n";
} elseif($this->text) {
$message .= "--$boundary_part\n";
$message .= "Content-Type: text/plain; charset={$this->charset}\nContent-Transfer-Encoding: base64\n\n";
$message .= chunk_split(base64_encode($this->text))."\n";
} elseif($html) {
$message .= "--$boundary_part\n";
$message .= "Content-Type: text/html; charset={$this->charset}\nContent-Transfer-Encoding: base64\n\n";
$message .= chunk_split(base64_encode($html))."\n";
}
/* attachments */
foreach($this->attachments as $att) {
$att['name'] = "=?UTF-8?B?".base64_encode($this->toUTF8($att['name']))."?=";
$message .= "--$boundary_part\n";
if($att['cid']) $message .= "Content-ID: <{$att['cid']}>\n";
$message .= "Content-Type: {$att['mime_type']}; name=\"{$att['name']}\"\n";
$message .= "Content-Disposition: attachment; filename=\"{$att['name']}\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$message .= chunk_split(base64_encode($att['data']))."\n";
}
if($multipart) $message .= "--$boundary_part--\n\n";
//TODO: bug? since php8 $message must starts with a newline
// otherwise email will be send with empty body
if (PHP_MAJOR_VERSION > 7) $message = "\n" . $message;
return mail($to, $subject, $message, $headers);
}
/**
* convert string to utf-8
* @param string $str
*
* @return string
*/
protected function toUTF8($str)
{
if(strtoupper($this->charset) == 'UTF-8') return $str;
else return iconv($this->charset, 'UTF-8', $str);
}
}