blob: 88398d31654b22755b7d29d93545714d1608e5f1 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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;
vlakofff3994252013-02-19 01:45:23 +010097
nisheeth-barthwalcf225572013-02-18 01:08:04 +053098 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +053099 * SMTP persistent connection
100 *
101 * @var bool
102 */
103 public $smtp_keepalive = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200104
105 /**
106 * SMTP Encryption
107 *
Andrey Andreev89b67b42013-03-12 19:34:23 +0200108 * @var string empty, 'tls' or 'ssl'
Andrey Andreev597ea272012-11-01 22:56:26 +0200109 */
Andrey Andreev89b67b42013-03-12 19:34:23 +0200110 public $smtp_crypto = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200111
112 /**
113 * Whether to apply word-wrapping to the message body.
114 *
115 * @var bool
116 */
117 public $wordwrap = TRUE;
118
119 /**
120 * Number of characters to wrap at.
121 *
122 * @see CI_Email::$wordwrap
123 * @var int
124 */
125 public $wrapchars = 76;
126
127 /**
128 * Message format.
129 *
130 * @var string 'text' or 'html'
131 */
132 public $mailtype = 'text';
133
134 /**
135 * Character set (default: utf-8)
136 *
137 * @var string
138 */
139 public $charset = 'utf-8';
140
141 /**
142 * Multipart message
143 *
144 * @var string 'mixed' (in the body) or 'related' (separate)
145 */
146 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
147
148 /**
149 * Alternative message (for HTML messages only)
150 *
151 * @var string
152 */
153 public $alt_message = '';
154
155 /**
156 * Whether to validate e-mail addresses.
157 *
158 * @var bool
159 */
160 public $validate = FALSE;
161
162 /**
163 * X-Priority header value.
164 *
165 * @var int 1-5
166 */
167 public $priority = 3; // Default priority (1 - 5)
168
169 /**
170 * Newline character sequence.
171 * Use "\r\n" to comply with RFC 822.
172 *
173 * @link http://www.ietf.org/rfc/rfc822.txt
174 * @var string "\r\n" or "\n"
175 */
176 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
177
178 /**
179 * CRLF character sequence
180 *
181 * RFC 2045 specifies that for 'quoted-printable' encoding,
182 * "\r\n" must be used. However, it appears that some servers
183 * (even on the receiving end) don't handle it properly and
184 * switching to "\n", while improper, is the only solution
185 * that seems to work for all environments.
186 *
187 * @link http://www.ietf.org/rfc/rfc822.txt
188 * @var string
189 */
190 public $crlf = "\n";
191
192 /**
193 * Whether to use Delivery Status Notification.
194 *
195 * @var bool
196 */
197 public $dsn = FALSE;
198
199 /**
200 * Whether to send multipart alternatives.
201 * Yahoo! doesn't seem to like these.
202 *
203 * @var bool
204 */
205 public $send_multipart = TRUE;
206
207 /**
208 * Whether to send messages to BCC recipients in batches.
209 *
210 * @var bool
211 */
212 public $bcc_batch_mode = FALSE;
213
214 /**
215 * BCC Batch max number size.
216 *
217 * @see CI_Email::$bcc_batch_mode
218 * @var int
219 */
220 public $bcc_batch_size = 200;
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Whether PHP is running in safe mode. Initialized by the class constructor.
226 *
227 * @var bool
228 */
Andrey Andreev50814092012-03-01 12:36:12 +0200229 protected $_safe_mode = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200230
231 /**
232 * Subject header
233 *
234 * @var string
235 */
Andrey Andreev50814092012-03-01 12:36:12 +0200236 protected $_subject = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200237
238 /**
239 * Message body
240 *
241 * @var string
242 */
Andrey Andreev50814092012-03-01 12:36:12 +0200243 protected $_body = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200244
245 /**
246 * Final message body to be sent.
247 *
248 * @var string
249 */
Andrey Andreev50814092012-03-01 12:36:12 +0200250 protected $_finalbody = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200251
252 /**
253 * multipart/alternative boundary
254 *
255 * @var string
256 */
Andrey Andreev50814092012-03-01 12:36:12 +0200257 protected $_alt_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200258
259 /**
260 * Attachment boundary
261 *
262 * @var string
263 */
Andrey Andreev50814092012-03-01 12:36:12 +0200264 protected $_atc_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200265
266 /**
267 * Final headers to send
268 *
269 * @var string
270 */
Andrey Andreev50814092012-03-01 12:36:12 +0200271 protected $_header_str = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200272
273 /**
274 * SMTP Connection socket placeholder
275 *
276 * @var resource
277 */
Andrey Andreev50814092012-03-01 12:36:12 +0200278 protected $_smtp_connect = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200279
280 /**
281 * Mail encoding
282 *
283 * @var string '8bit' or '7bit'
284 */
Andrey Andreev50814092012-03-01 12:36:12 +0200285 protected $_encoding = '8bit';
Andrey Andreev597ea272012-11-01 22:56:26 +0200286
287 /**
288 * Whether to perform SMTP authentication
289 *
290 * @var bool
291 */
Andrey Andreev50814092012-03-01 12:36:12 +0200292 protected $_smtp_auth = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200293
294 /**
295 * Whether to send a Reply-To header
296 *
297 * @var bool
298 */
Andrey Andreev50814092012-03-01 12:36:12 +0200299 protected $_replyto_flag = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200300
301 /**
302 * Debug messages
303 *
304 * @see CI_Email::print_debugger()
305 * @var string
306 */
Andrey Andreev50814092012-03-01 12:36:12 +0200307 protected $_debug_msg = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200308
309 /**
310 * Recipients
311 *
312 * @var string[]
313 */
Andrey Andreev50814092012-03-01 12:36:12 +0200314 protected $_recipients = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200315
316 /**
317 * CC Recipients
318 *
319 * @var string[]
320 */
Andrey Andreev50814092012-03-01 12:36:12 +0200321 protected $_cc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200322
323 /**
324 * BCC Recipients
325 *
326 * @var string[]
327 */
Andrey Andreev50814092012-03-01 12:36:12 +0200328 protected $_bcc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200329
330 /**
331 * Message headers
332 *
333 * @var string[]
334 */
Andrey Andreev50814092012-03-01 12:36:12 +0200335 protected $_headers = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200336
337 /**
338 * Attachment data
339 *
340 * @var array
341 */
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300342 protected $_attachments = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200343
344 /**
345 * Valid $protocol values
346 *
347 * @see CI_Email::$protocol
348 * @var string[]
349 */
Andrey Andreev50814092012-03-01 12:36:12 +0200350 protected $_protocols = array('mail', 'sendmail', 'smtp');
Andrey Andreev597ea272012-11-01 22:56:26 +0200351
352 /**
353 * Base charsets
354 *
355 * Character sets valid for 7-bit encoding,
356 * excluding language suffix.
357 *
358 * @var string[]
359 */
360 protected $_base_charsets = array('us-ascii', 'iso-2022-');
361
362 /**
363 * Bit depths
364 *
365 * Valid mail encodings
366 *
367 * @see CI_Email::$_encoding
368 * @var string[]
369 */
Andrey Andreev50814092012-03-01 12:36:12 +0200370 protected $_bit_depths = array('7bit', '8bit');
Andrey Andreev597ea272012-11-01 22:56:26 +0200371
372 /**
373 * $priority translations
374 *
375 * Actual values to send with the X-Priority header
376 *
377 * @var string[]
378 */
Andrey Andreevdea61772014-02-24 16:36:25 +0200379 protected $_priorities = array(
380 1 => '1 (Highest)',
381 2 => '2 (High)',
382 3 => '3 (Normal)',
383 4 => '4 (Low)',
384 5 => '5 (Lowest)'
385 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000386
Andrey Andreev597ea272012-11-01 22:56:26 +0200387 // --------------------------------------------------------------------
388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Constructor - Sets Email Preferences
391 *
392 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300393 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300394 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300395 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000397 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300399 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 if (count($config) > 0)
402 {
403 $this->initialize($config);
404 }
405 else
406 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100407 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
Andrey Andreevf6274742014-02-20 18:05:58 +0200410 $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300411 $this->charset = strtoupper($this->charset);
412
Andrey Andreev59c5b532012-03-01 14:09:51 +0200413 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530415
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530416 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100417
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530418 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530419 * Destructor - Releases Resources
420 *
421 * @return void
422 */
423 public function __destruct()
424 {
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530425 if (is_resource($this->_smtp_connect))
426 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530427 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530428 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530429 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000430
431 // --------------------------------------------------------------------
432
433 /**
434 * Initialize preferences
435 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @param array
Andrew Podner4296a652012-12-17 07:51:15 -0500437 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000439 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 foreach ($config as $key => $val)
442 {
443 if (isset($this->$key))
444 {
445 $method = 'set_'.$key;
446
447 if (method_exists($this, $method))
448 {
449 $this->$method($val);
450 }
451 else
452 {
453 $this->$key = $val;
454 }
455 }
456 }
Eric Barnes6113f542010-12-29 13:36:12 -0500457 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000458
Alex Bilbied261b1e2012-06-02 11:12:16 +0100459 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000460
461 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
465
466 /**
467 * Initialize the Email Data
468 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800469 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500470 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000472 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200474 $this->_subject = '';
475 $this->_body = '';
476 $this->_finalbody = '';
477 $this->_header_str = '';
478 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500480 $this->_cc_array = array();
481 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 $this->_headers = array();
483 $this->_debug_msg = array();
484
Mickey Wubfc1cad2012-05-31 22:28:40 -0700485 $this->set_header('User-Agent', $this->useragent);
486 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
488 if ($clear_attachments !== FALSE)
489 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300490 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Eric Barnes6113f542010-12-29 13:36:12 -0500492
Greg Akera769deb2010-11-10 15:47:29 -0600493 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
499 * Set FROM
500 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300501 * @param string $from
502 * @param string $name
503 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500504 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300506 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200508 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200510 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
513 if ($this->validate)
514 {
515 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300516 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200517 {
518 $this->validate_email($this->_str_to_array($return_path));
519 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
521
522 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100523 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 // only use Q encoding if there are characters that would require it
526 if ( ! preg_match('/[\200-\377]/', $name))
527 {
528 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000529 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
531 else
532 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300533 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535 }
536
Mickey Wubfc1cad2012-05-31 22:28:40 -0700537 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200538
Andrey Andreev925dd902012-10-19 11:06:31 +0300539 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200540 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500541
Greg Akera769deb2010-11-10 15:47:29 -0600542 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // --------------------------------------------------------------------
546
547 /**
548 * Set Reply-to
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string
551 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500552 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000554 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200556 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200558 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560
561 if ($this->validate)
562 {
563 $this->validate_email($this->_str_to_array($replyto));
564 }
565
Alex Bilbied261b1e2012-06-02 11:12:16 +0100566 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $name = $replyto;
569 }
570
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300571 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 $name = '"'.$name.'"';
574 }
575
Mickey Wubfc1cad2012-05-31 22:28:40 -0700576 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600578
579 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // --------------------------------------------------------------------
583
584 /**
585 * Set Recipients
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500588 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000590 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 $to = $this->_str_to_array($to);
593 $to = $this->clean_email($to);
594
595 if ($this->validate)
596 {
597 $this->validate_email($to);
598 }
599
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200600 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700602 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300605 $this->_recipients = $to;
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 CC
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500616 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000618 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreev56454792012-05-17 14:32:19 +0300620 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000621
622 if ($this->validate)
623 {
624 $this->validate_email($cc);
625 }
626
Mickey Wubfc1cad2012-05-31 22:28:40 -0700627 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000628
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200629 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
631 $this->_cc_array = $cc;
632 }
Eric Barnes6113f542010-12-29 13:36:12 -0500633
Greg Akera769deb2010-11-10 15:47:29 -0600634 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
640 * Set BCC
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @param string
643 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500644 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000646 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100648 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 $this->bcc_batch_mode = TRUE;
651 $this->bcc_batch_size = $limit;
652 }
653
Andrey Andreev56454792012-05-17 14:32:19 +0300654 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
656 if ($this->validate)
657 {
658 $this->validate_email($bcc);
659 }
660
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200661 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 $this->_bcc_array = $bcc;
664 }
665 else
666 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700667 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
Eric Barnes6113f542010-12-29 13:36:12 -0500669
Greg Akera769deb2010-11-10 15:47:29 -0600670 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // --------------------------------------------------------------------
674
675 /**
676 * Set Email Subject
677 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500679 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000681 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700684 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600685 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 // --------------------------------------------------------------------
689
690 /**
691 * Set Body
692 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500694 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000696 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200698 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200699
Andrey Andreevaf728622011-10-20 10:11:59 +0300700 /* strip slashes only if magic quotes is ON
701 if we do it with magic quotes OFF, it strips real, user-inputted chars.
702
703 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
704 it will probably not exist in future versions at all.
705 */
706 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200707 {
diegorivera33c32802011-10-19 02:56:15 -0200708 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200709 }
diegorivera33c32802011-10-19 02:56:15 -0200710
Greg Akera769deb2010-11-10 15:47:29 -0600711 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * Assign file attachments
718 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100719 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300720 * @param string $disposition = 'attachment'
721 * @param string $newname = NULL
722 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500723 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100725 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100727 if ($mime === '')
728 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100729 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100730 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100731 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100732 return FALSE;
733 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200734
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200735 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100736 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100737 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100738 return FALSE;
739 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100740
Petr Heralecky300e3f02014-01-10 11:49:11 +0100741 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100742 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100743 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100744 }
745 else
746 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100747 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100748 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200749
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300750 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100751 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300752 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100753 'type' => $mime,
754 'content' => chunk_split(base64_encode($file_content))
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300755 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100756
Greg Akera769deb2010-11-10 15:47:29 -0600757 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200759
Petr Heralecky300e3f02014-01-10 11:49:11 +0100760 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200761
Petr Heralecky300e3f02014-01-10 11:49:11 +0100762 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200763 * Set and return attachment Content-ID
764 *
765 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100766 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100767 * @param string $filename
768 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100769 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200770 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100771 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100772 if ($this->multipart !== 'related')
Petr Heralecky300e3f02014-01-10 11:49:11 +0100773 {
774 $this->multipart = 'related'; // Thunderbird need this for inline images
775 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200776
Petr Heraleckyde886152014-01-10 12:52:56 +0100777 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100778 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100779 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100780 {
Petr Heralecky230fca32014-01-10 12:55:57 +0100781 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100782 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100783 }
784 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200785
Petr Heralecky300e3f02014-01-10 11:49:11 +0100786 return FALSE;
787 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 // --------------------------------------------------------------------
790
791 /**
792 * Add a Header Item
793 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 * @param string
795 * @param string
796 * @return void
797 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700798 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
florisluitenf55d5142013-06-07 17:20:06 +0300800 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 // --------------------------------------------------------------------
804
805 /**
806 * Convert a String to an Array
807 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 * @param string
809 * @return array
810 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600811 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
813 if ( ! is_array($email))
814 {
Andrey Andreev56454792012-05-17 14:32:19 +0300815 return (strpos($email, ',') !== FALSE)
816 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
817 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
Andrey Andreev56454792012-05-17 14:32:19 +0300819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 return $email;
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 // --------------------------------------------------------------------
824
825 /**
826 * Set Multipart Value
827 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500829 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200831 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400833 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600834 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // --------------------------------------------------------------------
838
839 /**
840 * Set Mailtype
841 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500843 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000845 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Andrey Andreev56454792012-05-17 14:32:19 +0300847 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600848 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 /**
854 * Set Wordwrap
855 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400856 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500857 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000859 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400861 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600862 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
866
867 /**
868 * Set Protocol
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500871 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000873 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200875 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600876 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
880
881 /**
882 * Set Priority
883 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200884 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500885 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000887 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200889 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600890 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // --------------------------------------------------------------------
894
895 /**
896 * Set Newline Character
897 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500899 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000901 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200903 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600904 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 }
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 // --------------------------------------------------------------------
908
909 /**
910 * Set CRLF
911 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500913 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000915 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200917 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600918 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 // --------------------------------------------------------------------
922
923 /**
924 * Set Message Boundary
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return void
927 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600928 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200930 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
931 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 // --------------------------------------------------------------------
935
936 /**
937 * Get the Message ID
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @return string
940 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600941 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200943 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300944 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 // --------------------------------------------------------------------
948
949 /**
950 * Get Mail Protocol
951 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200953 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600955 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
957 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200958 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000959
Alex Bilbied261b1e2012-06-02 11:12:16 +0100960 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
962 return $this->protocol;
963 }
964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 // --------------------------------------------------------------------
967
968 /**
969 * Get Mail Encoding
970 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @param bool
972 * @return string
973 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600974 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200976 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000977
978 foreach ($this->_base_charsets as $charset)
979 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300980 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 $this->_encoding = '7bit';
983 }
984 }
985
Alex Bilbied261b1e2012-06-02 11:12:16 +0100986 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 return $this->_encoding;
989 }
990 }
991
992 // --------------------------------------------------------------------
993
994 /**
995 * Get content type (text/html/attachment)
996 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @return string
998 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600999 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev56454792012-05-17 14:32:19 +03001001 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001003 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001005 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 return 'plain-attach';
1008 }
1009 else
1010 {
1011 return 'plain';
1012 }
1013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
1016
1017 /**
1018 * Set RFC 822 Date
1019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @return string
1021 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001022 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001024 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001025 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001027 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +00001028
Andrey Andreev59c5b532012-03-01 14:09:51 +02001029 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
1033
1034 /**
1035 * Mime message
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @return string
1038 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001039 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001041 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 +00001042 }
Barry Mienydd671972010-10-04 16:33:58 +02001043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 // --------------------------------------------------------------------
1045
1046 /**
1047 * Validate Email Address
1048 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 * @param string
1050 * @return bool
1051 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001052 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 {
1054 if ( ! is_array($email))
1055 {
patworkb0707982011-04-08 15:10:05 +02001056 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 return FALSE;
1058 }
1059
1060 foreach ($email as $val)
1061 {
1062 if ( ! $this->valid_email($val))
1063 {
patworkb0707982011-04-08 15:10:05 +02001064 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 return FALSE;
1066 }
1067 }
1068
1069 return TRUE;
1070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 // --------------------------------------------------------------------
1073
1074 /**
1075 * Email Validation
1076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 * @param string
1078 * @return bool
1079 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001080 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreev95496662014-06-01 00:00:13 +03001082 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1083 {
1084 $email = substr($email, 0, ++$atpos).idn_to_ascii(substr($email, $atpos));
1085 }
1086
Andrey Andreev580388b2012-06-27 15:43:46 +03001087 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
Barry Mienydd671972010-10-04 16:33:58 +02001089
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 // --------------------------------------------------------------------
1091
1092 /**
1093 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @param string
1096 * @return string
1097 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001098 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 if ( ! is_array($email))
1101 {
Andrey Andreev56454792012-05-17 14:32:19 +03001102 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104
1105 $clean_email = array();
1106
1107 foreach ($email as $addy)
1108 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001109 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
1111
1112 return $clean_email;
1113 }
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // --------------------------------------------------------------------
1116
1117 /**
1118 * Build alternative plain text message
1119 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001120 * Provides the raw message for use in plain-text headers of
1121 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 * If the user hasn't specified his own alternative message
1123 * it creates one by stripping the HTML
1124 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 * @return string
1126 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001127 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001129 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001131 return ($this->wordwrap)
1132 ? $this->word_wrap($this->alt_message, 76)
1133 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 }
1135
Andrey Andreev56454792012-05-17 14:32:19 +03001136 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001137 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001138
1139 for ($i = 20; $i >= 3; $i--)
1140 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001141 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
1143
GDmac9bea4be2012-10-30 06:14:19 +01001144 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001145 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001146
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001147 return ($this->wordwrap)
1148 ? $this->word_wrap($body, 76)
1149 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
Barry Mienydd671972010-10-04 16:33:58 +02001151
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 // --------------------------------------------------------------------
1153
1154 /**
1155 * Word Wrap
1156 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001158 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 * @return string
1160 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001161 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001163 // Set the character limit, if not already present
1164 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001166 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
1168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 // Standardize newlines
1170 if (strpos($str, "\r") !== FALSE)
1171 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001172 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
GDmac9bea4be2012-10-30 06:14:19 +01001175 // Reduce multiple spaces at end of line
1176 $str = preg_replace('| +\n|', "\n", $str);
1177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 // If the current word is surrounded by {unwrap} tags we'll
1179 // strip the entire chunk and replace it with a marker.
1180 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001181 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001183 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001185 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001186 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 }
1188 }
1189
vlakofff0ab8132014-05-20 10:32:53 +02001190 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001192 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 $str = wordwrap($str, $charlim, "\n", FALSE);
1194
1195 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001196 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 foreach (explode("\n", $str) as $line)
1198 {
1199 // Is the line within the allowed character count?
1200 // If so we'll join it to the output and continue
vlakofff0ab8132014-05-20 10:32:53 +02001201 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
1203 $output .= $line.$this->newline;
1204 continue;
1205 }
1206
1207 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001208 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001211 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
1213 break;
1214 }
1215
1216 // Trim the word down
vlakofff0ab8132014-05-20 10:32:53 +02001217 $temp .= mb_substr($line, 0, $charlim - 1);
1218 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
vlakofff0ab8132014-05-20 10:32:53 +02001220 while (mb_strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001221
1222 // If $temp contains data it means we had to split up an over-length
1223 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001224 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001226 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 }
1228
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001229 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
1231
1232 // Put our markers back
1233 if (count($unwrap) > 0)
1234 {
1235 foreach ($unwrap as $key => $val)
1236 {
Andrey Andreev56454792012-05-17 14:32:19 +03001237 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
1239 }
1240
1241 return $output;
1242 }
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 // --------------------------------------------------------------------
1245
1246 /**
1247 * Build final headers
1248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @return string
1250 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001251 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001253 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1254 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001255 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001256 $this->set_header('Message-ID', $this->_get_message_id());
1257 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 // --------------------------------------------------------------------
1261
1262 /**
1263 * Write Headers as a string
1264 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 * @return void
1266 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001267 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001269 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301271 if (isset($this->_headers['Subject']))
1272 {
1273 $this->_subject = $this->_headers['Subject'];
1274 unset($this->_headers['Subject']);
1275 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 }
1277
1278 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001279 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001280
Pascal Kriete14287f32011-02-14 13:39:34 -05001281 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
1283 $val = trim($val);
1284
Alex Bilbied261b1e2012-06-02 11:12:16 +01001285 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 {
Andrey Andreev56454792012-05-17 14:32:19 +03001287 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
1289 }
1290
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001291 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 {
Derek Jones1d890882009-02-10 20:32:14 +00001293 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
1295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // --------------------------------------------------------------------
1298
1299 /**
1300 * Build Final Body and attachments
1301 *
buhayc6da1ef2013-04-18 09:06:49 -07001302 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001304 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001306 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
1308 $this->_body = $this->word_wrap($this->_body);
1309 }
1310
1311 $this->_set_boundaries();
1312 $this->_write_headers();
1313
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001314 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001315 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001316
1317 switch ($this->_get_content_type())
1318 {
1319 case 'plain' :
1320
Andrey Andreev56454792012-05-17 14:32:19 +03001321 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1322 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001323
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001324 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
1326 $this->_header_str .= $hdr;
1327 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 }
Brandon Jones485d7412010-11-09 16:38:17 -05001329 else
1330 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001331 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001332 }
Eric Barnes6113f542010-12-29 13:36:12 -05001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 return;
1335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 case 'html' :
1337
1338 if ($this->send_multipart === FALSE)
1339 {
Andrey Andreev56454792012-05-17 14:32:19 +03001340 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001341 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 }
1343 else
1344 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001345 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001346
Andrey Andreev56454792012-05-17 14:32:19 +03001347 $body .= $this->_get_mime_message().$this->newline.$this->newline
1348 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001349
Andrey Andreev56454792012-05-17 14:32:19 +03001350 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1351 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1352 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001353
Andrey Andreev56454792012-05-17 14:32:19 +03001354 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1355 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 }
Eric Barnes6113f542010-12-29 13:36:12 -05001357
Andrey Andreev56454792012-05-17 14:32:19 +03001358 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001359
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001360 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
1362 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001363 }
1364 else
1365 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001366 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 }
1368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 if ($this->send_multipart !== FALSE)
1370 {
Andrey Andreev56454792012-05-17 14:32:19 +03001371 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 }
1373
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 return;
1375
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 case 'plain-attach' :
1377
Andrey Andreev2b956af2013-08-07 14:32:56 +03001378 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001379
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001380 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
1382 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001383 }
1384
Andrey Andreev2b956af2013-08-07 14:32:56 +03001385 $body .= $this->_get_mime_message().$this->newline
1386 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001387 .'--'.$this->_atc_boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001388 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001389 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1390 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001391 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001392
1393 break;
1394 case 'html-attach' :
1395
Andrey Andreev2b956af2013-08-07 14:32:56 +03001396 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Eric Barnes6113f542010-12-29 13:36:12 -05001397
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001398 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 }
1402
Andrey Andreev56454792012-05-17 14:32:19 +03001403 $body .= $this->_get_mime_message().$this->newline.$this->newline
1404 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001405
Andrey Andreev56454792012-05-17 14:32:19 +03001406 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1407 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001408
Andrey Andreev56454792012-05-17 14:32:19 +03001409 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1410 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1411 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001412
Andrey Andreev56454792012-05-17 14:32:19 +03001413 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1414 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001415
Andrey Andreev56454792012-05-17 14:32:19 +03001416 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1417 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001418
1419 break;
1420 }
1421
1422 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001423 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001425 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001426 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001427 ? basename($filename) : $this->_attachments[$i]['name'][1];
Andrey Andreev56454792012-05-17 14:32:19 +03001428
1429 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001430 .'Content-type: '.$this->_attachments[$i]['type'].'; '
Andrey Andreev56454792012-05-17 14:32:19 +03001431 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001432 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001433 .'Content-Transfer-Encoding: base64'.$this->newline
Petr Heraleckyde886152014-01-10 12:52:56 +01001434 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
Derek Allard2067d1a2008-11-13 22:59:24 +00001435
Petr Heralecky300e3f02014-01-10 11:49:11 +01001436 $attachment[$z++] = $this->_attachments[$i]['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 }
1438
Andrey Andreev56454792012-05-17 14:32:19 +03001439 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
Andrey Andreev2b956af2013-08-07 14:32:56 +03001440 $this->_finalbody = ($this->_get_protocol() === 'mail')
1441 ? $body
1442 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001443
buhay466e8082013-04-17 14:25:34 -07001444 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 // --------------------------------------------------------------------
1448
1449 /**
1450 * Prep Quoted Printable
1451 *
1452 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1453 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1454 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 * @return string
1457 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001458 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001460 // We are intentionally wrapping so mail servers will encode characters
1461 // properly and MUAs will behave, so {unwrap} must go!
1462 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1463
Andrey Andreev683b34d2012-10-09 15:00:00 +03001464 // RFC 2045 specifies CRLF as "\r\n".
1465 // However, many developers choose to override that and violate
1466 // the RFC rules due to (apparently) a bug in MS Exchange,
1467 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001468 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001469 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001470 if (is_php('5.3'))
1471 {
1472 return quoted_printable_encode($str);
1473 }
1474 elseif (function_exists('imap_8bit'))
1475 {
1476 return imap_8bit($str);
1477 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001478 }
1479
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001480 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001481 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001482
1483 // Standardize newlines
1484 if (strpos($str, "\r") !== FALSE)
1485 {
1486 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1487 }
1488
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 $escape = '=';
1490 $output = '';
1491
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001492 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
1494 $length = strlen($line);
1495 $temp = '';
1496
1497 // Loop through each character in the line to add soft-wrap
1498 // characters at the end of a line " =\r\n" and add the newly
1499 // processed line(s) to the output (see comment on $crlf class property)
1500 for ($i = 0; $i < $length; $i++)
1501 {
1502 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001503 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 $ascii = ord($char);
1505
1506 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001507 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001509 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001511 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001513 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 }
1515
1516 // If we're at the character limit, add the line to the output,
1517 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001518 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 {
1520 $output .= $temp.$escape.$this->crlf;
1521 $temp = '';
1522 }
1523
1524 // Add the character to our temporary line
1525 $temp .= $char;
1526 }
1527
1528 // Add our completed line to the output
1529 $output .= $temp.$this->crlf;
1530 }
1531
1532 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001533 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 }
1535
1536 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001537
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 /**
1539 * Prep Q Encoding
1540 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001541 * Performs "Q Encoding" on a string for use in email headers.
1542 * It's related but not identical to quoted-printable, so it has its
1543 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001545 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001546 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001547 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001548 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001550 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001551
Andrey Andreev925dd902012-10-19 11:06:31 +03001552 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001554 if (MB_ENABLED === TRUE)
1555 {
1556 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1557 }
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001558 elseif (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001559 {
1560 $output = @iconv_mime_encode('', $str,
1561 array(
1562 'scheme' => 'Q',
1563 'line-length' => 76,
1564 'input-charset' => $this->charset,
1565 'output-charset' => $this->charset,
1566 'line-break-chars' => $this->crlf
1567 )
1568 );
1569
1570 // There are reports that iconv_mime_encode() might fail and return FALSE
1571 if ($output !== FALSE)
1572 {
1573 // iconv_mime_encode() will always put a header field name.
1574 // We've passed it an empty one, but it still prepends our
1575 // encoded string with ': ', so we need to strip it.
1576 return substr($output, 2);
1577 }
1578
1579 $chars = iconv_strlen($str, 'UTF-8');
1580 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 }
1582
Andrey Andreev925dd902012-10-19 11:06:31 +03001583 // We might already have this set for UTF-8
1584 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001585
Andrey Andreev925dd902012-10-19 11:06:31 +03001586 $output = '=?'.$this->charset.'?Q?';
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001587 for ($i = 0, $length = strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001589 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001590 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1591 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001592
Andrey Andreev925dd902012-10-19 11:06:31 +03001593 // RFC 2045 sets a limit of 76 characters per line.
1594 // We'll append ?= to the end of each line though.
1595 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001597 $output .= '?='.$this->crlf // EOL
1598 .' =?'.$this->charset.'?Q?'.$chr; // New line
1599 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001601 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001603 $output .= $chr;
1604 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 }
1607
Andrey Andreev925dd902012-10-19 11:06:31 +03001608 // End the header
1609 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 }
1611
1612 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001613
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 /**
1615 * Send Email
1616 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001617 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 * @return bool
1619 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001620 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001622 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 {
1624 $this->reply_to($this->_headers['From']);
1625 }
1626
Andrey Andreev76f15c92012-03-01 13:05:07 +02001627 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1628 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1629 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
patworkb0707982011-04-08 15:10:05 +02001631 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 return FALSE;
1633 }
1634
1635 $this->_build_headers();
1636
Andrey Andreev76f15c92012-03-01 13:05:07 +02001637 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001639 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001640
Alex Bilbiea87aab32012-07-30 09:50:37 +01001641 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001642 {
1643 $this->clear();
1644 }
1645
Alex Bilbieb901e732012-07-30 09:44:57 +01001646 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 }
1648
buhay466e8082013-04-17 14:25:34 -07001649 if ($this->_build_message() === FALSE)
1650 {
1651 return FALSE;
1652 }
buhayc6da1ef2013-04-18 09:06:49 -07001653
Alex Bilbieb901e732012-07-30 09:44:57 +01001654 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001655
Alex Bilbiea87aab32012-07-30 09:50:37 +01001656 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001657 {
1658 $this->clear();
1659 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001660
Alex Bilbieb901e732012-07-30 09:44:57 +01001661 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 // --------------------------------------------------------------------
1665
1666 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001667 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001669 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001671 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001673 $float = $this->bcc_batch_size - 1;
1674 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 $chunk = array();
1676
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001677 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
1679 if (isset($this->_bcc_array[$i]))
1680 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001681 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 }
1683
Alex Bilbied261b1e2012-06-02 11:12:16 +01001684 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 {
1686 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001687 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001688 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 }
1690
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001691 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 {
1693 $chunk[] = substr($set, 1);
1694 }
1695 }
1696
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001697 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 {
1699 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001700
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001701 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001702
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001703 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001705 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 }
1707 else
1708 {
1709 $this->_bcc_array = $bcc;
1710 }
1711
buhay466e8082013-04-17 14:25:34 -07001712 if ($this->_build_message() === FALSE)
1713 {
1714 return FALSE;
1715 }
buhayc6da1ef2013-04-18 09:06:49 -07001716
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 $this->_spool_email();
1718 }
1719 }
Barry Mienydd671972010-10-04 16:33:58 +02001720
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 // --------------------------------------------------------------------
1722
1723 /**
1724 * Unwrap special elements
1725 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 * @return void
1727 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001728 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 {
Andrey Andreev56454792012-05-17 14:32:19 +03001730 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
Barry Mienydd671972010-10-04 16:33:58 +02001732
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 // --------------------------------------------------------------------
1734
1735 /**
1736 * Strip line-breaks via callback
1737 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001738 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 * @return string
1740 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001741 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
1743 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1744 {
1745 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1746 }
1747
1748 return $matches[1];
1749 }
Barry Mienydd671972010-10-04 16:33:58 +02001750
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 // --------------------------------------------------------------------
1752
1753 /**
1754 * Spool mail to the mail server
1755 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 * @return bool
1757 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001758 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
1760 $this->_unwrap_specials();
1761
Andrey Andreev56454792012-05-17 14:32:19 +03001762 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001763 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 {
Andrey Andreev56454792012-05-17 14:32:19 +03001765 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001766 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
1768
patworkb0707982011-04-08 15:10:05 +02001769 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 return TRUE;
1771 }
Barry Mienydd671972010-10-04 16:33:58 +02001772
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 // --------------------------------------------------------------------
1774
1775 /**
1776 * Send using mail()
1777 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 * @return bool
1779 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001780 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001782 if (is_array($this->_recipients))
1783 {
1784 $this->_recipients = implode(', ', $this->_recipients);
1785 }
1786
Alex Bilbied261b1e2012-06-02 11:12:16 +01001787 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001789 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 }
1791 else
1792 {
1793 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1794 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001795 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 +00001796 }
1797 }
Barry Mienydd671972010-10-04 16:33:58 +02001798
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 // --------------------------------------------------------------------
1800
1801 /**
1802 * Send using Sendmail
1803 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 * @return bool
1805 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001806 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001808 // is popen() enabled?
1809 if ( ! function_usable('popen')
1810 OR FALSE === ($fp = @popen(
1811 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
1812 .' -t -r '.$this->clean_email($this->_headers['Return-Path'])
1813 , 'w'))
1814 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001815 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001816 return FALSE;
1817 }
Derek Jones71141ce2010-03-02 16:41:20 -06001818
Derek Jonesc630bcf2008-11-17 21:09:45 +00001819 fputs($fp, $this->_header_str);
1820 fputs($fp, $this->_finalbody);
1821
Barry Mienydd671972010-10-04 16:33:58 +02001822 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001823
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001824 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
patworkb0707982011-04-08 15:10:05 +02001826 $this->_set_error_message('lang:email_exit_status', $status);
1827 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 return FALSE;
1829 }
1830
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 return TRUE;
1832 }
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 // --------------------------------------------------------------------
1835
1836 /**
1837 * Send using SMTP
1838 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 * @return bool
1840 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001841 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301842 {
1843 if ($this->smtp_host === '')
1844 {
1845 $this->_set_error_message('lang:email_no_hostname');
1846 return FALSE;
1847 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001848
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301849 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1850 {
1851 return FALSE;
1852 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001853
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301854 $this->_send_command('from', $this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001855
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301856 foreach ($this->_recipients as $val)
1857 {
1858 $this->_send_command('to', $val);
1859 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001860
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301861 if (count($this->_cc_array) > 0)
1862 {
1863 foreach ($this->_cc_array as $val)
1864 {
1865 if ($val !== '')
1866 {
1867 $this->_send_command('to', $val);
1868 }
1869 }
1870 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001871
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301872 if (count($this->_bcc_array) > 0)
1873 {
1874 foreach ($this->_bcc_array as $val)
1875 {
1876 if ($val !== '')
1877 {
1878 $this->_send_command('to', $val);
1879 }
1880 }
1881 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001882
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301883 $this->_send_command('data');
Derek Allard2067d1a2008-11-13 22:59:24 +00001884
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301885 // perform dot transformation on any lines that begin with a dot
1886 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001887
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301888 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001889
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301890 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001891
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301892 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001893
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301894 if (strpos($reply, '250') !== 0)
1895 {
1896 $this->_set_error_message('lang:email_smtp_error', $reply);
1897 return FALSE;
1898 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001899
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301900 if ($this->smtp_keepalive)
1901 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301902 $this->_send_command('reset');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301903 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301904 else
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301905 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301906 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301907 }
Barry Mienydd671972010-10-04 16:33:58 +02001908
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301909 return TRUE;
1910 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001911
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301912 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001913
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301914 /**
1915 * SMTP Connect
1916 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301917 * @return string
1918 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301919 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301920 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301921 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301922 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301923 return TRUE;
1924 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001925
Andrey Andreev89b67b42013-03-12 19:34:23 +02001926 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001927
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301928 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1929 $this->smtp_port,
1930 $errno,
1931 $errstr,
1932 $this->smtp_timeout);
1933
1934 if ( ! is_resource($this->_smtp_connect))
1935 {
1936 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1937 return FALSE;
1938 }
1939
1940 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1941 $this->_set_error_message($this->_get_smtp_data());
1942
1943 if ($this->smtp_crypto === 'tls')
1944 {
1945 $this->_send_command('hello');
1946 $this->_send_command('starttls');
1947
1948 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1949
1950 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301951 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301952 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301953 return FALSE;
1954 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301955 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301956
1957 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301958 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001959
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301960 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001961
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301962 /**
1963 * Send SMTP command
1964 *
1965 * @param string
1966 * @param string
1967 * @return string
1968 */
1969 protected function _send_command($cmd, $data = '')
1970 {
1971 switch ($cmd)
1972 {
1973 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001974
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301975 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1976 {
1977 $this->_send_data('EHLO '.$this->_get_hostname());
1978 }
1979 else
1980 {
1981 $this->_send_data('HELO '.$this->_get_hostname());
1982 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001983
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301984 $resp = 250;
1985 break;
1986 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001987
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301988 $this->_send_data('STARTTLS');
1989 $resp = 220;
1990 break;
1991 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03001992
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301993 $this->_send_data('MAIL FROM:<'.$data.'>');
1994 $resp = 250;
1995 break;
1996 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03001997
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301998 if ($this->dsn)
1999 {
2000 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2001 }
2002 else
2003 {
2004 $this->_send_data('RCPT TO:<'.$data.'>');
2005 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002006
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302007 $resp = 250;
2008 break;
2009 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002010
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302011 $this->_send_data('DATA');
2012 $resp = 354;
2013 break;
2014 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002015
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302016 $this->_send_data('RSET');
2017 $resp = 250;
2018 break;
2019 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002020
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302021 $this->_send_data('QUIT');
2022 $resp = 221;
2023 break;
2024 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002025
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302026 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002027
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302028 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002029
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302030 if ((int) substr($reply, 0, 3) !== $resp)
2031 {
2032 $this->_set_error_message('lang:email_smtp_error', $reply);
2033 return FALSE;
2034 }
Barry Mienydd671972010-10-04 16:33:58 +02002035
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302036 if ($cmd === 'quit')
2037 {
2038 fclose($this->_smtp_connect);
2039 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002040
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302041 return TRUE;
2042 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002043
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302044 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002045
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302046 /**
2047 * SMTP Authenticate
2048 *
2049 * @return bool
2050 */
2051 protected function _smtp_authenticate()
2052 {
2053 if ( ! $this->_smtp_auth)
2054 {
2055 return TRUE;
2056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002057
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302058 if ($this->smtp_user === '' && $this->smtp_pass === '')
2059 {
2060 $this->_set_error_message('lang:email_no_smtp_unpw');
2061 return FALSE;
2062 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002063
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302064 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002065
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302066 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002067
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002068 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302069 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302070 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302071 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002072 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302073 {
2074 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2075 return FALSE;
2076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002077
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302078 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002079
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302080 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002081
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302082 if (strpos($reply, '334') !== 0)
2083 {
2084 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2085 return FALSE;
2086 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002087
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302088 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002089
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302090 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302091
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302092 if (strpos($reply, '235') !== 0)
2093 {
2094 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2095 return FALSE;
2096 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302097
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302098 return TRUE;
2099 }
Barry Mienydd671972010-10-04 16:33:58 +02002100
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 // --------------------------------------------------------------------
2102
2103 /**
2104 * Send SMTP data
2105 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002106 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 * @return bool
2108 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002109 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002111 $data .= $this->newline;
2112 for ($written = 0, $length = strlen($data); $written < $length; $written += $result)
2113 {
2114 if (($result = fwrite($this->_smtp_connect, substr($data, $written))) === FALSE)
2115 {
2116 break;
2117 }
2118 }
2119
2120 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 {
patworkb0707982011-04-08 15:10:05 +02002122 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 return FALSE;
2124 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002125
2126 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 }
Barry Mienydd671972010-10-04 16:33:58 +02002128
Derek Allard2067d1a2008-11-13 22:59:24 +00002129 // --------------------------------------------------------------------
2130
2131 /**
2132 * Get SMTP data
2133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 * @return string
2135 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002136 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002137 {
Andrey Andreev56454792012-05-17 14:32:19 +03002138 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002139
2140 while ($str = fgets($this->_smtp_connect, 512))
2141 {
2142 $data .= $str;
2143
Alex Bilbied261b1e2012-06-02 11:12:16 +01002144 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 {
2146 break;
2147 }
2148 }
2149
2150 return $data;
2151 }
Barry Mienydd671972010-10-04 16:33:58 +02002152
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 // --------------------------------------------------------------------
2154
2155 /**
2156 * Get Hostname
2157 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 * @return string
2159 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002160 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002161 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02002162 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 }
Barry Mienydd671972010-10-04 16:33:58 +02002164
Derek Allard2067d1a2008-11-13 22:59:24 +00002165 // --------------------------------------------------------------------
2166
2167 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 * Get Debug Message
2169 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002170 * @param array $include List of raw data chunks to include in the output
2171 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 * @return string
2173 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002174 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 {
2176 $msg = '';
2177
2178 if (count($this->_debug_msg) > 0)
2179 {
2180 foreach ($this->_debug_msg as $val)
2181 {
2182 $msg .= $val;
2183 }
2184 }
2185
Andrey Andreev61797f62012-11-26 16:15:12 +02002186 // Determine which parts of our raw data needs to be printed
2187 $raw_data = '';
2188 is_array($include) OR $include = array($include);
2189
2190 if (in_array('headers', $include, TRUE))
2191 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002192 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002193 }
2194
2195 if (in_array('subject', $include, TRUE))
2196 {
2197 $raw_data .= htmlspecialchars($this->_subject)."\n";
2198 }
2199
2200 if (in_array('body', $include, TRUE))
2201 {
2202 $raw_data .= htmlspecialchars($this->_finalbody);
2203 }
2204
2205 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 }
Barry Mienydd671972010-10-04 16:33:58 +02002207
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 // --------------------------------------------------------------------
2209
2210 /**
2211 * Set Message
2212 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002213 * @param string $msg
2214 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002215 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002217 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 {
2219 $CI =& get_instance();
2220 $CI->lang->load('email');
2221
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002222 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002223 {
Andrey Andreev56454792012-05-17 14:32:19 +03002224 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 }
2226 else
2227 {
Andrey Andreev56454792012-05-17 14:32:19 +03002228 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 }
2230 }
Barry Mienydd671972010-10-04 16:33:58 +02002231
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 // --------------------------------------------------------------------
2233
2234 /**
2235 * Mime Types
2236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 * @param string
2238 * @return string
2239 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002240 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002242 $ext = strtolower($ext);
2243
vlakoff66c7bb42014-05-19 13:45:12 +02002244 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002245
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002246 if (isset($mimes[$ext]))
2247 {
2248 return is_array($mimes[$ext])
2249 ? current($mimes[$ext])
2250 : $mimes[$ext];
2251 }
2252
2253 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
2255
2256}
Derek Allard2067d1a2008-11-13 22:59:24 +00002257
2258/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03002259/* Location: ./system/libraries/Email.php */