blob: a5a7079177f315fae4295045dcf476a15259f6c4 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, 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 /**
378 * mbstring.func_override flag
379 *
380 * @var bool
381 */
382 protected static $func_override;
383
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 Andreev4e2cdec2016-10-28 14:19:08 +0300400 isset(self::$func_override) OR self::$func_override = (extension_loaded('mbstring') && ini_get('mbstring.func_override'));
401
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('User-Agent', $this->useragent);
462 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
464 if ($clear_attachments !== FALSE)
465 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300466 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Eric Barnes6113f542010-12-29 13:36:12 -0500468
Greg Akera769deb2010-11-10 15:47:29 -0600469 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Set FROM
476 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300477 * @param string $from
478 * @param string $name
479 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500480 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300482 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200484 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200486 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 if ($this->validate)
490 {
491 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300492 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200493 {
494 $this->validate_email($this->_str_to_array($return_path));
495 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497
498 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100499 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 // only use Q encoding if there are characters that would require it
502 if ( ! preg_match('/[\200-\377]/', $name))
503 {
504 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000505 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507 else
508 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300509 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511 }
512
Mickey Wubfc1cad2012-05-31 22:28:40 -0700513 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200514
Andrey Andreev925dd902012-10-19 11:06:31 +0300515 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200516 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500517
Greg Akera769deb2010-11-10 15:47:29 -0600518 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 // --------------------------------------------------------------------
522
523 /**
524 * Set Reply-to
525 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @param string
527 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500528 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000530 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200532 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200534 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 if ($this->validate)
538 {
539 $this->validate_email($this->_str_to_array($replyto));
540 }
541
Andrey Andreevc70216d2016-01-20 17:25:13 +0200542 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 {
Andrey Andreevc70216d2016-01-20 17:25:13 +0200544 // only use Q encoding if there are characters that would require it
545 if ( ! preg_match('/[\200-\377]/', $name))
546 {
547 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
548 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
549 }
550 else
551 {
552 $name = $this->_prep_q_encoding($name);
553 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
Mickey Wubfc1cad2012-05-31 22:28:40 -0700556 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600558
559 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 // --------------------------------------------------------------------
563
564 /**
565 * Set Recipients
566 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500568 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000570 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 $to = $this->_str_to_array($to);
573 $to = $this->clean_email($to);
574
575 if ($this->validate)
576 {
577 $this->validate_email($to);
578 }
579
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200580 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700582 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300585 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500586
Greg Akera769deb2010-11-10 15:47:29 -0600587 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // --------------------------------------------------------------------
591
592 /**
593 * Set CC
594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500596 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000598 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
Andrey Andreev56454792012-05-17 14:32:19 +0300600 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000601
602 if ($this->validate)
603 {
604 $this->validate_email($cc);
605 }
606
Mickey Wubfc1cad2012-05-31 22:28:40 -0700607 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200609 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 $this->_cc_array = $cc;
612 }
Eric Barnes6113f542010-12-29 13:36:12 -0500613
Greg Akera769deb2010-11-10 15:47:29 -0600614 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // --------------------------------------------------------------------
618
619 /**
620 * Set BCC
621 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @param string
623 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500624 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000626 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100628 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 $this->bcc_batch_mode = TRUE;
631 $this->bcc_batch_size = $limit;
632 }
633
Andrey Andreev56454792012-05-17 14:32:19 +0300634 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000635
636 if ($this->validate)
637 {
638 $this->validate_email($bcc);
639 }
640
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200641 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 $this->_bcc_array = $bcc;
644 }
645 else
646 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700647 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
Eric Barnes6113f542010-12-29 13:36:12 -0500649
Greg Akera769deb2010-11-10 15:47:29 -0600650 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Set Email Subject
657 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500659 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000661 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700664 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600665 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // --------------------------------------------------------------------
669
670 /**
671 * Set Body
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500674 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000676 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200678 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200679
Andrey Andreevaf728622011-10-20 10:11:59 +0300680 /* strip slashes only if magic quotes is ON
681 if we do it with magic quotes OFF, it strips real, user-inputted chars.
682
683 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
684 it will probably not exist in future versions at all.
685 */
686 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200687 {
diegorivera33c32802011-10-19 02:56:15 -0200688 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200689 }
diegorivera33c32802011-10-19 02:56:15 -0200690
Greg Akera769deb2010-11-10 15:47:29 -0600691 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 // --------------------------------------------------------------------
695
696 /**
697 * Assign file attachments
698 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100699 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300700 * @param string $disposition = 'attachment'
701 * @param string $newname = NULL
702 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500703 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100705 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100707 if ($mime === '')
708 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100709 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100710 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100711 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100712 return FALSE;
713 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200714
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200715 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100716 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100717 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100718 return FALSE;
719 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100720
Petr Heralecky300e3f02014-01-10 11:49:11 +0100721 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100722 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100723 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100724 }
725 else
726 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100727 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100728 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200729
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300730 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100731 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300732 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100733 'type' => $mime,
Andrey Andreev3c422792016-06-06 09:44:50 +0300734 'content' => chunk_split(base64_encode($file_content)),
735 'multipart' => 'mixed'
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300736 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100737
Greg Akera769deb2010-11-10 15:47:29 -0600738 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200740
Petr Heralecky300e3f02014-01-10 11:49:11 +0100741 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200742
Petr Heralecky300e3f02014-01-10 11:49:11 +0100743 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200744 * Set and return attachment Content-ID
745 *
746 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100747 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100748 * @param string $filename
749 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100750 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200751 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100752 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100753 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100754 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100755 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100756 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300757 $this->_attachments[$i]['multipart'] = 'related';
Petr Heralecky230fca32014-01-10 12:55:57 +0100758 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100759 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100760 }
761 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200762
Petr Heralecky300e3f02014-01-10 11:49:11 +0100763 return FALSE;
764 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000765
766 // --------------------------------------------------------------------
767
768 /**
769 * Add a Header Item
770 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * @param string
772 * @param string
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300773 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700775 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
florisluitenf55d5142013-06-07 17:20:06 +0300777 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300778 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
782
783 /**
784 * Convert a String to an Array
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string
787 * @return array
788 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600789 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 if ( ! is_array($email))
792 {
Andrey Andreev56454792012-05-17 14:32:19 +0300793 return (strpos($email, ',') !== FALSE)
794 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
795 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 }
Andrey Andreev56454792012-05-17 14:32:19 +0300797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 return $email;
799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 // --------------------------------------------------------------------
802
803 /**
804 * Set Multipart Value
805 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500807 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200809 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400811 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600812 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 // --------------------------------------------------------------------
816
817 /**
818 * Set Mailtype
819 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500821 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000823 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
Andrey Andreev56454792012-05-17 14:32:19 +0300825 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600826 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 /**
832 * Set Wordwrap
833 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400834 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500835 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000837 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400839 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600840 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
844
845 /**
846 * Set Protocol
847 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500849 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000851 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200853 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600854 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 // --------------------------------------------------------------------
858
859 /**
860 * Set Priority
861 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200862 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500863 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000865 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200867 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600868 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // --------------------------------------------------------------------
872
873 /**
874 * Set Newline Character
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500877 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000879 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200881 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600882 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // --------------------------------------------------------------------
886
887 /**
888 * Set CRLF
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500891 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000893 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200895 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600896 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 // --------------------------------------------------------------------
900
901 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * Get the Message ID
903 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 * @return string
905 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600906 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200908 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300909 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Get Mail Protocol
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200918 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600920 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
922 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200923 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000924
Alex Bilbied261b1e2012-06-02 11:12:16 +0100925 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 return $this->protocol;
928 }
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
932
933 /**
934 * Get Mail Encoding
935 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 * @param bool
937 * @return string
938 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600939 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200941 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000942
943 foreach ($this->_base_charsets as $charset)
944 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300945 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
947 $this->_encoding = '7bit';
948 }
949 }
950
Alex Bilbied261b1e2012-06-02 11:12:16 +0100951 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 return $this->_encoding;
954 }
955 }
956
957 // --------------------------------------------------------------------
958
959 /**
960 * Get content type (text/html/attachment)
961 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 * @return string
963 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600964 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev56454792012-05-17 14:32:19 +0300966 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300968 return empty($this->_attachments) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Andrey Andreev3c422792016-06-06 09:44:50 +0300970 elseif ($this->mailtype === 'text' && ! empty($this->_attachments))
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
972 return 'plain-attach';
973 }
974 else
975 {
976 return 'plain';
977 }
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
981
982 /**
983 * Set RFC 822 Date
984 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 * @return string
986 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600987 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200989 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300990 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200992 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
Andrey Andreev59c5b532012-03-01 14:09:51 +0200994 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
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 * Mime message
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @return string
1003 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001004 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001006 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 +00001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // --------------------------------------------------------------------
1010
1011 /**
1012 * Validate Email Address
1013 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @param string
1015 * @return bool
1016 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001017 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 if ( ! is_array($email))
1020 {
patworkb0707982011-04-08 15:10:05 +02001021 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 return FALSE;
1023 }
1024
1025 foreach ($email as $val)
1026 {
1027 if ( ! $this->valid_email($val))
1028 {
patworkb0707982011-04-08 15:10:05 +02001029 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 return FALSE;
1031 }
1032 }
1033
1034 return TRUE;
1035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Email Validation
1041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @param string
1043 * @return bool
1044 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001045 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
Andrey Andreev95496662014-06-01 00:00:13 +03001047 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1048 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001049 $email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos));
Andrey Andreev95496662014-06-01 00:00:13 +03001050 }
1051
Andrey Andreev580388b2012-06-27 15:43:46 +03001052 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
1056
1057 /**
1058 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1059 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 * @param string
1061 * @return string
1062 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001063 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
1065 if ( ! is_array($email))
1066 {
Andrey Andreev56454792012-05-17 14:32:19 +03001067 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 }
1069
1070 $clean_email = array();
1071
1072 foreach ($email as $addy)
1073 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001074 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
1076
1077 return $clean_email;
1078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 // --------------------------------------------------------------------
1081
1082 /**
1083 * Build alternative plain text message
1084 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001085 * Provides the raw message for use in plain-text headers of
1086 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * If the user hasn't specified his own alternative message
1088 * it creates one by stripping the HTML
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @return string
1091 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001092 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001094 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001096 return ($this->wordwrap)
1097 ? $this->word_wrap($this->alt_message, 76)
1098 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 }
1100
Andrey Andreev56454792012-05-17 14:32:19 +03001101 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001102 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001103
1104 for ($i = 20; $i >= 3; $i--)
1105 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001106 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
1108
GDmac9bea4be2012-10-30 06:14:19 +01001109 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001110 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001111
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001112 return ($this->wordwrap)
1113 ? $this->word_wrap($body, 76)
1114 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 // --------------------------------------------------------------------
1118
1119 /**
1120 * Word Wrap
1121 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001123 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @return string
1125 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001126 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001128 // Set the character limit, if not already present
1129 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001131 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
1133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // Standardize newlines
1135 if (strpos($str, "\r") !== FALSE)
1136 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001137 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
1139
GDmac9bea4be2012-10-30 06:14:19 +01001140 // Reduce multiple spaces at end of line
1141 $str = preg_replace('| +\n|', "\n", $str);
1142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // If the current word is surrounded by {unwrap} tags we'll
1144 // strip the entire chunk and replace it with a marker.
1145 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001146 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001148 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001150 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001151 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
1153 }
1154
vlakofff0ab8132014-05-20 10:32:53 +02001155 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001157 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 $str = wordwrap($str, $charlim, "\n", FALSE);
1159
1160 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001161 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 foreach (explode("\n", $str) as $line)
1163 {
1164 // Is the line within the allowed character count?
1165 // If so we'll join it to the output and continue
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001166 if (self::strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 $output .= $line.$this->newline;
1169 continue;
1170 }
1171
1172 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001173 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
1175 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001176 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
1178 break;
1179 }
1180
1181 // Trim the word down
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001182 $temp .= self::substr($line, 0, $charlim - 1);
1183 $line = self::substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 }
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001185 while (self::strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001186
1187 // If $temp contains data it means we had to split up an over-length
1188 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001189 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001191 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 }
1193
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001194 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
1196
1197 // Put our markers back
1198 if (count($unwrap) > 0)
1199 {
1200 foreach ($unwrap as $key => $val)
1201 {
Andrey Andreev56454792012-05-17 14:32:19 +03001202 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 }
1204 }
1205
1206 return $output;
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
1210
1211 /**
1212 * Build final headers
1213 *
Andrey Andreev19277092016-08-22 11:34:56 +03001214 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001216 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreev6a5b5e72017-01-06 13:45:55 +02001218 $this->set_header('User-Agent', $this->useragent);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001219 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1220 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001221 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001222 $this->set_header('Message-ID', $this->_get_message_id());
1223 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 }
Barry Mienydd671972010-10-04 16:33:58 +02001225
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 // --------------------------------------------------------------------
1227
1228 /**
1229 * Write Headers as a string
1230 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 * @return void
1232 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001233 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001235 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301237 if (isset($this->_headers['Subject']))
1238 {
1239 $this->_subject = $this->_headers['Subject'];
1240 unset($this->_headers['Subject']);
1241 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
1243
1244 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001245 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001246
Pascal Kriete14287f32011-02-14 13:39:34 -05001247 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
1249 $val = trim($val);
1250
Alex Bilbied261b1e2012-06-02 11:12:16 +01001251 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Andrey Andreev56454792012-05-17 14:32:19 +03001253 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
1255 }
1256
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001257 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Derek Jones1d890882009-02-10 20:32:14 +00001259 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 }
1261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // --------------------------------------------------------------------
1264
1265 /**
1266 * Build Final Body and attachments
1267 *
buhayc6da1ef2013-04-18 09:06:49 -07001268 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001270 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001272 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
1274 $this->_body = $this->word_wrap($this->_body);
1275 }
1276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 $this->_write_headers();
1278
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001279 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001280 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001281
1282 switch ($this->_get_content_type())
1283 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001284 case 'plain':
Derek Allard2067d1a2008-11-13 22:59:24 +00001285
Andrey Andreev56454792012-05-17 14:32:19 +03001286 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1287 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001288
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001289 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
1291 $this->_header_str .= $hdr;
1292 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 }
Brandon Jones485d7412010-11-09 16:38:17 -05001294 else
1295 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001296 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001297 }
Eric Barnes6113f542010-12-29 13:36:12 -05001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 return;
1300
Andrey Andreev3c422792016-06-06 09:44:50 +03001301 case 'html':
Derek Allard2067d1a2008-11-13 22:59:24 +00001302
1303 if ($this->send_multipart === FALSE)
1304 {
Andrey Andreev56454792012-05-17 14:32:19 +03001305 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001306 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
1308 else
1309 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001310 $boundary = uniqid('B_ALT_');
1311 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001312
Andrey Andreev56454792012-05-17 14:32:19 +03001313 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001314 .'--'.$boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001315
Andrey Andreev56454792012-05-17 14:32:19 +03001316 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1317 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001318 .$this->_get_alt_message().$this->newline.$this->newline
1319 .'--'.$boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001320
Andrey Andreev56454792012-05-17 14:32:19 +03001321 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1322 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 }
Eric Barnes6113f542010-12-29 13:36:12 -05001324
Andrey Andreev56454792012-05-17 14:32:19 +03001325 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001326
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001327 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
1329 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001330 }
1331 else
1332 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001333 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 }
1335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 if ($this->send_multipart !== FALSE)
1337 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001338 $this->_finalbody .= '--'.$boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 }
1340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 return;
1342
Andrey Andreev3c422792016-06-06 09:44:50 +03001343 case 'plain-attach':
Derek Allard2067d1a2008-11-13 22:59:24 +00001344
Andrey Andreev3c422792016-06-06 09:44:50 +03001345 $boundary = uniqid('B_ATC_');
1346 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001347
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001348 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
1350 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001351 }
1352
Andrey Andreev2b956af2013-08-07 14:32:56 +03001353 $body .= $this->_get_mime_message().$this->newline
1354 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001355 .'--'.$boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001356 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001357 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1358 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001359 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001360
Andrey Andreev3c422792016-06-06 09:44:50 +03001361 $this->_append_attachments($body, $boundary);
Derek Allard2067d1a2008-11-13 22:59:24 +00001362
Andrey Andreev3c422792016-06-06 09:44:50 +03001363 break;
1364 case 'html-attach':
1365
1366 $alt_boundary = uniqid('B_ALT_');
1367 $last_boundary = NULL;
1368
1369 if ($this->_attachments_have_multipart('mixed'))
1370 {
1371 $atc_boundary = uniqid('B_ATC_');
1372 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$atc_boundary.'"';
1373 $last_boundary = $atc_boundary;
1374 }
1375
1376 if ($this->_attachments_have_multipart('related'))
1377 {
1378 $rel_boundary = uniqid('B_REL_');
1379 $rel_boundary_header = 'Content-Type: multipart/related; boundary="'.$rel_boundary.'"';
1380
1381 if (isset($last_boundary))
1382 {
1383 $body .= '--'.$last_boundary.$this->newline.$rel_boundary_header;
1384 }
1385 else
1386 {
1387 $hdr .= $rel_boundary_header;
1388 }
1389
1390 $last_boundary = $rel_boundary;
1391 }
Eric Barnes6113f542010-12-29 13:36:12 -05001392
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001393 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 {
1395 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 }
1397
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001398 self::strlen($body) && $body .= $this->newline.$this->newline;
Andrey Andreev56454792012-05-17 14:32:19 +03001399 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001400 .'--'.$last_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001401
Andrey Andreev3c422792016-06-06 09:44:50 +03001402 .'Content-Type: multipart/alternative; boundary="'.$alt_boundary.'"'.$this->newline.$this->newline
1403 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001404
Andrey Andreev56454792012-05-17 14:32:19 +03001405 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1406 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001407 .$this->_get_alt_message().$this->newline.$this->newline
1408 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001409
Andrey Andreev56454792012-05-17 14:32:19 +03001410 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1411 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001412
Andrey Andreev56454792012-05-17 14:32:19 +03001413 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001414 .'--'.$alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001415
Andrey Andreev3c422792016-06-06 09:44:50 +03001416 if ( ! empty($rel_boundary))
1417 {
1418 $body .= $this->newline.$this->newline;
1419 $this->_append_attachments($body, $rel_boundary, 'related');
1420 }
1421
1422 // multipart/mixed attachments
1423 if ( ! empty($atc_boundary))
1424 {
1425 $body .= $this->newline.$this->newline;
1426 $this->_append_attachments($body, $atc_boundary, 'mixed');
1427 }
1428
1429 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 }
1431
Andrey Andreev2b956af2013-08-07 14:32:56 +03001432 $this->_finalbody = ($this->_get_protocol() === 'mail')
1433 ? $body
1434 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001435
buhay466e8082013-04-17 14:25:34 -07001436 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 }
Barry Mienydd671972010-10-04 16:33:58 +02001438
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 // --------------------------------------------------------------------
1440
Andrey Andreev3c422792016-06-06 09:44:50 +03001441 protected function _attachments_have_multipart($type)
1442 {
1443 foreach ($this->_attachments as &$attachment)
1444 {
1445 if ($attachment['multipart'] === $type)
1446 {
1447 return TRUE;
1448 }
1449 }
1450
1451 return FALSE;
1452 }
1453
1454 // --------------------------------------------------------------------
1455
1456 /**
1457 * Prepares attachment string
1458 *
1459 * @param string $body Message body to append to
1460 * @param string $boundary Multipart boundary
1461 * @param string $multipart When provided, only attachments of this type will be processed
1462 * @return string
1463 */
1464 protected function _append_attachments(&$body, $boundary, $multipart = null)
1465 {
1466 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
1467 {
1468 if (isset($multipart) && $this->_attachments[$i]['multipart'] !== $multipart)
1469 {
1470 continue;
1471 }
1472
1473 $name = isset($this->_attachments[$i]['name'][1])
1474 ? $this->_attachments[$i]['name'][1]
1475 : basename($this->_attachments[$i]['name'][0]);
1476
1477 $body .= '--'.$boundary.$this->newline
1478 .'Content-Type: '.$this->_attachments[$i]['type'].'; name="'.$name.'"'.$this->newline
1479 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
1480 .'Content-Transfer-Encoding: base64'.$this->newline
Andrey Andreev9b0f5fa2016-08-01 13:54:06 +03001481 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline)
1482 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001483 .$this->_attachments[$i]['content'].$this->newline;
1484 }
1485
1486 // $name won't be set if no attachments were appended,
1487 // and therefore a boundary wouldn't be necessary
1488 empty($name) OR $body .= '--'.$boundary.'--';
1489 }
1490
1491 // --------------------------------------------------------------------
1492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 /**
1494 * Prep Quoted Printable
1495 *
1496 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1497 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1498 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 * @return string
1501 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001502 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001504 // ASCII code numbers for "safe" characters that can always be
1505 // used literally, without encoding, as described in RFC 2049.
1506 // http://www.ietf.org/rfc/rfc2049.txt
1507 static $ascii_safe_chars = array(
1508 // ' ( ) + , - . / : = ?
1509 39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
1510 // numbers
1511 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
1512 // upper-case letters
1513 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,
1514 // lower-case letters
1515 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
1516 );
1517
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001518 // We are intentionally wrapping so mail servers will encode characters
1519 // properly and MUAs will behave, so {unwrap} must go!
1520 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1521
Andrey Andreev683b34d2012-10-09 15:00:00 +03001522 // RFC 2045 specifies CRLF as "\r\n".
1523 // However, many developers choose to override that and violate
1524 // the RFC rules due to (apparently) a bug in MS Exchange,
1525 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001526 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001527 {
Andrey Andreeva8382792016-07-28 16:40:12 +03001528 return quoted_printable_encode($str);
Andrey Andreev683b34d2012-10-09 15:00:00 +03001529 }
1530
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001531 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001532 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001533
1534 // Standardize newlines
1535 if (strpos($str, "\r") !== FALSE)
1536 {
1537 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1538 }
1539
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 $escape = '=';
1541 $output = '';
1542
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001543 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001545 $length = self::strlen($line);
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 $temp = '';
1547
1548 // Loop through each character in the line to add soft-wrap
1549 // characters at the end of a line " =\r\n" and add the newly
1550 // processed line(s) to the output (see comment on $crlf class property)
1551 for ($i = 0; $i < $length; $i++)
1552 {
1553 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001554 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 $ascii = ord($char);
1556
1557 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001558 if ($ascii === 32 OR $ascii === 9)
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001560 if ($i === ($length - 1))
1561 {
1562 $char = $escape.sprintf('%02s', dechex($ascii));
1563 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001565 // DO NOT move this below the $ascii_safe_chars line!
1566 //
1567 // = (equals) signs are allowed by RFC2049, but must be encoded
1568 // as they are the encoding delimiter!
1569 elseif ($ascii === 61)
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001571 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001573 elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
1574 {
1575 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
1576 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001577
1578 // If we're at the character limit, add the line to the output,
1579 // reset our temp variable, and keep on chuggin'
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001580 if ((self::strlen($temp) + self::strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 {
1582 $output .= $temp.$escape.$this->crlf;
1583 $temp = '';
1584 }
1585
1586 // Add the character to our temporary line
1587 $temp .= $char;
1588 }
1589
1590 // Add our completed line to the output
1591 $output .= $temp.$this->crlf;
1592 }
1593
1594 // get rid of extra CRLF tacked onto the end
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001595 return self::substr($output, 0, self::strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
1597
1598 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001599
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 /**
1601 * Prep Q Encoding
1602 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001603 * Performs "Q Encoding" on a string for use in email headers.
1604 * It's related but not identical to quoted-printable, so it has its
1605 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001607 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001608 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001610 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001612 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001613
Andrey Andreev925dd902012-10-19 11:06:31 +03001614 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 {
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001616 // Note: We used to have mb_encode_mimeheader() as the first choice
1617 // here, but it turned out to be buggy and unreliable. DO NOT
1618 // re-add it! -- Narf
1619 if (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001620 {
1621 $output = @iconv_mime_encode('', $str,
1622 array(
1623 'scheme' => 'Q',
1624 'line-length' => 76,
1625 'input-charset' => $this->charset,
1626 'output-charset' => $this->charset,
1627 'line-break-chars' => $this->crlf
1628 )
1629 );
1630
1631 // There are reports that iconv_mime_encode() might fail and return FALSE
1632 if ($output !== FALSE)
1633 {
1634 // iconv_mime_encode() will always put a header field name.
1635 // We've passed it an empty one, but it still prepends our
1636 // encoded string with ': ', so we need to strip it.
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001637 return self::substr($output, 2);
Andrey Andreev925dd902012-10-19 11:06:31 +03001638 }
1639
1640 $chars = iconv_strlen($str, 'UTF-8');
1641 }
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001642 elseif (MB_ENABLED === TRUE)
1643 {
1644 $chars = mb_strlen($str, 'UTF-8');
1645 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001646 }
1647
Andrey Andreev925dd902012-10-19 11:06:31 +03001648 // We might already have this set for UTF-8
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001649 isset($chars) OR $chars = self::strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001650
Andrey Andreev925dd902012-10-19 11:06:31 +03001651 $output = '=?'.$this->charset.'?Q?';
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001652 for ($i = 0, $length = self::strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001654 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001655 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1656 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001657
Andrey Andreev925dd902012-10-19 11:06:31 +03001658 // RFC 2045 sets a limit of 76 characters per line.
1659 // We'll append ?= to the end of each line though.
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001660 if ($length + ($l = self::strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001662 $output .= '?='.$this->crlf // EOL
1663 .' =?'.$this->charset.'?Q?'.$chr; // New line
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001664 $length = 6 + self::strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001666 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001668 $output .= $chr;
1669 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
1672
Andrey Andreev925dd902012-10-19 11:06:31 +03001673 // End the header
1674 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 }
1676
1677 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001678
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 /**
1680 * Send Email
1681 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001682 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 * @return bool
1684 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001685 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001687 if ( ! isset($this->_headers['From']))
Michael Granados0b85d6b2014-11-09 02:43:48 -02001688 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001689 $this->_set_error_message('lang:email_no_from');
Michael Granados0b85d6b2014-11-09 02:43:48 -02001690 return FALSE;
1691 }
1692
Alex Bilbied261b1e2012-06-02 11:12:16 +01001693 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001694 {
1695 $this->reply_to($this->_headers['From']);
1696 }
1697
Andrey Andreev76f15c92012-03-01 13:05:07 +02001698 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1699 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1700 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 {
patworkb0707982011-04-08 15:10:05 +02001702 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 return FALSE;
1704 }
1705
1706 $this->_build_headers();
1707
Andrey Andreev76f15c92012-03-01 13:05:07 +02001708 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001710 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001711
Alex Bilbiea87aab32012-07-30 09:50:37 +01001712 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001713 {
1714 $this->clear();
1715 }
1716
Alex Bilbieb901e732012-07-30 09:44:57 +01001717 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 }
1719
buhay466e8082013-04-17 14:25:34 -07001720 if ($this->_build_message() === FALSE)
1721 {
1722 return FALSE;
1723 }
buhayc6da1ef2013-04-18 09:06:49 -07001724
Alex Bilbieb901e732012-07-30 09:44:57 +01001725 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001726
Alex Bilbiea87aab32012-07-30 09:50:37 +01001727 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001728 {
1729 $this->clear();
1730 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001731
Alex Bilbieb901e732012-07-30 09:44:57 +01001732 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 }
Barry Mienydd671972010-10-04 16:33:58 +02001734
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 // --------------------------------------------------------------------
1736
1737 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001738 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001740 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001742 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001744 $float = $this->bcc_batch_size - 1;
1745 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 $chunk = array();
1747
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001748 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
1750 if (isset($this->_bcc_array[$i]))
1751 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001752 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 }
1754
Alex Bilbied261b1e2012-06-02 11:12:16 +01001755 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001757 $chunk[] = self::substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001758 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001759 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 }
1761
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001762 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03001764 $chunk[] = self::substr($set, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 }
1766 }
1767
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001768 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 {
1770 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001771
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001772 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001773
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001774 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001776 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 }
1778 else
1779 {
1780 $this->_bcc_array = $bcc;
1781 }
1782
buhay466e8082013-04-17 14:25:34 -07001783 if ($this->_build_message() === FALSE)
1784 {
1785 return FALSE;
1786 }
buhayc6da1ef2013-04-18 09:06:49 -07001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 $this->_spool_email();
1789 }
1790 }
Barry Mienydd671972010-10-04 16:33:58 +02001791
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 // --------------------------------------------------------------------
1793
1794 /**
1795 * Unwrap special elements
1796 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 * @return void
1798 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001799 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 {
Andrey Andreev56454792012-05-17 14:32:19 +03001801 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 }
Barry Mienydd671972010-10-04 16:33:58 +02001803
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 // --------------------------------------------------------------------
1805
1806 /**
1807 * Strip line-breaks via callback
1808 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001809 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 * @return string
1811 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001812 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 {
1814 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1815 {
1816 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1817 }
1818
1819 return $matches[1];
1820 }
Barry Mienydd671972010-10-04 16:33:58 +02001821
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 // --------------------------------------------------------------------
1823
1824 /**
1825 * Spool mail to the mail server
1826 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 * @return bool
1828 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001829 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 {
1831 $this->_unwrap_specials();
1832
Andrey Andreev56454792012-05-17 14:32:19 +03001833 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001834 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 {
Andrey Andreev56454792012-05-17 14:32:19 +03001836 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001837 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 }
1839
patworkb0707982011-04-08 15:10:05 +02001840 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 return TRUE;
1842 }
Barry Mienydd671972010-10-04 16:33:58 +02001843
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 // --------------------------------------------------------------------
1845
1846 /**
Andrey Andreeva9600162017-01-05 17:35:28 +02001847 * Validate email for shell
1848 *
1849 * Applies stricter, shell-safe validation to email addresses.
1850 * Introduced to prevent RCE via sendmail's -f option.
1851 *
1852 * @see https://github.com/bcit-ci/CodeIgniter/issues/4963
1853 * @see https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36
1854 * @license https://creativecommons.org/publicdomain/zero/1.0/ CC0 1.0, Public Domain
1855 *
1856 * Credits for the base concept go to Paul Buonopane <paul@namepros.com>
1857 *
1858 * @param string $email
1859 * @return bool
1860 */
1861 protected function _validate_email_for_shell(&$email)
1862 {
1863 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1864 {
1865 $email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos));
1866 }
1867
1868 return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+@[a-z0-9.-]{1,253}\z#i', $email));
1869 }
1870
1871 // --------------------------------------------------------------------
1872
1873 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 * Send using mail()
1875 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 * @return bool
1877 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001878 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001880 if (is_array($this->_recipients))
1881 {
1882 $this->_recipients = implode(', ', $this->_recipients);
1883 }
1884
Andrey Andreeva9600162017-01-05 17:35:28 +02001885 // _validate_email_for_shell() below accepts by reference,
1886 // so this needs to be assigned to a variable
1887 $from = $this->clean_email($this->_headers['Return-Path']);
1888
1889 if ($this->_safe_mode === TRUE || ! $this->_validate_email_for_shell($from))
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001891 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 }
1893 else
1894 {
1895 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1896 // we've encountered servers that seem to require it to be in place.
Andrey Andreeva9600162017-01-05 17:35:28 +02001897 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 }
1899 }
Barry Mienydd671972010-10-04 16:33:58 +02001900
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 // --------------------------------------------------------------------
1902
1903 /**
1904 * Send using Sendmail
1905 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 * @return bool
1907 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001908 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 {
Andrey Andreeva9600162017-01-05 17:35:28 +02001910 // _validate_email_for_shell() below accepts by reference,
1911 // so this needs to be assigned to a variable
1912 $from = $this->clean_email($this->_headers['From']);
1913 if ($this->_validate_email_for_shell($from))
Derek Jones4cefaa42009-04-29 19:13:56 +00001914 {
Andrey Andreeva9600162017-01-05 17:35:28 +02001915 $from = '-f '.$from;
1916 }
1917 else
1918 {
1919 $from = '';
1920 }
1921
1922 // is popen() enabled?
1923 if ( ! function_usable('popen') OR FALSE === ($fp = @popen($this->mailpath.' -oi '.$from.' -t', 'w')))
1924 {
1925 // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001926 return FALSE;
1927 }
Derek Jones71141ce2010-03-02 16:41:20 -06001928
Derek Jonesc630bcf2008-11-17 21:09:45 +00001929 fputs($fp, $this->_header_str);
1930 fputs($fp, $this->_finalbody);
1931
Barry Mienydd671972010-10-04 16:33:58 +02001932 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001933
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001934 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 {
patworkb0707982011-04-08 15:10:05 +02001936 $this->_set_error_message('lang:email_exit_status', $status);
1937 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 return FALSE;
1939 }
1940
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 return TRUE;
1942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // --------------------------------------------------------------------
1945
1946 /**
1947 * Send using SMTP
1948 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 * @return bool
1950 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001951 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301952 {
1953 if ($this->smtp_host === '')
1954 {
1955 $this->_set_error_message('lang:email_no_hostname');
1956 return FALSE;
1957 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001958
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301959 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1960 {
1961 return FALSE;
1962 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001963
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001964 if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1965 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001966 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001967 return FALSE;
1968 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001969
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301970 foreach ($this->_recipients as $val)
1971 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001972 if ( ! $this->_send_command('to', $val))
1973 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001974 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001975 return FALSE;
1976 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301977 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001978
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301979 if (count($this->_cc_array) > 0)
1980 {
1981 foreach ($this->_cc_array as $val)
1982 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001983 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301984 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001985 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001986 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301987 }
1988 }
1989 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001990
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301991 if (count($this->_bcc_array) > 0)
1992 {
1993 foreach ($this->_bcc_array as $val)
1994 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001995 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301996 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001997 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001998 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301999 }
2000 }
2001 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002002
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002003 if ( ! $this->_send_command('data'))
2004 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002005 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03002006 return FALSE;
2007 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002008
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302009 // perform dot transformation on any lines that begin with a dot
2010 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00002011
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302012 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00002013
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302014 $reply = $this->_get_smtp_data();
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302015 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002017 $this->_smtp_end();
2018
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302019 if (strpos($reply, '250') !== 0)
2020 {
2021 $this->_set_error_message('lang:email_smtp_error', $reply);
2022 return FALSE;
2023 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002024
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302025 return TRUE;
2026 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002027
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302028 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03002029
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302030 /**
Andrey Andreev0c821bb2016-07-19 16:04:02 +03002031 * SMTP End
2032 *
2033 * Shortcut to send RSET or QUIT depending on keep-alive
2034 *
2035 * @return void
2036 */
2037 protected function _smtp_end()
2038 {
2039 ($this->smtp_keepalive)
2040 ? $this->_send_command('reset')
2041 : $this->_send_command('quit');
2042 }
2043
2044 // --------------------------------------------------------------------
2045
2046 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302047 * SMTP Connect
2048 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302049 * @return string
2050 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302051 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302052 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302053 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302054 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302055 return TRUE;
2056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002057
Andrey Andreev89b67b42013-03-12 19:34:23 +02002058 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002059
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302060 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
2061 $this->smtp_port,
2062 $errno,
2063 $errstr,
2064 $this->smtp_timeout);
2065
2066 if ( ! is_resource($this->_smtp_connect))
2067 {
2068 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
2069 return FALSE;
2070 }
2071
2072 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
2073 $this->_set_error_message($this->_get_smtp_data());
2074
2075 if ($this->smtp_crypto === 'tls')
2076 {
2077 $this->_send_command('hello');
2078 $this->_send_command('starttls');
2079
2080 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
2081
2082 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302083 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302084 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302085 return FALSE;
2086 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302087 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302088
2089 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302090 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002091
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302092 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002093
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302094 /**
2095 * Send SMTP command
2096 *
2097 * @param string
2098 * @param string
Andrey Andreev19277092016-08-22 11:34:56 +03002099 * @return bool
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302100 */
2101 protected function _send_command($cmd, $data = '')
2102 {
2103 switch ($cmd)
2104 {
2105 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002106
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302107 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2108 {
2109 $this->_send_data('EHLO '.$this->_get_hostname());
2110 }
2111 else
2112 {
2113 $this->_send_data('HELO '.$this->_get_hostname());
2114 }
Radu Potopbbf04b02011-09-28 13:57:51 +03002115
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302116 $resp = 250;
2117 break;
2118 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002119
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302120 $this->_send_data('STARTTLS');
2121 $resp = 220;
2122 break;
2123 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03002124
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302125 $this->_send_data('MAIL FROM:<'.$data.'>');
2126 $resp = 250;
2127 break;
2128 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03002129
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302130 if ($this->dsn)
2131 {
2132 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2133 }
2134 else
2135 {
2136 $this->_send_data('RCPT TO:<'.$data.'>');
2137 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002138
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302139 $resp = 250;
2140 break;
2141 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002142
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302143 $this->_send_data('DATA');
2144 $resp = 354;
2145 break;
2146 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002147
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302148 $this->_send_data('RSET');
2149 $resp = 250;
2150 break;
2151 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002152
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302153 $this->_send_data('QUIT');
2154 $resp = 221;
2155 break;
2156 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002157
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302158 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002159
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302160 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002161
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002162 if ((int) self::substr($reply, 0, 3) !== $resp)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302163 {
2164 $this->_set_error_message('lang:email_smtp_error', $reply);
2165 return FALSE;
2166 }
Barry Mienydd671972010-10-04 16:33:58 +02002167
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302168 if ($cmd === 'quit')
2169 {
2170 fclose($this->_smtp_connect);
2171 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002172
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302173 return TRUE;
2174 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002175
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302176 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002177
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302178 /**
2179 * SMTP Authenticate
2180 *
2181 * @return bool
2182 */
2183 protected function _smtp_authenticate()
2184 {
2185 if ( ! $this->_smtp_auth)
2186 {
2187 return TRUE;
2188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002189
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302190 if ($this->smtp_user === '' && $this->smtp_pass === '')
2191 {
2192 $this->_set_error_message('lang:email_no_smtp_unpw');
2193 return FALSE;
2194 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002195
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302196 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002197
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302198 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002199
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002200 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302201 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302202 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302203 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002204 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302205 {
2206 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2207 return FALSE;
2208 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002209
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302210 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002211
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302212 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002213
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302214 if (strpos($reply, '334') !== 0)
2215 {
2216 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2217 return FALSE;
2218 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002219
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302220 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002221
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302222 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302223
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302224 if (strpos($reply, '235') !== 0)
2225 {
2226 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2227 return FALSE;
2228 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302229
Andrey Andreev84253192016-05-09 12:24:52 +03002230 if ($this->smtp_keepalive)
2231 {
2232 $this->_smtp_auth = FALSE;
2233 }
2234
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302235 return TRUE;
2236 }
Barry Mienydd671972010-10-04 16:33:58 +02002237
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 // --------------------------------------------------------------------
2239
2240 /**
2241 * Send SMTP data
2242 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002243 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 * @return bool
2245 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002246 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002247 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002248 $data .= $this->newline;
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002249 for ($written = $timestamp = 0, $length = self::strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002250 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002251 if (($result = fwrite($this->_smtp_connect, self::substr($data, $written))) === FALSE)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002252 {
2253 break;
2254 }
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002255 // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2256 elseif ($result === 0)
2257 {
2258 if ($timestamp === 0)
2259 {
2260 $timestamp = time();
2261 }
2262 elseif ($timestamp < (time() - $this->smtp_timeout))
2263 {
2264 $result = FALSE;
2265 break;
2266 }
2267
2268 usleep(250000);
2269 continue;
2270 }
2271 else
2272 {
2273 $timestamp = 0;
2274 }
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002275 }
2276
2277 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002278 {
patworkb0707982011-04-08 15:10:05 +02002279 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 return FALSE;
2281 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002282
2283 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 }
Barry Mienydd671972010-10-04 16:33:58 +02002285
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 // --------------------------------------------------------------------
2287
2288 /**
2289 * Get SMTP data
2290 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 * @return string
2292 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002293 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 {
Andrey Andreev56454792012-05-17 14:32:19 +03002295 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002296
2297 while ($str = fgets($this->_smtp_connect, 512))
2298 {
2299 $data .= $str;
2300
Alex Bilbied261b1e2012-06-02 11:12:16 +01002301 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
2303 break;
2304 }
2305 }
2306
2307 return $data;
2308 }
Barry Mienydd671972010-10-04 16:33:58 +02002309
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 // --------------------------------------------------------------------
2311
2312 /**
2313 * Get Hostname
Andrey Andreev396eb892015-02-06 14:50:10 +02002314 *
2315 * There are only two legal types of hostname - either a fully
2316 * qualified domain name (eg: "mail.example.com") or an IP literal
2317 * (eg: "[1.2.3.4]").
2318 *
2319 * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
James Wade3245af42015-02-06 11:48:51 +00002320 * @link http://cbl.abuseat.org/namingproblems.html
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 * @return string
2322 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002323 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 {
Andrey Andreev396eb892015-02-06 14:50:10 +02002325 if (isset($_SERVER['SERVER_NAME']))
2326 {
2327 return $_SERVER['SERVER_NAME'];
2328 }
2329
2330 return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
Derek Allard2067d1a2008-11-13 22:59:24 +00002331 }
Barry Mienydd671972010-10-04 16:33:58 +02002332
Derek Allard2067d1a2008-11-13 22:59:24 +00002333 // --------------------------------------------------------------------
2334
2335 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 * Get Debug Message
2337 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002338 * @param array $include List of raw data chunks to include in the output
2339 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 * @return string
2341 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002342 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 {
2344 $msg = '';
2345
2346 if (count($this->_debug_msg) > 0)
2347 {
2348 foreach ($this->_debug_msg as $val)
2349 {
2350 $msg .= $val;
2351 }
2352 }
2353
Andrey Andreev61797f62012-11-26 16:15:12 +02002354 // Determine which parts of our raw data needs to be printed
2355 $raw_data = '';
2356 is_array($include) OR $include = array($include);
2357
2358 if (in_array('headers', $include, TRUE))
2359 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002360 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002361 }
2362
2363 if (in_array('subject', $include, TRUE))
2364 {
2365 $raw_data .= htmlspecialchars($this->_subject)."\n";
2366 }
2367
2368 if (in_array('body', $include, TRUE))
2369 {
2370 $raw_data .= htmlspecialchars($this->_finalbody);
2371 }
2372
2373 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002374 }
Barry Mienydd671972010-10-04 16:33:58 +02002375
Derek Allard2067d1a2008-11-13 22:59:24 +00002376 // --------------------------------------------------------------------
2377
2378 /**
2379 * Set Message
2380 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002381 * @param string $msg
2382 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002383 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002384 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002385 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002386 {
2387 $CI =& get_instance();
2388 $CI->lang->load('email');
2389
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002390 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 {
Andrey Andreev56454792012-05-17 14:32:19 +03002392 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002393 }
2394 else
2395 {
Andrey Andreev56454792012-05-17 14:32:19 +03002396 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002397 }
2398 }
Barry Mienydd671972010-10-04 16:33:58 +02002399
Derek Allard2067d1a2008-11-13 22:59:24 +00002400 // --------------------------------------------------------------------
2401
2402 /**
2403 * Mime Types
2404 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002405 * @param string
2406 * @return string
2407 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002408 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002409 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002410 $ext = strtolower($ext);
2411
vlakoff66c7bb42014-05-19 13:45:12 +02002412 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002413
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002414 if (isset($mimes[$ext]))
2415 {
2416 return is_array($mimes[$ext])
2417 ? current($mimes[$ext])
2418 : $mimes[$ext];
2419 }
2420
2421 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002422 }
2423
Andrey Andreev8c95c3d2016-05-09 12:35:41 +03002424 // --------------------------------------------------------------------
2425
2426 /**
2427 * Destructor
2428 *
2429 * @return void
2430 */
2431 public function __destruct()
2432 {
2433 is_resource($this->_smtp_connect) && $this->_send_command('quit');
2434 }
Andrey Andreev4e2cdec2016-10-28 14:19:08 +03002435
2436 // --------------------------------------------------------------------
2437
2438 /**
2439 * Byte-safe strlen()
2440 *
2441 * @param string $str
2442 * @return int
2443 */
2444 protected static function strlen($str)
2445 {
2446 return (self::$func_override)
2447 ? mb_strlen($str, '8bit')
2448 : strlen($str);
2449 }
2450
2451 // --------------------------------------------------------------------
2452
2453 /**
2454 * Byte-safe substr()
2455 *
2456 * @param string $str
2457 * @param int $start
2458 * @param int $length
2459 * @return string
2460 */
2461 protected static function substr($str, $start, $length = NULL)
2462 {
2463 if (self::$func_override)
2464 {
2465 // mb_substr($str, $start, null, '8bit') returns an empty
2466 // string on PHP 5.3
2467 isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
2468 return mb_substr($str, $start, $length, '8bit');
2469 }
2470
2471 return isset($length)
2472 ? substr($str, $start, $length)
2473 : substr($str, $start);
2474 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002475}