Quick Search:

View

Revision:

Diff

Diff from 448 to:

Annotations

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

Annotated File View

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