Quick Search:

View

Revision:

Diff

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