-
Notifications
You must be signed in to change notification settings - Fork 10
/
mail.php
484 lines (439 loc) · 15.5 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
<?php
/**
* @file mail.php
* @brief simple mail generator and sender
*
* copyright (c) 2006-2013 Frank Hellenkamp [jonas@depagecms.net]
*
* @author Frank Hellenkamp [jonas@depagecms.net]
*/
// {{{ 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("sender@domain.com");
*
* $mail->setSubject("new mail subject")
* ->setText("This will be the text inside of the mail")
* ->attachFile("path/to/filename.pdf");
*
* $mail->send("recipient@domain.com");
* @endcode
*/
class mail {
protected $sender;
protected $recipients;
protected $cc;
protected $bcc;
protected $replyto;
protected $subject;
protected $text;
protected $htmlText;
protected $attachements = array();
protected $boundary;
protected $encoding = "UTF-8";
protected $eol = "\n";
// {{{ constructor()
/**
* @brief construct a new mail object
*
* @param string $sender email of the sender
*/
public function __construct($sender) {
$this->sender = $sender;
$this->boundary = "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:
*
* - recipient@domain.com
* - recipient1@domain.com, recipient2@domain.com
* - Displayname <recipient@domain.com>
*
* @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;
}
// }}}
// {{{ 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($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($mailtext);
$this->htmlText = $mailtext;
$this->text = $this->stripTags($this->htmlText);
return $this;
}
// }}}
// {{{ 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)) . "{$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->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 (1.4.1){$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}" .
// @todo add to boundaries (mixed/alternative) depending on attachements
//"Content-Type: multipart/mixed; {$this->eol}\tboundary=\"{$this->boundary}\"{$this->eol}";
"Content-Type: multipart/alternative; {$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));
} 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: 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);
$message .= "{$this->eol}{$this->eol}";
$message .= "--{$this->boundary}{$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}";
}
foreach ($this->attachements as $att) {
$message .= "{$this->eol}{$this->eol}$att";
}
$message .= "--{$this->boundary}--{$this->eol}";
}
return $message;
}
// }}}
// {{{ 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() . $this->eol;
return $message;
}
// }}}
// {{{ 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) {
if (!is_null($recipients)) {
$this->setRecipients($recipients);
}
$success = mail($this->getRecipients(), $this->getSubject(), $this->getBody(), $this->getHeaders());
return $success;
}
// }}}
// {{{ 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
* @param string $charset charset to use, defaults to utf-8
* @return string wordwrapped text
*/
protected function wordwrap($string, $width = 75, $forceCut = false, $charset = 'utf-8') {
$stringWidth = mb_strlen($string, $charset);
$breakWidth = mb_strlen($this->eol, $charset);
if (strlen($string) === 0) {
return '';
} elseif ($breakWidth === null) {
throw new Exception('Break string cannot be empty');
} elseif ($width === 0 && $forceCut) {
throw new Exception('Can\'t force cut when width is zero');
}
$result = '';
$lastStart = $lastSpace = 0;
for ($current = 0; $current < $stringWidth; $current++) {
$char = mb_substr($string, $current, 1, $charset);
if ($breakWidth === 1) {
$possibleBreak = $char;
} else {
$possibleBreak = mb_substr($string, $current, $breakWidth, $charset);
}
if ($possibleBreak === $this->eol) {
$result .= mb_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset);
$current += $breakWidth - 1;
$lastStart = $lastSpace = $current + 1;
} elseif ($char === ' ') {
if ($current - $lastStart >= $width) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset) . $this->eol;
$lastStart = $current + 1;
}
$lastSpace = $current;
} elseif ($current - $lastStart >= $width && $forceCut && $lastStart >= $lastSpace) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset) . $this->eol;
$lastStart = $lastSpace = $current;
} elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) {
$result .= mb_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $this->eol;
$lastStart = $lastSpace = $lastSpace + 1;
}
}
if ($lastStart !== $current) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset);
}
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) {
$stripped = preg_replace(array(
// Remove invisible/unwanted content
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noframes[^>]*?.*?</noframes>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
), '', $string);
$stripped = strip_tags($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) {
$string = str_replace(array("\r\n", "\r", "\n"), $this->eol, $string);
return $string;
}
// }}}
// {{{ normalizeRecipients()
/**
* @brief Normalize recipients from array to a list of comma separated 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 : */