blob: 62f196c055e0fe78b2b29999623d68ed075132b9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Email Class
31 *
32 * Permits email to be sent using Mail, Sendmail, or SMTP.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/email.html
39 */
trita15dd4f2011-11-23 07:40:05 -050040class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Andrey Andreev597ea272012-11-01 22:56:26 +020042 /**
43 * Used as the User-Agent and X-Mailer headers' value.
44 *
45 * @var string
46 */
Andrey Andreev59c5b532012-03-01 14:09:51 +020047 public $useragent = 'CodeIgniter';
Andrey Andreev50814092012-03-01 12:36:12 +020048
Andrey Andreev597ea272012-11-01 22:56:26 +020049 /**
50 * Path to the Sendmail binary.
51 *
52 * @var string
53 */
54 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
55
56 /**
57 * Which method to use for sending e-mails.
58 *
59 * @var string 'mail', 'sendmail' or 'smtp'
60 */
61 public $protocol = 'mail'; // mail/sendmail/smtp
62
63 /**
64 * STMP Server host
65 *
66 * @var string
67 */
68 public $smtp_host = '';
69
70 /**
71 * SMTP Username
72 *
73 * @var string
74 */
75 public $smtp_user = '';
76
77 /**
78 * SMTP Password
79 *
80 * @var string
81 */
82 public $smtp_pass = '';
83
84 /**
85 * SMTP Server port
86 *
87 * @var int
88 */
89 public $smtp_port = 25;
90
91 /**
92 * SMTP connection timeout in seconds
93 *
94 * @var int
95 */
96 public $smtp_timeout = 5;
97
98 /**
99 * SMTP Encryption
100 *
101 * @var string NULL, 'tls' or 'ssl'
102 */
103 public $smtp_crypto = NULL;
104
105 /**
106 * Whether to apply word-wrapping to the message body.
107 *
108 * @var bool
109 */
110 public $wordwrap = TRUE;
111
112 /**
113 * Number of characters to wrap at.
114 *
115 * @see CI_Email::$wordwrap
116 * @var int
117 */
118 public $wrapchars = 76;
119
120 /**
121 * Message format.
122 *
123 * @var string 'text' or 'html'
124 */
125 public $mailtype = 'text';
126
127 /**
128 * Character set (default: utf-8)
129 *
130 * @var string
131 */
132 public $charset = 'utf-8';
133
134 /**
135 * Multipart message
136 *
137 * @var string 'mixed' (in the body) or 'related' (separate)
138 */
139 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
140
141 /**
142 * Alternative message (for HTML messages only)
143 *
144 * @var string
145 */
146 public $alt_message = '';
147
148 /**
149 * Whether to validate e-mail addresses.
150 *
151 * @var bool
152 */
153 public $validate = FALSE;
154
155 /**
156 * X-Priority header value.
157 *
158 * @var int 1-5
159 */
160 public $priority = 3; // Default priority (1 - 5)
161
162 /**
163 * Newline character sequence.
164 * Use "\r\n" to comply with RFC 822.
165 *
166 * @link http://www.ietf.org/rfc/rfc822.txt
167 * @var string "\r\n" or "\n"
168 */
169 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
170
171 /**
172 * CRLF character sequence
173 *
174 * RFC 2045 specifies that for 'quoted-printable' encoding,
175 * "\r\n" must be used. However, it appears that some servers
176 * (even on the receiving end) don't handle it properly and
177 * switching to "\n", while improper, is the only solution
178 * that seems to work for all environments.
179 *
180 * @link http://www.ietf.org/rfc/rfc822.txt
181 * @var string
182 */
183 public $crlf = "\n";
184
185 /**
186 * Whether to use Delivery Status Notification.
187 *
188 * @var bool
189 */
190 public $dsn = FALSE;
191
192 /**
193 * Whether to send multipart alternatives.
194 * Yahoo! doesn't seem to like these.
195 *
196 * @var bool
197 */
198 public $send_multipart = TRUE;
199
200 /**
201 * Whether to send messages to BCC recipients in batches.
202 *
203 * @var bool
204 */
205 public $bcc_batch_mode = FALSE;
206
207 /**
208 * BCC Batch max number size.
209 *
210 * @see CI_Email::$bcc_batch_mode
211 * @var int
212 */
213 public $bcc_batch_size = 200;
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Whether PHP is running in safe mode. Initialized by the class constructor.
219 *
220 * @var bool
221 */
Andrey Andreev50814092012-03-01 12:36:12 +0200222 protected $_safe_mode = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200223
224 /**
225 * Subject header
226 *
227 * @var string
228 */
Andrey Andreev50814092012-03-01 12:36:12 +0200229 protected $_subject = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200230
231 /**
232 * Message body
233 *
234 * @var string
235 */
Andrey Andreev50814092012-03-01 12:36:12 +0200236 protected $_body = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200237
238 /**
239 * Final message body to be sent.
240 *
241 * @var string
242 */
Andrey Andreev50814092012-03-01 12:36:12 +0200243 protected $_finalbody = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200244
245 /**
246 * multipart/alternative boundary
247 *
248 * @var string
249 */
Andrey Andreev50814092012-03-01 12:36:12 +0200250 protected $_alt_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200251
252 /**
253 * Attachment boundary
254 *
255 * @var string
256 */
Andrey Andreev50814092012-03-01 12:36:12 +0200257 protected $_atc_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200258
259 /**
260 * Final headers to send
261 *
262 * @var string
263 */
Andrey Andreev50814092012-03-01 12:36:12 +0200264 protected $_header_str = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200265
266 /**
267 * SMTP Connection socket placeholder
268 *
269 * @var resource
270 */
Andrey Andreev50814092012-03-01 12:36:12 +0200271 protected $_smtp_connect = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200272
273 /**
274 * Mail encoding
275 *
276 * @var string '8bit' or '7bit'
277 */
Andrey Andreev50814092012-03-01 12:36:12 +0200278 protected $_encoding = '8bit';
Andrey Andreev597ea272012-11-01 22:56:26 +0200279
280 /**
281 * Whether to perform SMTP authentication
282 *
283 * @var bool
284 */
Andrey Andreev50814092012-03-01 12:36:12 +0200285 protected $_smtp_auth = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200286
287 /**
288 * Whether to send a Reply-To header
289 *
290 * @var bool
291 */
Andrey Andreev50814092012-03-01 12:36:12 +0200292 protected $_replyto_flag = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200293
294 /**
295 * Debug messages
296 *
297 * @see CI_Email::print_debugger()
298 * @var string
299 */
Andrey Andreev50814092012-03-01 12:36:12 +0200300 protected $_debug_msg = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200301
302 /**
303 * Recipients
304 *
305 * @var string[]
306 */
Andrey Andreev50814092012-03-01 12:36:12 +0200307 protected $_recipients = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200308
309 /**
310 * CC Recipients
311 *
312 * @var string[]
313 */
Andrey Andreev50814092012-03-01 12:36:12 +0200314 protected $_cc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200315
316 /**
317 * BCC Recipients
318 *
319 * @var string[]
320 */
Andrey Andreev50814092012-03-01 12:36:12 +0200321 protected $_bcc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200322
323 /**
324 * Message headers
325 *
326 * @var string[]
327 */
Andrey Andreev50814092012-03-01 12:36:12 +0200328 protected $_headers = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200329
330 /**
331 * Attachment data
332 *
333 * @var array
334 */
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300335 protected $_attachments = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200336
337 /**
338 * Valid $protocol values
339 *
340 * @see CI_Email::$protocol
341 * @var string[]
342 */
Andrey Andreev50814092012-03-01 12:36:12 +0200343 protected $_protocols = array('mail', 'sendmail', 'smtp');
Andrey Andreev597ea272012-11-01 22:56:26 +0200344
345 /**
346 * Base charsets
347 *
348 * Character sets valid for 7-bit encoding,
349 * excluding language suffix.
350 *
351 * @var string[]
352 */
353 protected $_base_charsets = array('us-ascii', 'iso-2022-');
354
355 /**
356 * Bit depths
357 *
358 * Valid mail encodings
359 *
360 * @see CI_Email::$_encoding
361 * @var string[]
362 */
Andrey Andreev50814092012-03-01 12:36:12 +0200363 protected $_bit_depths = array('7bit', '8bit');
Andrey Andreev597ea272012-11-01 22:56:26 +0200364
365 /**
366 * $priority translations
367 *
368 * Actual values to send with the X-Priority header
369 *
370 * @var string[]
371 */
Andrey Andreev50814092012-03-01 12:36:12 +0200372 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +0000373
Andrey Andreev597ea272012-11-01 22:56:26 +0200374 // --------------------------------------------------------------------
375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 /**
377 * Constructor - Sets Email Preferences
378 *
379 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300380 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300381 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300382 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000384 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300386 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 if (count($config) > 0)
389 {
390 $this->initialize($config);
391 }
392 else
393 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100394 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200395 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
Andrey Andreev925dd902012-10-19 11:06:31 +0300398 $this->charset = strtoupper($this->charset);
399
Andrey Andreev59c5b532012-03-01 14:09:51 +0200400 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Initialize preferences
407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param array
409 * @return void
410 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000411 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 foreach ($config as $key => $val)
414 {
415 if (isset($this->$key))
416 {
417 $method = 'set_'.$key;
418
419 if (method_exists($this, $method))
420 {
421 $this->$method($val);
422 }
423 else
424 {
425 $this->$key = $val;
426 }
427 }
428 }
Eric Barnes6113f542010-12-29 13:36:12 -0500429 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000430
Alex Bilbied261b1e2012-06-02 11:12:16 +0100431 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200432 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000433
434 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
438
439 /**
440 * Initialize the Email Data
441 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800442 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200443 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000445 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200447 $this->_subject = '';
448 $this->_body = '';
449 $this->_finalbody = '';
450 $this->_header_str = '';
451 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500453 $this->_cc_array = array();
454 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $this->_headers = array();
456 $this->_debug_msg = array();
457
Mickey Wubfc1cad2012-05-31 22:28:40 -0700458 $this->set_header('User-Agent', $this->useragent);
459 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
461 if ($clear_attachments !== FALSE)
462 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300463 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Eric Barnes6113f542010-12-29 13:36:12 -0500465
Greg Akera769deb2010-11-10 15:47:29 -0600466 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
470
471 /**
472 * Set FROM
473 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300474 * @param string $from
475 * @param string $name
476 * @param string $return_path = NULL Return-Path
Andrey Andreev081c9462012-03-01 12:58:11 +0200477 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300479 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200481 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200483 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 if ($this->validate)
487 {
488 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300489 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200490 {
491 $this->validate_email($this->_str_to_array($return_path));
492 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
494
495 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100496 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
498 // only use Q encoding if there are characters that would require it
499 if ( ! preg_match('/[\200-\377]/', $name))
500 {
501 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000502 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504 else
505 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300506 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508 }
509
Mickey Wubfc1cad2012-05-31 22:28:40 -0700510 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200511
Andrey Andreev925dd902012-10-19 11:06:31 +0300512 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200513 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500514
Greg Akera769deb2010-11-10 15:47:29 -0600515 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Set Reply-to
522 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 * @param string
524 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200525 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000527 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200529 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200531 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533
534 if ($this->validate)
535 {
536 $this->validate_email($this->_str_to_array($replyto));
537 }
538
Alex Bilbied261b1e2012-06-02 11:12:16 +0100539 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 $name = $replyto;
542 }
543
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300544 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 $name = '"'.$name.'"';
547 }
548
Mickey Wubfc1cad2012-05-31 22:28:40 -0700549 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600551
552 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * Set Recipients
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200561 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000563 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
565 $to = $this->_str_to_array($to);
566 $to = $this->clean_email($to);
567
568 if ($this->validate)
569 {
570 $this->validate_email($to);
571 }
572
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200573 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700575 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300578 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500579
Greg Akera769deb2010-11-10 15:47:29 -0600580 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // --------------------------------------------------------------------
584
585 /**
586 * Set CC
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200589 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000591 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
Andrey Andreev56454792012-05-17 14:32:19 +0300593 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000594
595 if ($this->validate)
596 {
597 $this->validate_email($cc);
598 }
599
Mickey Wubfc1cad2012-05-31 22:28:40 -0700600 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000601
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200602 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 $this->_cc_array = $cc;
605 }
Eric Barnes6113f542010-12-29 13:36:12 -0500606
Greg Akera769deb2010-11-10 15:47:29 -0600607 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // --------------------------------------------------------------------
611
612 /**
613 * Set BCC
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param string
616 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200617 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000619 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100621 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 $this->bcc_batch_mode = TRUE;
624 $this->bcc_batch_size = $limit;
625 }
626
Andrey Andreev56454792012-05-17 14:32:19 +0300627 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000628
629 if ($this->validate)
630 {
631 $this->validate_email($bcc);
632 }
633
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200634 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 $this->_bcc_array = $bcc;
637 }
638 else
639 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700640 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
Eric Barnes6113f542010-12-29 13:36:12 -0500642
Greg Akera769deb2010-11-10 15:47:29 -0600643 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // --------------------------------------------------------------------
647
648 /**
649 * Set Email Subject
650 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200652 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000654 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700657 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600658 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Set Body
665 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200667 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000669 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200671 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200672
Andrey Andreevaf728622011-10-20 10:11:59 +0300673 /* strip slashes only if magic quotes is ON
674 if we do it with magic quotes OFF, it strips real, user-inputted chars.
675
676 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
677 it will probably not exist in future versions at all.
678 */
679 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200680 {
diegorivera33c32802011-10-19 02:56:15 -0200681 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200682 }
diegorivera33c32802011-10-19 02:56:15 -0200683
Greg Akera769deb2010-11-10 15:47:29 -0600684 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
690 * Assign file attachments
691 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300692 * @param string $filename
693 * @param string $disposition = 'attachment'
694 * @param string $newname = NULL
695 * @param string $mime = ''
Andrey Andreev081c9462012-03-01 12:58:11 +0200696 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200698 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300700 $this->_attachments[] = array(
701 'name' => array($filename, $newname),
702 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
703 'type' => $mime
704 );
705
Greg Akera769deb2010-11-10 15:47:29 -0600706 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708
709 // --------------------------------------------------------------------
710
711 /**
712 * Add a Header Item
713 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 * @param string
715 * @param string
716 * @return void
717 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700718 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
720 $this->_headers[$header] = $value;
721 }
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 // --------------------------------------------------------------------
724
725 /**
726 * Convert a String to an Array
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string
729 * @return array
730 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600731 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 if ( ! is_array($email))
734 {
Andrey Andreev56454792012-05-17 14:32:19 +0300735 return (strpos($email, ',') !== FALSE)
736 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
737 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Andrey Andreev56454792012-05-17 14:32:19 +0300739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 return $email;
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
746 * Set Multipart Value
747 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200749 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000751 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400753 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600754 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 // --------------------------------------------------------------------
758
759 /**
760 * Set Mailtype
761 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200763 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000765 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 {
Andrey Andreev56454792012-05-17 14:32:19 +0300767 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600768 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 // --------------------------------------------------------------------
772
773 /**
774 * Set Wordwrap
775 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400776 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200777 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000779 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400781 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600782 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 // --------------------------------------------------------------------
786
787 /**
788 * Set Protocol
789 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200791 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000793 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200795 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600796 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // --------------------------------------------------------------------
800
801 /**
802 * Set Priority
803 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200804 * @param int
805 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000807 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200809 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600810 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // --------------------------------------------------------------------
814
815 /**
816 * Set Newline Character
817 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200819 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000821 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200823 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600824 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 // --------------------------------------------------------------------
828
829 /**
830 * Set CRLF
831 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200833 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000835 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200837 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600838 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 // --------------------------------------------------------------------
842
843 /**
844 * Set Message Boundary
845 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 * @return void
847 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600848 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200850 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
851 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // --------------------------------------------------------------------
855
856 /**
857 * Get the Message ID
858 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * @return string
860 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600861 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200863 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300864 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
870 * Get Mail Protocol
871 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200873 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600875 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 {
877 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200878 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000879
Alex Bilbied261b1e2012-06-02 11:12:16 +0100880 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
882 return $this->protocol;
883 }
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
889 * Get Mail Encoding
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param bool
892 * @return string
893 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600894 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200896 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000897
898 foreach ($this->_base_charsets as $charset)
899 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300900 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
902 $this->_encoding = '7bit';
903 }
904 }
905
Alex Bilbied261b1e2012-06-02 11:12:16 +0100906 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
908 return $this->_encoding;
909 }
910 }
911
912 // --------------------------------------------------------------------
913
914 /**
915 * Get content type (text/html/attachment)
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @return string
918 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600919 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
Andrey Andreev56454792012-05-17 14:32:19 +0300921 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300923 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300925 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 return 'plain-attach';
928 }
929 else
930 {
931 return 'plain';
932 }
933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 // --------------------------------------------------------------------
936
937 /**
938 * Set RFC 822 Date
939 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 * @return string
941 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600942 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200944 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300945 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200947 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
Andrey Andreev59c5b532012-03-01 14:09:51 +0200949 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 // --------------------------------------------------------------------
953
954 /**
955 * Mime message
956 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 * @return string
958 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600959 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200961 return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.';
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 // --------------------------------------------------------------------
965
966 /**
967 * Validate Email Address
968 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 * @param string
970 * @return bool
971 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000972 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 if ( ! is_array($email))
975 {
patworkb0707982011-04-08 15:10:05 +0200976 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 return FALSE;
978 }
979
980 foreach ($email as $val)
981 {
982 if ( ! $this->valid_email($val))
983 {
patworkb0707982011-04-08 15:10:05 +0200984 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 return FALSE;
986 }
987 }
988
989 return TRUE;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // --------------------------------------------------------------------
993
994 /**
995 * Email Validation
996 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @param string
998 * @return bool
999 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001000 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001002 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1009 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 * @param string
1011 * @return string
1012 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001013 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 if ( ! is_array($email))
1016 {
Andrey Andreev56454792012-05-17 14:32:19 +03001017 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 }
1019
1020 $clean_email = array();
1021
1022 foreach ($email as $addy)
1023 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001024 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
1026
1027 return $clean_email;
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Build alternative plain text message
1034 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001035 * Provides the raw message for use in plain-text headers of
1036 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * If the user hasn't specified his own alternative message
1038 * it creates one by stripping the HTML
1039 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @return string
1041 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001042 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001044 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001046 return ($this->wordwrap)
1047 ? $this->word_wrap($this->alt_message, 76)
1048 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 }
1050
Andrey Andreev56454792012-05-17 14:32:19 +03001051 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001052 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
1054 for ($i = 20; $i >= 3; $i--)
1055 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001056 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
1058
GDmac9bea4be2012-10-30 06:14:19 +01001059 // Reduce multiple spaces
1060 $str = preg_replace('| +|', ' ', $str);
1061
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001062 return ($this->wordwrap)
1063 ? $this->word_wrap($body, 76)
1064 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 }
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 // --------------------------------------------------------------------
1068
1069 /**
1070 * Word Wrap
1071 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001073 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 * @return string
1075 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001076 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001078 // Set the character limit, if not already present
1079 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001081 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
1083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 // Standardize newlines
1085 if (strpos($str, "\r") !== FALSE)
1086 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001087 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089
GDmac9bea4be2012-10-30 06:14:19 +01001090 // Reduce multiple spaces at end of line
1091 $str = preg_replace('| +\n|', "\n", $str);
1092
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 // If the current word is surrounded by {unwrap} tags we'll
1094 // strip the entire chunk and replace it with a marker.
1095 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +03001096 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001098 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001100 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +03001101 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 }
1103 }
1104
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001105 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001107 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 $str = wordwrap($str, $charlim, "\n", FALSE);
1109
1110 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001111 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 foreach (explode("\n", $str) as $line)
1113 {
1114 // Is the line within the allowed character count?
1115 // If so we'll join it to the output and continue
1116 if (strlen($line) <= $charlim)
1117 {
1118 $output .= $line.$this->newline;
1119 continue;
1120 }
1121
1122 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001123 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +03001126 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
1128 break;
1129 }
1130
1131 // Trim the word down
1132 $temp .= substr($line, 0, $charlim-1);
1133 $line = substr($line, $charlim-1);
1134 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001135 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001136
1137 // If $temp contains data it means we had to split up an over-length
1138 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001139 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001141 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
1143
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001144 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
1146
1147 // Put our markers back
1148 if (count($unwrap) > 0)
1149 {
1150 foreach ($unwrap as $key => $val)
1151 {
Andrey Andreev56454792012-05-17 14:32:19 +03001152 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
1154 }
1155
1156 return $output;
1157 }
Barry Mienydd671972010-10-04 16:33:58 +02001158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 // --------------------------------------------------------------------
1160
1161 /**
1162 * Build final headers
1163 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 * @return string
1165 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001166 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001168 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1169 $this->set_header('X-Mailer', $this->useragent);
1170 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
1171 $this->set_header('Message-ID', $this->_get_message_id());
1172 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
Barry Mienydd671972010-10-04 16:33:58 +02001174
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 // --------------------------------------------------------------------
1176
1177 /**
1178 * Write Headers as a string
1179 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 * @return void
1181 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001182 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001184 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
1186 $this->_subject = $this->_headers['Subject'];
1187 unset($this->_headers['Subject']);
1188 }
1189
1190 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001191 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001192
Pascal Kriete14287f32011-02-14 13:39:34 -05001193 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
1195 $val = trim($val);
1196
Alex Bilbied261b1e2012-06-02 11:12:16 +01001197 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
Andrey Andreev56454792012-05-17 14:32:19 +03001199 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 }
1201 }
1202
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001203 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
Derek Jones1d890882009-02-10 20:32:14 +00001205 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 }
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
1210
1211 /**
1212 * Build Final Body and attachments
1213 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 * @return void
1215 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001216 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001218 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
1220 $this->_body = $this->word_wrap($this->_body);
1221 }
1222
1223 $this->_set_boundaries();
1224 $this->_write_headers();
1225
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001226 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001227 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001228
1229 switch ($this->_get_content_type())
1230 {
1231 case 'plain' :
1232
Andrey Andreev56454792012-05-17 14:32:19 +03001233 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1234 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001235
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001236 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
1238 $this->_header_str .= $hdr;
1239 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 }
Brandon Jones485d7412010-11-09 16:38:17 -05001241 else
1242 {
1243 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1244 }
Eric Barnes6113f542010-12-29 13:36:12 -05001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 return;
1247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 case 'html' :
1249
1250 if ($this->send_multipart === FALSE)
1251 {
Andrey Andreev56454792012-05-17 14:32:19 +03001252 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
1253 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
1255 else
1256 {
Andrey Andreev56454792012-05-17 14:32:19 +03001257 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001258
Andrey Andreev56454792012-05-17 14:32:19 +03001259 $body .= $this->_get_mime_message().$this->newline.$this->newline
1260 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001261
Andrey Andreev56454792012-05-17 14:32:19 +03001262 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1263 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1264 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001265
Andrey Andreev56454792012-05-17 14:32:19 +03001266 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1267 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 }
Eric Barnes6113f542010-12-29 13:36:12 -05001269
Andrey Andreev56454792012-05-17 14:32:19 +03001270 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001271
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001272 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
1274 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001275 }
1276 else
1277 {
Andrey Andreev56454792012-05-17 14:32:19 +03001278 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 }
1280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 if ($this->send_multipart !== FALSE)
1282 {
Andrey Andreev56454792012-05-17 14:32:19 +03001283 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
1285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 return;
1287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 case 'plain-attach' :
1289
Andrey Andreev56454792012-05-17 14:32:19 +03001290 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001291
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001292 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
1294 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001295 }
1296
Andrey Andreev56454792012-05-17 14:32:19 +03001297 $body .= $this->_get_mime_message().$this->newline.$this->newline
1298 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001299
Andrey Andreev56454792012-05-17 14:32:19 +03001300 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1301 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001302
Andrey Andreev56454792012-05-17 14:32:19 +03001303 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001304
1305 break;
1306 case 'html-attach' :
1307
Andrey Andreev56454792012-05-17 14:32:19 +03001308 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001309
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001310 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
1312 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 }
1314
Andrey Andreev56454792012-05-17 14:32:19 +03001315 $body .= $this->_get_mime_message().$this->newline.$this->newline
1316 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001317
Andrey Andreev56454792012-05-17 14:32:19 +03001318 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1319 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001320
Andrey Andreev56454792012-05-17 14:32:19 +03001321 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1322 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1323 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001324
Andrey Andreev56454792012-05-17 14:32:19 +03001325 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1326 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001327
Andrey Andreev56454792012-05-17 14:32:19 +03001328 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1329 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001330
1331 break;
1332 }
1333
1334 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001335 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001337 $filename = $this->_attachments[$i]['name'][0];
1338 $basename = is_null($this->_attachments[$i]['name'][1])
1339 ? basename($filename) : $this->_attachments[$i]['name'][1];
1340 $ctype = $this->_attachments[$i]['type'];
Matteo Mattei5688d582012-03-13 19:29:29 +01001341 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001342
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001343 if ($ctype === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001345 if ( ! file_exists($filename))
1346 {
1347 $this->_set_error_message('lang:email_attachment_missing', $filename);
1348 return FALSE;
1349 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001350
Matteo Mattei5688d582012-03-13 19:29:29 +01001351 $file = filesize($filename) +1;
1352
1353 if ( ! $fp = fopen($filename, FOPEN_READ))
1354 {
1355 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1356 return FALSE;
1357 }
1358
Matteo Matteic3b36f42012-03-26 10:27:17 +02001359 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001360 $file_content = fread($fp, $file);
1361 fclose($fp);
1362 }
1363 else
1364 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001365 $file_content =& $this->_attachments[$i]['name'][0];
Matteo Mattei5688d582012-03-13 19:29:29 +01001366 }
Andrey Andreev56454792012-05-17 14:32:19 +03001367
1368 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1369 .'Content-type: '.$ctype.'; '
1370 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001371 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001372 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001373
Matteo Mattei5688d582012-03-13 19:29:29 +01001374 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 }
1376
Andrey Andreev56454792012-05-17 14:32:19 +03001377 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1378 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 return;
1380 }
Barry Mienydd671972010-10-04 16:33:58 +02001381
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 // --------------------------------------------------------------------
1383
1384 /**
1385 * Prep Quoted Printable
1386 *
1387 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1388 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1389 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 * @return string
1392 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001393 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001395 // We are intentionally wrapping so mail servers will encode characters
1396 // properly and MUAs will behave, so {unwrap} must go!
1397 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1398
Andrey Andreev683b34d2012-10-09 15:00:00 +03001399 // RFC 2045 specifies CRLF as "\r\n".
1400 // However, many developers choose to override that and violate
1401 // the RFC rules due to (apparently) a bug in MS Exchange,
1402 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001403 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001404 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001405 if (is_php('5.3'))
1406 {
1407 return quoted_printable_encode($str);
1408 }
1409 elseif (function_exists('imap_8bit'))
1410 {
1411 return imap_8bit($str);
1412 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001413 }
1414
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001415 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001416 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001417
1418 // Standardize newlines
1419 if (strpos($str, "\r") !== FALSE)
1420 {
1421 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1422 }
1423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 $escape = '=';
1425 $output = '';
1426
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001427 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 {
1429 $length = strlen($line);
1430 $temp = '';
1431
1432 // Loop through each character in the line to add soft-wrap
1433 // characters at the end of a line " =\r\n" and add the newly
1434 // processed line(s) to the output (see comment on $crlf class property)
1435 for ($i = 0; $i < $length; $i++)
1436 {
1437 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001438 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 $ascii = ord($char);
1440
1441 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001442 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001444 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001446 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001448 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 }
1450
1451 // If we're at the character limit, add the line to the output,
1452 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001453 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
1455 $output .= $temp.$escape.$this->crlf;
1456 $temp = '';
1457 }
1458
1459 // Add the character to our temporary line
1460 $temp .= $char;
1461 }
1462
1463 // Add our completed line to the output
1464 $output .= $temp.$this->crlf;
1465 }
1466
1467 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001468 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 }
1470
1471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001472
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 /**
1474 * Prep Q Encoding
1475 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001476 * Performs "Q Encoding" on a string for use in email headers.
1477 * It's related but not identical to quoted-printable, so it has its
1478 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001480 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001481 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001483 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001485 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001486
Andrey Andreev925dd902012-10-19 11:06:31 +03001487 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001489 if (MB_ENABLED === TRUE)
1490 {
1491 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1492 }
1493 elseif (extension_loaded('iconv'))
1494 {
1495 $output = @iconv_mime_encode('', $str,
1496 array(
1497 'scheme' => 'Q',
1498 'line-length' => 76,
1499 'input-charset' => $this->charset,
1500 'output-charset' => $this->charset,
1501 'line-break-chars' => $this->crlf
1502 )
1503 );
1504
1505 // There are reports that iconv_mime_encode() might fail and return FALSE
1506 if ($output !== FALSE)
1507 {
1508 // iconv_mime_encode() will always put a header field name.
1509 // We've passed it an empty one, but it still prepends our
1510 // encoded string with ': ', so we need to strip it.
1511 return substr($output, 2);
1512 }
1513
1514 $chars = iconv_strlen($str, 'UTF-8');
1515 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 }
1517
Andrey Andreev925dd902012-10-19 11:06:31 +03001518 // We might already have this set for UTF-8
1519 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001520
Andrey Andreev925dd902012-10-19 11:06:31 +03001521 $output = '=?'.$this->charset.'?Q?';
1522 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001524 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1525 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1526 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001527
Andrey Andreev925dd902012-10-19 11:06:31 +03001528 // RFC 2045 sets a limit of 76 characters per line.
1529 // We'll append ?= to the end of each line though.
1530 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001532 $output .= '?='.$this->crlf // EOL
1533 .' =?'.$this->charset.'?Q?'.$chr; // New line
1534 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001536 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001538 $output .= $chr;
1539 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 }
1542
Andrey Andreev925dd902012-10-19 11:06:31 +03001543 // End the header
1544 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 }
1546
1547 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001548
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 /**
1550 * Send Email
1551 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001552 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 * @return bool
1554 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001555 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001557 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 {
1559 $this->reply_to($this->_headers['From']);
1560 }
1561
Andrey Andreev76f15c92012-03-01 13:05:07 +02001562 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1563 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1564 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 {
patworkb0707982011-04-08 15:10:05 +02001566 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 return FALSE;
1568 }
1569
1570 $this->_build_headers();
1571
Andrey Andreev76f15c92012-03-01 13:05:07 +02001572 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001574 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001575
Alex Bilbiea87aab32012-07-30 09:50:37 +01001576 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001577 {
1578 $this->clear();
1579 }
1580
Alex Bilbieb901e732012-07-30 09:44:57 +01001581 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 }
1583
1584 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001585 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001586
Alex Bilbiea87aab32012-07-30 09:50:37 +01001587 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001588 {
1589 $this->clear();
1590 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001591
Alex Bilbieb901e732012-07-30 09:44:57 +01001592 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 }
Barry Mienydd671972010-10-04 16:33:58 +02001594
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 // --------------------------------------------------------------------
1596
1597 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001598 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001600 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001602 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001604 $float = $this->bcc_batch_size - 1;
1605 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 $chunk = array();
1607
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001608 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 {
1610 if (isset($this->_bcc_array[$i]))
1611 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001612 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 }
1614
Alex Bilbied261b1e2012-06-02 11:12:16 +01001615 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 {
1617 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001618 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001619 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 }
1621
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001622 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 {
1624 $chunk[] = substr($set, 1);
1625 }
1626 }
1627
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001628 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 {
1630 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001631
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001632 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001633
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001634 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001636 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 }
1638 else
1639 {
1640 $this->_bcc_array = $bcc;
1641 }
1642
1643 $this->_build_message();
1644 $this->_spool_email();
1645 }
1646 }
Barry Mienydd671972010-10-04 16:33:58 +02001647
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 // --------------------------------------------------------------------
1649
1650 /**
1651 * Unwrap special elements
1652 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 * @return void
1654 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001655 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 {
Andrey Andreev56454792012-05-17 14:32:19 +03001657 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 }
Barry Mienydd671972010-10-04 16:33:58 +02001659
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 // --------------------------------------------------------------------
1661
1662 /**
1663 * Strip line-breaks via callback
1664 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001665 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 * @return string
1667 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001668 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 {
1670 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1671 {
1672 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1673 }
1674
1675 return $matches[1];
1676 }
Barry Mienydd671972010-10-04 16:33:58 +02001677
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 // --------------------------------------------------------------------
1679
1680 /**
1681 * Spool mail to the mail server
1682 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 * @return bool
1684 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001685 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 {
1687 $this->_unwrap_specials();
1688
Andrey Andreev56454792012-05-17 14:32:19 +03001689 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001690 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
Andrey Andreev56454792012-05-17 14:32:19 +03001692 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001693 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001694 }
1695
patworkb0707982011-04-08 15:10:05 +02001696 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 return TRUE;
1698 }
Barry Mienydd671972010-10-04 16:33:58 +02001699
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 // --------------------------------------------------------------------
1701
1702 /**
1703 * Send using mail()
1704 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 * @return bool
1706 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001707 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001709 if (is_array($this->_recipients))
1710 {
1711 $this->_recipients = implode(', ', $this->_recipients);
1712 }
1713
Alex Bilbied261b1e2012-06-02 11:12:16 +01001714 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001716 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 }
1718 else
1719 {
1720 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1721 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001722 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 }
1724 }
Barry Mienydd671972010-10-04 16:33:58 +02001725
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 // --------------------------------------------------------------------
1727
1728 /**
1729 * Send using Sendmail
1730 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 * @return bool
1732 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001733 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 {
Melounek58dfc082012-06-29 08:43:47 +02001735 $fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'.' -r '.$this->clean_email($this->_headers['Return-Path']), 'w');
Derek Allard2067d1a2008-11-13 22:59:24 +00001736
Derek Jones4cefaa42009-04-29 19:13:56 +00001737 if ($fp === FALSE OR $fp === NULL)
1738 {
1739 // server probably has popen disabled, so nothing we can do to get a verbose error.
1740 return FALSE;
1741 }
Derek Jones71141ce2010-03-02 16:41:20 -06001742
Derek Jonesc630bcf2008-11-17 21:09:45 +00001743 fputs($fp, $this->_header_str);
1744 fputs($fp, $this->_finalbody);
1745
Barry Mienydd671972010-10-04 16:33:58 +02001746 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001747
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001748 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
patworkb0707982011-04-08 15:10:05 +02001750 $this->_set_error_message('lang:email_exit_status', $status);
1751 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 return FALSE;
1753 }
1754
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 return TRUE;
1756 }
Barry Mienydd671972010-10-04 16:33:58 +02001757
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 // --------------------------------------------------------------------
1759
1760 /**
1761 * Send using SMTP
1762 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 * @return bool
1764 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001765 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001767 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 {
patworkb0707982011-04-08 15:10:05 +02001769 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 return FALSE;
1771 }
1772
Diogo Osório593f7982012-03-02 18:04:17 +00001773 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001774 {
1775 return FALSE;
1776 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001777
1778 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1779
Pascal Kriete14287f32011-02-14 13:39:34 -05001780 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
1782 $this->_send_command('to', $val);
1783 }
1784
1785 if (count($this->_cc_array) > 0)
1786 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001787 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001789 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
1791 $this->_send_command('to', $val);
1792 }
1793 }
1794 }
1795
1796 if (count($this->_bcc_array) > 0)
1797 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001798 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001800 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 {
1802 $this->_send_command('to', $val);
1803 }
1804 }
1805 }
1806
1807 $this->_send_command('data');
1808
1809 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001810 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001811
1812 $this->_send_data('.');
1813
1814 $reply = $this->_get_smtp_data();
1815
1816 $this->_set_error_message($reply);
1817
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001818 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 {
patworkb0707982011-04-08 15:10:05 +02001820 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 return FALSE;
1822 }
1823
1824 $this->_send_command('quit');
1825 return TRUE;
1826 }
Barry Mienydd671972010-10-04 16:33:58 +02001827
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 // --------------------------------------------------------------------
1829
1830 /**
1831 * SMTP Connect
1832 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 * @return string
1834 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001835 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001837 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001838
Radu Potopbbf04b02011-09-28 13:57:51 +03001839 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001840 $this->smtp_port,
1841 $errno,
1842 $errstr,
1843 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001844
Pascal Kriete14287f32011-02-14 13:39:34 -05001845 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 {
Andrey Andreev56454792012-05-17 14:32:19 +03001847 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 return FALSE;
1849 }
1850
1851 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001852
Alex Bilbied261b1e2012-06-02 11:12:16 +01001853 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001854 {
1855 $this->_send_command('hello');
1856 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001857
Radu Potop4c589ae2011-09-29 10:19:55 +03001858 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001859
Sean Fishere862ddd2011-10-26 22:45:00 -03001860 if ($crypto !== TRUE)
1861 {
1862 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1863 return FALSE;
1864 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001865 }
1866
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 return $this->_send_command('hello');
1868 }
Barry Mienydd671972010-10-04 16:33:58 +02001869
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 // --------------------------------------------------------------------
1871
1872 /**
1873 * Send SMTP command
1874 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 * @param string
1876 * @param string
1877 * @return string
1878 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001879 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 {
1881 switch ($cmd)
1882 {
1883 case 'hello' :
1884
Andrey Andreev56454792012-05-17 14:32:19 +03001885 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1886 {
1887 $this->_send_data('EHLO '.$this->_get_hostname());
1888 }
1889 else
1890 {
1891 $this->_send_data('HELO '.$this->_get_hostname());
1892 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001893
1894 $resp = 250;
1895 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001896 case 'starttls' :
1897
1898 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001899 $resp = 220;
1900 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 case 'from' :
1902
1903 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001904 $resp = 250;
1905 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001906 case 'to' :
1907
leandronf7686c122012-03-22 08:07:31 -03001908 if ($this->dsn)
1909 {
leandronfb4552942012-03-21 23:54:26 -03001910 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001911 }
leandronfb4552942012-03-21 23:54:26 -03001912 else
leandronf7686c122012-03-22 08:07:31 -03001913 {
leandronfb4552942012-03-21 23:54:26 -03001914 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001915 }
Andrey Andreev56454792012-05-17 14:32:19 +03001916
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 $resp = 250;
1918 break;
1919 case 'data' :
1920
1921 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 $resp = 354;
1923 break;
1924 case 'quit' :
1925
1926 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 $resp = 221;
1928 break;
1929 }
1930
1931 $reply = $this->_get_smtp_data();
1932
Andrey Andreev56454792012-05-17 14:32:19 +03001933 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001934
Andrey Andreevba261762012-06-11 14:11:35 +03001935 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 {
patworkb0707982011-04-08 15:10:05 +02001937 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 return FALSE;
1939 }
1940
Alex Bilbied261b1e2012-06-02 11:12:16 +01001941 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 {
1943 fclose($this->_smtp_connect);
1944 }
1945
1946 return TRUE;
1947 }
Barry Mienydd671972010-10-04 16:33:58 +02001948
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 // --------------------------------------------------------------------
1950
1951 /**
vkeranov7e4356b2012-10-27 18:41:00 +03001952 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 * @return bool
1955 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001956 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 {
1958 if ( ! $this->_smtp_auth)
1959 {
1960 return TRUE;
1961 }
1962
Alex Bilbied261b1e2012-06-02 11:12:16 +01001963 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 {
patworkb0707982011-04-08 15:10:05 +02001965 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001966 return FALSE;
1967 }
1968
1969 $this->_send_data('AUTH LOGIN');
1970
1971 $reply = $this->_get_smtp_data();
1972
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001973 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 {
patworkb0707982011-04-08 15:10:05 +02001975 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 return FALSE;
1977 }
1978
1979 $this->_send_data(base64_encode($this->smtp_user));
1980
1981 $reply = $this->_get_smtp_data();
1982
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001983 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 {
patworkb0707982011-04-08 15:10:05 +02001985 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 return FALSE;
1987 }
1988
1989 $this->_send_data(base64_encode($this->smtp_pass));
1990
1991 $reply = $this->_get_smtp_data();
1992
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001993 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 {
patworkb0707982011-04-08 15:10:05 +02001995 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 return FALSE;
1997 }
1998
1999 return TRUE;
2000 }
Barry Mienydd671972010-10-04 16:33:58 +02002001
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 // --------------------------------------------------------------------
2003
2004 /**
2005 * Send SMTP data
2006 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002007 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * @return bool
2009 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002010 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002012 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 {
patworkb0707982011-04-08 15:10:05 +02002014 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 return FALSE;
2016 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002017
2018 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 }
Barry Mienydd671972010-10-04 16:33:58 +02002020
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 // --------------------------------------------------------------------
2022
2023 /**
2024 * Get SMTP data
2025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 * @return string
2027 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002028 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 {
Andrey Andreev56454792012-05-17 14:32:19 +03002030 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002031
2032 while ($str = fgets($this->_smtp_connect, 512))
2033 {
2034 $data .= $str;
2035
Alex Bilbied261b1e2012-06-02 11:12:16 +01002036 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 {
2038 break;
2039 }
2040 }
2041
2042 return $data;
2043 }
Barry Mienydd671972010-10-04 16:33:58 +02002044
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 // --------------------------------------------------------------------
2046
2047 /**
2048 * Get Hostname
2049 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 * @return string
2051 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002052 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02002054 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 }
Barry Mienydd671972010-10-04 16:33:58 +02002056
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 // --------------------------------------------------------------------
2058
2059 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 * Get Debug Message
2061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 * @return string
2063 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00002064 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 {
2066 $msg = '';
2067
2068 if (count($this->_debug_msg) > 0)
2069 {
2070 foreach ($this->_debug_msg as $val)
2071 {
2072 $msg .= $val;
2073 }
2074 }
2075
Andrey Andreev59c5b532012-03-01 14:09:51 +02002076 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
Barry Mienydd671972010-10-04 16:33:58 +02002078
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 // --------------------------------------------------------------------
2080
2081 /**
2082 * Set Message
2083 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002084 * @param string $msg
2085 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002086 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002088 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 {
2090 $CI =& get_instance();
2091 $CI->lang->load('email');
2092
Andrey Andreev76f15c92012-03-01 13:05:07 +02002093 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 {
Andrey Andreev56454792012-05-17 14:32:19 +03002095 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 }
2097 else
2098 {
Andrey Andreev56454792012-05-17 14:32:19 +03002099 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 }
2101 }
Barry Mienydd671972010-10-04 16:33:58 +02002102
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 // --------------------------------------------------------------------
2104
2105 /**
2106 * Mime Types
2107 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 * @param string
2109 * @return string
2110 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002111 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002113 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00002114
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002115 $ext = strtolower($ext);
2116
2117 if ( ! is_array($mimes))
2118 {
2119 $mimes =& get_mimes();
2120 }
2121
2122 if (isset($mimes[$ext]))
2123 {
2124 return is_array($mimes[$ext])
2125 ? current($mimes[$ext])
2126 : $mimes[$ext];
2127 }
2128
2129 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 }
2131
2132}
Derek Allard2067d1a2008-11-13 22:59:24 +00002133
2134/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03002135/* Location: ./system/libraries/Email.php */