-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mail.php
667 lines (605 loc) · 19.8 KB
/
Mail.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?php
/**
* @file Mail.php
* @brief simple mail generator and sender
*
* copyright (c) 2006-2014 Frank Hellenkamp [[email protected]]
*
* @author Frank Hellenkamp [[email protected]]
*/
// {{{ documentation
/**
* @mainpage
*
* @intro
* @image html icon_depage-forms.png
* @htmlinclude main-intro.html
* @endintro
*
* @section Usage
*
* @endsection
*
* @htmlinclude main-extended.html
**/
// }}}
//
namespace Depage\Mail;
/**
* @brief A simple mail generator and sender
*
* depage::mail::mail is a simple class to generate emails with text- and/or
* html-content with the simple ability to add various attachments. It takes
* care of mail-boundaries automatically and sends the mail through the native
* mail() function.
*
* It also wordwraps text automatically, and tries to generate a plain-text
* version of an html-text when no plain-text is provided.
*
* @code
* <?php
* $mail = new Depage\Mail\Mail("[email protected]");
*
* $mail->setSubject("new mail subject")
* ->setText("This will be the text inside of the mail")
* ->attachFile("path/to/filename.pdf");
*
* $mail->send("[email protected]");
* @endcode
*/
class Mail
{
protected $version = "2.0.0";
protected $sender = "";
protected $recipients = "";
protected $cc = "";
protected $bcc = "";
protected $replyto = "";
protected $returnPath = "";
protected $listUnsubscribe = "";
protected $subject = "";
protected $text = "";
protected $htmlText = "";
protected $trackerImage = "";
protected $dontShowEmail = true;
protected $attachements = array();
protected $boundary = "";
protected $boundary2 = "";
protected $encoding = "UTF-8";
protected $eol = PHP_EOL;
protected $mailFunction = "mail";
// {{{ constructor()
/**
* @brief construct a new mail object
*
* @param string $sender email of the sender
*/
public function __construct($sender)
{
$this->sender = $sender;
$this->returnPath = $sender;
$this->boundary = "depage-att=" . hash("sha1", date("r") . mt_rand()) . "=";
$this->boundary2 = "depage-mail=" . hash("sha1", date("r") . mt_rand()) . "=";
}
// }}}
// {{{ setSubject()
/**
* @brief Sets the mails subject.
*
* @param string $subject new subject
* @return object returns the mail object (for chaining)
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
// }}}
// {{{ setRecipients()
/**
* @brief Sets the recipients of the mail.
*
* Recipients can either be set as a string or as an array of strings. All
* strings can also be comma separated emails.
*
* You also can use all valid email notations:
*
* - Displayname <[email protected]>
*
* @param string|array $recipients new recipients
* @return object returns the mail object (for chaining)
*/
public function setRecipients($recipients)
{
$this->recipients = $recipients;
return $this;
}
// }}}
// {{{ setCC()
/**
* @brief Sets the CC recipients of the mail.
*
* @param string|array $recipients new recipients
* @return object returns the mail object (for chaining)
*/
public function setCC($recipients)
{
$this->cc = $recipients;
return $this;
}
// }}}
// {{{ setBCC()
/**
* @brief Sets the BCC recipients of the mail.
*
* @param string|array $recipients new recipients
* @return object returns the mail object (for chaining)
*/
public function setBCC($recipients)
{
$this->bcc = $recipients;
return $this;
}
// }}}
// {{{ setReplyTo()
/**
* @brief Sets the reply-to header
*
* @param string $subject new reply-to address
* @return object returns the mail object (for chaining)
*/
public function setReplyTo($email)
{
$this->replyto = $email;
return $this;
}
// }}}
// {{{ setReturnPath()
/**
* @brief Sets the reply-to header
*
* @param string $subject new reply-to address
* @return object returns the mail object (for chaining)
*/
public function setReturnPath($email)
{
$this->returnPath = $email;
return $this;
}
// }}}
// {{{ setListUnsubscribe()
/**
* @brief Sets the list-unsubscribe header
*
* @param string $subject new reply-to address
* @return object returns the mail object (for chaining)
*/
public function setListUnsubscribe($header)
{
$this->listUnsubscribe = $header;
return $this;
}
// }}}
// {{{ setText()
/**
* @brief Sets the content of the mail as plain text.
*
* @param string $mailtext new mail content
* @return object returns the mail object (for chaining)
*/
public function setText($mailtext)
{
$mailtext = $this->normalizeLineEndings((string) $mailtext);
$this->text = $mailtext;
return $this;
}
// }}}
// {{{ setHtmlText()
/**
* @brief Sets the content of the mail as html text.
*
* It also sets the plaintext-content of the message by stripping out any
* tags but leaving the whitespace.
*
* @param string $mailtext new mail html-content
* @return object returns the mail object (for chaining)
*/
public function setHtmlText($mailtext)
{
$mailtext = $this->normalizeLineEndings((string) $mailtext);
$this->htmlText = $mailtext;
$this->text = $this->stripTags($mailtext);
return $this;
}
// }}}
// {{{ setTrackerImage()
/**
* @brief setTrackerImage
*
* @param mixed $
* @return void
**/
public function setTrackerImage($url)
{
if (!empty($url)) {
$this->trackerImage = $url;
}
}
// }}}
// {{{ attachFile()
/**
* @brief Attaches a file to a message.
*
* @param string $filename path to filename to attach
* @param string $mimetype optional mimetype of the attachment. Defaults to "application/octet_stream"
* @return object returns the mail object (for chaining)
*/
public function attachFile($filename, $mimetype = "application/octet_stream")
{
$fstring = file_get_contents($filename);
$this->attachStr($fstring, $mimetype, basename($filename));
}
// }}}
// {{{ attachStr()
/**
* @brief Attaches a string as a file to a message.
*
* @param string $filename path to filename to attach
* @param string $mimetype optional mimetype of the attachment. Defaults to "application/octet_stream"
* @param string $filename filename to use as a name for the attachment
* @return object returns the mail object (for chaining)
*/
public function attachStr($string, $mimetype, $filename = "")
{
$astring = "--{$this->boundary}{$this->eol}" .
"Content-type: $mimetype{$this->eol}" .
"Content-transfer-encoding: base64{$this->eol}" .
"Content-disposition: attachement;{$this->eol} filename=\"$filename\"{$this->eol}{$this->eol}";
$astring .= chunk_split(base64_encode($string), 76, $this->eol) . "{$this->eol}";
$this->attachements[] = $astring;
return $this;
}
// }}}
// {{{ getSubject()
/**
* @brief Gets the mail subject as an encoded string.
*
* @return string $subject encoded subject
*/
public function getSubject()
{
$subject = "=?{$this->encoding}?B?" . base64_encode($this->subject) . "?=";
return $subject;
}
// }}}
// {{{ getRecipients()
/**
* @brief Gets the mail recipients as a comma separated list.
*
* @return string $recipients all recipients (comma separated)
*/
public function getRecipients()
{
return $this->normalizeRecipients($this->recipients);
}
// }}}
// {{{ getHeaders()
/**
* @brief Gets the mail headers.
*
* @return string $headers the mail headers
*/
public function getHeaders()
{
$headers = "";
$headers .= "From: {$this->sender}{$this->eol}";
if ($this->replyto != "") {
$headers .= "Reply-To: {$this->replyto}{$this->eol}";
}
if ($this->returnPath != "") {
$headers .= "Return-Path: {$this->returnPath}{$this->eol}";
}
if ($this->listUnsubscribe != "") {
$headers .= "List-Unsubscribe: {$this->listUnsubscribe}{$this->eol}";
}
if ($this->cc != "") {
$headers .= "CC: " . $this->normalizeRecipients($this->cc) . $this->eol;
}
if ($this->bcc != "") {
$headers .= "BCC: " . $this->normalizeRecipients($this->bcc) . $this->eol;
}
$headers .= "X-Mailer: depage-mail ({$this->getVersion()}){$this->eol}";
if (count($this->attachements) == 0 && empty($this->htmlText)) {
$headers .=
"Content-type: text/plain; charset={$this->encoding}{$this->eol}" .
"Content-transfer-encoding: quoted-printable";
} else {
$headers .=
"MIME-Version: 1.0{$this->eol}" .
"Content-Type: multipart/mixed; {$this->eol}\tboundary=\"{$this->boundary}\"{$this->eol}";
}
return $headers;
}
// }}}
// {{{ getBody()
/**
* @brief Gets the message mail body including all attachments.
*
* @return string $message the mail body
*/
public function getBody()
{
$message = "";
if (count($this->attachements) == 0 && empty($this->htmlText)) {
$message .= $this->quotedPrintableEncode($this->wordwrap($this->text)) . $this->eol;
} else {
$message .=
_("This is a MIME encapsulated multipart message.") . $this->eol .
_("Please use a MIME-compliant e-mail program to open it.") . $this->eol . $this->eol;
$message .=
"--{$this->boundary}{$this->eol}" .
"Content-Type: multipart/alternative; {$this->eol}" .
"\tboundary=\"{$this->boundary2}\"{$this->eol}{$this->eol}";
$message .=
"--{$this->boundary2}{$this->eol}" .
"Content-type: text/plain; charset=\"{$this->encoding}\"{$this->eol}" .
"Content-transfer-encoding: quoted-printable{$this->eol}{$this->eol}";
$message .= $this->quotedPrintableEncode($this->wordwrap($this->text));
if (!empty($this->htmlText)) {
$htmlText = str_replace("<title></title>", "<title>" . htmlspecialchars($this->subject) . "</title>", $this->htmlText);
if (!empty($this->trackerImage)) {
$htmlText = str_replace("</body>", $this->getTracker() . "</body>", $htmlText);
}
$message .= "{$this->eol}{$this->eol}";
$message .= "--{$this->boundary2}{$this->eol}" .
"Content-type: text/html; charset=\"{$this->encoding}\"{$this->eol}" .
"Content-Transfer-encoding: quoted-printable{$this->eol}{$this->eol}";
$message .= $this->quotedPrintableEncode($this->wordwrap($htmlText)) . $this->eol;
}
$message .= "--{$this->boundary2}--{$this->eol}";
foreach ($this->attachements as $att) {
$message .= "{$this->eol}{$this->eol}$att";
}
$message .= "--{$this->boundary}--{$this->eol}";
}
return $message;
}
// }}}
// {{{ getTracker()
/**
* @brief getTracker
*
* @param mixed
* @return void
**/
public function getTracker()
{
$html = "";
$html .= "<table border=\"0\"><tr><td style=\"color: #ffffff;\" width=\"100%\">";
$html .= "<img src=\"{$this->trackerImage}\" alt=\"-\" width=\"100\" height=\"10\">";
$html .= "</td></tr></table>";
return $html;
}
// }}}
// {{{ getEml()
/**
* @brief Gets the whole message in EML format
*
* @return string $message whole message
*/
public function getEml()
{
$message = "";
$message .= "To: " . $this->getRecipients() . $this->eol;
$message .= "Subject: " . $this->getSubject(). $this->eol;
$message .= $this->getHeaders() . $this->eol . $this->eol;
$message .= $this->getBody();
return $message;
}
// }}}
// {{{ getVersion()
/**
* @brief Gets the Version number of depage-mail
*
* @return string $version version number
*/
public function getVersion()
{
return $this->version;
}
// }}}
// {{{ send()
/**
* @brief Sends the mail out to all recipients.
*
* @param string|array $recipients new recipients
* @return bool true on success, false on error
*/
public function send($recipients = null, $trackerImage = null)
{
if (!is_null($recipients)) {
$this->setRecipients($recipients);
}
$this->setTrackerImage($trackerImage);
$success = call_user_func($this->mailFunction, $this->getRecipients(), $this->getSubject(), $this->getBody(), $this->getHeaders());
return $success;
}
// }}}
// {{{ sendLater()
/**
* @brief sendLater
*
* @param mixed $
* @return void
**/
public function sendLater(\Depage\Tasks\Task $task, $recipients = null, $trackerImage = null)
{
if (!is_null($recipients)) {
$this->setRecipients($recipients);
}
$recipients = array_unique(explode(",", $this->getRecipients()));
$this->setRecipients(null);
foreach($recipients as $i => $to) {
if ($this->dontShowEmail) {
$title = "sending mail " . ($i + 1);
} else {
$title = "sending mail to $to";
}
if (!empty($to)) {
$task->addSubtask($title, "%s->send(%s, %s);", [
$this,
$to,
$trackerImage,
]);
}
}
$task->begin();
}
// }}}
// {{{ wordwrap()
/**
* @brief Word wraps the text content
*
* @param string $string text to wrao
* @param integer $width text width to wrap after, defaults to 75
* @param boolean $forceCut force the textbreak, even whan a word is longer the the text-width
* @return string wordwrapped text
*/
protected function wordwrap($string, $width = 75, $forceCut = false)
{
$stringWidth = mb_strlen($string, $this->encoding);
$breakWidth = mb_strlen($this->eol, $this->encoding);
if (strlen($string) === 0) {
return '';
} elseif ($width < 1 && $forceCut) {
// Disable forceCut when width is lower than 1
$forceCut = false;
}
$result = '';
$lastStart = $lastSpace = 0;
for ($current = 0; $current < $stringWidth; $current++) {
$char = mb_substr($string, $current, 1, $this->encoding);
if ($breakWidth === 1) {
$possibleBreak = $char;
} else {
$possibleBreak = mb_substr($string, $current, $breakWidth, $this->encoding);
}
if ($possibleBreak === $this->eol) {
$result .= mb_substr($string, $lastStart, $current - $lastStart + $breakWidth, $this->encoding);
$current += $breakWidth - 1;
$lastStart = $lastSpace = $current + 1;
} elseif ($char === ' ') {
if ($current - $lastStart >= $width) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $this->encoding) . $this->eol;
$lastStart = $current + 1;
}
$lastSpace = $current;
} elseif ($current - $lastStart >= $width && $forceCut && $lastStart >= $lastSpace) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $this->encoding) . $this->eol;
$lastStart = $lastSpace = $current;
} elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) {
$result .= mb_substr($string, $lastStart, $lastSpace - $lastStart, $this->encoding) . $this->eol;
$lastStart = $lastSpace = $lastSpace + 1;
}
}
if ($lastStart !== $current) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $this->encoding);
}
return $result;
}
// }}}
// {{{ stripTags()
/**
* @brief Strips tags from the html content.
*
* @param string $string html-markup
* @return string text with tags removed
*/
protected function stripTags($string)
{
// insert html links as text
// @todo only do this for links with specific class or attribute
$stripped = preg_replace_callback(array(
'@<a[^>]*?href="([^"]*)"[^>]*?>(.*)</a>@iu',
), function($m) {
if ($m[1] == $m[2]) {
return "{$m[2]}";
} else if (substr($m[1], 0, 7) == "mailto:") {
return substr($m[1], 7);
} else {
return "{$m[2]} [{$m[1]}]";
}
}, $string);
// replace images with alt-text
$stripped = preg_replace(array(
'@<img[^>]*?alt="([^"]*)"[^>]*?>@iu',
), '${1}', $stripped);
// Remove invisible/unwanted content
$stripped = preg_replace(array(
'@<title[^>]*?>.*?</title>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noframes[^>]*?.*?</noframes>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
), '', $stripped);
$stripped = strip_tags($stripped);
// remove duplicate newlines with just 2
$stripped = trim(preg_replace("/(\r?\n){2,}/", "\n\n", $stripped));
return $stripped;
}
// }}}
// {{{ quotedPrintableEncode()
/**
* @brief encodes text a quoted-printable and normalizes line endings.
*
* @param string $string text to be encoded
* @return string encoded string
*/
protected function quotedPrintableEncode($string)
{
$string = $this->normalizeLineEndings(quoted_printable_encode($string));
return $string;
}
// }}}
// {{{ normalizeLineEndings()
/**
* @brief Normalizes line endings to current eol.
*
* @param string $string text to be normalized
* @return string normalized string
*/
protected function normalizeLineEndings($string)
{
// replace with \n first
$string = str_replace(array("\r\n", "\r", "\n"), "\n", $string);
if ($this->eol !== "\n") {
// replace with real eol afterwords
$string = str_replace("\n", $this->eol, $string);
}
return $string;
}
// }}}
// {{{ normalizeRecipients()
/**
* @brief Normalize recipients from array to a list of comma separated emails
*
* @todo validate emails
*
* @param string|array $recipients new recipients
* @return string $recipients all recipients (comma separated)
*/
protected function normalizeRecipients($recipients)
{
if (is_array($recipients)) {
$recipients = implode(",", $recipients);
}
return trim($recipients);
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */