Quick Search:

View

Revision:

Diff

Diff from 1533 to:

Annotations

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

Annotated File View

hpdl
1518
1 <?php
2 /*
3   $Id: $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2007 osCommerce
9
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.
13 */
14
15   class osC_Mail {
16     var $_to = array(),
17         $_from = array(),
18         $_cc = array(),
19         $_bcc = array(),
20         $_subject,
21         $_body_plain,
22         $_body_html,
23         $_attachments = array(),
24         $_images = array(),
25         $_boundary,
26         $_headers = array('X-Mailer' => 'osCommerce'),
27         $_body,
28         $_content_transfer_encoding = '7bit',
29         $_charset = 'iso-8859-1';
30
31     function osC_Mail($to = null, $to_email_address = null, $from = null, $from_email_address = null, $subject = null) {
32       if ( !empty($to_email_address) ) {
33         $this->_to[] = array('name' => $to,
34                              'email_address' => $to_email_address);
35       }
36
37       if ( !empty($from_email_address) ) {
38         $this->_from = array('name' => $from,
39                              'email_address' => $from_email_address);
40       }
41
42       if ( !empty($subject) ) {
43         $this->_subject = $subject;
44       }
45     }
46
47     function addTo($name = null, $email_address) {
48       $this->_to[] = array('name' => $name,
49                            'email_address' => $email_address);
50     }
51
52     function setFrom($name = null, $email_address) {
53       $this->_from = array('name' => $name,
54                            'email_address' => $email_address);
55     }
56
57     function addCC($name = null, $email_address) {
58       $this->_cc[] = array('name' => $name,
59                            'email_address' => $email_address);
60     }
61
62     function addBCC($name = null, $email_address) {
63       $this->_bcc[] = array('name' => $name,
64                             'email_address' => $email_address);
65     }
66
67     function clearTo() {
68       $this->_to = array();
69       $this->_cc = array();
70       $this->_bcc = array();
71       $this->_headers = array('X-Mailer' => 'osCommerce');
72     }
73
74     function setSubject($subject) {
75       $this->_subject = $subject;
76     }
77
78     function setBodyPlain($body) {
hpdl
1533
79       $this->_body_plain = $body;
hpdl
1519
80       $this->_body = null;
hpdl
1518
81     }
82
83     function setBodyHTML($body) {
hpdl
1533
84       $this->_body_html = $body;
hpdl
1519
85       $this->_body = null;
hpdl
1518
86     }
87
88     function setContentTransferEncoding($encoding) {
89       $this->_content_transfer_encoding = $encoding;
90     }
91
92     function setCharset($charset) {
93       $this->_charset = $charset;
94     }
95
96     function addHeader($key, $value) {
97       if ( ( strpos($key, "\n") !== false ) || ( strpos($key, "\r") !== false ) ) {
98         return false;
99       }
100
101       if ( ( strpos($value, "\n") !== false ) || ( strpos($value, "\r") !== false ) ) {
102         return false;
103       }
104
105       $this->_headers[$key] = $value;
106     }
107
108     function addAttachment($file, $is_uploaded = false) {
109       if ( $is_uploaded === true ) {
110       } elseif ( file_exists($file) && is_readable($file) ) {
111         $data = file_get_contents($file);
112         $filename = basename($file);
113         $mimetype = $this->_get_mime_type($filename);
114       } else {
115         return false;
116       }
117
118       $this->_attachments[] = array('filename' => $filename,
119                                     'mimetype' => $mimetype,
120                                     'data' => chunk_split(base64_encode($data)));
121     }
122
123     function addImage($file, $is_uploaded = false) {
124       if ( $is_uploaded === true ) {
125       } elseif ( file_exists($file) && is_readable($file) ) {
126         $data = file_get_contents($file);
127         $filename = basename($file);
128         $mimetype = $this->_get_mime_type($filename);
129       } else {
130         return false;
131       }
132
133       $this->_images[] = array('id' => md5(uniqid(time())),
134                                'filename' => $filename,
135                                'mimetype' => $mimetype,
136                                'data' => chunk_split(base64_encode($data)));
137     }
138
139     function send() {
hpdl
1519
140       if ( empty($this->_body) ) {
hpdl
1518
141         if ( !empty($this->_body_plain) && !empty($this->_body_html) ) {
142           $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
143           $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
144           $this->_alternative_boundary = '=_____MULTIPART_ALTERNATIVE_BOUNDARY____';
145
146           $this->_headers['MIME-Version'] = '1.0';
147           $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
148           $this->_headers['Content-Transfer-Encoding'] = $this->_content_transfer_encoding;
149
150           if ( !empty($this->_images) ) {
151             foreach ( $this->_images as $image ) {
152               $this->_body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->_body_html);
153             }
154
155             unset($image);
156           }
157
158           $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
159                          '--' . $this->_boundary . "\n";
160
161           $this->_body .= 'Content-Type: multipart/alternative; boundary="' . $this->_alternative_boundary . '";' . "\n\n" .
162                           '--' . $this->_alternative_boundary . "\n" .
163                           'Content-Type: text/plain; charset="' . $this->_charset . '"' . "\n" .
164                           'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
165                           $this->_body_plain . "\n\n" .
166                           '--' . $this->_alternative_boundary . "\n" .
167                           'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '"' . "\n\n" .
168                           '--' . $this->_related_boundary . "\n" .
169                           'Content-Type: text/html; charset="' . $this->_charset . '"' . "\n" .
170                           'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
171                           $this->_body_html . "\n\n";
172
173           if ( !empty($this->_images) ) {
174             foreach ( $this->_images as $image ) {
175               $this->_body .= $this->_build_image($image, $this->_related_boundary);
176             }
177
178             unset($image);
179           }
180
181           $this->_body .= '--' . $this->_related_boundary . '--' . "\n\n" .
182                           '--' . $this->_alternative_boundary . '--' . "\n\n";
183
184           if ( !empty($this->_attachments) ) {
185             foreach ( $this->_attachments as $attachment ) {
186               $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
187             }
188
189             unset($attachment);
190           }
191
192           $this->_body .= '--' . $this->_boundary . '--' . "\n\n";
193         } elseif ( !empty($this->_body_html) && !empty($this->_images) ) {
194           $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
195           $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
196
197           $this->_headers['MIME-Version'] = '1.0';
198           $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
199
200           foreach ( $this->_images as $image ) {
201             $this->_body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->_body_html);
202           }
203
204           unset($image);
205
206           $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
207                          '--' . $this->_boundary . "\n" .
208                          'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '";' . "\n\n" .
209                           '--' . $this->_related_boundary . "\n" .
210                           'Content-Type: text/html; charset="' . $this->_charset . '"' . "\n" .
211                           'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
212                           $this->_body_html . "\n\n";
213
214           foreach ( $this->_images as $image ) {
215             $this->_body .= $this->_build_image($image, $this->_related_boundary);
216           }
217
218           unset($image);
219
220           $this->_body .= '--' . $this->_related_boundary . '--' . "\n\n";
221
222           foreach ( $this->_attachments as $attachment ) {
223             $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
224           }
225
226           unset($attachment);
227
228           $this->_body .= '--' . $this->_boundary . '--' . "\n";
229         } elseif ( !empty($this->_attachments) ) {
230           $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
231           $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
232
233           $this->_headers['MIME-Version'] = '1.0';
234           $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
235
236           $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
237                          '--' . $this->_boundary . "\n" .
238                          'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '";' . "\n\n" .
239                          '--' . $this->_related_boundary . "\n" .
240                          'Content-Type: text/' . (empty($this->_body_plain) ? 'html' : 'plain') . '; charset="' . $this->_charset . '"' . "\n" .
241                          'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
242                          (empty($this->_body_plain) ? $this->_body_html : $this->_body_plain) . "\n\n" .
243                          '--' . $this->_related_boundary . '--' . "\n\n";
244
245           foreach ( $this->_attachments as $attachment ) {
246             $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
247           }
248
249           unset($attachment);
250
251           $this->_body .= '--' . $this->_boundary . '--' . "\n";
252         } elseif ( !empty($this->_body_html) ) {
253           $this->_headers['MIME-Version'] = '1.0';
254           $this->_headers['Content-Type'] = 'text/html; charset="' . $this->_charset . '"';
255           $this->_headers['Content-Transfer-Encoding'] = $this->_content_transfer_encoding;
256
257           $this->_body = $this->_body_html . "\n";
258         } else {
259           $this->_body = $this->_body_plain . "\n";
260         }
261       }
262
263       $to_email_addresses = array();
264
265       foreach ( $this->_to as $to ) {
266         if ( ( strpos($to['email_address'], "\n") !== false ) || ( strpos($to['email_address'], "\r") !== false ) ) {
267           return false;
268         }
269
270         if ( ( strpos($to['name'], "\n") !== false ) || ( strpos($to['name'], "\r") !== false ) ) {
271           return false;
272         }
273
274         if ( empty($to['name']) ) {
275           $to_email_addresses[] = $to['email_address'];
276         } else {
277           $to_email_addresses[] = '"' . $to['name'] . '" <' . $to['email_address'] . '>';
278         }
279       }
280
281       unset($to);
282
283       $cc_email_addresses = array();
284
285       foreach ( $this->_cc as $cc ) {
286         if ( empty($cc['name']) ) {
287           $cc_email_addresses[] = $cc['email_address'];
288         } else {
289           $cc_email_addresses[] = '"' . $cc['name'] . '" <' . $cc['email_address'] . '>';
290         }
291       }
292
293       unset($cc);
294
295       $bcc_email_addresses = array();
296
297       foreach ( $this->_bcc as $bcc ) {
298         if ( empty($bcc['name']) ) {
299           $bcc_email_addresses[] = $bcc['email_address'];
300         } else {
301           $bcc_email_addresses[] = '"' . $bcc['name'] . '" <' . $bcc['email_address'] . '>';
302         }
303       }
304
305       unset($bcc);
306
307       if ( empty($this->_from['name']) ) {
308         $this->addHeader('From', $this->_from['email_address']);
309       } else {
310         $this->addHeader('From', '"' . $this->_from['name'] . '" <' . $this->_from['email_address'] . '>');
311       }
312
313       if ( !empty($cc_email_addresses) ) {
314         $this->addHeader('Cc', implode(', ', $cc_email_addresses));
315       }
316
317       if ( !empty($bcc_email_addresses) ) {
318         $this->addHeader('Bcc', implode(', ', $bcc_email_addresses));
319       }
320
321       $headers = '';
322
323       foreach ( $this->_headers as $key => $value ) {
324         $headers .= $key . ': ' . $value . "\n";
325       }
326
327       if ( empty($this->_from['email_address']) || empty($to_email_addresses) ) {
328         return false;
329       }
330
331       if ( empty($this->_from['name']) ) {
332         @ini_set('sendmail_from', $this->_from['email_address']);
333       } else {
334         @ini_set('sendmail_from', '"' . $this->_from['name'] . '" <' . $this->_from['email_address'] . '>');
335       }
336
337       @mail(implode(', ', $to_email_addresses), $this->_subject, $this->_body, $headers);
338
339       @ini_restore('sendmail_from');
340     }
341
342     function _get_mime_type($file) {
343       $ext = substr($file, strrpos($file, '.') + 1);
344
345       $mime_types = array('gif' => 'image/gif',
346                           'jpg' => 'image/jpeg',
347                           'jpeg' => 'image/jpeg',
348                           'jpe' => 'image/jpeg',
349                           'bmp' => 'image/bmp',
350                           'png' => 'image/png',
351                           'tif' => 'image/tiff',
352                           'tiff' => 'image/tiff',
353                           'swf' => 'application/x-shockwave-flash');
354
355       if (isset($mime_types[$ext])) {
356         return $mime_types[$ext];
357       } else {
358         return 'application/octet-stream';
359       }
360     }
361
362     function _build_attachment($attachment, $boundary) {
363       return '--' . $boundary . "\n" .
364              'Content-Type: ' . $attachment['mimetype'] . '; name="' . $attachment['filename'] . '"' . "\n" .
365              'Content-Disposition: attachment' . "\n" .
366              'Content-Transfer-Encoding: base64' . "\n\n" .
367               $attachment['data'] . "\n\n";
368     }
369   
370     function _build_image($image, $boundary) {
371       return '--' . $boundary . "\n" .
372              'Content-Type: ' . $image['mimetype'] . '; name="' . $image['filename'] . '"' . "\n" .
373              'Content-ID: ' . $image['id'] . "\n" .
374              'Content-Disposition: inline' . "\n" .
375              'Content-Transfer-Encoding: base64' . "\n\n" .
376               $image['data'] . "\n\n";
377     }
378   }
379 ?>