blob: 7f49c1b3d8912cce016a6c2ce4a47d0de2a6a819 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, 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 Andreev597ea272012-11-01 22:56:26 +0200377 // --------------------------------------------------------------------
378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Constructor - Sets Email Preferences
381 *
382 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300383 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300384 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300385 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Andrey Andreev0b1fd2c2015-03-10 20:00:19 +0200387 public function __construct(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300389 $this->charset = config_item('charset');
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300390 $this->initialize($config);
Andrey Andreevf6274742014-02-20 18:05:58 +0200391 $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300392
Andrey Andreev90726b82015-01-20 12:39:22 +0200393 log_message('info', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530395
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530396 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100397
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530398 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * Initialize preferences
400 *
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300401 * @param array $config
Andrew Podner4296a652012-12-17 07:51:15 -0500402 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 */
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300404 public function initialize(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300406 $this->clear();
407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 foreach ($config as $key => $val)
409 {
410 if (isset($this->$key))
411 {
412 $method = 'set_'.$key;
413
414 if (method_exists($this, $method))
415 {
416 $this->$method($val);
417 }
418 else
419 {
420 $this->$key = $val;
421 }
422 }
423 }
424
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300425 $this->charset = strtoupper($this->charset);
426 $this->_smtp_auth = isset($this->smtp_user[0], $this->smtp_pass[0]);
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000427
428 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
432
433 /**
434 * Initialize the Email Data
435 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800436 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500437 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000439 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200441 $this->_subject = '';
442 $this->_body = '';
443 $this->_finalbody = '';
444 $this->_header_str = '';
445 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500447 $this->_cc_array = array();
448 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 $this->_headers = array();
450 $this->_debug_msg = array();
451
Mickey Wubfc1cad2012-05-31 22:28:40 -0700452 $this->set_header('User-Agent', $this->useragent);
453 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000454
455 if ($clear_attachments !== FALSE)
456 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300457 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Eric Barnes6113f542010-12-29 13:36:12 -0500459
Greg Akera769deb2010-11-10 15:47:29 -0600460 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
464
465 /**
466 * Set FROM
467 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300468 * @param string $from
469 * @param string $name
470 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500471 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300473 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200475 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200477 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479
480 if ($this->validate)
481 {
482 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300483 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200484 {
485 $this->validate_email($this->_str_to_array($return_path));
486 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100490 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 // only use Q encoding if there are characters that would require it
493 if ( ! preg_match('/[\200-\377]/', $name))
494 {
495 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000496 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498 else
499 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300500 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502 }
503
Mickey Wubfc1cad2012-05-31 22:28:40 -0700504 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200505
Andrey Andreev925dd902012-10-19 11:06:31 +0300506 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200507 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500508
Greg Akera769deb2010-11-10 15:47:29 -0600509 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Set Reply-to
516 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @param string
518 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500519 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000521 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200523 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200525 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
527
528 if ($this->validate)
529 {
530 $this->validate_email($this->_str_to_array($replyto));
531 }
532
Andrey Andreevc70216d2016-01-20 17:25:13 +0200533 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Andrey Andreevc70216d2016-01-20 17:25:13 +0200535 // only use Q encoding if there are characters that would require it
536 if ( ! preg_match('/[\200-\377]/', $name))
537 {
538 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
539 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
540 }
541 else
542 {
543 $name = $this->_prep_q_encoding($name);
544 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546
Mickey Wubfc1cad2012-05-31 22:28:40 -0700547 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600549
550 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Set Recipients
557 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500559 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000561 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 $to = $this->_str_to_array($to);
564 $to = $this->clean_email($to);
565
566 if ($this->validate)
567 {
568 $this->validate_email($to);
569 }
570
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200571 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700573 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300576 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500577
Greg Akera769deb2010-11-10 15:47:29 -0600578 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
582
583 /**
584 * Set CC
585 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500587 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000589 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Andrey Andreev56454792012-05-17 14:32:19 +0300591 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000592
593 if ($this->validate)
594 {
595 $this->validate_email($cc);
596 }
597
Mickey Wubfc1cad2012-05-31 22:28:40 -0700598 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200600 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 $this->_cc_array = $cc;
603 }
Eric Barnes6113f542010-12-29 13:36:12 -0500604
Greg Akera769deb2010-11-10 15:47:29 -0600605 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 // --------------------------------------------------------------------
609
610 /**
611 * Set BCC
612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @param string
614 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500615 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000617 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100619 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 $this->bcc_batch_mode = TRUE;
622 $this->bcc_batch_size = $limit;
623 }
624
Andrey Andreev56454792012-05-17 14:32:19 +0300625 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000626
627 if ($this->validate)
628 {
629 $this->validate_email($bcc);
630 }
631
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200632 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 $this->_bcc_array = $bcc;
635 }
636 else
637 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700638 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
Eric Barnes6113f542010-12-29 13:36:12 -0500640
Greg Akera769deb2010-11-10 15:47:29 -0600641 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // --------------------------------------------------------------------
645
646 /**
647 * Set Email Subject
648 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500650 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000652 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700655 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600656 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * Set Body
663 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500665 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000667 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200669 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200670
Andrey Andreevaf728622011-10-20 10:11:59 +0300671 /* strip slashes only if magic quotes is ON
672 if we do it with magic quotes OFF, it strips real, user-inputted chars.
673
674 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
675 it will probably not exist in future versions at all.
676 */
677 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200678 {
diegorivera33c32802011-10-19 02:56:15 -0200679 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200680 }
diegorivera33c32802011-10-19 02:56:15 -0200681
Greg Akera769deb2010-11-10 15:47:29 -0600682 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 // --------------------------------------------------------------------
686
687 /**
688 * Assign file attachments
689 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100690 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300691 * @param string $disposition = 'attachment'
692 * @param string $newname = NULL
693 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500694 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100696 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100698 if ($mime === '')
699 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100700 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100701 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100702 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100703 return FALSE;
704 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200705
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200706 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100707 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100708 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100709 return FALSE;
710 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100711
Petr Heralecky300e3f02014-01-10 11:49:11 +0100712 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100713 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100714 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100715 }
716 else
717 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100718 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100719 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200720
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300721 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100722 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300723 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100724 'type' => $mime,
Andrey Andreev3c422792016-06-06 09:44:50 +0300725 'content' => chunk_split(base64_encode($file_content)),
726 'multipart' => 'mixed'
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300727 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100728
Greg Akera769deb2010-11-10 15:47:29 -0600729 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200731
Petr Heralecky300e3f02014-01-10 11:49:11 +0100732 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200733
Petr Heralecky300e3f02014-01-10 11:49:11 +0100734 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200735 * Set and return attachment Content-ID
736 *
737 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100738 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100739 * @param string $filename
740 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100741 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200742 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100743 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100744 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100745 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100746 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100747 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300748 $this->_attachments[$i]['multipart'] = 'related';
Petr Heralecky230fca32014-01-10 12:55:57 +0100749 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100750 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100751 }
752 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200753
Petr Heralecky300e3f02014-01-10 11:49:11 +0100754 return FALSE;
755 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000756
757 // --------------------------------------------------------------------
758
759 /**
760 * Add a Header Item
761 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * @param string
763 * @param string
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300764 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700766 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
florisluitenf55d5142013-06-07 17:20:06 +0300768 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300769 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 // --------------------------------------------------------------------
773
774 /**
775 * Convert a String to an Array
776 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 * @param string
778 * @return array
779 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600780 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 if ( ! is_array($email))
783 {
Andrey Andreev56454792012-05-17 14:32:19 +0300784 return (strpos($email, ',') !== FALSE)
785 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
786 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
Andrey Andreev56454792012-05-17 14:32:19 +0300788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 return $email;
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // --------------------------------------------------------------------
793
794 /**
795 * Set Multipart Value
796 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500798 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200800 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400802 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600803 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 // --------------------------------------------------------------------
807
808 /**
809 * Set Mailtype
810 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500812 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000814 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Andrey Andreev56454792012-05-17 14:32:19 +0300816 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600817 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 /**
823 * Set Wordwrap
824 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400825 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500826 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000828 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400830 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600831 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // --------------------------------------------------------------------
835
836 /**
837 * Set Protocol
838 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500840 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000842 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200844 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600845 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 // --------------------------------------------------------------------
849
850 /**
851 * Set Priority
852 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200853 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500854 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000856 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200858 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600859 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
863
864 /**
865 * Set Newline Character
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500868 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000870 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200872 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600873 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 // --------------------------------------------------------------------
877
878 /**
879 * Set CRLF
880 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500882 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000884 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200886 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600887 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
891
892 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * Get the Message ID
894 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @return string
896 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600897 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200899 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300900 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 // --------------------------------------------------------------------
904
905 /**
906 * Get Mail Protocol
907 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200909 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600911 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
913 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200914 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000915
Alex Bilbied261b1e2012-06-02 11:12:16 +0100916 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 return $this->protocol;
919 }
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 // --------------------------------------------------------------------
923
924 /**
925 * Get Mail Encoding
926 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 * @param bool
928 * @return string
929 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600930 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200932 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000933
934 foreach ($this->_base_charsets as $charset)
935 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300936 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 $this->_encoding = '7bit';
939 }
940 }
941
Alex Bilbied261b1e2012-06-02 11:12:16 +0100942 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
944 return $this->_encoding;
945 }
946 }
947
948 // --------------------------------------------------------------------
949
950 /**
951 * Get content type (text/html/attachment)
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @return string
954 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600955 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreev56454792012-05-17 14:32:19 +0300957 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev3c422792016-06-06 09:44:50 +0300959 return empty($this->_attachments) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Andrey Andreev3c422792016-06-06 09:44:50 +0300961 elseif ($this->mailtype === 'text' && ! empty($this->_attachments))
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
963 return 'plain-attach';
964 }
965 else
966 {
967 return 'plain';
968 }
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // --------------------------------------------------------------------
972
973 /**
974 * Set RFC 822 Date
975 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @return string
977 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600978 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200980 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300981 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200983 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000984
Andrey Andreev59c5b532012-03-01 14:09:51 +0200985 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 // --------------------------------------------------------------------
989
990 /**
991 * Mime message
992 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 * @return string
994 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600995 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200997 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 +0000998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Validate Email Address
1004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * @param string
1006 * @return bool
1007 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001008 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
1010 if ( ! is_array($email))
1011 {
patworkb0707982011-04-08 15:10:05 +02001012 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 return FALSE;
1014 }
1015
1016 foreach ($email as $val)
1017 {
1018 if ( ! $this->valid_email($val))
1019 {
patworkb0707982011-04-08 15:10:05 +02001020 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 return FALSE;
1022 }
1023 }
1024
1025 return TRUE;
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 // --------------------------------------------------------------------
1029
1030 /**
1031 * Email Validation
1032 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 * @param string
1034 * @return bool
1035 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001036 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
Andrey Andreev95496662014-06-01 00:00:13 +03001038 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1039 {
1040 $email = substr($email, 0, ++$atpos).idn_to_ascii(substr($email, $atpos));
1041 }
1042
Andrey Andreev580388b2012-06-27 15:43:46 +03001043 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 // --------------------------------------------------------------------
1047
1048 /**
1049 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1050 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 * @param string
1052 * @return string
1053 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001054 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
1056 if ( ! is_array($email))
1057 {
Andrey Andreev56454792012-05-17 14:32:19 +03001058 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
1060
1061 $clean_email = array();
1062
1063 foreach ($email as $addy)
1064 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001065 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 }
1067
1068 return $clean_email;
1069 }
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 // --------------------------------------------------------------------
1072
1073 /**
1074 * Build alternative plain text message
1075 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001076 * Provides the raw message for use in plain-text headers of
1077 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 * If the user hasn't specified his own alternative message
1079 * it creates one by stripping the HTML
1080 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 * @return string
1082 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001083 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001085 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001087 return ($this->wordwrap)
1088 ? $this->word_wrap($this->alt_message, 76)
1089 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
1091
Andrey Andreev56454792012-05-17 14:32:19 +03001092 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001093 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001094
1095 for ($i = 20; $i >= 3; $i--)
1096 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001097 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 }
1099
GDmac9bea4be2012-10-30 06:14:19 +01001100 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001101 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001102
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001103 return ($this->wordwrap)
1104 ? $this->word_wrap($body, 76)
1105 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Word Wrap
1112 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001114 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * @return string
1116 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001117 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001119 // Set the character limit, if not already present
1120 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001122 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
1124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 // Standardize newlines
1126 if (strpos($str, "\r") !== FALSE)
1127 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001128 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 }
1130
GDmac9bea4be2012-10-30 06:14:19 +01001131 // Reduce multiple spaces at end of line
1132 $str = preg_replace('| +\n|', "\n", $str);
1133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // If the current word is surrounded by {unwrap} tags we'll
1135 // strip the entire chunk and replace it with a marker.
1136 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001137 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001139 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001141 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001142 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 }
1144 }
1145
vlakofff0ab8132014-05-20 10:32:53 +02001146 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001148 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 $str = wordwrap($str, $charlim, "\n", FALSE);
1150
1151 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001152 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 foreach (explode("\n", $str) as $line)
1154 {
1155 // Is the line within the allowed character count?
1156 // If so we'll join it to the output and continue
vlakofff0ab8132014-05-20 10:32:53 +02001157 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
1159 $output .= $line.$this->newline;
1160 continue;
1161 }
1162
1163 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001164 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
1166 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001167 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
1169 break;
1170 }
1171
1172 // Trim the word down
vlakofff0ab8132014-05-20 10:32:53 +02001173 $temp .= mb_substr($line, 0, $charlim - 1);
1174 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
vlakofff0ab8132014-05-20 10:32:53 +02001176 while (mb_strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001177
1178 // If $temp contains data it means we had to split up an over-length
1179 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001180 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001182 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 }
1184
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001185 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
1187
1188 // Put our markers back
1189 if (count($unwrap) > 0)
1190 {
1191 foreach ($unwrap as $key => $val)
1192 {
Andrey Andreev56454792012-05-17 14:32:19 +03001193 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
1195 }
1196
1197 return $output;
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Build final headers
1204 *
Andrey Andreev19277092016-08-22 11:34:56 +03001205 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001207 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001209 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1210 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001211 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001212 $this->set_header('Message-ID', $this->_get_message_id());
1213 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 // --------------------------------------------------------------------
1217
1218 /**
1219 * Write Headers as a string
1220 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 * @return void
1222 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001223 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001225 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301227 if (isset($this->_headers['Subject']))
1228 {
1229 $this->_subject = $this->_headers['Subject'];
1230 unset($this->_headers['Subject']);
1231 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 }
1233
1234 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001235 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001236
Pascal Kriete14287f32011-02-14 13:39:34 -05001237 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
1239 $val = trim($val);
1240
Alex Bilbied261b1e2012-06-02 11:12:16 +01001241 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreev56454792012-05-17 14:32:19 +03001243 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
1245 }
1246
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001247 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
Derek Jones1d890882009-02-10 20:32:14 +00001249 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
1251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // --------------------------------------------------------------------
1254
1255 /**
1256 * Build Final Body and attachments
1257 *
buhayc6da1ef2013-04-18 09:06:49 -07001258 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001260 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001262 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
1264 $this->_body = $this->word_wrap($this->_body);
1265 }
1266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 $this->_write_headers();
1268
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001269 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001270 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001271
1272 switch ($this->_get_content_type())
1273 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001274 case 'plain':
Derek Allard2067d1a2008-11-13 22:59:24 +00001275
Andrey Andreev56454792012-05-17 14:32:19 +03001276 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1277 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001278
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001279 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
1281 $this->_header_str .= $hdr;
1282 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
Brandon Jones485d7412010-11-09 16:38:17 -05001284 else
1285 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001286 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001287 }
Eric Barnes6113f542010-12-29 13:36:12 -05001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 return;
1290
Andrey Andreev3c422792016-06-06 09:44:50 +03001291 case 'html':
Derek Allard2067d1a2008-11-13 22:59:24 +00001292
1293 if ($this->send_multipart === FALSE)
1294 {
Andrey Andreev56454792012-05-17 14:32:19 +03001295 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001296 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 }
1298 else
1299 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001300 $boundary = uniqid('B_ALT_');
1301 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001302
Andrey Andreev56454792012-05-17 14:32:19 +03001303 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001304 .'--'.$boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001305
Andrey Andreev56454792012-05-17 14:32:19 +03001306 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1307 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001308 .$this->_get_alt_message().$this->newline.$this->newline
1309 .'--'.$boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001310
Andrey Andreev56454792012-05-17 14:32:19 +03001311 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1312 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 }
Eric Barnes6113f542010-12-29 13:36:12 -05001314
Andrey Andreev56454792012-05-17 14:32:19 +03001315 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001316
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001317 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
1319 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001320 }
1321 else
1322 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001323 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 }
1325
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 if ($this->send_multipart !== FALSE)
1327 {
Andrey Andreev3c422792016-06-06 09:44:50 +03001328 $this->_finalbody .= '--'.$boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 }
1330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 return;
1332
Andrey Andreev3c422792016-06-06 09:44:50 +03001333 case 'plain-attach':
Derek Allard2067d1a2008-11-13 22:59:24 +00001334
Andrey Andreev3c422792016-06-06 09:44:50 +03001335 $boundary = uniqid('B_ATC_');
1336 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001337
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001338 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 {
1340 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001341 }
1342
Andrey Andreev2b956af2013-08-07 14:32:56 +03001343 $body .= $this->_get_mime_message().$this->newline
1344 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001345 .'--'.$boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001346 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001347 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1348 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001349 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001350
Andrey Andreev3c422792016-06-06 09:44:50 +03001351 $this->_append_attachments($body, $boundary);
Derek Allard2067d1a2008-11-13 22:59:24 +00001352
Andrey Andreev3c422792016-06-06 09:44:50 +03001353 break;
1354 case 'html-attach':
1355
1356 $alt_boundary = uniqid('B_ALT_');
1357 $last_boundary = NULL;
1358
1359 if ($this->_attachments_have_multipart('mixed'))
1360 {
1361 $atc_boundary = uniqid('B_ATC_');
1362 $hdr .= 'Content-Type: multipart/mixed; boundary="'.$atc_boundary.'"';
1363 $last_boundary = $atc_boundary;
1364 }
1365
1366 if ($this->_attachments_have_multipart('related'))
1367 {
1368 $rel_boundary = uniqid('B_REL_');
1369 $rel_boundary_header = 'Content-Type: multipart/related; boundary="'.$rel_boundary.'"';
1370
1371 if (isset($last_boundary))
1372 {
1373 $body .= '--'.$last_boundary.$this->newline.$rel_boundary_header;
1374 }
1375 else
1376 {
1377 $hdr .= $rel_boundary_header;
1378 }
1379
1380 $last_boundary = $rel_boundary;
1381 }
Eric Barnes6113f542010-12-29 13:36:12 -05001382
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001383 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
1385 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 }
1387
Andrey Andreev3c422792016-06-06 09:44:50 +03001388 strlen($body) && $body .= $this->newline.$this->newline;
Andrey Andreev56454792012-05-17 14:32:19 +03001389 $body .= $this->_get_mime_message().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001390 .'--'.$last_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001391
Andrey Andreev3c422792016-06-06 09:44:50 +03001392 .'Content-Type: multipart/alternative; boundary="'.$alt_boundary.'"'.$this->newline.$this->newline
1393 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001394
Andrey Andreev56454792012-05-17 14:32:19 +03001395 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1396 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001397 .$this->_get_alt_message().$this->newline.$this->newline
1398 .'--'.$alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001399
Andrey Andreev56454792012-05-17 14:32:19 +03001400 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1401 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001402
Andrey Andreev56454792012-05-17 14:32:19 +03001403 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001404 .'--'.$alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001405
Andrey Andreev3c422792016-06-06 09:44:50 +03001406 if ( ! empty($rel_boundary))
1407 {
1408 $body .= $this->newline.$this->newline;
1409 $this->_append_attachments($body, $rel_boundary, 'related');
1410 }
1411
1412 // multipart/mixed attachments
1413 if ( ! empty($atc_boundary))
1414 {
1415 $body .= $this->newline.$this->newline;
1416 $this->_append_attachments($body, $atc_boundary, 'mixed');
1417 }
1418
1419 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
1421
Andrey Andreev2b956af2013-08-07 14:32:56 +03001422 $this->_finalbody = ($this->_get_protocol() === 'mail')
1423 ? $body
1424 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001425
buhay466e8082013-04-17 14:25:34 -07001426 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 // --------------------------------------------------------------------
1430
Andrey Andreev3c422792016-06-06 09:44:50 +03001431 protected function _attachments_have_multipart($type)
1432 {
1433 foreach ($this->_attachments as &$attachment)
1434 {
1435 if ($attachment['multipart'] === $type)
1436 {
1437 return TRUE;
1438 }
1439 }
1440
1441 return FALSE;
1442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
1447 * Prepares attachment string
1448 *
1449 * @param string $body Message body to append to
1450 * @param string $boundary Multipart boundary
1451 * @param string $multipart When provided, only attachments of this type will be processed
1452 * @return string
1453 */
1454 protected function _append_attachments(&$body, $boundary, $multipart = null)
1455 {
1456 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
1457 {
1458 if (isset($multipart) && $this->_attachments[$i]['multipart'] !== $multipart)
1459 {
1460 continue;
1461 }
1462
1463 $name = isset($this->_attachments[$i]['name'][1])
1464 ? $this->_attachments[$i]['name'][1]
1465 : basename($this->_attachments[$i]['name'][0]);
1466
1467 $body .= '--'.$boundary.$this->newline
1468 .'Content-Type: '.$this->_attachments[$i]['type'].'; name="'.$name.'"'.$this->newline
1469 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
1470 .'Content-Transfer-Encoding: base64'.$this->newline
Andrey Andreev9b0f5fa2016-08-01 13:54:06 +03001471 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline)
1472 .$this->newline
Andrey Andreev3c422792016-06-06 09:44:50 +03001473 .$this->_attachments[$i]['content'].$this->newline;
1474 }
1475
1476 // $name won't be set if no attachments were appended,
1477 // and therefore a boundary wouldn't be necessary
1478 empty($name) OR $body .= '--'.$boundary.'--';
1479 }
1480
1481 // --------------------------------------------------------------------
1482
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 /**
1484 * Prep Quoted Printable
1485 *
1486 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1487 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1488 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 * @return string
1491 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001492 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001494 // ASCII code numbers for "safe" characters that can always be
1495 // used literally, without encoding, as described in RFC 2049.
1496 // http://www.ietf.org/rfc/rfc2049.txt
1497 static $ascii_safe_chars = array(
1498 // ' ( ) + , - . / : = ?
1499 39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
1500 // numbers
1501 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
1502 // upper-case letters
1503 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,
1504 // lower-case letters
1505 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
1506 );
1507
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001508 // We are intentionally wrapping so mail servers will encode characters
1509 // properly and MUAs will behave, so {unwrap} must go!
1510 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1511
Andrey Andreev683b34d2012-10-09 15:00:00 +03001512 // RFC 2045 specifies CRLF as "\r\n".
1513 // However, many developers choose to override that and violate
1514 // the RFC rules due to (apparently) a bug in MS Exchange,
1515 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001516 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001517 {
Andrey Andreeva8382792016-07-28 16:40:12 +03001518 return quoted_printable_encode($str);
Andrey Andreev683b34d2012-10-09 15:00:00 +03001519 }
1520
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001521 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001522 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001523
1524 // Standardize newlines
1525 if (strpos($str, "\r") !== FALSE)
1526 {
1527 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1528 }
1529
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 $escape = '=';
1531 $output = '';
1532
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001533 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
1535 $length = strlen($line);
1536 $temp = '';
1537
1538 // Loop through each character in the line to add soft-wrap
1539 // characters at the end of a line " =\r\n" and add the newly
1540 // processed line(s) to the output (see comment on $crlf class property)
1541 for ($i = 0; $i < $length; $i++)
1542 {
1543 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001544 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 $ascii = ord($char);
1546
1547 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001548 if ($ascii === 32 OR $ascii === 9)
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001550 if ($i === ($length - 1))
1551 {
1552 $char = $escape.sprintf('%02s', dechex($ascii));
1553 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001555 // DO NOT move this below the $ascii_safe_chars line!
1556 //
1557 // = (equals) signs are allowed by RFC2049, but must be encoded
1558 // as they are the encoding delimiter!
1559 elseif ($ascii === 61)
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001561 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001563 elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
1564 {
1565 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
1566 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001567
1568 // If we're at the character limit, add the line to the output,
1569 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001570 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
1572 $output .= $temp.$escape.$this->crlf;
1573 $temp = '';
1574 }
1575
1576 // Add the character to our temporary line
1577 $temp .= $char;
1578 }
1579
1580 // Add our completed line to the output
1581 $output .= $temp.$this->crlf;
1582 }
1583
1584 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001585 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 }
1587
1588 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001589
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 /**
1591 * Prep Q Encoding
1592 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001593 * Performs "Q Encoding" on a string for use in email headers.
1594 * It's related but not identical to quoted-printable, so it has its
1595 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001597 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001598 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001600 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001602 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001603
Andrey Andreev925dd902012-10-19 11:06:31 +03001604 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 {
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001606 // Note: We used to have mb_encode_mimeheader() as the first choice
1607 // here, but it turned out to be buggy and unreliable. DO NOT
1608 // re-add it! -- Narf
1609 if (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001610 {
1611 $output = @iconv_mime_encode('', $str,
1612 array(
1613 'scheme' => 'Q',
1614 'line-length' => 76,
1615 'input-charset' => $this->charset,
1616 'output-charset' => $this->charset,
1617 'line-break-chars' => $this->crlf
1618 )
1619 );
1620
1621 // There are reports that iconv_mime_encode() might fail and return FALSE
1622 if ($output !== FALSE)
1623 {
1624 // iconv_mime_encode() will always put a header field name.
1625 // We've passed it an empty one, but it still prepends our
1626 // encoded string with ': ', so we need to strip it.
1627 return substr($output, 2);
1628 }
1629
1630 $chars = iconv_strlen($str, 'UTF-8');
1631 }
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001632 elseif (MB_ENABLED === TRUE)
1633 {
1634 $chars = mb_strlen($str, 'UTF-8');
1635 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 }
1637
Andrey Andreev925dd902012-10-19 11:06:31 +03001638 // We might already have this set for UTF-8
1639 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001640
Andrey Andreev925dd902012-10-19 11:06:31 +03001641 $output = '=?'.$this->charset.'?Q?';
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001642 for ($i = 0, $length = strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001644 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001645 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1646 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001647
Andrey Andreev925dd902012-10-19 11:06:31 +03001648 // RFC 2045 sets a limit of 76 characters per line.
1649 // We'll append ?= to the end of each line though.
1650 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001652 $output .= '?='.$this->crlf // EOL
1653 .' =?'.$this->charset.'?Q?'.$chr; // New line
1654 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001656 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001658 $output .= $chr;
1659 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 }
1662
Andrey Andreev925dd902012-10-19 11:06:31 +03001663 // End the header
1664 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 }
1666
1667 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001668
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 /**
1670 * Send Email
1671 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001672 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 * @return bool
1674 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001675 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001677 if ( ! isset($this->_headers['From']))
Michael Granados0b85d6b2014-11-09 02:43:48 -02001678 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001679 $this->_set_error_message('lang:email_no_from');
Michael Granados0b85d6b2014-11-09 02:43:48 -02001680 return FALSE;
1681 }
1682
Alex Bilbied261b1e2012-06-02 11:12:16 +01001683 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 {
1685 $this->reply_to($this->_headers['From']);
1686 }
1687
Andrey Andreev76f15c92012-03-01 13:05:07 +02001688 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1689 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1690 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
patworkb0707982011-04-08 15:10:05 +02001692 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 return FALSE;
1694 }
1695
1696 $this->_build_headers();
1697
Andrey Andreev76f15c92012-03-01 13:05:07 +02001698 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001700 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001701
Alex Bilbiea87aab32012-07-30 09:50:37 +01001702 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001703 {
1704 $this->clear();
1705 }
1706
Alex Bilbieb901e732012-07-30 09:44:57 +01001707 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 }
1709
buhay466e8082013-04-17 14:25:34 -07001710 if ($this->_build_message() === FALSE)
1711 {
1712 return FALSE;
1713 }
buhayc6da1ef2013-04-18 09:06:49 -07001714
Alex Bilbieb901e732012-07-30 09:44:57 +01001715 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001716
Alex Bilbiea87aab32012-07-30 09:50:37 +01001717 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001718 {
1719 $this->clear();
1720 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001721
Alex Bilbieb901e732012-07-30 09:44:57 +01001722 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 }
Barry Mienydd671972010-10-04 16:33:58 +02001724
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 // --------------------------------------------------------------------
1726
1727 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001728 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001730 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001732 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001734 $float = $this->bcc_batch_size - 1;
1735 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 $chunk = array();
1737
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001738 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
1740 if (isset($this->_bcc_array[$i]))
1741 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001742 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 }
1744
Alex Bilbied261b1e2012-06-02 11:12:16 +01001745 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 {
1747 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001748 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001749 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 }
1751
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001752 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 {
1754 $chunk[] = substr($set, 1);
1755 }
1756 }
1757
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001758 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
1760 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001761
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001762 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001763
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001764 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001766 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
1768 else
1769 {
1770 $this->_bcc_array = $bcc;
1771 }
1772
buhay466e8082013-04-17 14:25:34 -07001773 if ($this->_build_message() === FALSE)
1774 {
1775 return FALSE;
1776 }
buhayc6da1ef2013-04-18 09:06:49 -07001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 $this->_spool_email();
1779 }
1780 }
Barry Mienydd671972010-10-04 16:33:58 +02001781
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 // --------------------------------------------------------------------
1783
1784 /**
1785 * Unwrap special elements
1786 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 * @return void
1788 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001789 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
Andrey Andreev56454792012-05-17 14:32:19 +03001791 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Strip line-breaks via callback
1798 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001799 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 * @return string
1801 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001802 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 {
1804 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1805 {
1806 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1807 }
1808
1809 return $matches[1];
1810 }
Barry Mienydd671972010-10-04 16:33:58 +02001811
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 // --------------------------------------------------------------------
1813
1814 /**
1815 * Spool mail to the mail server
1816 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 * @return bool
1818 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001819 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 {
1821 $this->_unwrap_specials();
1822
Andrey Andreev56454792012-05-17 14:32:19 +03001823 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001824 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
Andrey Andreev56454792012-05-17 14:32:19 +03001826 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001827 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 }
1829
patworkb0707982011-04-08 15:10:05 +02001830 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 return TRUE;
1832 }
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 // --------------------------------------------------------------------
1835
1836 /**
1837 * Send using mail()
1838 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 * @return bool
1840 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001841 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001843 if (is_array($this->_recipients))
1844 {
1845 $this->_recipients = implode(', ', $this->_recipients);
1846 }
1847
Alex Bilbied261b1e2012-06-02 11:12:16 +01001848 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001850 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 }
1852 else
1853 {
1854 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1855 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001856 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 }
1858 }
Barry Mienydd671972010-10-04 16:33:58 +02001859
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 // --------------------------------------------------------------------
1861
1862 /**
1863 * Send using Sendmail
1864 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 * @return bool
1866 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001867 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001869 // is popen() enabled?
1870 if ( ! function_usable('popen')
1871 OR FALSE === ($fp = @popen(
Andrey Andreev9d84d3c2015-12-30 21:50:20 +02001872 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001873 , 'w'))
1874 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001875 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001876 return FALSE;
1877 }
Derek Jones71141ce2010-03-02 16:41:20 -06001878
Derek Jonesc630bcf2008-11-17 21:09:45 +00001879 fputs($fp, $this->_header_str);
1880 fputs($fp, $this->_finalbody);
1881
Barry Mienydd671972010-10-04 16:33:58 +02001882 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001883
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001884 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 {
patworkb0707982011-04-08 15:10:05 +02001886 $this->_set_error_message('lang:email_exit_status', $status);
1887 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 return FALSE;
1889 }
1890
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 return TRUE;
1892 }
Barry Mienydd671972010-10-04 16:33:58 +02001893
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 // --------------------------------------------------------------------
1895
1896 /**
1897 * Send using SMTP
1898 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 * @return bool
1900 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001901 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301902 {
1903 if ($this->smtp_host === '')
1904 {
1905 $this->_set_error_message('lang:email_no_hostname');
1906 return FALSE;
1907 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001908
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301909 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1910 {
1911 return FALSE;
1912 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001913
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001914 if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1915 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001916 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001917 return FALSE;
1918 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001919
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301920 foreach ($this->_recipients as $val)
1921 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001922 if ( ! $this->_send_command('to', $val))
1923 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001924 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001925 return FALSE;
1926 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301927 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001928
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301929 if (count($this->_cc_array) > 0)
1930 {
1931 foreach ($this->_cc_array as $val)
1932 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001933 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301934 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001935 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001936 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301937 }
1938 }
1939 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001940
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301941 if (count($this->_bcc_array) > 0)
1942 {
1943 foreach ($this->_bcc_array as $val)
1944 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001945 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301946 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001947 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001948 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301949 }
1950 }
1951 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001952
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001953 if ( ! $this->_send_command('data'))
1954 {
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001955 $this->_smtp_end();
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001956 return FALSE;
1957 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001958
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301959 // perform dot transformation on any lines that begin with a dot
1960 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001961
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301962 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001963
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301964 $reply = $this->_get_smtp_data();
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301965 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001966
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001967 $this->_smtp_end();
1968
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301969 if (strpos($reply, '250') !== 0)
1970 {
1971 $this->_set_error_message('lang:email_smtp_error', $reply);
1972 return FALSE;
1973 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001974
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301975 return TRUE;
1976 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001977
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301978 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001979
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301980 /**
Andrey Andreev0c821bb2016-07-19 16:04:02 +03001981 * SMTP End
1982 *
1983 * Shortcut to send RSET or QUIT depending on keep-alive
1984 *
1985 * @return void
1986 */
1987 protected function _smtp_end()
1988 {
1989 ($this->smtp_keepalive)
1990 ? $this->_send_command('reset')
1991 : $this->_send_command('quit');
1992 }
1993
1994 // --------------------------------------------------------------------
1995
1996 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301997 * SMTP Connect
1998 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301999 * @return string
2000 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302001 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302002 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302003 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302004 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302005 return TRUE;
2006 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002007
Andrey Andreev89b67b42013-03-12 19:34:23 +02002008 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002009
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302010 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
2011 $this->smtp_port,
2012 $errno,
2013 $errstr,
2014 $this->smtp_timeout);
2015
2016 if ( ! is_resource($this->_smtp_connect))
2017 {
2018 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
2019 return FALSE;
2020 }
2021
2022 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
2023 $this->_set_error_message($this->_get_smtp_data());
2024
2025 if ($this->smtp_crypto === 'tls')
2026 {
2027 $this->_send_command('hello');
2028 $this->_send_command('starttls');
2029
2030 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
2031
2032 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302033 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302034 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302035 return FALSE;
2036 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302037 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05302038
2039 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302040 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002041
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302042 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002043
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302044 /**
2045 * Send SMTP command
2046 *
2047 * @param string
2048 * @param string
Andrey Andreev19277092016-08-22 11:34:56 +03002049 * @return bool
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302050 */
2051 protected function _send_command($cmd, $data = '')
2052 {
2053 switch ($cmd)
2054 {
2055 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002056
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302057 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2058 {
2059 $this->_send_data('EHLO '.$this->_get_hostname());
2060 }
2061 else
2062 {
2063 $this->_send_data('HELO '.$this->_get_hostname());
2064 }
Radu Potopbbf04b02011-09-28 13:57:51 +03002065
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302066 $resp = 250;
2067 break;
2068 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002069
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302070 $this->_send_data('STARTTLS');
2071 $resp = 220;
2072 break;
2073 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03002074
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302075 $this->_send_data('MAIL FROM:<'.$data.'>');
2076 $resp = 250;
2077 break;
2078 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03002079
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302080 if ($this->dsn)
2081 {
2082 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2083 }
2084 else
2085 {
2086 $this->_send_data('RCPT TO:<'.$data.'>');
2087 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002088
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302089 $resp = 250;
2090 break;
2091 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002092
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302093 $this->_send_data('DATA');
2094 $resp = 354;
2095 break;
2096 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002097
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302098 $this->_send_data('RSET');
2099 $resp = 250;
2100 break;
2101 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002102
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302103 $this->_send_data('QUIT');
2104 $resp = 221;
2105 break;
2106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002107
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302108 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002109
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302110 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002111
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302112 if ((int) substr($reply, 0, 3) !== $resp)
2113 {
2114 $this->_set_error_message('lang:email_smtp_error', $reply);
2115 return FALSE;
2116 }
Barry Mienydd671972010-10-04 16:33:58 +02002117
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302118 if ($cmd === 'quit')
2119 {
2120 fclose($this->_smtp_connect);
2121 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002122
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302123 return TRUE;
2124 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002125
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302126 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002127
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302128 /**
2129 * SMTP Authenticate
2130 *
2131 * @return bool
2132 */
2133 protected function _smtp_authenticate()
2134 {
2135 if ( ! $this->_smtp_auth)
2136 {
2137 return TRUE;
2138 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002139
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302140 if ($this->smtp_user === '' && $this->smtp_pass === '')
2141 {
2142 $this->_set_error_message('lang:email_no_smtp_unpw');
2143 return FALSE;
2144 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002145
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302146 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002147
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302148 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002149
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002150 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302151 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302152 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302153 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002154 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302155 {
2156 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2157 return FALSE;
2158 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002159
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302160 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002161
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302162 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002163
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302164 if (strpos($reply, '334') !== 0)
2165 {
2166 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2167 return FALSE;
2168 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002169
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302170 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002171
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302172 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302173
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302174 if (strpos($reply, '235') !== 0)
2175 {
2176 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2177 return FALSE;
2178 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302179
Andrey Andreev84253192016-05-09 12:24:52 +03002180 if ($this->smtp_keepalive)
2181 {
2182 $this->_smtp_auth = FALSE;
2183 }
2184
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302185 return TRUE;
2186 }
Barry Mienydd671972010-10-04 16:33:58 +02002187
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 // --------------------------------------------------------------------
2189
2190 /**
2191 * Send SMTP data
2192 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002193 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 * @return bool
2195 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002196 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002198 $data .= $this->newline;
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002199 for ($written = $timestamp = 0, $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002200 {
2201 if (($result = fwrite($this->_smtp_connect, substr($data, $written))) === FALSE)
2202 {
2203 break;
2204 }
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002205 // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2206 elseif ($result === 0)
2207 {
2208 if ($timestamp === 0)
2209 {
2210 $timestamp = time();
2211 }
2212 elseif ($timestamp < (time() - $this->smtp_timeout))
2213 {
2214 $result = FALSE;
2215 break;
2216 }
2217
2218 usleep(250000);
2219 continue;
2220 }
2221 else
2222 {
2223 $timestamp = 0;
2224 }
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002225 }
2226
2227 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 {
patworkb0707982011-04-08 15:10:05 +02002229 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 return FALSE;
2231 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002232
2233 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 }
Barry Mienydd671972010-10-04 16:33:58 +02002235
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 // --------------------------------------------------------------------
2237
2238 /**
2239 * Get SMTP data
2240 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 * @return string
2242 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002243 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 {
Andrey Andreev56454792012-05-17 14:32:19 +03002245 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002246
2247 while ($str = fgets($this->_smtp_connect, 512))
2248 {
2249 $data .= $str;
2250
Alex Bilbied261b1e2012-06-02 11:12:16 +01002251 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 {
2253 break;
2254 }
2255 }
2256
2257 return $data;
2258 }
Barry Mienydd671972010-10-04 16:33:58 +02002259
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 // --------------------------------------------------------------------
2261
2262 /**
2263 * Get Hostname
Andrey Andreev396eb892015-02-06 14:50:10 +02002264 *
2265 * There are only two legal types of hostname - either a fully
2266 * qualified domain name (eg: "mail.example.com") or an IP literal
2267 * (eg: "[1.2.3.4]").
2268 *
2269 * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
James Wade3245af42015-02-06 11:48:51 +00002270 * @link http://cbl.abuseat.org/namingproblems.html
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 * @return string
2272 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002273 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 {
Andrey Andreev396eb892015-02-06 14:50:10 +02002275 if (isset($_SERVER['SERVER_NAME']))
2276 {
2277 return $_SERVER['SERVER_NAME'];
2278 }
2279
2280 return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
Derek Allard2067d1a2008-11-13 22:59:24 +00002281 }
Barry Mienydd671972010-10-04 16:33:58 +02002282
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 // --------------------------------------------------------------------
2284
2285 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 * Get Debug Message
2287 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002288 * @param array $include List of raw data chunks to include in the output
2289 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 * @return string
2291 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002292 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 {
2294 $msg = '';
2295
2296 if (count($this->_debug_msg) > 0)
2297 {
2298 foreach ($this->_debug_msg as $val)
2299 {
2300 $msg .= $val;
2301 }
2302 }
2303
Andrey Andreev61797f62012-11-26 16:15:12 +02002304 // Determine which parts of our raw data needs to be printed
2305 $raw_data = '';
2306 is_array($include) OR $include = array($include);
2307
2308 if (in_array('headers', $include, TRUE))
2309 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002310 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002311 }
2312
2313 if (in_array('subject', $include, TRUE))
2314 {
2315 $raw_data .= htmlspecialchars($this->_subject)."\n";
2316 }
2317
2318 if (in_array('body', $include, TRUE))
2319 {
2320 $raw_data .= htmlspecialchars($this->_finalbody);
2321 }
2322
2323 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 }
Barry Mienydd671972010-10-04 16:33:58 +02002325
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 // --------------------------------------------------------------------
2327
2328 /**
2329 * Set Message
2330 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002331 * @param string $msg
2332 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002333 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002335 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 {
2337 $CI =& get_instance();
2338 $CI->lang->load('email');
2339
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002340 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 {
Andrey Andreev56454792012-05-17 14:32:19 +03002342 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 }
2344 else
2345 {
Andrey Andreev56454792012-05-17 14:32:19 +03002346 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002347 }
2348 }
Barry Mienydd671972010-10-04 16:33:58 +02002349
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 // --------------------------------------------------------------------
2351
2352 /**
2353 * Mime Types
2354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002355 * @param string
2356 * @return string
2357 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002358 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002359 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002360 $ext = strtolower($ext);
2361
vlakoff66c7bb42014-05-19 13:45:12 +02002362 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002363
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002364 if (isset($mimes[$ext]))
2365 {
2366 return is_array($mimes[$ext])
2367 ? current($mimes[$ext])
2368 : $mimes[$ext];
2369 }
2370
2371 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002372 }
2373
Andrey Andreev8c95c3d2016-05-09 12:35:41 +03002374 // --------------------------------------------------------------------
2375
2376 /**
2377 * Destructor
2378 *
2379 * @return void
2380 */
2381 public function __destruct()
2382 {
2383 is_resource($this->_smtp_connect) && $this->_send_command('quit');
2384 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002385}