blob: cd74d6da168855bbdad550219da4542c7ccda544 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Email Class
42 *
43 * Permits email to be sent using Mail, Sendmail, or SMTP.
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/email.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
trita15dd4f2011-11-23 07:40:05 -050051class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000052
Andrey Andreev597ea272012-11-01 22:56:26 +020053 /**
54 * Used as the User-Agent and X-Mailer headers' value.
55 *
56 * @var string
57 */
Andrey Andreev59c5b532012-03-01 14:09:51 +020058 public $useragent = 'CodeIgniter';
Andrey Andreev50814092012-03-01 12:36:12 +020059
Andrey Andreev597ea272012-11-01 22:56:26 +020060 /**
61 * Path to the Sendmail binary.
62 *
63 * @var string
64 */
65 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
66
67 /**
68 * Which method to use for sending e-mails.
69 *
70 * @var string 'mail', 'sendmail' or 'smtp'
71 */
72 public $protocol = 'mail'; // mail/sendmail/smtp
73
74 /**
75 * STMP Server host
76 *
77 * @var string
78 */
79 public $smtp_host = '';
80
81 /**
82 * SMTP Username
83 *
84 * @var string
85 */
86 public $smtp_user = '';
87
88 /**
89 * SMTP Password
90 *
91 * @var string
92 */
93 public $smtp_pass = '';
94
95 /**
96 * SMTP Server port
97 *
98 * @var int
99 */
100 public $smtp_port = 25;
101
102 /**
103 * SMTP connection timeout in seconds
104 *
105 * @var int
106 */
107 public $smtp_timeout = 5;
vlakofff3994252013-02-19 01:45:23 +0100108
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530109 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530110 * SMTP persistent connection
111 *
112 * @var bool
113 */
114 public $smtp_keepalive = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200115
116 /**
117 * SMTP Encryption
118 *
Andrey Andreev89b67b42013-03-12 19:34:23 +0200119 * @var string empty, 'tls' or 'ssl'
Andrey Andreev597ea272012-11-01 22:56:26 +0200120 */
Andrey Andreev89b67b42013-03-12 19:34:23 +0200121 public $smtp_crypto = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200122
123 /**
124 * Whether to apply word-wrapping to the message body.
125 *
126 * @var bool
127 */
128 public $wordwrap = TRUE;
129
130 /**
131 * Number of characters to wrap at.
132 *
133 * @see CI_Email::$wordwrap
134 * @var int
135 */
136 public $wrapchars = 76;
137
138 /**
139 * Message format.
140 *
141 * @var string 'text' or 'html'
142 */
143 public $mailtype = 'text';
144
145 /**
146 * Character set (default: utf-8)
147 *
148 * @var string
149 */
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300150 public $charset = 'UTF-8';
Andrey Andreev597ea272012-11-01 22:56:26 +0200151
152 /**
Andrey Andreev597ea272012-11-01 22:56:26 +0200153 * Alternative message (for HTML messages only)
154 *
155 * @var string
156 */
157 public $alt_message = '';
158
159 /**
160 * Whether to validate e-mail addresses.
161 *
162 * @var bool
163 */
164 public $validate = FALSE;
165
166 /**
167 * X-Priority header value.
168 *
169 * @var int 1-5
170 */
171 public $priority = 3; // Default priority (1 - 5)
172
173 /**
174 * Newline character sequence.
175 * Use "\r\n" to comply with RFC 822.
176 *
177 * @link http://www.ietf.org/rfc/rfc822.txt
178 * @var string "\r\n" or "\n"
179 */
180 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
181
182 /**
183 * CRLF character sequence
184 *
185 * RFC 2045 specifies that for 'quoted-printable' encoding,
186 * "\r\n" must be used. However, it appears that some servers
187 * (even on the receiving end) don't handle it properly and
188 * switching to "\n", while improper, is the only solution
189 * that seems to work for all environments.
190 *
191 * @link http://www.ietf.org/rfc/rfc822.txt
192 * @var string
193 */
194 public $crlf = "\n";
195
196 /**
197 * Whether to use Delivery Status Notification.
198 *
199 * @var bool
200 */
201 public $dsn = FALSE;
202
203 /**
204 * Whether to send multipart alternatives.
205 * Yahoo! doesn't seem to like these.
206 *
207 * @var bool
208 */
209 public $send_multipart = TRUE;
210
211 /**
212 * Whether to send messages to BCC recipients in batches.
213 *
214 * @var bool
215 */
216 public $bcc_batch_mode = FALSE;
217
218 /**
219 * BCC Batch max number size.
220 *
221 * @see CI_Email::$bcc_batch_mode
222 * @var int
223 */
224 public $bcc_batch_size = 200;
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Whether PHP is running in safe mode. Initialized by the class constructor.
230 *
231 * @var bool
232 */
Andrey Andreev50814092012-03-01 12:36:12 +0200233 protected $_safe_mode = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200234
235 /**
236 * Subject header
237 *
238 * @var string
239 */
Andrey Andreev50814092012-03-01 12:36:12 +0200240 protected $_subject = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200241
242 /**
243 * Message body
244 *
245 * @var string
246 */
Andrey Andreev50814092012-03-01 12:36:12 +0200247 protected $_body = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200248
249 /**
250 * Final message body to be sent.
251 *
252 * @var string
253 */
Andrey Andreev50814092012-03-01 12:36:12 +0200254 protected $_finalbody = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200255
256 /**
Andrey Andreev597ea272012-11-01 22:56:26 +0200257 * Final headers to send
258 *
259 * @var string
260 */
Andrey Andreev50814092012-03-01 12:36:12 +0200261 protected $_header_str = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200262
263 /**
264 * SMTP Connection socket placeholder
265 *
266 * @var resource
267 */
Andrey Andreev50814092012-03-01 12:36:12 +0200268 protected $_smtp_connect = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200269
270 /**
271 * Mail encoding
272 *
273 * @var string '8bit' or '7bit'
274 */
Andrey Andreev50814092012-03-01 12:36:12 +0200275 protected $_encoding = '8bit';
Andrey Andreev597ea272012-11-01 22:56:26 +0200276
277 /**
278 * Whether to perform SMTP authentication
279 *
280 * @var bool
281 */
Andrey Andreev50814092012-03-01 12:36:12 +0200282 protected $_smtp_auth = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200283
284 /**
285 * Whether to send a Reply-To header
286 *
287 * @var bool
288 */
Andrey Andreev50814092012-03-01 12:36:12 +0200289 protected $_replyto_flag = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200290
291 /**
292 * Debug messages
293 *
294 * @see CI_Email::print_debugger()
295 * @var string
296 */
Andrey Andreev50814092012-03-01 12:36:12 +0200297 protected $_debug_msg = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200298
299 /**
300 * Recipients
301 *
302 * @var string[]
303 */
Andrey Andreev50814092012-03-01 12:36:12 +0200304 protected $_recipients = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200305
306 /**
307 * CC Recipients
308 *
309 * @var string[]
310 */
Andrey Andreev50814092012-03-01 12:36:12 +0200311 protected $_cc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200312
313 /**
314 * BCC Recipients
315 *
316 * @var string[]
317 */
Andrey Andreev50814092012-03-01 12:36:12 +0200318 protected $_bcc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200319
320 /**
321 * Message headers
322 *
323 * @var string[]
324 */
Andrey Andreev50814092012-03-01 12:36:12 +0200325 protected $_headers = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200326
327 /**
328 * Attachment data
329 *
330 * @var array
331 */
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300332 protected $_attachments = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200333
334 /**
335 * Valid $protocol values
336 *
337 * @see CI_Email::$protocol
338 * @var string[]
339 */
Andrey Andreev50814092012-03-01 12:36:12 +0200340 protected $_protocols = array('mail', 'sendmail', 'smtp');
Andrey Andreev597ea272012-11-01 22:56:26 +0200341
342 /**
343 * Base charsets
344 *
345 * Character sets valid for 7-bit encoding,
346 * excluding language suffix.
347 *
348 * @var string[]
349 */
350 protected $_base_charsets = array('us-ascii', 'iso-2022-');
351
352 /**
353 * Bit depths
354 *
355 * Valid mail encodings
356 *
357 * @see CI_Email::$_encoding
358 * @var string[]
359 */
Andrey Andreev50814092012-03-01 12:36:12 +0200360 protected $_bit_depths = array('7bit', '8bit');
Andrey Andreev597ea272012-11-01 22:56:26 +0200361
362 /**
363 * $priority translations
364 *
365 * Actual values to send with the X-Priority header
366 *
367 * @var string[]
368 */
Andrey Andreevdea61772014-02-24 16:36:25 +0200369 protected $_priorities = array(
370 1 => '1 (Highest)',
371 2 => '2 (High)',
372 3 => '3 (Normal)',
373 4 => '4 (Low)',
374 5 => '5 (Lowest)'
375 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000376
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300377 /**
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200378 * mbstring.func_overload flag
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300379 *
380 * @var bool
381 */
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200382 protected static $func_overload;
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300383
Andrey Andreev597ea272012-11-01 22:56:26 +0200384 // --------------------------------------------------------------------
385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 /**
387 * Constructor - Sets Email Preferences
388 *
389 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300390 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300391 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300392 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Andrey Andreev0b1fd2c2015-03-10 20:00:19 +0200394 public function __construct(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300396 $this->charset = config_item('charset');
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300397 $this->initialize($config);
Andrey Andreevf6274742014-02-20 18:05:58 +0200398 $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300399
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200400 isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300401
Andrey Andreev90726b82015-01-20 12:39:22 +0200402 log_message('info', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530404
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530405 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100406
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530407 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * Initialize preferences
409 *
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300410 * @param array $config
Andrew Podner4296a652012-12-17 07:51:15 -0500411 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300413 public function initialize(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300415 $this->clear();
416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 foreach ($config as $key => $val)
418 {
419 if (isset($this->$key))
420 {
421 $method = 'set_'.$key;
422
423 if (method_exists($this, $method))
424 {
425 $this->$method($val);
426 }
427 else
428 {
429 $this->$key = $val;
430 }
431 }
432 }
433
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300434 $this->charset = strtoupper($this->charset);
435 $this->_smtp_auth = isset($this->smtp_user[0], $this->smtp_pass[0]);
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000436
437 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * Initialize the Email Data
444 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800445 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500446 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000448 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200450 $this->_subject = '';
451 $this->_body = '';
452 $this->_finalbody = '';
453 $this->_header_str = '';
454 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500456 $this->_cc_array = array();
457 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 $this->_headers = array();
459 $this->_debug_msg = array();
460
Mickey Wubfc1cad2012-05-31 22:28:40 -0700461 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000462
463 if ($clear_attachments !== FALSE)
464 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300465 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Eric Barnes6113f542010-12-29 13:36:12 -0500467
Greg Akera769deb2010-11-10 15:47:29 -0600468 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Set FROM
475 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300476 * @param string $from
477 * @param string $name
478 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500479 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300481 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200483 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200485 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 if ($this->validate)
489 {
490 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300491 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200492 {
493 $this->validate_email($this->_str_to_array($return_path));
494 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100498 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 // only use Q encoding if there are characters that would require it
501 if ( ! preg_match('/[\200-\377]/', $name))
502 {
503 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000504 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506 else
507 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300508 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510 }
511
Mickey Wubfc1cad2012-05-31 22:28:40 -0700512 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200513
Andrey Andreev925dd902012-10-19 11:06:31 +0300514 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200515 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500516
Greg Akera769deb2010-11-10 15:47:29 -0600517 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Set Reply-to
524 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @param string
526 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500527 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000529 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200531 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200533 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536 if ($this->validate)
537 {
538 $this->validate_email($this->_str_to_array($replyto));
539 }
540
Andrey Andreevc70216d2016-01-20 17:25:13 +0200541 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreevc70216d2016-01-20 17:25:13 +0200543 // only use Q encoding if there are characters that would require it
544 if ( ! preg_match('/[\200-\377]/', $name))
545 {
546 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
547 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
548 }
549 else
550 {
551 $name = $this->_prep_q_encoding($name);
552 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554
Mickey Wubfc1cad2012-05-31 22:28:40 -0700555 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600557
558 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // --------------------------------------------------------------------
562
563 /**
564 * Set Recipients
565 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500567 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000569 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 $to = $this->_str_to_array($to);
572 $to = $this->clean_email($to);
573
574 if ($this->validate)
575 {
576 $this->validate_email($to);
577 }
578
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200579 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700581 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300584 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500585
Greg Akera769deb2010-11-10 15:47:29 -0600586 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
590
591 /**
592 * Set CC
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500595 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000597 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Andrey Andreev56454792012-05-17 14:32:19 +0300599 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000600
601 if ($this->validate)
602 {
603 $this->validate_email($cc);
604 }
605
Mickey Wubfc1cad2012-05-31 22:28:40 -0700606 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000607
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200608 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $this->_cc_array = $cc;
611 }
Eric Barnes6113f542010-12-29 13:36:12 -0500612
Greg Akera769deb2010-11-10 15:47:29 -0600613 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // --------------------------------------------------------------------
617
618 /**
619 * Set BCC
620 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 * @param string
622 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500623 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000625 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100627 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 $this->bcc_batch_mode = TRUE;
630 $this->bcc_batch_size = $limit;
631 }
632
Andrey Andreev56454792012-05-17 14:32:19 +0300633 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000634
635 if ($this->validate)
636 {
637 $this->validate_email($bcc);
638 }
639
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200640 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 $this->_bcc_array = $bcc;
643 }
644 else
645 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700646 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
Eric Barnes6113f542010-12-29 13:36:12 -0500648
Greg Akera769deb2010-11-10 15:47:29 -0600649 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
653
654 /**
655 * Set Email Subject
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500658 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000660 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
662 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700663 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600664 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Set Body
671 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500673 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000675 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200677 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200678
Andrey Andreevaf728622011-10-20 10:11:59 +0300679 /* strip slashes only if magic quotes is ON
680 if we do it with magic quotes OFF, it strips real, user-inputted chars.
681
682 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
683 it will probably not exist in future versions at all.
684 */
685 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200686 {
diegorivera33c32802011-10-19 02:56:15 -0200687 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200688 }
diegorivera33c32802011-10-19 02:56:15 -0200689
Greg Akera769deb2010-11-10 15:47:29 -0600690 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 // --------------------------------------------------------------------
694
695 /**
696 * Assign file attachments
697 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100698 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300699 * @param string $disposition = 'attachment'
700 * @param string $newname = NULL
701 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500702 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100704 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100706 if ($mime === '')
707 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100708 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100709 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100710 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100711 return FALSE;
712 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200713
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200714 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100715 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100716 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100717 return FALSE;
718 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100719
Petr Heralecky300e3f02014-01-10 11:49:11 +0100720 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100721 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100722 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100723 }
724 else
725 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100726 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100727 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200728
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300729 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100730 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300731 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100732 'type' => $mime,
Andrey Andreev3c422792016-06-06 09:44:50 +0300733 'content' => chunk_split(base64_encode($file_content)),
734 'multipart' => 'mixed'
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300735 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100736
Greg Akera769deb2010-11-10 15:47:29 -0600737 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200739
Petr Heralecky300e3f02014-01-10 11:49:11 +0100740 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200741
Petr Heralecky300e3f02014-01-10 11:49:11 +0100742 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200743 * Set and return attachment Content-ID
744 *
745 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100746 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100747 * @param string $filename
748 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100749 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200750 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100751 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100752 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100753 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100754 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100755 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300756 $this->_attachments[$i]['multipart'] = 'related';
Petr Heralecky230fca32014-01-10 12:55:57 +0100757 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100758 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100759 }
760 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200761
Petr Heralecky300e3f02014-01-10 11:49:11 +0100762 return FALSE;
763 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000764
765 // --------------------------------------------------------------------
766
767 /**
768 * Add a Header Item
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string
771 * @param string
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300772 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700774 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
florisluitenf55d5142013-06-07 17:20:06 +0300776 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300777 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 // --------------------------------------------------------------------
781
782 /**
783 * Convert a String to an Array
784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @param string
786 * @return array
787 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600788 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 if ( ! is_array($email))
791 {
Andrey Andreev56454792012-05-17 14:32:19 +0300792 return (strpos($email, ',') !== FALSE)
793 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
794 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Andrey Andreev56454792012-05-17 14:32:19 +0300796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 return $email;
798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 // --------------------------------------------------------------------
801
802 /**
803 * Set Multipart Value
804 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500806 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200808 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400810 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600811 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 // --------------------------------------------------------------------
815
816 /**
817 * Set Mailtype
818 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500820 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000822 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
Andrey Andreev56454792012-05-17 14:32:19 +0300824 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600825 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 /**
831 * Set Wordwrap
832 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400833 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500834 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000836 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400838 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600839 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // --------------------------------------------------------------------
843
844 /**
845 * Set Protocol
846 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500848 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000850 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200852 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600853 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
857
858 /**
859 * Set Priority
860 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200861 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500862 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000864 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200866 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600867 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 // --------------------------------------------------------------------
871
872 /**
873 * Set Newline Character
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500876 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000878 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200880 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600881 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 // --------------------------------------------------------------------
885
886 /**
887 * Set CRLF
888 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500890 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000892 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200894 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600895 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 // --------------------------------------------------------------------
899
900 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * Get the Message ID
902 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * @return string
904 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600905 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200907 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300908 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Get Mail Protocol
915 *
Andrey Andreev59c5b532012-03-01 14:09:51 +0200916 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 */
Andrey Andreevb9e45e32017-04-06 14:36:19 +0300918 protected function _get_protocol()
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
920 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200921 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Andrey Andreevb9e45e32017-04-06 14:36:19 +0300922 return $this->protocol;
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Get Mail Encoding
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @return string
931 */
Andrey Andreevb9e45e32017-04-06 14:36:19 +0300932 protected function _get_encoding()
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200934 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000935
936 foreach ($this->_base_charsets as $charset)
937 {
Andrey Andreevbdcafdf2017-03-28 17:31:09 +0300938 if (strpos($this->charset, $charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $this->_encoding = '7bit';
941 }
942 }
943
Andrey Andreevb9e45e32017-04-06 14:36:19 +0300944 return $this->_encoding;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
946
947 // --------------------------------------------------------------------
948
949 /**
950 * Get content type (text/html/attachment)
951 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 * @return string
953 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600954 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev56454792012-05-17 14:32:19 +0300956 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300958 return empty($this->_attachments) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Andrey Andreev3c422792016-06-06 09:44:50 +0300960 elseif ($this->mailtype === 'text' && ! empty($this->_attachments))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
962 return 'plain-attach';
963 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200964
965 return 'plain';
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 // --------------------------------------------------------------------
969
970 /**
971 * Set RFC 822 Date
972 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 * @return string
974 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600975 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200977 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300978 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200980 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000981
Andrey Andreev59c5b532012-03-01 14:09:51 +0200982 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
988 * Mime message
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @return string
991 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600992 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200994 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 +0000995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 // --------------------------------------------------------------------
998
999 /**
1000 * Validate Email Address
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @param string
1003 * @return bool
1004 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001005 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 if ( ! is_array($email))
1008 {
patworkb0707982011-04-08 15:10:05 +02001009 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 return FALSE;
1011 }
1012
1013 foreach ($email as $val)
1014 {
1015 if ( ! $this->valid_email($val))
1016 {
patworkb0707982011-04-08 15:10:05 +02001017 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return FALSE;
1019 }
1020 }
1021
1022 return TRUE;
1023 }
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Email Validation
1029 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * @param string
1031 * @return bool
1032 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001033 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001035 if (function_exists('idn_to_ascii') && strpos($email, '@'))
Andrey Andreev95496662014-06-01 00:00:13 +03001036 {
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001037 list($account, $domain) = explode('@', $email, 2);
Andrey Andreev221c0952018-01-22 10:29:19 +02001038 $domain = defined('INTL_IDNA_VARIANT_UTS46')
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001039 ? idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46)
1040 : idn_to_ascii($domain);
Andrey Andreev32f8c932018-06-12 16:25:44 +03001041
1042 if ($domain !== FALSE)
1043 {
1044 $email = $account.'@'.$domain;
1045 }
Andrey Andreev95496662014-06-01 00:00:13 +03001046 }
1047
Andrey Andreev580388b2012-06-27 15:43:46 +03001048 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * @param string
1057 * @return string
1058 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001059 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
1061 if ( ! is_array($email))
1062 {
Andrey Andreev56454792012-05-17 14:32:19 +03001063 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 }
1065
1066 $clean_email = array();
1067
1068 foreach ($email as $addy)
1069 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001070 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
1072
1073 return $clean_email;
1074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 // --------------------------------------------------------------------
1077
1078 /**
1079 * Build alternative plain text message
1080 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001081 * Provides the raw message for use in plain-text headers of
1082 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 * If the user hasn't specified his own alternative message
1084 * it creates one by stripping the HTML
1085 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 * @return string
1087 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001088 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001090 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001092 return ($this->wordwrap)
1093 ? $this->word_wrap($this->alt_message, 76)
1094 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
1096
Andrey Andreev56454792012-05-17 14:32:19 +03001097 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001098 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
1100 for ($i = 20; $i >= 3; $i--)
1101 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001102 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104
GDmac9bea4be2012-10-30 06:14:19 +01001105 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001106 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001107
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001108 return ($this->wordwrap)
1109 ? $this->word_wrap($body, 76)
1110 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 // --------------------------------------------------------------------
1114
1115 /**
1116 * Word Wrap
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001119 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @return string
1121 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001122 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001124 // Set the character limit, if not already present
1125 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001127 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 }
1129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // Standardize newlines
1131 if (strpos($str, "\r") !== FALSE)
1132 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001133 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 }
1135
GDmac9bea4be2012-10-30 06:14:19 +01001136 // Reduce multiple spaces at end of line
1137 $str = preg_replace('| +\n|', "\n", $str);
1138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // If the current word is surrounded by {unwrap} tags we'll
1140 // strip the entire chunk and replace it with a marker.
1141 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001142 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001144 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001146 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001147 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 }
1149 }
1150
vlakofff0ab8132014-05-20 10:32:53 +02001151 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001153 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $str = wordwrap($str, $charlim, "\n", FALSE);
1155
1156 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001157 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 foreach (explode("\n", $str) as $line)
1159 {
1160 // Is the line within the allowed character count?
1161 // If so we'll join it to the output and continue
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001162 if (self::strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 $output .= $line.$this->newline;
1165 continue;
1166 }
1167
1168 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001169 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
1171 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001172 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 break;
1175 }
1176
1177 // Trim the word down
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001178 $temp .= self::substr($line, 0, $charlim - 1);
1179 $line = self::substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001181 while (self::strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001182
1183 // If $temp contains data it means we had to split up an over-length
1184 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001185 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001187 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 }
1189
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001190 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 }
1192
1193 // Put our markers back
1194 if (count($unwrap) > 0)
1195 {
1196 foreach ($unwrap as $key => $val)
1197 {
Andrey Andreev56454792012-05-17 14:32:19 +03001198 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
1200 }
1201
1202 return $output;
1203 }
Barry Mienydd671972010-10-04 16:33:58 +02001204
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 // --------------------------------------------------------------------
1206
1207 /**
1208 * Build final headers
1209 *
Andrey Andreev19277092016-08-22 11:34:56 +03001210 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001212 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
Andrey Andreev6a5b5e72017-01-06 13:45:55 +02001214 $this->set_header('User-Agent', $this->useragent);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001215 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1216 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001217 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001218 $this->set_header('Message-ID', $this->_get_message_id());
1219 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 // --------------------------------------------------------------------
1223
1224 /**
1225 * Write Headers as a string
1226 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 * @return void
1228 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001229 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001231 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301233 if (isset($this->_headers['Subject']))
1234 {
1235 $this->_subject = $this->_headers['Subject'];
1236 unset($this->_headers['Subject']);
1237 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
1239
1240 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001241 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001242
Pascal Kriete14287f32011-02-14 13:39:34 -05001243 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 $val = trim($val);
1246
Alex Bilbied261b1e2012-06-02 11:12:16 +01001247 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
Andrey Andreev56454792012-05-17 14:32:19 +03001249 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
1251 }
1252
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001253 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Derek Jones1d890882009-02-10 20:32:14 +00001255 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 }
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Build Final Body and attachments
1263 *
buhayc6da1ef2013-04-18 09:06:49 -07001264 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001266 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001268 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 {
1270 $this->_body = $this->word_wrap($this->_body);
1271 }
1272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 $this->_write_headers();
1274
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001275 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001276 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001277
1278 switch ($this->_get_content_type())
1279 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001280 case 'plain':
Derek Allard2067d1a2008-11-13 22:59:24 +00001281
Andrey Andreev56454792012-05-17 14:32:19 +03001282 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1283 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001284
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001285 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 {
1287 $this->_header_str .= $hdr;
1288 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 }
Brandon Jones485d7412010-11-09 16:38:17 -05001290 else
1291 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001292 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001293 }
Eric Barnes6113f542010-12-29 13:36:12 -05001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 return;
1296
Andrey Andreev3c422792016-06-06 09:44:50 +03001297 case 'html':
Derek Allard2067d1a2008-11-13 22:59:24 +00001298
1299 if ($this->send_multipart === FALSE)
1300 {
Andrey Andreev56454792012-05-17 14:32:19 +03001301 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001302 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 }
1304 else
1305 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001306 $boundary = uniqid('B_ALT_');
1307 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001308
Andrey Andreev56454792012-05-17 14:32:19 +03001309 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001310 .'--'.$boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001311
Andrey Andreev56454792012-05-17 14:32:19 +03001312 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1313 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001314 .$this->_get_alt_message().$this->newline.$this->newline
1315 .'--'.$boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001316
Andrey Andreev56454792012-05-17 14:32:19 +03001317 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1318 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 }
Eric Barnes6113f542010-12-29 13:36:12 -05001320
Andrey Andreev56454792012-05-17 14:32:19 +03001321 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001322
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001323 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001326 }
1327 else
1328 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001329 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 }
1331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 if ($this->send_multipart !== FALSE)
1333 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001334 $this->_finalbody .= '--'.$boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 }
1336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 return;
1338
Andrey Andreev3c422792016-06-06 09:44:50 +03001339 case 'plain-attach':
Derek Allard2067d1a2008-11-13 22:59:24 +00001340
Andrey Andreev3c422792016-06-06 09:44:50 +03001341 $boundary = uniqid('B_ATC_');
1342 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001343
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001344 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
1346 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001347 }
1348
Andrey Andreev2b956af2013-08-07 14:32:56 +03001349 $body .= $this->_get_mime_message().$this->newline
1350 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001351 .'--'.$boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001352 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001353 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1354 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001355 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001356
Andrey Andreev3c422792016-06-06 09:44:50 +03001357 $this->_append_attachments($body, $boundary);
Derek Allard2067d1a2008-11-13 22:59:24 +00001358
Andrey Andreev3c422792016-06-06 09:44:50 +03001359 break;
1360 case 'html-attach':
1361
1362 $alt_boundary = uniqid('B_ALT_');
1363 $last_boundary = NULL;
1364
1365 if ($this->_attachments_have_multipart('mixed'))
1366 {
1367 $atc_boundary = uniqid('B_ATC_');
1368 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$atc_boundary.'"';
1369 $last_boundary = $atc_boundary;
1370 }
1371
1372 if ($this->_attachments_have_multipart('related'))
1373 {
1374 $rel_boundary = uniqid('B_REL_');
1375 $rel_boundary_header = 'Content-Type: multipart/related; boundary="'.$rel_boundary.'"';
1376
1377 if (isset($last_boundary))
1378 {
1379 $body .= '--'.$last_boundary.$this->newline.$rel_boundary_header;
1380 }
1381 else
1382 {
1383 $hdr .= $rel_boundary_header;
1384 }
1385
1386 $last_boundary = $rel_boundary;
1387 }
Eric Barnes6113f542010-12-29 13:36:12 -05001388
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001389 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
1391 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 }
1393
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001394 self::strlen($body) && $body .= $this->newline.$this->newline;
Andrey Andreev56454792012-05-17 14:32:19 +03001395 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001396 .'--'.$last_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001397
Andrey Andreev3c422792016-06-06 09:44:50 +03001398 .'Content-Type: multipart/alternative; boundary="'.$alt_boundary.'"'.$this->newline.$this->newline
1399 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001400
Andrey Andreev56454792012-05-17 14:32:19 +03001401 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1402 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001403 .$this->_get_alt_message().$this->newline.$this->newline
1404 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001405
Andrey Andreev56454792012-05-17 14:32:19 +03001406 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1407 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001408
Andrey Andreev56454792012-05-17 14:32:19 +03001409 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001410 .'--'.$alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001411
Andrey Andreev3c422792016-06-06 09:44:50 +03001412 if ( ! empty($rel_boundary))
1413 {
1414 $body .= $this->newline.$this->newline;
1415 $this->_append_attachments($body, $rel_boundary, 'related');
1416 }
1417
1418 // multipart/mixed attachments
1419 if ( ! empty($atc_boundary))
1420 {
1421 $body .= $this->newline.$this->newline;
1422 $this->_append_attachments($body, $atc_boundary, 'mixed');
1423 }
1424
1425 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 }
1427
Andrey Andreev2b956af2013-08-07 14:32:56 +03001428 $this->_finalbody = ($this->_get_protocol() === 'mail')
1429 ? $body
1430 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001431
buhay466e8082013-04-17 14:25:34 -07001432 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 }
Barry Mienydd671972010-10-04 16:33:58 +02001434
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 // --------------------------------------------------------------------
1436
Andrey Andreev3c422792016-06-06 09:44:50 +03001437 protected function _attachments_have_multipart($type)
1438 {
1439 foreach ($this->_attachments as &$attachment)
1440 {
1441 if ($attachment['multipart'] === $type)
1442 {
1443 return TRUE;
1444 }
1445 }
1446
1447 return FALSE;
1448 }
1449
1450 // --------------------------------------------------------------------
1451
1452 /**
1453 * Prepares attachment string
1454 *
1455 * @param string $body Message body to append to
1456 * @param string $boundary Multipart boundary
1457 * @param string $multipart When provided, only attachments of this type will be processed
1458 * @return string
1459 */
1460 protected function _append_attachments(&$body, $boundary, $multipart = null)
1461 {
1462 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
1463 {
1464 if (isset($multipart) && $this->_attachments[$i]['multipart'] !== $multipart)
1465 {
1466 continue;
1467 }
1468
1469 $name = isset($this->_attachments[$i]['name'][1])
1470 ? $this->_attachments[$i]['name'][1]
1471 : basename($this->_attachments[$i]['name'][0]);
1472
1473 $body .= '--'.$boundary.$this->newline
1474 .'Content-Type: '.$this->_attachments[$i]['type'].'; name="'.$name.'"'.$this->newline
1475 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
1476 .'Content-Transfer-Encoding: base64'.$this->newline
Andrey Andreev9b0f5fa2016-08-01 13:54:06 +03001477 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline)
1478 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001479 .$this->_attachments[$i]['content'].$this->newline;
1480 }
1481
1482 // $name won't be set if no attachments were appended,
1483 // and therefore a boundary wouldn't be necessary
1484 empty($name) OR $body .= '--'.$boundary.'--';
1485 }
1486
1487 // --------------------------------------------------------------------
1488
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 /**
1490 * Prep Quoted Printable
1491 *
1492 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1493 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1494 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 * @return string
1497 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001498 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001500 // ASCII code numbers for "safe" characters that can always be
1501 // used literally, without encoding, as described in RFC 2049.
1502 // http://www.ietf.org/rfc/rfc2049.txt
1503 static $ascii_safe_chars = array(
1504 // ' ( ) + , - . / : = ?
1505 39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
1506 // numbers
1507 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
1508 // upper-case letters
1509 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
1510 // lower-case letters
1511 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122
1512 );
1513
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001514 // We are intentionally wrapping so mail servers will encode characters
1515 // properly and MUAs will behave, so {unwrap} must go!
1516 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1517
Andrey Andreev683b34d2012-10-09 15:00:00 +03001518 // RFC 2045 specifies CRLF as "\r\n".
1519 // However, many developers choose to override that and violate
1520 // the RFC rules due to (apparently) a bug in MS Exchange,
1521 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001522 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001523 {
Andrey Andreeva8382792016-07-28 16:40:12 +03001524 return quoted_printable_encode($str);
Andrey Andreev683b34d2012-10-09 15:00:00 +03001525 }
1526
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001527 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001528 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001529
1530 // Standardize newlines
1531 if (strpos($str, "\r") !== FALSE)
1532 {
1533 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1534 }
1535
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 $escape = '=';
1537 $output = '';
1538
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001539 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001541 $length = self::strlen($line);
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 $temp = '';
1543
1544 // Loop through each character in the line to add soft-wrap
1545 // characters at the end of a line " =\r\n" and add the newly
1546 // processed line(s) to the output (see comment on $crlf class property)
1547 for ($i = 0; $i < $length; $i++)
1548 {
1549 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001550 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 $ascii = ord($char);
1552
1553 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001554 if ($ascii === 32 OR $ascii === 9)
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001556 if ($i === ($length - 1))
1557 {
1558 $char = $escape.sprintf('%02s', dechex($ascii));
1559 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001561 // DO NOT move this below the $ascii_safe_chars line!
1562 //
1563 // = (equals) signs are allowed by RFC2049, but must be encoded
1564 // as they are the encoding delimiter!
1565 elseif ($ascii === 61)
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001567 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001569 elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
1570 {
1571 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
1572 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001573
1574 // If we're at the character limit, add the line to the output,
1575 // reset our temp variable, and keep on chuggin'
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001576 if ((self::strlen($temp) + self::strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 {
1578 $output .= $temp.$escape.$this->crlf;
1579 $temp = '';
1580 }
1581
1582 // Add the character to our temporary line
1583 $temp .= $char;
1584 }
1585
1586 // Add our completed line to the output
1587 $output .= $temp.$this->crlf;
1588 }
1589
1590 // get rid of extra CRLF tacked onto the end
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001591 return self::substr($output, 0, self::strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 }
1593
1594 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001595
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 /**
1597 * Prep Q Encoding
1598 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001599 * Performs "Q Encoding" on a string for use in email headers.
1600 * It's related but not identical to quoted-printable, so it has its
1601 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001603 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001604 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001606 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001608 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001609
Andrey Andreev925dd902012-10-19 11:06:31 +03001610 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001612 // Note: We used to have mb_encode_mimeheader() as the first choice
1613 // here, but it turned out to be buggy and unreliable. DO NOT
1614 // re-add it! -- Narf
1615 if (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001616 {
1617 $output = @iconv_mime_encode('', $str,
1618 array(
1619 'scheme' => 'Q',
1620 'line-length' => 76,
1621 'input-charset' => $this->charset,
1622 'output-charset' => $this->charset,
1623 'line-break-chars' => $this->crlf
1624 )
1625 );
1626
1627 // There are reports that iconv_mime_encode() might fail and return FALSE
1628 if ($output !== FALSE)
1629 {
1630 // iconv_mime_encode() will always put a header field name.
1631 // We've passed it an empty one, but it still prepends our
1632 // encoded string with ': ', so we need to strip it.
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001633 return self::substr($output, 2);
Andrey Andreev925dd902012-10-19 11:06:31 +03001634 }
1635
1636 $chars = iconv_strlen($str, 'UTF-8');
1637 }
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001638 elseif (MB_ENABLED === TRUE)
1639 {
1640 $chars = mb_strlen($str, 'UTF-8');
1641 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 }
1643
Andrey Andreev925dd902012-10-19 11:06:31 +03001644 // We might already have this set for UTF-8
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001645 isset($chars) OR $chars = self::strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001646
Andrey Andreev925dd902012-10-19 11:06:31 +03001647 $output = '=?'.$this->charset.'?Q?';
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001648 for ($i = 0, $length = self::strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001649 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001650 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001651 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1652 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001653
Andrey Andreev925dd902012-10-19 11:06:31 +03001654 // RFC 2045 sets a limit of 76 characters per line.
1655 // We'll append ?= to the end of each line though.
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001656 if ($length + ($l = self::strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001658 $output .= '?='.$this->crlf // EOL
1659 .' =?'.$this->charset.'?Q?'.$chr; // New line
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001660 $length = 6 + self::strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001662 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001664 $output .= $chr;
1665 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 }
1668
Andrey Andreev925dd902012-10-19 11:06:31 +03001669 // End the header
1670 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
1672
1673 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001674
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 /**
1676 * Send Email
1677 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001678 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 * @return bool
1680 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001681 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001683 if ( ! isset($this->_headers['From']))
Michael Granados0b85d6b2014-11-09 02:43:48 -02001684 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001685 $this->_set_error_message('lang:email_no_from');
Michael Granados0b85d6b2014-11-09 02:43:48 -02001686 return FALSE;
1687 }
1688
Alex Bilbied261b1e2012-06-02 11:12:16 +01001689 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 {
1691 $this->reply_to($this->_headers['From']);
1692 }
1693
Andrey Andreev76f15c92012-03-01 13:05:07 +02001694 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1695 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1696 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 {
patworkb0707982011-04-08 15:10:05 +02001698 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 return FALSE;
1700 }
1701
1702 $this->_build_headers();
1703
Andrey Andreev76f15c92012-03-01 13:05:07 +02001704 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001706 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001707
Alex Bilbiea87aab32012-07-30 09:50:37 +01001708 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001709 {
1710 $this->clear();
1711 }
1712
Alex Bilbieb901e732012-07-30 09:44:57 +01001713 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 }
1715
buhay466e8082013-04-17 14:25:34 -07001716 if ($this->_build_message() === FALSE)
1717 {
1718 return FALSE;
1719 }
buhayc6da1ef2013-04-18 09:06:49 -07001720
Alex Bilbieb901e732012-07-30 09:44:57 +01001721 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001722
Alex Bilbiea87aab32012-07-30 09:50:37 +01001723 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001724 {
1725 $this->clear();
1726 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001727
Alex Bilbieb901e732012-07-30 09:44:57 +01001728 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 }
Barry Mienydd671972010-10-04 16:33:58 +02001730
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 // --------------------------------------------------------------------
1732
1733 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001734 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001736 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001738 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001740 $float = $this->bcc_batch_size - 1;
1741 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 $chunk = array();
1743
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001744 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 {
1746 if (isset($this->_bcc_array[$i]))
1747 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001748 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 }
1750
Alex Bilbied261b1e2012-06-02 11:12:16 +01001751 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001753 $chunk[] = self::substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001754 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001755 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 }
1757
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001758 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001760 $chunk[] = self::substr($set, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 }
1762 }
1763
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001764 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
1766 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001767
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001768 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001769
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001770 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001772 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 }
1774 else
1775 {
1776 $this->_bcc_array = $bcc;
1777 }
1778
buhay466e8082013-04-17 14:25:34 -07001779 if ($this->_build_message() === FALSE)
1780 {
1781 return FALSE;
1782 }
buhayc6da1ef2013-04-18 09:06:49 -07001783
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 $this->_spool_email();
1785 }
1786 }
Barry Mienydd671972010-10-04 16:33:58 +02001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 // --------------------------------------------------------------------
1789
1790 /**
1791 * Unwrap special elements
1792 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 * @return void
1794 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001795 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 {
Andrey Andreev56454792012-05-17 14:32:19 +03001797 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 }
Barry Mienydd671972010-10-04 16:33:58 +02001799
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 // --------------------------------------------------------------------
1801
1802 /**
1803 * Strip line-breaks via callback
1804 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001805 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 * @return string
1807 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001808 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
1810 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1811 {
1812 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1813 }
1814
1815 return $matches[1];
1816 }
Barry Mienydd671972010-10-04 16:33:58 +02001817
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 // --------------------------------------------------------------------
1819
1820 /**
1821 * Spool mail to the mail server
1822 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 * @return bool
1824 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001825 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
1827 $this->_unwrap_specials();
1828
Andrey Andreevb9e45e32017-04-06 14:36:19 +03001829 $protocol = $this->_get_protocol();
1830 $method = '_send_with_'.$protocol;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001831 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 {
Andrey Andreevb9e45e32017-04-06 14:36:19 +03001833 $this->_set_error_message('lang:email_send_failure_'.($protocol === 'mail' ? 'phpmail' : $protocol));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001834 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 }
1836
Andrey Andreevb9e45e32017-04-06 14:36:19 +03001837 $this->_set_error_message('lang:email_sent', $protocol);
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 return TRUE;
1839 }
Barry Mienydd671972010-10-04 16:33:58 +02001840
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 // --------------------------------------------------------------------
1842
1843 /**
Andrey Andreeva9600162017-01-05 17:35:28 +02001844 * Validate email for shell
1845 *
1846 * Applies stricter, shell-safe validation to email addresses.
1847 * Introduced to prevent RCE via sendmail's -f option.
1848 *
1849 * @see https://github.com/bcit-ci/CodeIgniter/issues/4963
1850 * @see https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36
1851 * @license https://creativecommons.org/publicdomain/zero/1.0/ CC0 1.0, Public Domain
1852 *
1853 * Credits for the base concept go to Paul Buonopane <paul@namepros.com>
1854 *
1855 * @param string $email
1856 * @return bool
1857 */
1858 protected function _validate_email_for_shell(&$email)
1859 {
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001860 if (function_exists('idn_to_ascii') && strpos($email, '@'))
Andrey Andreeva9600162017-01-05 17:35:28 +02001861 {
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001862 list($account, $domain) = explode('@', $email, 2);
Andrey Andreev7c71b8f2018-01-27 06:09:02 +02001863 $domain = defined('INTL_IDNA_VARIANT_UTS46')
Andrey Andreev4fd2d492017-11-09 20:38:33 +02001864 ? idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46)
1865 : idn_to_ascii($domain);
Andrey Andreev32f8c932018-06-12 16:25:44 +03001866
1867 if ($domain !== FALSE)
1868 {
1869 $email = $account.'@'.$domain;
1870 }
Andrey Andreeva9600162017-01-05 17:35:28 +02001871 }
1872
1873 return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+@[a-z0-9.-]{1,253}\z#i', $email));
1874 }
1875
1876 // --------------------------------------------------------------------
1877
1878 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 * Send using mail()
1880 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 * @return bool
1882 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001883 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001885 if (is_array($this->_recipients))
1886 {
1887 $this->_recipients = implode(', ', $this->_recipients);
1888 }
1889
Andrey Andreeva9600162017-01-05 17:35:28 +02001890 // _validate_email_for_shell() below accepts by reference,
1891 // so this needs to be assigned to a variable
1892 $from = $this->clean_email($this->_headers['Return-Path']);
1893
1894 if ($this->_safe_mode === TRUE || ! $this->_validate_email_for_shell($from))
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001896 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 }
1898 else
1899 {
1900 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1901 // we've encountered servers that seem to require it to be in place.
Andrey Andreeva9600162017-01-05 17:35:28 +02001902 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 }
1904 }
Barry Mienydd671972010-10-04 16:33:58 +02001905
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 // --------------------------------------------------------------------
1907
1908 /**
1909 * Send using Sendmail
1910 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 * @return bool
1912 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001913 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 {
Andrey Andreeva9600162017-01-05 17:35:28 +02001915 // _validate_email_for_shell() below accepts by reference,
1916 // so this needs to be assigned to a variable
1917 $from = $this->clean_email($this->_headers['From']);
1918 if ($this->_validate_email_for_shell($from))
Derek Jones4cefaa42009-04-29 19:13:56 +00001919 {
Andrey Andreeva9600162017-01-05 17:35:28 +02001920 $from = '-f '.$from;
1921 }
1922 else
1923 {
1924 $from = '';
1925 }
1926
1927 // is popen() enabled?
1928 if ( ! function_usable('popen') OR FALSE === ($fp = @popen($this->mailpath.' -oi '.$from.' -t', 'w')))
1929 {
1930 // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001931 return FALSE;
1932 }
Derek Jones71141ce2010-03-02 16:41:20 -06001933
Derek Jonesc630bcf2008-11-17 21:09:45 +00001934 fputs($fp, $this->_header_str);
1935 fputs($fp, $this->_finalbody);
1936
Barry Mienydd671972010-10-04 16:33:58 +02001937 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001938
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001939 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 {
patworkb0707982011-04-08 15:10:05 +02001941 $this->_set_error_message('lang:email_exit_status', $status);
1942 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 return FALSE;
1944 }
1945
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 return TRUE;
1947 }
Barry Mienydd671972010-10-04 16:33:58 +02001948
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 // --------------------------------------------------------------------
1950
1951 /**
1952 * Send using SMTP
1953 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 * @return bool
1955 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001956 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301957 {
1958 if ($this->smtp_host === '')
1959 {
1960 $this->_set_error_message('lang:email_no_hostname');
1961 return FALSE;
1962 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001963
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301964 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1965 {
1966 return FALSE;
1967 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001968
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001969 if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1970 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001971 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001972 return FALSE;
1973 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001974
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301975 foreach ($this->_recipients as $val)
1976 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001977 if ( ! $this->_send_command('to', $val))
1978 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001979 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001980 return FALSE;
1981 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301982 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001983
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301984 if (count($this->_cc_array) > 0)
1985 {
1986 foreach ($this->_cc_array as $val)
1987 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001988 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301989 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001990 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001991 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301992 }
1993 }
1994 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001995
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301996 if (count($this->_bcc_array) > 0)
1997 {
1998 foreach ($this->_bcc_array as $val)
1999 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002000 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302001 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002002 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002003 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302004 }
2005 }
2006 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002007
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002008 if ( ! $this->_send_command('data'))
2009 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002010 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002011 return FALSE;
2012 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002013
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302014 // perform dot transformation on any lines that begin with a dot
2015 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00002016
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302017 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00002018
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302019 $reply = $this->_get_smtp_data();
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302020 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00002021
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002022 $this->_smtp_end();
2023
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302024 if (strpos($reply, '250') !== 0)
2025 {
2026 $this->_set_error_message('lang:email_smtp_error', $reply);
2027 return FALSE;
2028 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002029
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302030 return TRUE;
2031 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002032
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302033 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03002034
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302035 /**
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002036 * SMTP End
2037 *
2038 * Shortcut to send RSET or QUIT depending on keep-alive
2039 *
2040 * @return void
2041 */
2042 protected function _smtp_end()
2043 {
2044 ($this->smtp_keepalive)
2045 ? $this->_send_command('reset')
2046 : $this->_send_command('quit');
2047 }
2048
2049 // --------------------------------------------------------------------
2050
2051 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302052 * SMTP Connect
2053 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302054 * @return string
2055 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302056 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302057 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302058 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302059 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302060 return TRUE;
2061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002062
Andrey Andreev89b67b42013-03-12 19:34:23 +02002063 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002064
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302065 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
2066 $this->smtp_port,
2067 $errno,
2068 $errstr,
2069 $this->smtp_timeout);
2070
2071 if ( ! is_resource($this->_smtp_connect))
2072 {
2073 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
2074 return FALSE;
2075 }
2076
2077 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
2078 $this->_set_error_message($this->_get_smtp_data());
2079
2080 if ($this->smtp_crypto === 'tls')
2081 {
2082 $this->_send_command('hello');
2083 $this->_send_command('starttls');
2084
Andrey Andreev329dd032018-01-22 10:54:10 +02002085 /**
2086 * STREAM_CRYPTO_METHOD_TLS_CLIENT is quite the mess ...
2087 *
2088 * - On PHP <5.6 it doesn't even mean TLS, but SSL 2.0, and there's no option to use actual TLS
2089 * - On PHP 5.6.0-5.6.6, >=7.2 it means negotiation with any of TLS 1.0, 1.1, 1.2
2090 * - On PHP 5.6.7-7.1.* it means only TLS 1.0
2091 *
2092 * We want the negotiation, so we'll force it below ...
2093 */
2094 $method = is_php('5.6')
2095 ? STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
2096 : STREAM_CRYPTO_METHOD_TLS_CLIENT;
2097 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, $method);
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302098
2099 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302100 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302101 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302102 return FALSE;
2103 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302104 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302105
2106 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302107 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002108
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302109 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002110
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302111 /**
2112 * Send SMTP command
2113 *
2114 * @param string
2115 * @param string
Andrey Andreev19277092016-08-22 11:34:56 +03002116 * @return bool
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302117 */
2118 protected function _send_command($cmd, $data = '')
2119 {
2120 switch ($cmd)
2121 {
2122 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002123
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302124 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2125 {
2126 $this->_send_data('EHLO '.$this->_get_hostname());
2127 }
2128 else
2129 {
2130 $this->_send_data('HELO '.$this->_get_hostname());
2131 }
Radu Potopbbf04b02011-09-28 13:57:51 +03002132
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302133 $resp = 250;
2134 break;
2135 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002136
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302137 $this->_send_data('STARTTLS');
2138 $resp = 220;
2139 break;
2140 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03002141
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302142 $this->_send_data('MAIL FROM:<'.$data.'>');
2143 $resp = 250;
2144 break;
2145 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03002146
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302147 if ($this->dsn)
2148 {
2149 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2150 }
2151 else
2152 {
2153 $this->_send_data('RCPT TO:<'.$data.'>');
2154 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002155
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302156 $resp = 250;
2157 break;
2158 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002159
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302160 $this->_send_data('DATA');
2161 $resp = 354;
2162 break;
2163 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002164
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302165 $this->_send_data('RSET');
2166 $resp = 250;
2167 break;
2168 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002169
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302170 $this->_send_data('QUIT');
2171 $resp = 221;
2172 break;
2173 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002174
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302175 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002176
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302177 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002178
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002179 if ((int) self::substr($reply, 0, 3) !== $resp)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302180 {
2181 $this->_set_error_message('lang:email_smtp_error', $reply);
2182 return FALSE;
2183 }
Barry Mienydd671972010-10-04 16:33:58 +02002184
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302185 if ($cmd === 'quit')
2186 {
2187 fclose($this->_smtp_connect);
2188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002189
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302190 return TRUE;
2191 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002192
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302193 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002194
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302195 /**
2196 * SMTP Authenticate
2197 *
2198 * @return bool
2199 */
2200 protected function _smtp_authenticate()
2201 {
2202 if ( ! $this->_smtp_auth)
2203 {
2204 return TRUE;
2205 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002206
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302207 if ($this->smtp_user === '' && $this->smtp_pass === '')
2208 {
2209 $this->_set_error_message('lang:email_no_smtp_unpw');
2210 return FALSE;
2211 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002212
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302213 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002214
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302215 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002216
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002217 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302218 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302219 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302220 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002221 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302222 {
2223 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2224 return FALSE;
2225 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002226
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302227 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002228
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302229 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002230
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302231 if (strpos($reply, '334') !== 0)
2232 {
2233 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2234 return FALSE;
2235 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002236
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302237 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002238
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302239 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302240
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302241 if (strpos($reply, '235') !== 0)
2242 {
2243 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2244 return FALSE;
2245 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302246
Andrey Andreev84253192016-05-09 12:24:52 +03002247 if ($this->smtp_keepalive)
2248 {
2249 $this->_smtp_auth = FALSE;
2250 }
2251
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302252 return TRUE;
2253 }
Barry Mienydd671972010-10-04 16:33:58 +02002254
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 // --------------------------------------------------------------------
2256
2257 /**
2258 * Send SMTP data
2259 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002260 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 * @return bool
2262 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002263 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002265 $data .= $this->newline;
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002266 for ($written = $timestamp = 0, $length = self::strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002267 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002268 if (($result = fwrite($this->_smtp_connect, self::substr($data, $written))) === FALSE)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002269 {
2270 break;
2271 }
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002272 // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2273 elseif ($result === 0)
2274 {
2275 if ($timestamp === 0)
2276 {
2277 $timestamp = time();
2278 }
2279 elseif ($timestamp < (time() - $this->smtp_timeout))
2280 {
2281 $result = FALSE;
2282 break;
2283 }
2284
2285 usleep(250000);
2286 continue;
2287 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +02002288
2289 $timestamp = 0;
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002290 }
2291
2292 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 {
patworkb0707982011-04-08 15:10:05 +02002294 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 return FALSE;
2296 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002297
2298 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 }
Barry Mienydd671972010-10-04 16:33:58 +02002300
Derek Allard2067d1a2008-11-13 22:59:24 +00002301 // --------------------------------------------------------------------
2302
2303 /**
2304 * Get SMTP data
2305 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 * @return string
2307 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002308 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 {
Andrey Andreev56454792012-05-17 14:32:19 +03002310 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002311
2312 while ($str = fgets($this->_smtp_connect, 512))
2313 {
2314 $data .= $str;
2315
Alex Bilbied261b1e2012-06-02 11:12:16 +01002316 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 {
2318 break;
2319 }
2320 }
2321
2322 return $data;
2323 }
Barry Mienydd671972010-10-04 16:33:58 +02002324
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 // --------------------------------------------------------------------
2326
2327 /**
2328 * Get Hostname
Andrey Andreev396eb892015-02-06 14:50:10 +02002329 *
2330 * There are only two legal types of hostname - either a fully
2331 * qualified domain name (eg: "mail.example.com") or an IP literal
2332 * (eg: "[1.2.3.4]").
2333 *
2334 * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
James Wade3245af42015-02-06 11:48:51 +00002335 * @link http://cbl.abuseat.org/namingproblems.html
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 * @return string
2337 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002338 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 {
Andrey Andreev396eb892015-02-06 14:50:10 +02002340 if (isset($_SERVER['SERVER_NAME']))
2341 {
2342 return $_SERVER['SERVER_NAME'];
2343 }
2344
2345 return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 }
Barry Mienydd671972010-10-04 16:33:58 +02002347
Derek Allard2067d1a2008-11-13 22:59:24 +00002348 // --------------------------------------------------------------------
2349
2350 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002351 * Get Debug Message
2352 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002353 * @param array $include List of raw data chunks to include in the output
2354 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002355 * @return string
2356 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002357 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002358 {
2359 $msg = '';
2360
2361 if (count($this->_debug_msg) > 0)
2362 {
2363 foreach ($this->_debug_msg as $val)
2364 {
2365 $msg .= $val;
2366 }
2367 }
2368
Andrey Andreev61797f62012-11-26 16:15:12 +02002369 // Determine which parts of our raw data needs to be printed
2370 $raw_data = '';
2371 is_array($include) OR $include = array($include);
2372
2373 if (in_array('headers', $include, TRUE))
2374 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002375 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002376 }
2377
2378 if (in_array('subject', $include, TRUE))
2379 {
2380 $raw_data .= htmlspecialchars($this->_subject)."\n";
2381 }
2382
2383 if (in_array('body', $include, TRUE))
2384 {
2385 $raw_data .= htmlspecialchars($this->_finalbody);
2386 }
2387
2388 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002389 }
Barry Mienydd671972010-10-04 16:33:58 +02002390
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 // --------------------------------------------------------------------
2392
2393 /**
2394 * Set Message
2395 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002396 * @param string $msg
2397 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002398 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002399 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002400 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002401 {
2402 $CI =& get_instance();
2403 $CI->lang->load('email');
2404
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002405 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002406 {
Andrey Andreev56454792012-05-17 14:32:19 +03002407 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002408 }
2409 else
2410 {
Andrey Andreev56454792012-05-17 14:32:19 +03002411 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002412 }
2413 }
Barry Mienydd671972010-10-04 16:33:58 +02002414
Derek Allard2067d1a2008-11-13 22:59:24 +00002415 // --------------------------------------------------------------------
2416
2417 /**
2418 * Mime Types
2419 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002420 * @param string
2421 * @return string
2422 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002423 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002424 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002425 $ext = strtolower($ext);
2426
vlakoff66c7bb42014-05-19 13:45:12 +02002427 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002428
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002429 if (isset($mimes[$ext]))
2430 {
2431 return is_array($mimes[$ext])
2432 ? current($mimes[$ext])
2433 : $mimes[$ext];
2434 }
2435
2436 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002437 }
2438
Andrey Andreev8c95c3d2016-05-09 12:35:41 +03002439 // --------------------------------------------------------------------
2440
2441 /**
2442 * Destructor
2443 *
2444 * @return void
2445 */
2446 public function __destruct()
2447 {
2448 is_resource($this->_smtp_connect) && $this->_send_command('quit');
2449 }
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002450
2451 // --------------------------------------------------------------------
2452
2453 /**
2454 * Byte-safe strlen()
2455 *
2456 * @param string $str
2457 * @return int
2458 */
2459 protected static function strlen($str)
2460 {
Andrey Andreevc0c74d52017-01-19 15:26:35 +02002461 return (self::$func_overload)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002462 ? mb_strlen($str, '8bit')
2463 : strlen($str);
2464 }
2465
2466 // --------------------------------------------------------------------
2467
2468 /**
2469 * Byte-safe substr()
2470 *
2471 * @param string $str
2472 * @param int $start
2473 * @param int $length
2474 * @return string
2475 */
2476 protected static function substr($str, $start, $length = NULL)
2477 {
Andrey Andreevc0c74d52017-01-19 15:26:35 +02002478 if (self::$func_overload)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002479 {
2480 // mb_substr($str, $start, null, '8bit') returns an empty
2481 // string on PHP 5.3
2482 isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
2483 return mb_substr($str, $start, $length, '8bit');
2484 }
2485
2486 return isset($length)
2487 ? substr($str, $start, $length)
2488 : substr($str, $start);
2489 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002490}