htmlMimeMail.php
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* This file is part of the htmlMimeMail package (http://www.phpguru.org/)
|
4 |
*
|
5 |
* htmlMimeMail is free software; you can redistribute it and/or modify
|
6 |
* it under the terms of the GNU General Public License as published by
|
7 |
* the Free Software Foundation; either version 2 of the License, or
|
8 |
* (at your option) any later version.
|
9 |
*
|
10 |
* htmlMimeMail is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with htmlMimeMail; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
*
|
19 |
* � Copyright 2004 Richard Heyes
|
20 |
*/
|
21 |
|
22 |
require_once(dirname(__FILE__) . '/mimePart.php'); |
23 |
|
24 |
class htmlMimeMail |
25 |
{ |
26 |
/**
|
27 |
* The html part of the message
|
28 |
* @var string
|
29 |
*/
|
30 |
var $html; |
31 |
|
32 |
/**
|
33 |
* The text part of the message(only used in TEXT only messages)
|
34 |
* @var string
|
35 |
*/
|
36 |
var $text; |
37 |
|
38 |
/**
|
39 |
* The main body of the message after building
|
40 |
* @var string
|
41 |
*/
|
42 |
var $output; |
43 |
|
44 |
/**
|
45 |
* The alternative text to the HTML part (only used in HTML messages)
|
46 |
* @var string
|
47 |
*/
|
48 |
var $html_text; |
49 |
|
50 |
/**
|
51 |
* An array of embedded images/objects
|
52 |
* @var array
|
53 |
*/
|
54 |
var $html_images; |
55 |
|
56 |
/**
|
57 |
* An array of recognised image types for the findHtmlImages() method
|
58 |
* @var array
|
59 |
*/
|
60 |
var $image_types; |
61 |
|
62 |
/**
|
63 |
* Parameters that affect the build process
|
64 |
* @var array
|
65 |
*/
|
66 |
var $build_params; |
67 |
|
68 |
/**
|
69 |
* Array of attachments
|
70 |
* @var array
|
71 |
*/
|
72 |
var $attachments; |
73 |
|
74 |
/**
|
75 |
* The main message headers
|
76 |
* @var array
|
77 |
*/
|
78 |
var $headers; |
79 |
|
80 |
/**
|
81 |
* Whether the message has been built or not
|
82 |
* @var boolean
|
83 |
*/
|
84 |
var $is_built; |
85 |
|
86 |
/**
|
87 |
* The return path address. If not set the From:
|
88 |
* address is used instead
|
89 |
* @var string
|
90 |
*/
|
91 |
var $return_path; |
92 |
|
93 |
/**
|
94 |
* Array of information needed for smtp sending
|
95 |
* @var array
|
96 |
*/
|
97 |
var $smtp_params; |
98 |
|
99 |
var $sendmail_path; |
100 |
|
101 |
/**
|
102 |
* Constructor function. Sets the headers
|
103 |
* if supplied.
|
104 |
*/
|
105 |
public function __construct() |
106 |
{ |
107 |
/**
|
108 |
* Initialise some variables.
|
109 |
*/
|
110 |
$this->html_images = array(); |
111 |
$this->headers = array(); |
112 |
$this->is_built = false; |
113 |
$this->sendmail_path = '/usr/lib/sendmail -ti'; |
114 |
|
115 |
/**
|
116 |
* If you want the auto load functionality
|
117 |
* to find other image/file types, add the
|
118 |
* extension and content type here.
|
119 |
*/
|
120 |
$this->image_types = array( |
121 |
'gif' => 'image/gif', |
122 |
'jpg' => 'image/jpeg', |
123 |
'jpeg' => 'image/jpeg', |
124 |
'jpe' => 'image/jpeg', |
125 |
'bmp' => 'image/bmp', |
126 |
'png' => 'image/png', |
127 |
'tif' => 'image/tiff', |
128 |
'tiff' => 'image/tiff', |
129 |
'swf' => 'application/x-shockwave-flash' |
130 |
); |
131 |
|
132 |
/**
|
133 |
* Set these up
|
134 |
*/
|
135 |
$this->build_params['html_encoding'] = 'quoted-printable'; |
136 |
$this->build_params['text_encoding'] = '7bit'; |
137 |
$this->build_params['html_charset'] = 'ISO-8859-1'; |
138 |
$this->build_params['text_charset'] = 'ISO-8859-1'; |
139 |
$this->build_params['head_charset'] = 'ISO-8859-1'; |
140 |
$this->build_params['text_wrap'] = 998; |
141 |
|
142 |
/**
|
143 |
* Defaults for smtp sending
|
144 |
*/
|
145 |
if (!empty($GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'])) { |
146 |
$helo = $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST']; |
147 |
} elseif (!empty($GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME'])) { |
148 |
$helo = $GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME']; |
149 |
} else {
|
150 |
$helo = 'localhost'; |
151 |
} |
152 |
|
153 |
$this->smtp_params['host'] = 'localhost'; |
154 |
$this->smtp_params['port'] = 25; |
155 |
$this->smtp_params['helo'] = $helo; |
156 |
$this->smtp_params['auth'] = false; |
157 |
$this->smtp_params['user'] = ''; |
158 |
$this->smtp_params['pass'] = ''; |
159 |
|
160 |
/**
|
161 |
* Make sure the MIME version header is first.
|
162 |
*/
|
163 |
$this->headers['MIME-Version'] = '1.0'; |
164 |
} |
165 |
|
166 |
/**
|
167 |
* This function will read a file in
|
168 |
* from a supplied filename and return
|
169 |
* it. This can then be given as the first
|
170 |
* argument of the the functions
|
171 |
* add_html_image() or add_attachment().
|
172 |
*/
|
173 |
function getFile($filename) |
174 |
{ |
175 |
$return = ''; |
176 |
if ($fp = fopen($filename, 'rb')) { |
177 |
while (!feof($fp)) { |
178 |
$return .= fread($fp, 1024); |
179 |
} |
180 |
fclose($fp); |
181 |
return $return; |
182 |
|
183 |
} else {
|
184 |
return false; |
185 |
} |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Accessor to set the CRLF style
|
190 |
*/
|
191 |
function setCrlf($crlf = "\n") |
192 |
{ |
193 |
if (!defined('CRLF')) { |
194 |
define('CRLF', $crlf, true); |
195 |
} |
196 |
|
197 |
if (!defined('MAIL_MIMEPART_CRLF')) { |
198 |
define('MAIL_MIMEPART_CRLF', $crlf, true); |
199 |
} |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Accessor to set the SMTP parameters
|
204 |
*/
|
205 |
function setSMTPParams($host = null, $port = null, $helo = null, $auth = null, $user = null, $pass = null) |
206 |
{ |
207 |
if (!is_null($host)) $this->smtp_params['host'] = $host; |
208 |
if (!is_null($port)) $this->smtp_params['port'] = $port; |
209 |
if (!is_null($helo)) $this->smtp_params['helo'] = $helo; |
210 |
if (!is_null($auth)) $this->smtp_params['auth'] = $auth; |
211 |
if (!is_null($user)) $this->smtp_params['user'] = $user; |
212 |
if (!is_null($pass)) $this->smtp_params['pass'] = $pass; |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Sets sendmail path and options (optionally) (when directly piping to sendmail)
|
217 |
*/
|
218 |
function setSendmailPath($path) |
219 |
{ |
220 |
$this->sendmail_path = $path; |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Accessor function to set the text encoding
|
225 |
*/
|
226 |
function setTextEncoding($encoding = '7bit') |
227 |
{ |
228 |
$this->build_params['text_encoding'] = $encoding; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Accessor function to set the HTML encoding
|
233 |
*/
|
234 |
function setHtmlEncoding($encoding = 'quoted-printable') |
235 |
{ |
236 |
$this->build_params['html_encoding'] = $encoding; |
237 |
} |
238 |
|
239 |
/**
|
240 |
* Accessor function to set the text charset
|
241 |
*/
|
242 |
function setTextCharset($charset = 'ISO-8859-1') |
243 |
{ |
244 |
$this->build_params['text_charset'] = $charset; |
245 |
} |
246 |
|
247 |
/**
|
248 |
* Accessor function to set the HTML charset
|
249 |
*/
|
250 |
function setHtmlCharset($charset = 'ISO-8859-1') |
251 |
{ |
252 |
$this->build_params['html_charset'] = $charset; |
253 |
} |
254 |
|
255 |
/**
|
256 |
* Accessor function to set the header encoding charset
|
257 |
*/
|
258 |
function setHeadCharset($charset = 'ISO-8859-1') |
259 |
{ |
260 |
$this->build_params['head_charset'] = $charset; |
261 |
} |
262 |
|
263 |
/**
|
264 |
* Accessor function to set the text wrap count
|
265 |
*/
|
266 |
function setTextWrap($count = 998) |
267 |
{ |
268 |
$this->build_params['text_wrap'] = $count; |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Accessor to set a header
|
273 |
*/
|
274 |
function setHeader($name, $value) |
275 |
{ |
276 |
$this->headers[$name] = $value; |
277 |
} |
278 |
|
279 |
/**
|
280 |
* Accessor to add a Subject: header
|
281 |
*/
|
282 |
function setSubject($subject) |
283 |
{ |
284 |
$this->headers['Subject'] = $subject; |
285 |
} |
286 |
|
287 |
/**
|
288 |
* Accessor to add a From: header
|
289 |
*/
|
290 |
function setFrom($from) |
291 |
{ |
292 |
$this->headers['From'] = $from; |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Accessor to set the return path
|
297 |
*/
|
298 |
function setReturnPath($return_path) |
299 |
{ |
300 |
$this->return_path = $return_path; |
301 |
} |
302 |
|
303 |
/**
|
304 |
* Accessor to set the Disposition-Notification-To
|
305 |
*/
|
306 |
function setDispositionNotificationTo($email) |
307 |
{ |
308 |
$this->headers['Disposition-Notification-To'] = $email; |
309 |
} |
310 |
|
311 |
/**
|
312 |
* Accessor to set the reply-to
|
313 |
*/
|
314 |
function setReplyTo($replyTo) |
315 |
{ |
316 |
$this->headers['Reply-To'] = $replyTo; |
317 |
} |
318 |
|
319 |
/**
|
320 |
* Accessor to add a Cc: header
|
321 |
*/
|
322 |
function setCc($cc) |
323 |
{ |
324 |
$this->headers['Cc'] = $cc; |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Accessor to add a Bcc: header
|
329 |
*/
|
330 |
function setBcc($bcc) |
331 |
{ |
332 |
$this->headers['Bcc'] = $bcc; |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Adds plain text. Use this function
|
337 |
* when NOT sending html email
|
338 |
*/
|
339 |
function setText($text = '') |
340 |
{ |
341 |
$this->text = $text; |
342 |
} |
343 |
|
344 |
/**
|
345 |
* Adds notification to
|
346 |
*/
|
347 |
function setNotification($mail) |
348 |
{ |
349 |
$this->headers['Disposition-Notification-To'] = $mail; |
350 |
} |
351 |
|
352 |
/**
|
353 |
* Adds a html part to the mail.
|
354 |
* Also replaces image names with
|
355 |
* content-id's.
|
356 |
*/
|
357 |
function setHtml($html, $text = null, $images_dir = null) |
358 |
{ |
359 |
$this->html = $html; |
360 |
$this->html_text = $text; |
361 |
|
362 |
if (isset($images_dir)) { |
363 |
$this->_findHtmlImages($images_dir); |
364 |
} |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Function for extracting images from
|
369 |
* html source. This function will look
|
370 |
* through the html code supplied by add_html()
|
371 |
* and find any file that ends in one of the
|
372 |
* extensions defined in $obj->image_types.
|
373 |
* If the file exists it will read it in and
|
374 |
* embed it, (not an attachment).
|
375 |
*
|
376 |
* @author Dan Allen
|
377 |
*/
|
378 |
function _findHtmlImages($images_dir) |
379 |
{ |
380 |
// Build the list of image extensions
|
381 |
while (list($key,) = each($this->image_types)) { |
382 |
$extensions[] = $key; |
383 |
} |
384 |
|
385 |
preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $images); |
386 |
|
387 |
for ($i=0; $i<count($images[1]); $i++) { |
388 |
if (file_exists($images_dir . $images[1][$i])) { |
389 |
$html_images[] = $images[1][$i]; |
390 |
$this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html); |
391 |
} |
392 |
} |
393 |
|
394 |
if (!empty($html_images)) { |
395 |
|
396 |
// If duplicate images are embedded, they may show up as attachments, so remove them.
|
397 |
$html_images = array_unique($html_images); |
398 |
sort($html_images); |
399 |
|
400 |
for ($i=0; $i<count($html_images); $i++) { |
401 |
if ($image = $this->getFile($images_dir.$html_images[$i])) { |
402 |
$ext = substr($html_images[$i], strrpos($html_images[$i], '.') + 1); |
403 |
$content_type = $this->image_types[strtolower($ext)]; |
404 |
$this->addHtmlImage($image, basename($html_images[$i]), $content_type); |
405 |
} |
406 |
} |
407 |
} |
408 |
} |
409 |
|
410 |
/**
|
411 |
* Adds an image to the list of embedded
|
412 |
* images.
|
413 |
*/
|
414 |
function addHtmlImage($file, $name = '', $c_type='application/octet-stream') |
415 |
{ |
416 |
$this->html_images[] = array( |
417 |
'body' => $file, |
418 |
'name' => $name, |
419 |
'c_type' => $c_type, |
420 |
'cid' => md5(uniqid(time())) |
421 |
); |
422 |
} |
423 |
|
424 |
|
425 |
/**
|
426 |
* Adds a file to the list of attachments.
|
427 |
*/
|
428 |
function addAttachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') |
429 |
{ |
430 |
$this->attachments[] = array( |
431 |
'body' => $file, |
432 |
'name' => $name, |
433 |
'c_type' => $c_type, |
434 |
'encoding' => $encoding |
435 |
); |
436 |
} |
437 |
|
438 |
/**
|
439 |
* Adds a text subpart to a mime_part object
|
440 |
*/
|
441 |
function &_addTextPart(&$obj, $text) |
442 |
{ |
443 |
$params['content_type'] = 'text/plain'; |
444 |
$params['encoding'] = $this->build_params['text_encoding']; |
445 |
$params['charset'] = $this->build_params['text_charset']; |
446 |
if (is_object($obj)) { |
447 |
$return = $obj->addSubpart($text, $params); |
448 |
} else {
|
449 |
$return = new Mail_mimePart($text, $params); |
450 |
} |
451 |
|
452 |
return $return; |
453 |
} |
454 |
|
455 |
/**
|
456 |
* Adds a html subpart to a mime_part object
|
457 |
*/
|
458 |
function &_addHtmlPart(&$obj) |
459 |
{ |
460 |
$params['content_type'] = 'text/html'; |
461 |
$params['encoding'] = $this->build_params['html_encoding']; |
462 |
$params['charset'] = $this->build_params['html_charset']; |
463 |
if (is_object($obj)) { |
464 |
$return = $obj->addSubpart($this->html, $params); |
465 |
} else {
|
466 |
$return = new Mail_mimePart($this->html, $params); |
467 |
} |
468 |
|
469 |
return $return; |
470 |
} |
471 |
|
472 |
/**
|
473 |
* Starts a message with a mixed part
|
474 |
*/
|
475 |
function &_addMixedPart() |
476 |
{ |
477 |
$params['content_type'] = 'multipart/mixed'; |
478 |
$return = new Mail_mimePart('', $params); |
479 |
|
480 |
return $return; |
481 |
} |
482 |
|
483 |
/**
|
484 |
* Adds an alternative part to a mime_part object
|
485 |
*/
|
486 |
function &_addAlternativePart(&$obj) |
487 |
{ |
488 |
$params['content_type'] = 'multipart/alternative'; |
489 |
if (is_object($obj)) { |
490 |
$return = $obj->addSubpart('', $params); |
491 |
} else {
|
492 |
$return = new Mail_mimePart('', $params); |
493 |
} |
494 |
|
495 |
return $return; |
496 |
} |
497 |
|
498 |
/**
|
499 |
* Adds a html subpart to a mime_part object
|
500 |
*/
|
501 |
function &_addRelatedPart(&$obj) |
502 |
{ |
503 |
$params['content_type'] = 'multipart/related'; |
504 |
if (is_object($obj)) { |
505 |
$return = $obj->addSubpart('', $params); |
506 |
} else {
|
507 |
$return = new Mail_mimePart('', $params); |
508 |
} |
509 |
|
510 |
return $return; |
511 |
} |
512 |
|
513 |
/**
|
514 |
* Adds an html image subpart to a mime_part object
|
515 |
*/
|
516 |
function _addHtmlImagePart(&$obj, $value) |
517 |
{ |
518 |
$params['content_type'] = $value['c_type']; |
519 |
$params['encoding'] = 'base64'; |
520 |
$params['disposition'] = 'inline'; |
521 |
$params['dfilename'] = $value['name']; |
522 |
$params['cid'] = $value['cid']; |
523 |
$obj->addSubpart($value['body'], $params); |
524 |
} |
525 |
|
526 |
/**
|
527 |
* Adds an attachment subpart to a mime_part object
|
528 |
*/
|
529 |
function _addAttachmentPart(&$obj, $value) |
530 |
{ |
531 |
$params['content_type'] = $value['c_type']; |
532 |
$params['encoding'] = $value['encoding']; |
533 |
$params['disposition'] = 'attachment'; |
534 |
$params['dfilename'] = $value['name']; |
535 |
$obj->addSubpart($value['body'], $params); |
536 |
} |
537 |
|
538 |
/**
|
539 |
* Builds the multipart message from the
|
540 |
* list ($this->_parts). $params is an
|
541 |
* array of parameters that shape the building
|
542 |
* of the message. Currently supported are:
|
543 |
*
|
544 |
* $params['html_encoding'] - The type of encoding to use on html. Valid options are
|
545 |
* "7bit", "quoted-printable" or "base64" (all without quotes).
|
546 |
* 7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
|
547 |
* $params['text_encoding'] - The type of encoding to use on plain text Valid options are
|
548 |
* "7bit", "quoted-printable" or "base64" (all without quotes).
|
549 |
* Default is 7bit
|
550 |
* $params['text_wrap'] - The character count at which to wrap 7bit encoded data.
|
551 |
* Default this is 998.
|
552 |
* $params['html_charset'] - The character set to use for a html section.
|
553 |
* Default is ISO-8859-1
|
554 |
* $params['text_charset'] - The character set to use for a text section.
|
555 |
* - Default is ISO-8859-1
|
556 |
* $params['head_charset'] - The character set to use for header encoding should it be needed.
|
557 |
* - Default is ISO-8859-1
|
558 |
*/
|
559 |
function buildMessage($params = array()) |
560 |
{ |
561 |
if (!empty($params)) { |
562 |
while (list($key, $value) = each($params)) { |
563 |
$this->build_params[$key] = $value; |
564 |
} |
565 |
} |
566 |
|
567 |
if (!empty($this->html_images)) { |
568 |
foreach ($this->html_images as $value) { |
569 |
$this->html = str_replace($value['name'], 'cid:'.$value['cid'], $this->html); |
570 |
} |
571 |
} |
572 |
|
573 |
$null = null; |
574 |
$attachments = !empty($this->attachments) ? true : false; |
575 |
$html_images = !empty($this->html_images) ? true : false; |
576 |
$html = !empty($this->html) ? true : false; |
577 |
$text = isset($this->text) ? true : false; |
578 |
|
579 |
switch (true) { |
580 |
case $text AND !$attachments: |
581 |
$message = &$this->_addTextPart($null, $this->text); |
582 |
break;
|
583 |
|
584 |
case !$text AND $attachments AND !$html: |
585 |
$message = &$this->_addMixedPart(); |
586 |
|
587 |
for ($i=0; $i<count($this->attachments); $i++) { |
588 |
$this->_addAttachmentPart($message, $this->attachments[$i]); |
589 |
} |
590 |
break;
|
591 |
|
592 |
case $text AND $attachments: |
593 |
$message = &$this->_addMixedPart(); |
594 |
$this->_addTextPart($message, $this->text); |
595 |
|
596 |
for ($i=0; $i<count($this->attachments); $i++) { |
597 |
$this->_addAttachmentPart($message, $this->attachments[$i]); |
598 |
} |
599 |
break;
|
600 |
|
601 |
case $html AND !$attachments AND !$html_images: |
602 |
if (!is_null($this->html_text)) { |
603 |
$message = &$this->_addAlternativePart($null); |
604 |
$this->_addTextPart($message, $this->html_text); |
605 |
$this->_addHtmlPart($message); |
606 |
} else {
|
607 |
$message = &$this->_addHtmlPart($null); |
608 |
} |
609 |
break;
|
610 |
|
611 |
case $html AND !$attachments AND $html_images: |
612 |
if (!is_null($this->html_text)) { |
613 |
$message = &$this->_addAlternativePart($null); |
614 |
$this->_addTextPart($message, $this->html_text); |
615 |
$related = &$this->_addRelatedPart($message); |
616 |
} else {
|
617 |
$message = &$this->_addRelatedPart($null); |
618 |
$related = &$message; |
619 |
} |
620 |
$this->_addHtmlPart($related); |
621 |
for ($i=0; $i<count($this->html_images); $i++) { |
622 |
$this->_addHtmlImagePart($related, $this->html_images[$i]); |
623 |
} |
624 |
break;
|
625 |
|
626 |
case $html AND $attachments AND !$html_images: |
627 |
$message = &$this->_addMixedPart(); |
628 |
if (!is_null($this->html_text)) { |
629 |
$alt = &$this->_addAlternativePart($message); |
630 |
$this->_addTextPart($alt, $this->html_text); |
631 |
$this->_addHtmlPart($alt); |
632 |
} else {
|
633 |
$this->_addHtmlPart($message); |
634 |
} |
635 |
for ($i=0; $i<count($this->attachments); $i++) { |
636 |
$this->_addAttachmentPart($message, $this->attachments[$i]); |
637 |
} |
638 |
break;
|
639 |
|
640 |
case $html AND $attachments AND $html_images: |
641 |
$message = &$this->_addMixedPart(); |
642 |
if (!is_null($this->html_text)) { |
643 |
$alt = &$this->_addAlternativePart($message); |
644 |
$this->_addTextPart($alt, $this->html_text); |
645 |
$rel = &$this->_addRelatedPart($alt); |
646 |
} else {
|
647 |
$rel = &$this->_addRelatedPart($message); |
648 |
} |
649 |
$this->_addHtmlPart($rel); |
650 |
for ($i=0; $i<count($this->html_images); $i++) { |
651 |
$this->_addHtmlImagePart($rel, $this->html_images[$i]); |
652 |
} |
653 |
for ($i=0; $i<count($this->attachments); $i++) { |
654 |
$this->_addAttachmentPart($message, $this->attachments[$i]); |
655 |
} |
656 |
break;
|
657 |
|
658 |
} |
659 |
|
660 |
if (isset($message)) { |
661 |
$output = $message->encode(); |
662 |
$this->output = $output['body']; |
663 |
$this->headers = array_merge($this->headers, $output['headers']); |
664 |
|
665 |
// Add message ID header
|
666 |
srand((double)microtime()*10000000); |
667 |
$message_id = sprintf('<%s.%s@%s>', base_convert(time(), 10, 36), base_convert(rand(), 10, 36), !empty($GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST']) ? $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'] : $GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME']); |
668 |
$this->headers['Message-ID'] = $message_id; |
669 |
|
670 |
$this->is_built = true; |
671 |
return true; |
672 |
} else {
|
673 |
return false; |
674 |
} |
675 |
} |
676 |
|
677 |
/**
|
678 |
* Function to encode a header if necessary
|
679 |
* according to RFC2047
|
680 |
*/
|
681 |
function _encodeHeader($input, $charset = 'ISO-8859-1') |
682 |
{ |
683 |
preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches); |
684 |
foreach ($matches[1] as $value) { |
685 |
$replacement = @preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value); |
686 |
$input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input); |
687 |
} |
688 |
|
689 |
return $input; |
690 |
} |
691 |
|
692 |
/**
|
693 |
* Sends the mail.
|
694 |
*
|
695 |
* @param array $recipients
|
696 |
* @param string $type OPTIONAL
|
697 |
* @return mixed
|
698 |
*/
|
699 |
function send($recipients, $type = 'mail') |
700 |
{ |
701 |
if (!defined('CRLF')) { |
702 |
$this->setCrlf( ($type == 'mail' OR $type == 'sendmail') ? "\n" : "\r\n"); |
703 |
} |
704 |
|
705 |
if (!$this->is_built) { |
706 |
$this->buildMessage();
|
707 |
} |
708 |
|
709 |
switch ($type) { |
710 |
case 'mail': |
711 |
$subject = ''; |
712 |
if (!empty($this->headers['Subject'])) { |
713 |
$subject = $this->headers['Subject']; |
714 |
unset($this->headers['Subject']); |
715 |
} |
716 |
|
717 |
// Get flat representation of headers
|
718 |
foreach ($this->headers as $name => $value) { |
719 |
$headers[] = $name . ': ' . $value; |
720 |
} |
721 |
|
722 |
$to = implode(', ', $recipients); |
723 |
|
724 |
if (!empty($this->return_path)) { |
725 |
$result = mail($to, $subject, $this->output, implode(CRLF, $headers), '-f' . $this->return_path); |
726 |
} else {
|
727 |
$result = mail($to, $subject, $this->output, implode(CRLF, $headers)); |
728 |
} |
729 |
|
730 |
// Reset the subject in case mail is resent
|
731 |
if ($subject !== '') { |
732 |
$this->headers['Subject'] = $subject; |
733 |
} |
734 |
|
735 |
// Return
|
736 |
return $result; |
737 |
break;
|
738 |
|
739 |
case 'sendmail': |
740 |
// Get flat representation of headers
|
741 |
foreach ($this->headers as $name => $value) { |
742 |
$headers[] = $name . ': ' . $value; |
743 |
} |
744 |
|
745 |
// Encode To:
|
746 |
$headers[] = 'To: ' . implode(', ', $recipients); |
747 |
|
748 |
// Get return path arg for sendmail command if necessary
|
749 |
$returnPath = ''; |
750 |
if (!empty($this->return_path)) { |
751 |
$returnPath = '-f' . $this->return_path; |
752 |
} |
753 |
if(ini_get('sendmail_path') <> ""){ |
754 |
$this->setSendmailPath(ini_get('sendmail_path')); |
755 |
} |
756 |
|
757 |
$pipe = popen($this->sendmail_path . " " . $returnPath, 'w'); |
758 |
$bytes = fputs($pipe, implode(CRLF, $headers) . CRLF . CRLF . $this->output); |
759 |
$r = pclose($pipe); |
760 |
|
761 |
return $r; |
762 |
break;
|
763 |
|
764 |
case 'smtp': |
765 |
require_once(dirname(__FILE__) . '/smtp.php'); |
766 |
require_once(dirname(__FILE__) . '/RFC822.php'); |
767 |
$smtpObj = new smtpHtmlMimeMail($this->smtp_params); |
768 |
$RFC822Obj = new Mail_RFC822(); |
769 |
$smtp = $smtpObj->connect(); |
770 |
|
771 |
if (!$smtp) { |
772 |
return $smtpObj->errors; |
773 |
} |
774 |
|
775 |
// Parse recipients argument for internet addresses
|
776 |
foreach ($recipients as $recipient) { |
777 |
$addresses = $RFC822Obj->parseAddressList($recipient, $this->smtp_params['helo'], null, false); |
778 |
foreach ($addresses as $address) { |
779 |
$smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host); |
780 |
} |
781 |
} |
782 |
unset($addresses); // These are reused |
783 |
unset($address); // These are reused |
784 |
|
785 |
// Get flat representation of headers, parsing
|
786 |
// Cc and Bcc as we go
|
787 |
foreach ($this->headers as $name => $value) { |
788 |
if ($name == 'Cc' OR $name == 'Bcc') { |
789 |
$addresses = $RFC822Obj->parseAddressList($value, $this->smtp_params['helo'], null, false); |
790 |
foreach ($addresses as $address) { |
791 |
$smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host); |
792 |
} |
793 |
} |
794 |
if ($name == 'Bcc') { |
795 |
continue;
|
796 |
} |
797 |
//ajout d'une condition pour subject, car _encodeHeader ne prend pas les caract�res sp�ciaux avec php7 en revanche fonctionne tr�s bien avec php5.6
|
798 |
if ($name == 'Subject'){ |
799 |
$headers[] = $name . ': ' . $value; |
800 |
}else{
|
801 |
$headers[] = $name . ': ' . $this->_encodeHeader($value, $this->build_params['head_charset']); |
802 |
} |
803 |
|
804 |
} |
805 |
// Add To header based on $recipients argument
|
806 |
$headers[] = 'To: ' . implode(', ', $recipients); |
807 |
|
808 |
// Add headers to send_params
|
809 |
$send_params['headers'] = $headers; |
810 |
$send_params['recipients'] = array_values(array_unique($smtp_recipients)); |
811 |
$send_params['body'] = $this->output; |
812 |
|
813 |
// Add Notification To
|
814 |
if (isset($this->headers['Disposition-Notification-To'])) { |
815 |
$send_params['Disposition-Notification-To'] = $this->headers['Disposition-Notification-To']; |
816 |
} |
817 |
|
818 |
// Setup return path
|
819 |
if (isset($this->return_path)) { |
820 |
$send_params['from'] = $this->return_path; |
821 |
} elseif (!empty($this->headers['From'])) { |
822 |
$from = $RFC822Obj->parseAddressList($this->headers['From']); |
823 |
$send_params['from'] = sprintf('%s@%s', $from[0]->mailbox, $from[0]->host); |
824 |
} else {
|
825 |
$send_params['from'] = 'postmaster@' . $this->smtp_params['helo']; |
826 |
} |
827 |
|
828 |
// Send it
|
829 |
if (!$smtpObj->send($send_params)) { |
830 |
$this->errors = $smtpObj->errors; |
831 |
return $this->errors; |
832 |
} |
833 |
return true; |
834 |
break;
|
835 |
} |
836 |
} |
837 |
|
838 |
/**
|
839 |
* Use this method to return the email
|
840 |
* in message/rfc822 format. Useful for
|
841 |
* adding an email to another email as
|
842 |
* an attachment. there's a commented
|
843 |
* out example in example.php.
|
844 |
*/
|
845 |
function getRFC822($recipients) |
846 |
{ |
847 |
// Make up the date header as according to RFC822
|
848 |
$this->setHeader('Date', date('D, d M y H:i:s O')); |
849 |
|
850 |
if (!defined('CRLF')) { |
851 |
$this->setCrlf($type == 'mail' ? "\n" : "\r\n"); |
852 |
} |
853 |
|
854 |
if (!$this->is_built) { |
855 |
$this->buildMessage();
|
856 |
} |
857 |
|
858 |
// Return path ?
|
859 |
if (isset($this->return_path)) { |
860 |
$headers[] = 'Return-Path: ' . $this->return_path; |
861 |
} |
862 |
|
863 |
// Get flat representation of headers
|
864 |
foreach ($this->headers as $name => $value) { |
865 |
$headers[] = $name . ': ' . $value; |
866 |
} |
867 |
$headers[] = 'To: ' . implode(', ', $recipients); |
868 |
|
869 |
return implode(CRLF, $headers) . CRLF . CRLF . $this->output; |
870 |
} |
871 |
} // End of class.
|
872 |
?>
|