1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The Mail_Mime class is used to create MIME E-mail messages |
5
|
|
|
* |
6
|
|
|
* The Mail_Mime class provides an OO interface to create MIME |
7
|
|
|
* enabled email messages. This way you can create emails that |
8
|
|
|
* contain plain-text bodies, HTML bodies, attachments, inline |
9
|
|
|
* images and specific headers. |
10
|
|
|
* |
11
|
|
|
* Compatible with PHP versions 4 and 5 |
12
|
|
|
* |
13
|
|
|
* LICENSE: This LICENSE is in the BSD license style. |
14
|
|
|
* Copyright (c) 2002-2003, Richard Heyes <[email protected]> |
15
|
|
|
* Copyright (c) 2003-2006, PEAR <[email protected]> |
16
|
|
|
* All rights reserved. |
17
|
|
|
* |
18
|
|
|
* Redistribution and use in source and binary forms, with or |
19
|
|
|
* without modification, are permitted provided that the following |
20
|
|
|
* conditions are met: |
21
|
|
|
* |
22
|
|
|
* - Redistributions of source code must retain the above copyright |
23
|
|
|
* notice, this list of conditions and the following disclaimer. |
24
|
|
|
* - Redistributions in binary form must reproduce the above copyright |
25
|
|
|
* notice, this list of conditions and the following disclaimer in the |
26
|
|
|
* documentation and/or other materials provided with the distribution. |
27
|
|
|
* - Neither the name of the authors, nor the names of its contributors |
28
|
|
|
* may be used to endorse or promote products derived from this |
29
|
|
|
* software without specific prior written permission. |
30
|
|
|
* |
31
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
32
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
33
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
34
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
35
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
36
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
37
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
38
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
39
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
41
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE. |
42
|
|
|
* |
43
|
|
|
* @category Mail |
44
|
|
|
* @package Mail_Mime |
45
|
|
|
* @author Richard Heyes <[email protected]> |
46
|
|
|
* @author Tomas V.V. Cox <[email protected]> |
47
|
|
|
* @author Cipriano Groenendal <[email protected]> |
48
|
|
|
* @author Sean Coates <[email protected]> |
49
|
|
|
* @author Aleksander Machniak <[email protected]> |
50
|
|
|
* @copyright 2003-2006 PEAR <[email protected]> |
51
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
52
|
|
|
* @version CVS: $Id$ |
53
|
|
|
* @link http://pear.php.net/package/Mail_mime |
54
|
|
|
* |
55
|
|
|
* This class is based on HTML Mime Mail class from |
56
|
|
|
* Richard Heyes <[email protected]> which was based also |
57
|
|
|
* in the mime_mail.class by Tobias Ratschiller <[email protected]> |
58
|
|
|
* and Sascha Schumann <[email protected]> |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* require PEAR |
63
|
|
|
* |
64
|
|
|
* This package depends on PEAR to raise errors. |
65
|
|
|
*/ |
66
|
|
|
require_once 'PEAR.php'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* require Mail_mimePart |
70
|
|
|
* |
71
|
|
|
* Mail_mimePart contains the code required to |
72
|
|
|
* create all the different parts a mail can |
73
|
|
|
* consist of. |
74
|
|
|
*/ |
75
|
|
|
require_once 'Mail/mimePart.php'; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The Mail_Mime class provides an OO interface to create MIME |
80
|
|
|
* enabled email messages. This way you can create emails that |
81
|
|
|
* contain plain-text bodies, HTML bodies, attachments, inline |
82
|
|
|
* images and specific headers. |
83
|
|
|
* |
84
|
|
|
* @category Mail |
85
|
|
|
* @package Mail_Mime |
86
|
|
|
* @author Richard Heyes <[email protected]> |
87
|
|
|
* @author Tomas V.V. Cox <[email protected]> |
88
|
|
|
* @author Cipriano Groenendal <[email protected]> |
89
|
|
|
* @author Sean Coates <[email protected]> |
90
|
|
|
* @copyright 2003-2006 PEAR <[email protected]> |
91
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
92
|
|
|
* @version Release: @package_version@ |
93
|
|
|
* @link http://pear.php.net/package/Mail_mime |
94
|
|
|
*/ |
95
|
|
|
class Mail_mime |
96
|
|
|
{ |
97
|
|
|
/** |
98
|
|
|
* Contains the plain text part of the email |
99
|
|
|
* |
100
|
|
|
* @var string |
101
|
|
|
* @access private |
102
|
|
|
*/ |
103
|
|
|
var $_txtbody; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Contains the html part of the email |
107
|
|
|
* |
108
|
|
|
* @var string |
109
|
|
|
* @access private |
110
|
|
|
*/ |
111
|
|
|
var $_htmlbody; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* list of the attached images |
115
|
|
|
* |
116
|
|
|
* @var array |
117
|
|
|
* @access private |
118
|
|
|
*/ |
119
|
|
|
var $_html_images = array(); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* list of the attachements |
123
|
|
|
* |
124
|
|
|
* @var array |
125
|
|
|
* @access private |
126
|
|
|
*/ |
127
|
|
|
var $_parts = array(); |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Headers for the mail |
131
|
|
|
* |
132
|
|
|
* @var array |
133
|
|
|
* @access private |
134
|
|
|
*/ |
135
|
|
|
var $_headers = array(); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Build parameters |
139
|
|
|
* |
140
|
|
|
* @var array |
141
|
|
|
* @access private |
142
|
|
|
*/ |
143
|
|
|
var $_build_params = array( |
144
|
|
|
// What encoding to use for the headers |
145
|
|
|
// Options: quoted-printable or base64 |
146
|
|
|
'head_encoding' => 'quoted-printable', |
147
|
|
|
// What encoding to use for plain text |
148
|
|
|
// Options: 7bit, 8bit, base64, or quoted-printable |
149
|
|
|
'text_encoding' => 'quoted-printable', |
150
|
|
|
// What encoding to use for html |
151
|
|
|
// Options: 7bit, 8bit, base64, or quoted-printable |
152
|
|
|
'html_encoding' => 'quoted-printable', |
153
|
|
|
// The character set to use for html |
154
|
|
|
'html_charset' => 'ISO-8859-1', |
155
|
|
|
// The character set to use for text |
156
|
|
|
'text_charset' => 'ISO-8859-1', |
157
|
|
|
// The character set to use for headers |
158
|
|
|
'head_charset' => 'ISO-8859-1', |
159
|
|
|
// End-of-line sequence |
160
|
|
|
'eol' => "\r\n", |
161
|
|
|
// Delay attachment files IO until building the message |
162
|
|
|
'delay_file_io' => false |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Constructor function |
167
|
|
|
* |
168
|
|
|
* @param mixed $params Build parameters that change the way the email |
169
|
|
|
* is built. Should be an associative array. |
170
|
|
|
* See $_build_params. |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
* @access public |
174
|
|
|
*/ |
175
|
|
|
function Mail_mime($params = array()) |
176
|
|
|
{ |
177
|
|
|
// Backward-compatible EOL setting |
178
|
|
|
if (is_string($params)) { |
179
|
|
|
$this->_build_params['eol'] = $params; |
180
|
|
|
} else if (defined('MAIL_MIME_CRLF') && !isset($params['eol'])) { |
181
|
|
|
$this->_build_params['eol'] = MAIL_MIME_CRLF; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Update build parameters |
185
|
|
|
if (!empty($params) && is_array($params)) { |
186
|
|
|
while (list($key, $value) = each($params)) { |
187
|
|
|
$this->_build_params[$key] = $value; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set build parameter value |
194
|
|
|
* |
195
|
|
|
* @param string $name Parameter name |
196
|
|
|
* @param string $value Parameter value |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
* @access public |
200
|
|
|
* @since 1.6.0 |
201
|
|
|
*/ |
202
|
|
|
function setParam($name, $value) |
203
|
|
|
{ |
204
|
|
|
$this->_build_params[$name] = $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get build parameter value |
209
|
|
|
* |
210
|
|
|
* @param string $name Parameter name |
211
|
|
|
* |
212
|
|
|
* @return mixed Parameter value |
213
|
|
|
* @access public |
214
|
|
|
* @since 1.6.0 |
215
|
|
|
*/ |
216
|
|
|
function getParam($name) |
217
|
|
|
{ |
218
|
|
|
return isset($this->_build_params[$name]) ? $this->_build_params[$name] : null; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Accessor function to set the body text. Body text is used if |
223
|
|
|
* it's not an html mail being sent or else is used to fill the |
224
|
|
|
* text/plain part that emails clients who don't support |
225
|
|
|
* html should show. |
226
|
|
|
* |
227
|
|
|
* @param string $data Either a string or |
228
|
|
|
* the file name with the contents |
229
|
|
|
* @param bool $isfile If true the first param should be treated |
230
|
|
|
* as a file name, else as a string (default) |
231
|
|
|
* @param bool $append If true the text or file is appended to |
232
|
|
|
* the existing body, else the old body is |
233
|
|
|
* overwritten |
234
|
|
|
* |
235
|
|
|
* @return mixed True on success or PEAR_Error object |
236
|
|
|
* @access public |
237
|
|
|
*/ |
238
|
|
|
function setTXTBody($data, $isfile = false, $append = false) |
239
|
|
|
{ |
240
|
|
|
if (!$isfile) { |
241
|
|
|
if (!$append) { |
242
|
|
|
$this->_txtbody = $data; |
243
|
|
|
} else { |
244
|
|
|
$this->_txtbody .= $data; |
245
|
|
|
} |
246
|
|
|
} else { |
247
|
|
|
$cont = $this->_file2str($data); |
248
|
|
|
if ($this->_isError($cont)) { |
249
|
|
|
return $cont; |
250
|
|
|
} |
251
|
|
|
if (!$append) { |
252
|
|
|
$this->_txtbody = $cont; |
253
|
|
|
} else { |
254
|
|
|
$this->_txtbody .= $cont; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get message text body |
263
|
|
|
* |
264
|
|
|
* @return string Text body |
265
|
|
|
* @access public |
266
|
|
|
* @since 1.6.0 |
267
|
|
|
*/ |
268
|
|
|
function getTXTBody() |
269
|
|
|
{ |
270
|
|
|
return $this->_txtbody; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Adds a html part to the mail. |
275
|
|
|
* |
276
|
|
|
* @param string $data Either a string or the file name with the |
277
|
|
|
* contents |
278
|
|
|
* @param bool $isfile A flag that determines whether $data is a |
279
|
|
|
* filename, or a string(false, default) |
280
|
|
|
* |
281
|
|
|
* @return bool True on success |
282
|
|
|
* @access public |
283
|
|
|
*/ |
284
|
|
|
function setHTMLBody($data, $isfile = false) |
285
|
|
|
{ |
286
|
|
|
if (!$isfile) { |
287
|
|
|
$this->_htmlbody = $data; |
288
|
|
|
} else { |
289
|
|
|
$cont = $this->_file2str($data); |
290
|
|
|
if ($this->_isError($cont)) { |
291
|
|
|
return $cont; |
292
|
|
|
} |
293
|
|
|
$this->_htmlbody = $cont; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return true; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get message HTML body |
301
|
|
|
* |
302
|
|
|
* @return string HTML body |
303
|
|
|
* @access public |
304
|
|
|
* @since 1.6.0 |
305
|
|
|
*/ |
306
|
|
|
function getHTMLBody() |
307
|
|
|
{ |
308
|
|
|
return $this->_htmlbody; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Adds an image to the list of embedded images. |
313
|
|
|
* |
314
|
|
|
* @param string $file The image file name OR image data itself |
315
|
|
|
* @param string $c_type The content type |
316
|
|
|
* @param string $name The filename of the image. |
317
|
|
|
* Only used if $file is the image data. |
318
|
|
|
* @param bool $isfile Whether $file is a filename or not. |
319
|
|
|
* Defaults to true |
320
|
|
|
* @param string $content_id Desired Content-ID of MIME part |
321
|
|
|
* Defaults to generated unique ID |
322
|
|
|
* |
323
|
|
|
* @return bool True on success |
324
|
|
|
* @access public |
325
|
|
|
*/ |
326
|
|
|
function addHTMLImage( |
327
|
|
|
$file, |
328
|
|
|
$c_type = 'application/octet-stream', |
329
|
|
|
$name = '', |
330
|
|
|
$isfile = true, |
331
|
|
|
$content_id = null |
332
|
|
|
) { |
333
|
|
|
$bodyfile = null; |
334
|
|
|
|
335
|
|
|
if ($isfile) { |
336
|
|
|
// Don't load file into memory |
337
|
|
|
if ($this->_build_params['delay_file_io']) { |
338
|
|
|
$filedata = null; |
339
|
|
|
$bodyfile = $file; |
340
|
|
|
} else { |
341
|
|
|
if ($this->_isError($filedata = $this->_file2str($file))) { |
342
|
|
|
return $filedata; |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
$filename = ($name ? $name : $file); |
346
|
|
|
} else { |
347
|
|
|
$filedata = $file; |
348
|
|
|
$filename = $name; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
if (!$content_id) { |
352
|
|
|
$content_id = preg_replace('/[^0-9a-zA-Z]/', '', uniqid(time(), true)); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
$this->_html_images[] = array( |
356
|
|
|
'body' => $filedata, |
357
|
|
|
'body_file' => $bodyfile, |
358
|
|
|
'name' => $filename, |
359
|
|
|
'c_type' => $c_type, |
360
|
|
|
'cid' => $content_id |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
return true; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Adds a file to the list of attachments. |
368
|
|
|
* |
369
|
|
|
* @param string $file The file name of the file to attach |
370
|
|
|
* or the file contents itself |
371
|
|
|
* @param string $c_type The content type |
372
|
|
|
* @param string $name The filename of the attachment |
373
|
|
|
* Only use if $file is the contents |
374
|
|
|
* @param bool $isfile Whether $file is a filename or not. Defaults to true |
375
|
|
|
* @param string $encoding The type of encoding to use. Defaults to base64. |
376
|
|
|
* Possible values: 7bit, 8bit, base64 or quoted-printable. |
377
|
|
|
* @param string $disposition The content-disposition of this file |
378
|
|
|
* Defaults to attachment. |
379
|
|
|
* Possible values: attachment, inline. |
380
|
|
|
* @param string $charset The character set of attachment's content. |
381
|
|
|
* @param string $language The language of the attachment |
382
|
|
|
* @param string $location The RFC 2557.4 location of the attachment |
383
|
|
|
* @param string $n_encoding Encoding of the attachment's name in Content-Type |
384
|
|
|
* By default filenames are encoded using RFC2231 method |
385
|
|
|
* Here you can set RFC2047 encoding (quoted-printable |
386
|
|
|
* or base64) instead |
387
|
|
|
* @param string $f_encoding Encoding of the attachment's filename |
388
|
|
|
* in Content-Disposition header. |
389
|
|
|
* @param string $description Content-Description header |
390
|
|
|
* @param string $h_charset The character set of the headers e.g. filename |
391
|
|
|
* If not specified, $charset will be used |
392
|
|
|
* @param array $add_headers Additional part headers. Array keys can be in form |
393
|
|
|
* of <header_name>:<parameter_name> |
394
|
|
|
* |
395
|
|
|
* @return mixed True on success or PEAR_Error object |
396
|
|
|
* @access public |
397
|
|
|
*/ |
398
|
|
|
function addAttachment( |
399
|
|
|
$file, |
400
|
|
|
$c_type = 'application/octet-stream', |
401
|
|
|
$name = '', |
402
|
|
|
$isfile = true, |
403
|
|
|
$encoding = 'base64', |
404
|
|
|
$disposition = 'attachment', |
405
|
|
|
$charset = '', |
406
|
|
|
$language = '', |
407
|
|
|
$location = '', |
408
|
|
|
$n_encoding = null, |
409
|
|
|
$f_encoding = null, |
410
|
|
|
$description = '', |
411
|
|
|
$h_charset = null, |
412
|
|
|
$add_headers = array() |
413
|
|
|
) { |
414
|
|
|
$bodyfile = null; |
415
|
|
|
|
416
|
|
|
if ($isfile) { |
417
|
|
|
// Don't load file into memory |
418
|
|
|
if ($this->_build_params['delay_file_io']) { |
419
|
|
|
$filedata = null; |
420
|
|
|
$bodyfile = $file; |
421
|
|
|
} else { |
422
|
|
|
if ($this->_isError($filedata = $this->_file2str($file))) { |
423
|
|
|
return $filedata; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
// Force the name the user supplied, otherwise use $file |
427
|
|
|
$filename = ($name ? $name : $this->_basename($file)); |
428
|
|
|
} else { |
429
|
|
|
$filedata = $file; |
430
|
|
|
$filename = $name; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if (!strlen($filename)) { |
434
|
|
|
$msg = "The supplied filename for the attachment can't be empty"; |
435
|
|
|
return $this->_raiseError($msg); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$this->_parts[] = array( |
439
|
|
|
'body' => $filedata, |
440
|
|
|
'body_file' => $bodyfile, |
441
|
|
|
'name' => $filename, |
442
|
|
|
'c_type' => $c_type, |
443
|
|
|
'charset' => $charset, |
444
|
|
|
'encoding' => $encoding, |
445
|
|
|
'language' => $language, |
446
|
|
|
'location' => $location, |
447
|
|
|
'disposition' => $disposition, |
448
|
|
|
'description' => $description, |
449
|
|
|
'add_headers' => $add_headers, |
450
|
|
|
'name_encoding' => $n_encoding, |
451
|
|
|
'filename_encoding' => $f_encoding, |
452
|
|
|
'headers_charset' => $h_charset, |
453
|
|
|
); |
454
|
|
|
|
455
|
|
|
return true; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Get the contents of the given file name as string |
460
|
|
|
* |
461
|
|
|
* @param string $file_name Path of file to process |
462
|
|
|
* |
463
|
|
|
* @return string Contents of $file_name |
464
|
|
|
* @access private |
465
|
|
|
*/ |
466
|
|
|
function _file2str($file_name) |
467
|
|
|
{ |
468
|
|
|
// Check state of file and raise an error properly |
469
|
|
|
if (!file_exists($file_name)) { |
470
|
|
|
return $this->_raiseError('File not found: ' . $file_name); |
471
|
|
|
} |
472
|
|
|
if (!is_file($file_name)) { |
473
|
|
|
return $this->_raiseError('Not a regular file: ' . $file_name); |
474
|
|
|
} |
475
|
|
|
if (!is_readable($file_name)) { |
476
|
|
|
return $this->_raiseError('File is not readable: ' . $file_name); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
// Temporarily reset magic_quotes_runtime and read file contents |
480
|
|
|
if ($magic_quote_setting = get_magic_quotes_runtime()) { |
481
|
|
|
@ini_set('magic_quotes_runtime', 0); |
482
|
|
|
} |
483
|
|
|
$cont = file_get_contents($file_name); |
484
|
|
|
if ($magic_quote_setting) { |
485
|
|
|
@ini_set('magic_quotes_runtime', $magic_quote_setting); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
return $cont; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Adds a text subpart to the mimePart object and |
493
|
|
|
* returns it during the build process. |
494
|
|
|
* |
495
|
|
|
* @param mixed &$obj The object to add the part to, or |
496
|
|
|
* anything else if a new object is to be created. |
497
|
|
|
* @param string $text The text to add. |
498
|
|
|
* |
499
|
|
|
* @return object The text mimePart object |
500
|
|
|
* @access private |
501
|
|
|
*/ |
502
|
|
|
function &_addTextPart(&$obj, $text = '') |
503
|
|
|
{ |
504
|
|
|
$params['content_type'] = 'text/plain'; |
505
|
|
|
$params['encoding'] = $this->_build_params['text_encoding']; |
506
|
|
|
$params['charset'] = $this->_build_params['text_charset']; |
507
|
|
|
$params['eol'] = $this->_build_params['eol']; |
508
|
|
|
|
509
|
|
|
if (is_object($obj)) { |
510
|
|
|
$ret = $obj->addSubpart($text, $params); |
511
|
|
|
} else { |
512
|
|
|
$ret = new Mail_mimePart($text, $params); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
return $ret; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Adds a html subpart to the mimePart object and |
520
|
|
|
* returns it during the build process. |
521
|
|
|
* |
522
|
|
|
* @param mixed &$obj The object to add the part to, or |
523
|
|
|
* anything else if a new object is to be created. |
524
|
|
|
* |
525
|
|
|
* @return object The html mimePart object |
526
|
|
|
* @access private |
527
|
|
|
*/ |
528
|
|
|
function &_addHtmlPart(&$obj) |
529
|
|
|
{ |
530
|
|
|
$params['content_type'] = 'text/html'; |
531
|
|
|
$params['encoding'] = $this->_build_params['html_encoding']; |
532
|
|
|
$params['charset'] = $this->_build_params['html_charset']; |
533
|
|
|
$params['eol'] = $this->_build_params['eol']; |
534
|
|
|
|
535
|
|
|
if (is_object($obj)) { |
536
|
|
|
$ret = $obj->addSubpart($this->_htmlbody, $params); |
537
|
|
|
} else { |
538
|
|
|
$ret = new Mail_mimePart($this->_htmlbody, $params); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
return $ret; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Creates a new mimePart object, using multipart/mixed as |
546
|
|
|
* the initial content-type and returns it during the |
547
|
|
|
* build process. |
548
|
|
|
* |
549
|
|
|
* @return object The multipart/mixed mimePart object |
550
|
|
|
* @access private |
551
|
|
|
*/ |
552
|
|
|
function &_addMixedPart() |
553
|
|
|
{ |
554
|
|
|
$params['content_type'] = 'multipart/mixed'; |
555
|
|
|
$params['eol'] = $this->_build_params['eol']; |
556
|
|
|
|
557
|
|
|
// Create empty multipart/mixed Mail_mimePart object to return |
558
|
|
|
$ret = new Mail_mimePart('', $params); |
559
|
|
|
return $ret; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Adds a multipart/alternative part to a mimePart |
564
|
|
|
* object (or creates one), and returns it during |
565
|
|
|
* the build process. |
566
|
|
|
* |
567
|
|
|
* @param mixed &$obj The object to add the part to, or |
568
|
|
|
* anything else if a new object is to be created. |
569
|
|
|
* |
570
|
|
|
* @return object The multipart/mixed mimePart object |
571
|
|
|
* @access private |
572
|
|
|
*/ |
573
|
|
|
function &_addAlternativePart(&$obj) |
574
|
|
|
{ |
575
|
|
|
$params['content_type'] = 'multipart/alternative'; |
576
|
|
|
$params['eol'] = $this->_build_params['eol']; |
577
|
|
|
|
578
|
|
|
if (is_object($obj)) { |
579
|
|
|
$ret = $obj->addSubpart('', $params); |
580
|
|
|
} else { |
581
|
|
|
$ret = new Mail_mimePart('', $params); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
return $ret; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Adds a multipart/related part to a mimePart |
589
|
|
|
* object (or creates one), and returns it during |
590
|
|
|
* the build process. |
591
|
|
|
* |
592
|
|
|
* @param mixed &$obj The object to add the part to, or |
593
|
|
|
* anything else if a new object is to be created |
594
|
|
|
* |
595
|
|
|
* @return object The multipart/mixed mimePart object |
596
|
|
|
* @access private |
597
|
|
|
*/ |
598
|
|
|
function &_addRelatedPart(&$obj) |
599
|
|
|
{ |
600
|
|
|
$params['content_type'] = 'multipart/related'; |
601
|
|
|
$params['eol'] = $this->_build_params['eol']; |
602
|
|
|
|
603
|
|
|
if (is_object($obj)) { |
604
|
|
|
$ret = $obj->addSubpart('', $params); |
605
|
|
|
} else { |
606
|
|
|
$ret = new Mail_mimePart('', $params); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
return $ret; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Adds an html image subpart to a mimePart object |
614
|
|
|
* and returns it during the build process. |
615
|
|
|
* |
616
|
|
|
* @param object &$obj The mimePart to add the image to |
617
|
|
|
* @param array $value The image information |
618
|
|
|
* |
619
|
|
|
* @return object The image mimePart object |
620
|
|
|
* @access private |
621
|
|
|
*/ |
622
|
|
|
function &_addHtmlImagePart(&$obj, $value) |
623
|
|
|
{ |
624
|
|
|
$params['content_type'] = $value['c_type']; |
625
|
|
|
$params['encoding'] = 'base64'; |
626
|
|
|
$params['disposition'] = 'inline'; |
627
|
|
|
$params['filename'] = $value['name']; |
628
|
|
|
$params['cid'] = $value['cid']; |
629
|
|
|
$params['body_file'] = $value['body_file']; |
630
|
|
|
$params['eol'] = $this->_build_params['eol']; |
631
|
|
|
|
632
|
|
|
if (!empty($value['name_encoding'])) { |
633
|
|
|
$params['name_encoding'] = $value['name_encoding']; |
634
|
|
|
} |
635
|
|
|
if (!empty($value['filename_encoding'])) { |
636
|
|
|
$params['filename_encoding'] = $value['filename_encoding']; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
$ret = $obj->addSubpart($value['body'], $params); |
640
|
|
|
return $ret; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Adds an attachment subpart to a mimePart object |
645
|
|
|
* and returns it during the build process. |
646
|
|
|
* |
647
|
|
|
* @param object &$obj The mimePart to add the image to |
648
|
|
|
* @param array $value The attachment information |
649
|
|
|
* |
650
|
|
|
* @return object The image mimePart object |
651
|
|
|
* @access private |
652
|
|
|
*/ |
653
|
|
|
function &_addAttachmentPart(&$obj, $value) |
654
|
|
|
{ |
655
|
|
|
$params['eol'] = $this->_build_params['eol']; |
656
|
|
|
$params['filename'] = $value['name']; |
657
|
|
|
$params['encoding'] = $value['encoding']; |
658
|
|
|
$params['content_type'] = $value['c_type']; |
659
|
|
|
$params['body_file'] = $value['body_file']; |
660
|
|
|
$params['disposition'] = isset($value['disposition']) ? |
661
|
|
|
$value['disposition'] : 'attachment'; |
662
|
|
|
|
663
|
|
|
// content charset |
664
|
|
|
if (!empty($value['charset'])) { |
665
|
|
|
$params['charset'] = $value['charset']; |
666
|
|
|
} |
667
|
|
|
// headers charset (filename, description) |
668
|
|
|
if (!empty($value['headers_charset'])) { |
669
|
|
|
$params['headers_charset'] = $value['headers_charset']; |
670
|
|
|
} |
671
|
|
|
if (!empty($value['language'])) { |
672
|
|
|
$params['language'] = $value['language']; |
673
|
|
|
} |
674
|
|
|
if (!empty($value['location'])) { |
675
|
|
|
$params['location'] = $value['location']; |
676
|
|
|
} |
677
|
|
|
if (!empty($value['name_encoding'])) { |
678
|
|
|
$params['name_encoding'] = $value['name_encoding']; |
679
|
|
|
} |
680
|
|
|
if (!empty($value['filename_encoding'])) { |
681
|
|
|
$params['filename_encoding'] = $value['filename_encoding']; |
682
|
|
|
} |
683
|
|
|
if (!empty($value['description'])) { |
684
|
|
|
$params['description'] = $value['description']; |
685
|
|
|
} |
686
|
|
|
if (is_array($value['add_headers'])) { |
687
|
|
|
$params['headers'] = $value['add_headers']; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
$ret = $obj->addSubpart($value['body'], $params); |
691
|
|
|
return $ret; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Returns the complete e-mail, ready to send using an alternative |
696
|
|
|
* mail delivery method. Note that only the mailpart that is made |
697
|
|
|
* with Mail_Mime is created. This means that, |
698
|
|
|
* YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF |
699
|
|
|
* using the $headers parameter! |
700
|
|
|
* |
701
|
|
|
* @param string $separation The separation between these two parts. |
702
|
|
|
* @param array $params The Build parameters passed to the |
703
|
|
|
* get() function. See get() for more info. |
704
|
|
|
* @param array $headers The extra headers that should be passed |
705
|
|
|
* to the headers() method. |
706
|
|
|
* See that function for more info. |
707
|
|
|
* @param bool $overwrite Overwrite the existing headers with new. |
708
|
|
|
* |
709
|
|
|
* @return mixed The complete e-mail or PEAR error object |
710
|
|
|
* @access public |
711
|
|
|
*/ |
712
|
|
|
function getMessage( |
713
|
|
|
$separation = null, |
714
|
|
|
$params = null, |
715
|
|
|
$headers = null, |
716
|
|
|
$overwrite = false |
717
|
|
|
) { |
718
|
|
|
if ($separation === null) { |
719
|
|
|
$separation = $this->_build_params['eol']; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
$body = $this->get($params); |
723
|
|
|
|
724
|
|
|
if ($this->_isError($body)) { |
725
|
|
|
return $body; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
return $this->txtHeaders($headers, $overwrite) . $separation . $body; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Returns the complete e-mail body, ready to send using an alternative |
733
|
|
|
* mail delivery method. |
734
|
|
|
* |
735
|
|
|
* @param array $params The Build parameters passed to the |
736
|
|
|
* get() method. See get() for more info. |
737
|
|
|
* |
738
|
|
|
* @return mixed The e-mail body or PEAR error object |
739
|
|
|
* @access public |
740
|
|
|
* @since 1.6.0 |
741
|
|
|
*/ |
742
|
|
|
function getMessageBody($params = null) |
743
|
|
|
{ |
744
|
|
|
return $this->get($params, null, true); |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Writes (appends) the complete e-mail into file. |
749
|
|
|
* |
750
|
|
|
* @param string $filename Output file location |
751
|
|
|
* @param array $params The Build parameters passed to the |
752
|
|
|
* get() method. See get() for more info. |
753
|
|
|
* @param array $headers The extra headers that should be passed |
754
|
|
|
* to the headers() function. |
755
|
|
|
* See that function for more info. |
756
|
|
|
* @param bool $overwrite Overwrite the existing headers with new. |
757
|
|
|
* |
758
|
|
|
* @return mixed True or PEAR error object |
759
|
|
|
* @access public |
760
|
|
|
* @since 1.6.0 |
761
|
|
|
*/ |
762
|
|
|
function saveMessage($filename, $params = null, $headers = null, $overwrite = false) |
763
|
|
|
{ |
764
|
|
|
// Check state of file and raise an error properly |
765
|
|
|
if (file_exists($filename) && !is_writable($filename)) { |
766
|
|
|
return $this->_raiseError('File is not writable: ' . $filename); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
// Temporarily reset magic_quotes_runtime and read file contents |
770
|
|
|
if ($magic_quote_setting = get_magic_quotes_runtime()) { |
771
|
|
|
@ini_set('magic_quotes_runtime', 0); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
if (!($fh = fopen($filename, 'ab'))) { |
775
|
|
|
return $this->_raiseError('Unable to open file: ' . $filename); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
// Write message headers into file (skipping Content-* headers) |
779
|
|
|
$head = $this->txtHeaders($headers, $overwrite, true); |
780
|
|
|
if (fwrite($fh, $head) === false) { |
781
|
|
|
return $this->_raiseError('Error writing to file: ' . $filename); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
fclose($fh); |
785
|
|
|
|
786
|
|
|
if ($magic_quote_setting) { |
787
|
|
|
@ini_set('magic_quotes_runtime', $magic_quote_setting); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
// Write the rest of the message into file |
791
|
|
|
$res = $this->get($params, $filename); |
792
|
|
|
|
793
|
|
|
return $res ? $res : true; |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
/** |
797
|
|
|
* Writes (appends) the complete e-mail body into file. |
798
|
|
|
* |
799
|
|
|
* @param string $filename Output file location |
800
|
|
|
* @param array $params The Build parameters passed to the |
801
|
|
|
* get() method. See get() for more info. |
802
|
|
|
* |
803
|
|
|
* @return mixed True or PEAR error object |
804
|
|
|
* @access public |
805
|
|
|
* @since 1.6.0 |
806
|
|
|
*/ |
807
|
|
|
function saveMessageBody($filename, $params = null) |
808
|
|
|
{ |
809
|
|
|
// Check state of file and raise an error properly |
810
|
|
|
if (file_exists($filename) && !is_writable($filename)) { |
811
|
|
|
return $this->_raiseError('File is not writable: ' . $filename); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
// Temporarily reset magic_quotes_runtime and read file contents |
815
|
|
|
if ($magic_quote_setting = get_magic_quotes_runtime()) { |
816
|
|
|
@ini_set('magic_quotes_runtime', 0); |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
if (!($fh = fopen($filename, 'ab'))) { |
820
|
|
|
return $this->_raiseError('Unable to open file: ' . $filename); |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
// Write the rest of the message into file |
824
|
|
|
$res = $this->get($params, $filename, true); |
825
|
|
|
|
826
|
|
|
return $res ? $res : true; |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
/** |
830
|
|
|
* Builds the multipart message from the list ($this->_parts) and |
831
|
|
|
* returns the mime content. |
832
|
|
|
* |
833
|
|
|
* @param array $params Build parameters that change the way the email |
834
|
|
|
* is built. Should be associative. See $_build_params. |
835
|
|
|
* @param resource $filename Output file where to save the message instead of |
836
|
|
|
* returning it |
837
|
|
|
* @param boolean $skip_head True if you want to return/save only the message |
838
|
|
|
* without headers |
839
|
|
|
* |
840
|
|
|
* @return mixed The MIME message content string, null or PEAR error object |
841
|
|
|
* @access public |
842
|
|
|
*/ |
843
|
|
|
function get($params = null, $filename = null, $skip_head = false) |
844
|
|
|
{ |
845
|
|
|
if (isset($params)) { |
846
|
|
|
while (list($key, $value) = each($params)) { |
847
|
|
|
$this->_build_params[$key] = $value; |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
if (isset($this->_headers['From'])) { |
852
|
|
|
// Bug #11381: Illegal characters in domain ID |
853
|
|
|
if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $this->_headers['From'], $matches)) { |
854
|
|
|
$domainID = $matches[1]; |
855
|
|
|
} else { |
856
|
|
|
$domainID = '@localhost'; |
857
|
|
|
} |
858
|
|
|
foreach ($this->_html_images as $i => $img) { |
859
|
|
|
$cid = $this->_html_images[$i]['cid']; |
860
|
|
|
if (!preg_match('#' . preg_quote($domainID) . '$#', $cid)) { |
861
|
|
|
$this->_html_images[$i]['cid'] = $cid . $domainID; |
862
|
|
|
} |
863
|
|
|
} |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
if (count($this->_html_images) && isset($this->_htmlbody)) { |
867
|
|
|
foreach ($this->_html_images as $key => $value) { |
868
|
|
|
$regex = array(); |
869
|
|
|
$regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . |
870
|
|
|
preg_quote($value['name'], '#') . '\3#'; |
871
|
|
|
$regex[] = '#(?i)url(?-i)\(\s*(["\']?)' . |
872
|
|
|
preg_quote($value['name'], '#') . '\1\s*\)#'; |
873
|
|
|
|
874
|
|
|
$rep = array(); |
875
|
|
|
$rep[] = '\1\2=\3cid:' . $value['cid'] . '\3'; |
876
|
|
|
$rep[] = 'url(\1cid:' . $value['cid'] . '\1)'; |
877
|
|
|
|
878
|
|
|
$this->_htmlbody = preg_replace($regex, $rep, $this->_htmlbody); |
879
|
|
|
$this->_html_images[$key]['name'] |
880
|
|
|
= $this->_basename($this->_html_images[$key]['name']); |
881
|
|
|
} |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
$this->_checkParams(); |
885
|
|
|
|
886
|
|
|
$null = -1; |
887
|
|
|
$attachments = count($this->_parts) > 0; |
888
|
|
|
$html_images = count($this->_html_images) > 0; |
889
|
|
|
$html = strlen($this->_htmlbody) > 0; |
890
|
|
|
$text = !$html && strlen($this->_txtbody); |
891
|
|
|
|
892
|
|
|
switch (true) { |
893
|
|
|
case $text && !$attachments: |
894
|
|
|
$message =& $this->_addTextPart($null, $this->_txtbody); |
895
|
|
|
break; |
896
|
|
|
|
897
|
|
|
case !$text && !$html && $attachments: |
898
|
|
|
$message =& $this->_addMixedPart(); |
899
|
|
|
for ($i = 0; $i < count($this->_parts); $i++) { |
900
|
|
|
$this->_addAttachmentPart($message, $this->_parts[$i]); |
901
|
|
|
} |
902
|
|
|
break; |
903
|
|
|
|
904
|
|
|
case $text && $attachments: |
905
|
|
|
$message =& $this->_addMixedPart(); |
906
|
|
|
$this->_addTextPart($message, $this->_txtbody); |
907
|
|
|
for ($i = 0; $i < count($this->_parts); $i++) { |
908
|
|
|
$this->_addAttachmentPart($message, $this->_parts[$i]); |
909
|
|
|
} |
910
|
|
|
break; |
911
|
|
|
|
912
|
|
|
case $html && !$attachments && !$html_images: |
913
|
|
|
if (isset($this->_txtbody)) { |
914
|
|
|
$message =& $this->_addAlternativePart($null); |
915
|
|
|
$this->_addTextPart($message, $this->_txtbody); |
916
|
|
|
$this->_addHtmlPart($message); |
917
|
|
|
} else { |
918
|
|
|
$message =& $this->_addHtmlPart($null); |
919
|
|
|
} |
920
|
|
|
break; |
921
|
|
|
|
922
|
|
|
case $html && !$attachments && $html_images: |
923
|
|
|
// * Content-Type: multipart/alternative; |
924
|
|
|
// * text |
925
|
|
|
// * Content-Type: multipart/related; |
926
|
|
|
// * html |
927
|
|
|
// * image... |
928
|
|
|
if (isset($this->_txtbody)) { |
929
|
|
|
$message =& $this->_addAlternativePart($null); |
930
|
|
|
$this->_addTextPart($message, $this->_txtbody); |
931
|
|
|
|
932
|
|
|
$ht =& $this->_addRelatedPart($message); |
933
|
|
|
$this->_addHtmlPart($ht); |
934
|
|
|
for ($i = 0; $i < count($this->_html_images); $i++) { |
935
|
|
|
$this->_addHtmlImagePart($ht, $this->_html_images[$i]); |
936
|
|
|
} |
937
|
|
|
} else { |
938
|
|
|
// * Content-Type: multipart/related; |
939
|
|
|
// * html |
940
|
|
|
// * image... |
941
|
|
|
$message =& $this->_addRelatedPart($null); |
942
|
|
|
$this->_addHtmlPart($message); |
943
|
|
|
for ($i = 0; $i < count($this->_html_images); $i++) { |
944
|
|
|
$this->_addHtmlImagePart($message, $this->_html_images[$i]); |
945
|
|
|
} |
946
|
|
|
} |
947
|
|
|
/* |
948
|
|
|
// #13444, #9725: the code below was a non-RFC compliant hack |
949
|
|
|
// * Content-Type: multipart/related; |
950
|
|
|
// * Content-Type: multipart/alternative; |
951
|
|
|
// * text |
952
|
|
|
// * html |
953
|
|
|
// * image... |
954
|
|
|
$message =& $this->_addRelatedPart($null); |
955
|
|
|
if (isset($this->_txtbody)) { |
956
|
|
|
$alt =& $this->_addAlternativePart($message); |
957
|
|
|
$this->_addTextPart($alt, $this->_txtbody); |
958
|
|
|
$this->_addHtmlPart($alt); |
959
|
|
|
} else { |
960
|
|
|
$this->_addHtmlPart($message); |
961
|
|
|
} |
962
|
|
|
for ($i = 0; $i < count($this->_html_images); $i++) { |
963
|
|
|
$this->_addHtmlImagePart($message, $this->_html_images[$i]); |
964
|
|
|
} |
965
|
|
|
*/ |
966
|
|
|
break; |
967
|
|
|
|
968
|
|
|
case $html && $attachments && !$html_images: |
969
|
|
|
$message =& $this->_addMixedPart(); |
970
|
|
|
if (isset($this->_txtbody)) { |
971
|
|
|
$alt =& $this->_addAlternativePart($message); |
972
|
|
|
$this->_addTextPart($alt, $this->_txtbody); |
973
|
|
|
$this->_addHtmlPart($alt); |
974
|
|
|
} else { |
975
|
|
|
$this->_addHtmlPart($message); |
976
|
|
|
} |
977
|
|
|
for ($i = 0; $i < count($this->_parts); $i++) { |
978
|
|
|
$this->_addAttachmentPart($message, $this->_parts[$i]); |
979
|
|
|
} |
980
|
|
|
break; |
981
|
|
|
|
982
|
|
|
case $html && $attachments && $html_images: |
983
|
|
|
$message =& $this->_addMixedPart(); |
984
|
|
|
if (isset($this->_txtbody)) { |
985
|
|
|
$alt =& $this->_addAlternativePart($message); |
986
|
|
|
$this->_addTextPart($alt, $this->_txtbody); |
987
|
|
|
$rel =& $this->_addRelatedPart($alt); |
988
|
|
|
} else { |
989
|
|
|
$rel =& $this->_addRelatedPart($message); |
990
|
|
|
} |
991
|
|
|
$this->_addHtmlPart($rel); |
992
|
|
|
for ($i = 0; $i < count($this->_html_images); $i++) { |
993
|
|
|
$this->_addHtmlImagePart($rel, $this->_html_images[$i]); |
994
|
|
|
} |
995
|
|
|
for ($i = 0; $i < count($this->_parts); $i++) { |
996
|
|
|
$this->_addAttachmentPart($message, $this->_parts[$i]); |
997
|
|
|
} |
998
|
|
|
break; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
if (!isset($message)) { |
1002
|
|
|
return null; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
// Use saved boundary |
1006
|
|
|
if (!empty($this->_build_params['boundary'])) { |
1007
|
|
|
$boundary = $this->_build_params['boundary']; |
1008
|
|
|
} else { |
1009
|
|
|
$boundary = null; |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
// Write output to file |
1013
|
|
|
if ($filename) { |
1014
|
|
|
// Append mimePart message headers and body into file |
1015
|
|
|
$headers = $message->encodeToFile($filename, $boundary, $skip_head); |
1016
|
|
|
if ($this->_isError($headers)) { |
1017
|
|
|
return $headers; |
1018
|
|
|
} |
1019
|
|
|
$this->_headers = array_merge($this->_headers, $headers); |
1020
|
|
|
return null; |
1021
|
|
|
} else { |
1022
|
|
|
$output = $message->encode($boundary, $skip_head); |
1023
|
|
|
if ($this->_isError($output)) { |
1024
|
|
|
return $output; |
1025
|
|
|
} |
1026
|
|
|
$this->_headers = array_merge($this->_headers, $output['headers']); |
1027
|
|
|
return $output['body']; |
1028
|
|
|
} |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
|
|
/** |
1032
|
|
|
* Returns an array with the headers needed to prepend to the email |
1033
|
|
|
* (MIME-Version and Content-Type). Format of argument is: |
1034
|
|
|
* $array['header-name'] = 'header-value'; |
1035
|
|
|
* |
1036
|
|
|
* @param array $xtra_headers Assoc array with any extra headers (optional) |
1037
|
|
|
* (Don't set Content-Type for multipart messages here!) |
1038
|
|
|
* @param bool $overwrite Overwrite already existing headers. |
1039
|
|
|
* @param bool $skip_content Don't return content headers: Content-Type, |
1040
|
|
|
* Content-Disposition and Content-Transfer-Encoding |
1041
|
|
|
* |
1042
|
|
|
* @return array Assoc array with the mime headers |
1043
|
|
|
* @access public |
1044
|
|
|
*/ |
1045
|
|
|
function headers($xtra_headers = null, $overwrite = false, $skip_content = false) |
1046
|
|
|
{ |
1047
|
|
|
// Add mime version header |
1048
|
|
|
$headers['MIME-Version'] = '1.0'; |
1049
|
|
|
|
1050
|
|
|
// Content-Type and Content-Transfer-Encoding headers should already |
1051
|
|
|
// be present if get() was called, but we'll re-set them to make sure |
1052
|
|
|
// we got them when called before get() or something in the message |
1053
|
|
|
// has been changed after get() [#14780] |
1054
|
|
|
if (!$skip_content) { |
1055
|
|
|
$headers += $this->_contentHeaders(); |
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
if (!empty($xtra_headers)) { |
1059
|
|
|
$headers = array_merge($headers, $xtra_headers); |
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
if ($overwrite) { |
1063
|
|
|
$this->_headers = array_merge($this->_headers, $headers); |
1064
|
|
|
} else { |
1065
|
|
|
$this->_headers = array_merge($headers, $this->_headers); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
$headers = $this->_headers; |
1069
|
|
|
|
1070
|
|
|
if ($skip_content) { |
1071
|
|
|
unset($headers['Content-Type']); |
1072
|
|
|
unset($headers['Content-Transfer-Encoding']); |
1073
|
|
|
unset($headers['Content-Disposition']); |
1074
|
|
|
} else if (!empty($this->_build_params['ctype'])) { |
1075
|
|
|
$headers['Content-Type'] = $this->_build_params['ctype']; |
1076
|
|
|
} |
1077
|
|
|
|
1078
|
|
|
$encodedHeaders = $this->_encodeHeaders($headers); |
1079
|
|
|
return $encodedHeaders; |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
|
|
/** |
1083
|
|
|
* Get the text version of the headers |
1084
|
|
|
* (usefull if you want to use the PHP mail() function) |
1085
|
|
|
* |
1086
|
|
|
* @param array $xtra_headers Assoc array with any extra headers (optional) |
1087
|
|
|
* (Don't set Content-Type for multipart messages here!) |
1088
|
|
|
* @param bool $overwrite Overwrite the existing headers with new. |
1089
|
|
|
* @param bool $skip_content Don't return content headers: Content-Type, |
1090
|
|
|
* Content-Disposition and Content-Transfer-Encoding |
1091
|
|
|
* |
1092
|
|
|
* @return string Plain text headers |
1093
|
|
|
* @access public |
1094
|
|
|
*/ |
1095
|
|
|
function txtHeaders($xtra_headers = null, $overwrite = false, $skip_content = false) |
1096
|
|
|
{ |
1097
|
|
|
$headers = $this->headers($xtra_headers, $overwrite, $skip_content); |
1098
|
|
|
|
1099
|
|
|
// Place Received: headers at the beginning of the message |
1100
|
|
|
// Spam detectors often flag messages with it after the Subject: as spam |
1101
|
|
|
if (isset($headers['Received'])) { |
1102
|
|
|
$received = $headers['Received']; |
1103
|
|
|
unset($headers['Received']); |
1104
|
|
|
$headers = array('Received' => $received) + $headers; |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
|
|
$ret = ''; |
1108
|
|
|
$eol = $this->_build_params['eol']; |
1109
|
|
|
|
1110
|
|
|
foreach ($headers as $key => $val) { |
1111
|
|
|
if (is_array($val)) { |
1112
|
|
|
foreach ($val as $value) { |
1113
|
|
|
$ret .= "$key: $value" . $eol; |
1114
|
|
|
} |
1115
|
|
|
} else { |
1116
|
|
|
$ret .= "$key: $val" . $eol; |
1117
|
|
|
} |
1118
|
|
|
} |
1119
|
|
|
|
1120
|
|
|
return $ret; |
1121
|
|
|
} |
1122
|
|
|
|
1123
|
|
|
/** |
1124
|
|
|
* Sets message Content-Type header. |
1125
|
|
|
* Use it to build messages with various content-types e.g. miltipart/raport |
1126
|
|
|
* not supported by _contentHeaders() function. |
1127
|
|
|
* |
1128
|
|
|
* @param string $type Type name |
1129
|
|
|
* @param array $params Hash array of header parameters |
1130
|
|
|
* |
1131
|
|
|
* @return void |
1132
|
|
|
* @access public |
1133
|
|
|
* @since 1.7.0 |
1134
|
|
|
*/ |
1135
|
|
|
function setContentType($type, $params = array()) |
1136
|
|
|
{ |
1137
|
|
|
$header = $type; |
1138
|
|
|
|
1139
|
|
|
$eol = !empty($this->_build_params['eol']) |
1140
|
|
|
? $this->_build_params['eol'] : "\r\n"; |
1141
|
|
|
|
1142
|
|
|
// add parameters |
1143
|
|
|
$token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D' |
1144
|
|
|
. '\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#'; |
1145
|
|
|
if (is_array($params)) { |
1146
|
|
|
foreach ($params as $name => $value) { |
1147
|
|
|
if ($name == 'boundary') { |
1148
|
|
|
$this->_build_params['boundary'] = $value; |
1149
|
|
|
} |
1150
|
|
|
if (!preg_match($token_regexp, $value)) { |
1151
|
|
|
$header .= ";$eol $name=$value"; |
1152
|
|
|
} else { |
1153
|
|
|
$value = addcslashes($value, '\\"'); |
1154
|
|
|
$header .= ";$eol $name=\"$value\""; |
1155
|
|
|
} |
1156
|
|
|
} |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
|
|
// add required boundary parameter if not defined |
1160
|
|
|
if (preg_match('/^multipart\//i', $type)) { |
1161
|
|
|
if (empty($this->_build_params['boundary'])) { |
1162
|
|
|
$this->_build_params['boundary'] = '=_' . md5(rand() . microtime()); |
1163
|
|
|
} |
1164
|
|
|
|
1165
|
|
|
$header .= ";$eol boundary=\"" . $this->_build_params['boundary'] . "\""; |
1166
|
|
|
} |
1167
|
|
|
|
1168
|
|
|
$this->_build_params['ctype'] = $header; |
1169
|
|
|
} |
1170
|
|
|
|
1171
|
|
|
/** |
1172
|
|
|
* Sets the Subject header |
1173
|
|
|
* |
1174
|
|
|
* @param string $subject String to set the subject to. |
1175
|
|
|
* |
1176
|
|
|
* @return void |
1177
|
|
|
* @access public |
1178
|
|
|
*/ |
1179
|
|
|
function setSubject($subject) |
1180
|
|
|
{ |
1181
|
|
|
$this->_headers['Subject'] = $subject; |
1182
|
|
|
} |
1183
|
|
|
|
1184
|
|
|
/** |
1185
|
|
|
* Set an email to the From (the sender) header |
1186
|
|
|
* |
1187
|
|
|
* @param string $email The email address to use |
1188
|
|
|
* |
1189
|
|
|
* @return void |
1190
|
|
|
* @access public |
1191
|
|
|
*/ |
1192
|
|
|
function setFrom($email) |
1193
|
|
|
{ |
1194
|
|
|
$this->_headers['From'] = $email; |
1195
|
|
|
} |
1196
|
|
|
|
1197
|
|
|
/** |
1198
|
|
|
* Add an email to the To header |
1199
|
|
|
* (multiple calls to this method are allowed) |
1200
|
|
|
* |
1201
|
|
|
* @param string $email The email direction to add |
1202
|
|
|
* |
1203
|
|
|
* @return void |
1204
|
|
|
* @access public |
1205
|
|
|
*/ |
1206
|
|
|
function addTo($email) |
1207
|
|
|
{ |
1208
|
|
|
if (isset($this->_headers['To'])) { |
1209
|
|
|
$this->_headers['To'] .= ", $email"; |
1210
|
|
|
} else { |
1211
|
|
|
$this->_headers['To'] = $email; |
1212
|
|
|
} |
1213
|
|
|
} |
1214
|
|
|
|
1215
|
|
|
/** |
1216
|
|
|
* Add an email to the Cc (carbon copy) header |
1217
|
|
|
* (multiple calls to this method are allowed) |
1218
|
|
|
* |
1219
|
|
|
* @param string $email The email direction to add |
1220
|
|
|
* |
1221
|
|
|
* @return void |
1222
|
|
|
* @access public |
1223
|
|
|
*/ |
1224
|
|
|
function addCc($email) |
1225
|
|
|
{ |
1226
|
|
|
if (isset($this->_headers['Cc'])) { |
1227
|
|
|
$this->_headers['Cc'] .= ", $email"; |
1228
|
|
|
} else { |
1229
|
|
|
$this->_headers['Cc'] = $email; |
1230
|
|
|
} |
1231
|
|
|
} |
1232
|
|
|
|
1233
|
|
|
/** |
1234
|
|
|
* Add an email to the Bcc (blank carbon copy) header |
1235
|
|
|
* (multiple calls to this method are allowed) |
1236
|
|
|
* |
1237
|
|
|
* @param string $email The email direction to add |
1238
|
|
|
* |
1239
|
|
|
* @return void |
1240
|
|
|
* @access public |
1241
|
|
|
*/ |
1242
|
|
|
function addBcc($email) |
1243
|
|
|
{ |
1244
|
|
|
if (isset($this->_headers['Bcc'])) { |
1245
|
|
|
$this->_headers['Bcc'] .= ", $email"; |
1246
|
|
|
} else { |
1247
|
|
|
$this->_headers['Bcc'] = $email; |
1248
|
|
|
} |
1249
|
|
|
} |
1250
|
|
|
|
1251
|
|
|
/** |
1252
|
|
|
* Since the PHP send function requires you to specify |
1253
|
|
|
* recipients (To: header) separately from the other |
1254
|
|
|
* headers, the To: header is not properly encoded. |
1255
|
|
|
* To fix this, you can use this public method to |
1256
|
|
|
* encode your recipients before sending to the send |
1257
|
|
|
* function |
1258
|
|
|
* |
1259
|
|
|
* @param string $recipients A comma-delimited list of recipients |
1260
|
|
|
* |
1261
|
|
|
* @return string Encoded data |
1262
|
|
|
* @access public |
1263
|
|
|
*/ |
1264
|
|
|
function encodeRecipients($recipients) |
1265
|
|
|
{ |
1266
|
|
|
$input = array("To" => $recipients); |
1267
|
|
|
$retval = $this->_encodeHeaders($input); |
1268
|
|
|
return $retval["To"] ; |
1269
|
|
|
} |
1270
|
|
|
|
1271
|
|
|
/** |
1272
|
|
|
* Encodes headers as per RFC2047 |
1273
|
|
|
* |
1274
|
|
|
* @param array $input The header data to encode |
1275
|
|
|
* @param array $params Extra build parameters |
1276
|
|
|
* |
1277
|
|
|
* @return array Encoded data |
1278
|
|
|
* @access private |
1279
|
|
|
*/ |
1280
|
|
|
function _encodeHeaders($input, $params = array()) |
1281
|
|
|
{ |
1282
|
|
|
$build_params = $this->_build_params; |
1283
|
|
|
while (list($key, $value) = each($params)) { |
1284
|
|
|
$build_params[$key] = $value; |
1285
|
|
|
} |
1286
|
|
|
|
1287
|
|
|
foreach ($input as $hdr_name => $hdr_value) { |
1288
|
|
|
if (is_array($hdr_value)) { |
1289
|
|
|
foreach ($hdr_value as $idx => $value) { |
1290
|
|
|
$input[$hdr_name][$idx] = $this->encodeHeader( |
1291
|
|
|
$hdr_name, $value, |
1292
|
|
|
$build_params['head_charset'], $build_params['head_encoding'] |
1293
|
|
|
); |
1294
|
|
|
} |
1295
|
|
|
} else { |
1296
|
|
|
$input[$hdr_name] = $this->encodeHeader( |
1297
|
|
|
$hdr_name, $hdr_value, |
1298
|
|
|
$build_params['head_charset'], $build_params['head_encoding'] |
1299
|
|
|
); |
1300
|
|
|
} |
1301
|
|
|
} |
1302
|
|
|
|
1303
|
|
|
return $input; |
1304
|
|
|
} |
1305
|
|
|
|
1306
|
|
|
/** |
1307
|
|
|
* Encodes a header as per RFC2047 |
1308
|
|
|
* |
1309
|
|
|
* @param string $name The header name |
1310
|
|
|
* @param string $value The header data to encode |
1311
|
|
|
* @param string $charset Character set name |
1312
|
|
|
* @param string $encoding Encoding name (base64 or quoted-printable) |
1313
|
|
|
* |
1314
|
|
|
* @return string Encoded header data (without a name) |
1315
|
|
|
* @access public |
1316
|
|
|
* @since 1.5.3 |
1317
|
|
|
*/ |
1318
|
|
|
function encodeHeader($name, $value, $charset, $encoding) |
1319
|
|
|
{ |
1320
|
|
|
$mime_part = new Mail_mimePart(); |
1321
|
|
|
return $mime_part->encodeHeader( |
1322
|
|
|
$name, $value, $charset, $encoding, $this->_build_params['eol'] |
1323
|
|
|
); |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
/** |
1327
|
|
|
* Get file's basename (locale independent) |
1328
|
|
|
* |
1329
|
|
|
* @param string $filename Filename |
1330
|
|
|
* |
1331
|
|
|
* @return string Basename |
1332
|
|
|
* @access private |
1333
|
|
|
*/ |
1334
|
|
|
function _basename($filename) |
1335
|
|
|
{ |
1336
|
|
|
// basename() is not unicode safe and locale dependent |
1337
|
|
|
if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) { |
1338
|
|
|
return preg_replace('/^.*[\\\\\\/]/', '', $filename); |
1339
|
|
|
} else { |
1340
|
|
|
return preg_replace('/^.*[\/]/', '', $filename); |
1341
|
|
|
} |
1342
|
|
|
} |
1343
|
|
|
|
1344
|
|
|
/** |
1345
|
|
|
* Get Content-Type and Content-Transfer-Encoding headers of the message |
1346
|
|
|
* |
1347
|
|
|
* @return array Headers array |
1348
|
|
|
* @access private |
1349
|
|
|
*/ |
1350
|
|
|
function _contentHeaders() |
1351
|
|
|
{ |
1352
|
|
|
$attachments = count($this->_parts) ? true : false; |
1353
|
|
|
$html_images = count($this->_html_images) ? true : false; |
1354
|
|
|
$html = strlen($this->_htmlbody) ? true : false; |
1355
|
|
|
$text = (!$html && strlen($this->_txtbody)) ? true : false; |
1356
|
|
|
$headers = array(); |
1357
|
|
|
|
1358
|
|
|
// See get() |
1359
|
|
|
switch (true) { |
1360
|
|
|
case $text && !$attachments: |
1361
|
|
|
$headers['Content-Type'] = 'text/plain'; |
1362
|
|
|
break; |
1363
|
|
|
|
1364
|
|
|
case !$text && !$html && $attachments: |
1365
|
|
|
case $text && $attachments: |
1366
|
|
|
case $html && $attachments && !$html_images: |
1367
|
|
|
case $html && $attachments && $html_images: |
1368
|
|
|
$headers['Content-Type'] = 'multipart/mixed'; |
1369
|
|
|
break; |
1370
|
|
|
|
1371
|
|
|
case $html && !$attachments && !$html_images && isset($this->_txtbody): |
1372
|
|
|
case $html && !$attachments && $html_images && isset($this->_txtbody): |
1373
|
|
|
$headers['Content-Type'] = 'multipart/alternative'; |
1374
|
|
|
break; |
1375
|
|
|
|
1376
|
|
|
case $html && !$attachments && !$html_images && !isset($this->_txtbody): |
1377
|
|
|
$headers['Content-Type'] = 'text/html'; |
1378
|
|
|
break; |
1379
|
|
|
|
1380
|
|
|
case $html && !$attachments && $html_images && !isset($this->_txtbody): |
1381
|
|
|
$headers['Content-Type'] = 'multipart/related'; |
1382
|
|
|
break; |
1383
|
|
|
|
1384
|
|
|
default: |
1385
|
|
|
return $headers; |
1386
|
|
|
} |
1387
|
|
|
|
1388
|
|
|
$this->_checkParams(); |
1389
|
|
|
|
1390
|
|
|
$eol = !empty($this->_build_params['eol']) |
1391
|
|
|
? $this->_build_params['eol'] : "\r\n"; |
1392
|
|
|
|
1393
|
|
|
if ($headers['Content-Type'] == 'text/plain') { |
1394
|
|
|
// single-part message: add charset and encoding |
1395
|
|
|
$charset = 'charset=' . $this->_build_params['text_charset']; |
1396
|
|
|
// place charset parameter in the same line, if possible |
1397
|
|
|
// 26 = strlen("Content-Type: text/plain; ") |
1398
|
|
|
$headers['Content-Type'] |
1399
|
|
|
.= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset"; |
1400
|
|
|
$headers['Content-Transfer-Encoding'] |
1401
|
|
|
= $this->_build_params['text_encoding']; |
1402
|
|
|
} else if ($headers['Content-Type'] == 'text/html') { |
1403
|
|
|
// single-part message: add charset and encoding |
1404
|
|
|
$charset = 'charset=' . $this->_build_params['html_charset']; |
1405
|
|
|
// place charset parameter in the same line, if possible |
1406
|
|
|
$headers['Content-Type'] |
1407
|
|
|
.= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset"; |
1408
|
|
|
$headers['Content-Transfer-Encoding'] |
1409
|
|
|
= $this->_build_params['html_encoding']; |
1410
|
|
|
} else { |
1411
|
|
|
// multipart message: and boundary |
1412
|
|
|
if (!empty($this->_build_params['boundary'])) { |
1413
|
|
|
$boundary = $this->_build_params['boundary']; |
1414
|
|
|
} else if ( |
1415
|
|
|
!empty($this->_headers['Content-Type']) |
1416
|
|
|
&& preg_match('/boundary="([^"]+)"/', $this->_headers['Content-Type'], $m) |
1417
|
|
|
) { |
1418
|
|
|
$boundary = $m[1]; |
1419
|
|
|
} else { |
1420
|
|
|
$boundary = '=_' . md5(rand() . microtime()); |
1421
|
|
|
} |
1422
|
|
|
|
1423
|
|
|
$this->_build_params['boundary'] = $boundary; |
1424
|
|
|
$headers['Content-Type'] .= ";$eol boundary=\"$boundary\""; |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
return $headers; |
1428
|
|
|
} |
1429
|
|
|
|
1430
|
|
|
/** |
1431
|
|
|
* Validate and set build parameters |
1432
|
|
|
* |
1433
|
|
|
* @return void |
1434
|
|
|
* @access private |
1435
|
|
|
*/ |
1436
|
|
|
function _checkParams() |
1437
|
|
|
{ |
1438
|
|
|
$encodings = array('7bit', '8bit', 'base64', 'quoted-printable'); |
1439
|
|
|
|
1440
|
|
|
$this->_build_params['text_encoding'] |
1441
|
|
|
= strtolower($this->_build_params['text_encoding']); |
1442
|
|
|
$this->_build_params['html_encoding'] |
1443
|
|
|
= strtolower($this->_build_params['html_encoding']); |
1444
|
|
|
|
1445
|
|
|
if (!in_array($this->_build_params['text_encoding'], $encodings)) { |
1446
|
|
|
$this->_build_params['text_encoding'] = '7bit'; |
1447
|
|
|
} |
1448
|
|
|
if (!in_array($this->_build_params['html_encoding'], $encodings)) { |
1449
|
|
|
$this->_build_params['html_encoding'] = '7bit'; |
1450
|
|
|
} |
1451
|
|
|
|
1452
|
|
|
// text body |
1453
|
|
|
if ( |
1454
|
|
|
$this->_build_params['text_encoding'] == '7bit' |
1455
|
|
|
&& !preg_match('/ascii/i', $this->_build_params['text_charset']) |
1456
|
|
|
&& preg_match('/[^\x00-\x7F]/', $this->_txtbody) |
1457
|
|
|
) { |
1458
|
|
|
$this->_build_params['text_encoding'] = 'quoted-printable'; |
1459
|
|
|
} |
1460
|
|
|
// html body |
1461
|
|
|
if ( |
1462
|
|
|
$this->_build_params['html_encoding'] == '7bit' |
1463
|
|
|
&& !preg_match('/ascii/i', $this->_build_params['html_charset']) |
1464
|
|
|
&& preg_match('/[^\x00-\x7F]/', $this->_htmlbody) |
1465
|
|
|
) { |
1466
|
|
|
$this->_build_params['html_encoding'] = 'quoted-printable'; |
1467
|
|
|
} |
1468
|
|
|
} |
1469
|
|
|
|
1470
|
|
|
/** |
1471
|
|
|
* PEAR::isError implementation |
1472
|
|
|
* |
1473
|
|
|
* @param mixed $data Object |
1474
|
|
|
* |
1475
|
|
|
* @return bool True if object is an instance of PEAR_Error |
1476
|
|
|
* @access private |
1477
|
|
|
*/ |
1478
|
|
|
function _isError($data) |
1479
|
|
|
{ |
1480
|
|
|
// PEAR::isError() is not PHP 5.4 compatible (see Bug #19473) |
1481
|
|
|
if (is_object($data) && is_a($data, 'PEAR_Error')) { |
1482
|
|
|
return true; |
1483
|
|
|
} |
1484
|
|
|
|
1485
|
|
|
return false; |
1486
|
|
|
} |
1487
|
|
|
|
1488
|
|
|
/** |
1489
|
|
|
* PEAR::raiseError implementation |
1490
|
|
|
* |
1491
|
|
|
* @param $message A text error message |
1492
|
|
|
* |
1493
|
|
|
* @return PEAR_Error Instance of PEAR_Error |
1494
|
|
|
* @access private |
1495
|
|
|
*/ |
1496
|
|
|
function _raiseError($message) |
1497
|
|
|
{ |
1498
|
|
|
// PEAR::raiseError() is not PHP 5.4 compatible |
1499
|
|
|
return new PEAR_Error($message); |
1500
|
|
|
} |
1501
|
|
|
} // End of class |
1502
|
|
|
|