Quick Search:

View

Revision:

Diff

Diff from 1497 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/email.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
153
3   $Id: email.php 1497 2007-03-29 13:40:05Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2003 osCommerce
9
hpdl
1497
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13
14   mail.php - a class to assist in building mime-HTML eMails
15
16   The original class was made by Richard Heyes <richard@phpguru.org>
17   and can be found here: http://www.phpguru.org
18
19   Renamed and Modified by Jan Wildeboer for osCommerce
20 */
21
22   class email {
23     var $html;
24     var $text;
25     var $output;
26     var $html_text;
27     var $html_images;
28     var $image_types;
29     var $build_params;
30     var $attachments;
31     var $headers;
32
33     function email($headers = '') {
hpdl
386
34       global $osC_Language;
35
hpdl
1
36       if ($headers == '') $headers = array();
37
38       $this->html_images = array();
39       $this->headers = array();
40
41       if (EMAIL_LINEFEED == 'CRLF') {
42         $this->lf = "\r\n";
43       } else {
44         $this->lf = "\n";
45       }
46
47 /**
48  * If you want the auto load functionality
49  * to find other mime-image/file types, add the
50  * extension and content type here.
51  */
52
53       $this->image_types = array('gif' => 'image/gif',
54                                  'jpg' => 'image/jpeg',
55                                  'jpeg' => 'image/jpeg',
56                                  'jpe' => 'image/jpeg',
57                                  'bmp' => 'image/bmp',
58                                  'png' => 'image/png',
59                                  'tif' => 'image/tiff',
60                                  'tiff' => 'image/tiff',
61                                  'swf' => 'application/x-shockwave-flash');
62
63       $this->build_params['html_encoding'] = 'quoted-printable';
64       $this->build_params['text_encoding'] = '7bit';
hpdl
386
65       $this->build_params['html_charset'] = $osC_Language->getCharacterSet();
66       $this->build_params['text_charset'] = $osC_Language->getCharacterSet();
hpdl
1
67       $this->build_params['text_wrap'] = 998;
68
69 /**
70  * Make sure the MIME version header is first.
71  */
72
73       $this->headers[] = 'MIME-Version: 1.0';
74
75       reset($headers);
76       while (list(,$value) = each($headers)) {
hpdl
734
77         if (!empty($value)) {
hpdl
1
78           $this->headers[] = $value;
79         }
80       }
81     }
82
83 /**
84  * This function will read a file in
85  * from a supplied filename and return
86  * it. This can then be given as the first
87  * argument of the the functions
88  * add_html_image() or add_attachment().
89  */
90
91     function get_file($filename) {
92       $return = '';
93
94       if ($fp = fopen($filename, 'rb')) {
95         while (!feof($fp)) {
96           $return .= fread($fp, 1024);
97         }
98         fclose($fp);
99
100         return $return;
101       } else {
102         return false;
103       }
104     }
105
106 /**
107  * Function for extracting images from
108  * html source. This function will look
109  * through the html code supplied by add_html()
110  * and find any file that ends in one of the
111  * extensions defined in $obj->image_types.
112  * If the file exists it will read it in and
113  * embed it, (not an attachment).
114  *
115  * Function contributed by Dan Allen
116  */
117
118     function find_html_images($images_dir) {
119 // Build the list of image extensions
120       while (list($key, ) = each($this->image_types)) {
121         $extensions[] = $key;
122       }
123
124       preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images);
125
126       for ($i=0; $i<count($images[1]); $i++) {
127         if (file_exists($images_dir . $images[1][$i])) {
128           $html_images[] = $images[1][$i];
129           $this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
130         }
131       }
132
hpdl
734
133       if (!empty($html_images)) {
hpdl
1
134 // If duplicate images are embedded, they may show up as attachments, so remove them.
135         $html_images = array_unique($html_images);
136         sort($html_images);
137
138         for ($i=0; $i<count($html_images); $i++) {
139           if ($image = $this->get_file($images_dir . $html_images[$i])) {
140             $content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];
141             $this->add_html_image($image, basename($html_images[$i]), $content_type);
142           }
143         }
144       }
145     }
146
147 /**
148  * Adds plain text. Use this function
149  * when NOT sending html email
150  */
151
152     function add_text($text = '') {
hpdl
733
153       $this->text = str_replace(array("\r\n", "\n", "\r"), $this->lf, $text);
hpdl
1
154     }
155
156 /**
157  * Adds a html part to the mail.
158  * Also replaces image names with
159  * content-id's.
160  */
161
162     function add_html($html, $text = NULL, $images_dir = NULL) {
hpdl
733
163       $this->html = str_replace(array("\r\n", "\n", "\r"), '<br />', $html);
164       $this->html_text = str_replace(array("\r\n", "\n", "\r"), $this->lf, $text);
hpdl
1
165
166       if (isset($images_dir)) $this->find_html_images($images_dir);
167     }
168
169 /**
170  * Adds an image to the list of embedded
171  * images.
172  */
173
174     function add_html_image($file, $name = '', $c_type='application/octet-stream') {
175       $this->html_images[] = array('body' => $file,
176                                    'name' => $name,
177                                    'c_type' => $c_type,
178                                    'cid' => md5(uniqid(time())));
179     }
180
181 /**
182  * Adds a file to the list of attachments.
183  */
184
185     function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') {
186       $this->attachments[] = array('body' => $file,
187                                    'name' => $name,
188                                    'c_type' => $c_type,
189                                    'encoding' => $encoding);
190     }
191
192 /**
193  * Adds a text subpart to a mime_part object
194  */
195
196 /* HPDL PHP3 */
197 //    function &add_text_part(&$obj, $text) {
198     function add_text_part(&$obj, $text) {
199       $params['content_type'] = 'text/plain';
200       $params['encoding'] = $this->build_params['text_encoding'];
201       $params['charset'] = $this->build_params['text_charset'];
202
203       if (is_object($obj)) {
204         return $obj->addSubpart($text, $params);
205       } else {
206         return new mime($text, $params);
207       }
208     }
209
210 /**
211  * Adds a html subpart to a mime_part object
212  */
213
214 /* HPDL PHP3 */
215 //    function &add_html_part(&$obj) {
216     function add_html_part(&$obj) {
217       $params['content_type'] = 'text/html';
218       $params['encoding'] = $this->build_params['html_encoding'];
219       $params['charset'] = $this->build_params['html_charset'];
220
221       if (is_object($obj)) {
222         return $obj->addSubpart($this->html, $params);
223       } else {
224         return new mime($this->html, $params);
225       }
226     }
227
228 /**
229  * Starts a message with a mixed part
230  */
231
232 /* HPDL PHP3 */
233 //    function &add_mixed_part() {
234     function add_mixed_part() {
235       $params['content_type'] = 'multipart/mixed';
236
237       return new mime('', $params);
238     }
239
240 /**
241  * Adds an alternative part to a mime_part object
242  */
243
244 /* HPDL PHP3 */
245 //    function &add_alternative_part(&$obj) {
246     function add_alternative_part(&$obj) {
247       $params['content_type'] = 'multipart/alternative';
248
249       if (is_object($obj)) {
250         return $obj->addSubpart('', $params);
251       } else {
252         return new mime('', $params);
253       }
254     }
255
256 /**
257  * Adds a html subpart to a mime_part object
258  */
259
260 /* HPDL PHP3 */
261 //    function &add_related_part(&$obj) {
262     function add_related_part(&$obj) {
263       $params['content_type'] = 'multipart/related';
264
265       if (is_object($obj)) {
266         return $obj->addSubpart('', $params);
267       } else {
268         return new mime('', $params);
269       }
270     }
271
272 /**
273  * Adds an html image subpart to a mime_part object
274  */
275
276 /* HPDL PHP3 */
277 //    function &add_html_image_part(&$obj, $value) {
278     function add_html_image_part(&$obj, $value) {
279       $params['content_type'] = $value['c_type'];
280       $params['encoding'] = 'base64';
281       $params['disposition'] = 'inline';
282       $params['dfilename'] = $value['name'];
283       $params['cid'] = $value['cid'];
284
285       $obj->addSubpart($value['body'], $params);
286     }
287
288 /**
289  * Adds an attachment subpart to a mime_part object
290  */
291
292 /* HPDL PHP3 */
293 //    function &add_attachment_part(&$obj, $value) {
294     function add_attachment_part(&$obj, $value) {
295       $params['content_type'] = $value['c_type'];
296       $params['encoding'] = $value['encoding'];
297       $params['disposition'] = 'attachment';
298       $params['dfilename'] = $value['name'];
299
300       $obj->addSubpart($value['body'], $params);
301     }
302
303 /**
304  * Builds the multipart message from the
305  * list ($this->_parts). $params is an
306  * array of parameters that shape the building
307  * of the message. Currently supported are:
308  *
309  * $params['html_encoding'] - The type of encoding to use on html. Valid options are
310  *                            "7bit", "quoted-printable" or "base64" (all without quotes).
311  *                            7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
312  * $params['text_encoding'] - The type of encoding to use on plain text Valid options are
313  *                            "7bit", "quoted-printable" or "base64" (all without quotes).
314  *                            Default is 7bit
315  * $params['text_wrap']     - The character count at which to wrap 7bit encoded data.
316  *                            Default this is 998.
317  * $params['html_charset']  - The character set to use for a html section.
318  *                            Default is iso-8859-1
319  * $params['text_charset']  - The character set to use for a text section.
320  *                          - Default is iso-8859-1
321  */
322
323 /* HPDL PHP3 */
324 //    function build_message($params = array()) {
325     function build_message($params = '') {
326       if ($params == '') $params = array();
327
328       if (count($params) > 0) {
329         reset($params);
330         while(list($key, $value) = each($params)) {
331           $this->build_params[$key] = $value;
332         }
333       }
334
hpdl
734
335       if (!empty($this->html_images)) {
hpdl
1
336         reset($this->html_images);
337         while (list(,$value) = each($this->html_images)) {
338           $this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html);
339         }
340       }
341
342       $null = NULL;
hpdl
734
343       $attachments = ((!empty($this->attachments)) ? true : false);
344       $html_images = ((!empty($this->html_images)) ? true : false);
345       $html = ((!empty($this->html)) ? true : false);
346       $text = ((!empty($this->text)) ? true : false);
hpdl
1
347
348       switch (true) {
349         case (($text == true) && ($attachments == false)):
350 /* HPDL PHP3 */
351 //          $message =& $this->add_text_part($null, $this->text);
352           $message = $this->add_text_part($null, $this->text);
353           break;
354         case (($text == false) && ($attachments == true) && ($html == false)):
355 /* HPDL PHP3 */
356 //          $message =& $this->add_mixed_part();
357           $message = $this->add_mixed_part();
358
359           for ($i=0; $i<count($this->attachments); $i++) {
360             $this->add_attachment_part($message, $this->attachments[$i]);
361           }
362           break;
363         case (($text == true) && ($attachments == true)):
364 /* HPDL PHP3 */
365 //          $message =& $this->add_mixed_part();
366           $message = $this->add_mixed_part();
367           $this->add_text_part($message, $this->text);
368
369           for ($i=0; $i<count($this->attachments); $i++) {
370             $this->add_attachment_part($message, $this->attachments[$i]);
371           }
372           break;
373         case (($html == true) && ($attachments == false) && ($html_images == false)):
hpdl
734
374           if (!empty($this->html_text)) {
hpdl
1
375 /* HPDL PHP3 */
376 //            $message =& $this->add_alternative_part($null);
377             $message = $this->add_alternative_part($null);
378             $this->add_text_part($message, $this->html_text);
379             $this->add_html_part($message);
380           } else {
381 /* HPDL PHP3 */
382 //            $message =& $this->add_html_part($null);
383             $message = $this->add_html_part($null);
384           }
385           break;
386         case (($html == true) && ($attachments == false) && ($html_images == true)):
hpdl
734
387           if (!empty($this->html_text)) {
hpdl
1
388 /* HPDL PHP3 */
389 //            $message =& $this->add_alternative_part($null);
390             $message = $this->add_alternative_part($null);
391             $this->add_text_part($message, $this->html_text);
392 /* HPDL PHP3 */
393 //            $related =& $this->add_related_part($message);
394             $related = $this->add_related_part($message);
395           } else {
396 /* HPDL PHP3 */
397 //            $message =& $this->add_related_part($null);
398 //            $related =& $message;
399             $message = $this->add_related_part($null);
400             $related = $message;
401           }
402           $this->add_html_part($related);
403
404           for ($i=0; $i<count($this->html_images); $i++) {
405             $this->add_html_image_part($related, $this->html_images[$i]);
406           }
407           break;
408         case (($html == true) && ($attachments == true) && ($html_images == false)):
409 /* HPDL PHP3 */
410 //          $message =& $this->add_mixed_part();
411           $message = $this->add_mixed_part();
hpdl
734
412           if (!empty($this->html_text)) {
hpdl
1
413 /* HPDL PHP3 */
414 //            $alt =& $this->add_alternative_part($message);
415             $alt = $this->add_alternative_part($message);
416             $this->add_text_part($alt, $this->html_text);
417             $this->add_html_part($alt);
418           } else {
419             $this->add_html_part($message);
420           }
421
422           for ($i=0; $i<count($this->attachments); $i++) {
423             $this->add_attachment_part($message, $this->attachments[$i]);
424           }
425           break;
426         case (($html == true) && ($attachments == true) && ($html_images == true)):
427 /* HPDL PHP3 */
428 //          $message =& $this->add_mixed_part();
429           $message = $this->add_mixed_part();
430
hpdl
734
431           if (!empty($this->html_text)) {
hpdl
1
432 /* HPDL PHP3 */
433 //            $alt =& $this->add_alternative_part($message);
434             $alt = $this->add_alternative_part($message);
435             $this->add_text_part($alt, $this->html_text);
436 /* HPDL PHP3 */
437 //            $rel =& $this->add_related_part($alt);
438             $rel = $this->add_related_part($alt);
439           } else {
440 /* HPDL PHP3 */
441 //            $rel =& $this->add_related_part($message);
442             $rel = $this->add_related_part($message);
443           }
444           $this->add_html_part($rel);
445
446           for ($i=0; $i<count($this->html_images); $i++) {
447             $this->add_html_image_part($rel, $this->html_images[$i]);
448           }
449
450           for ($i=0; $i<count($this->attachments); $i++) {
451             $this->add_attachment_part($message, $this->attachments[$i]);
452           }
453           break;
454       }
455
456       if ( (isset($message)) && (is_object($message)) ) {
457         $output = $message->encode();
458         $this->output = $output['body'];
459
460         reset($output['headers']);
461         while (list($key, $value) = each($output['headers'])) {
462           $headers[] = $key . ': ' . $value;
463         }
464
465         $this->headers = array_merge($this->headers, $headers);
466
467         return true;
468       } else {
469         return false;
470       }
471     }
472
473 /**
474  * Sends the mail.
475  */
476
477     function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
hpdl
476
478       if ((strpos($to_name, "\n") !== false) || (strpos($to_name, "\r") !== false)) {
hpdl
444
479         return false;
480       }
481
482       if ((strpos($to_addr, "\n") !== false) || (strpos($to_addr, "\r") !== false)) {
483         return false;
484       }
485
486       if ((strpos($subject, "\n") !== false) || (strpos($subject, "\r") !== false)) {
487         return false;
488       }
489
490       if ((strpos($from_name, "\n") !== false) || (strpos($from_name, "\r") !== false)) {
491         return false;
492       }
493
494       if ((strpos($from_addr, "\n") !== false) || (strpos($from_addr, "\r") !== false)) {
495         return false;
496       }
497
hpdl
1
498       $to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr);
499       $from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr);
500
501       if (is_string($headers)) {
502         $headers = explode($this->lf, trim($headers));
503       }
504
505       for ($i=0; $i<count($headers); $i++) {
506         if (is_array($headers[$i])) {
507           for ($j=0; $j<count($headers[$i]); $j++) {
508             if ($headers[$i][$j] != '') {
509               $xtra_headers[] = $headers[$i][$j];
510             }
511           }
512         }
513
514         if ($headers[$i] != '') {
515           $xtra_headers[] = $headers[$i];
516         }
517       }
518
519       if (!isset($xtra_headers)) {
520         $xtra_headers = array();
521       }
522
523       if (EMAIL_TRANSPORT == 'smtp') {
524         return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
525       } else {
526         return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
527       }
528     }
529
530 /**
531  * Use this method to return the email
532  * in message/rfc822 format. Useful for
533  * adding an email to another email as
534  * an attachment. there's a commented
535  * out example in example.php.
536  *
537  * string get_rfc822(string To name,
538  *       string To email,
539  *       string From name,
540  *       string From email,
541  *       [string Subject,
542  *        string Extra headers])
543  */
544
545     function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
546 // Make up the date header as according to RFC822
547       $date = 'Date: ' . date('D, d M y H:i:s');
548       $to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr);
549       $from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr);
550
551       if (is_string($subject)) {
552         $subject = 'Subject: ' . $subject;
553       }
554
555       if (is_string($headers)) {
556         $headers = explode($this->lf, trim($headers));
557       }
558
559       for ($i=0; $i<count($headers); $i++) {
560         if (is_array($headers[$i])) {
561           for ($j=0; $j<count($headers[$i]); $j++) {
562             if ($headers[$i][$j] != '') {
563               $xtra_headers[] = $headers[$i][$j];
564             }
565           }
566         }
567
568         if ($headers[$i] != '') {
569           $xtra_headers[] = $headers[$i];
570         }
571       }
572
573       if (!isset($xtra_headers)) {
574         $xtra_headers = array();
575       }
576
577       $headers = array_merge($this->headers, $xtra_headers);
578
579       return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output;
580     }
581   }
582 ?>