blob: 6ff3efad97000f0ec518a40bf133922a8c2d9183 [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 /**
153 * Multipart message
154 *
155 * @var string 'mixed' (in the body) or 'related' (separate)
156 */
157 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
158
159 /**
160 * Alternative message (for HTML messages only)
161 *
162 * @var string
163 */
164 public $alt_message = '';
165
166 /**
167 * Whether to validate e-mail addresses.
168 *
169 * @var bool
170 */
171 public $validate = FALSE;
172
173 /**
174 * X-Priority header value.
175 *
176 * @var int 1-5
177 */
178 public $priority = 3; // Default priority (1 - 5)
179
180 /**
181 * Newline character sequence.
182 * Use "\r\n" to comply with RFC 822.
183 *
184 * @link http://www.ietf.org/rfc/rfc822.txt
185 * @var string "\r\n" or "\n"
186 */
187 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
188
189 /**
190 * CRLF character sequence
191 *
192 * RFC 2045 specifies that for 'quoted-printable' encoding,
193 * "\r\n" must be used. However, it appears that some servers
194 * (even on the receiving end) don't handle it properly and
195 * switching to "\n", while improper, is the only solution
196 * that seems to work for all environments.
197 *
198 * @link http://www.ietf.org/rfc/rfc822.txt
199 * @var string
200 */
201 public $crlf = "\n";
202
203 /**
204 * Whether to use Delivery Status Notification.
205 *
206 * @var bool
207 */
208 public $dsn = FALSE;
209
210 /**
211 * Whether to send multipart alternatives.
212 * Yahoo! doesn't seem to like these.
213 *
214 * @var bool
215 */
216 public $send_multipart = TRUE;
217
218 /**
219 * Whether to send messages to BCC recipients in batches.
220 *
221 * @var bool
222 */
223 public $bcc_batch_mode = FALSE;
224
225 /**
226 * BCC Batch max number size.
227 *
228 * @see CI_Email::$bcc_batch_mode
229 * @var int
230 */
231 public $bcc_batch_size = 200;
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Whether PHP is running in safe mode. Initialized by the class constructor.
237 *
238 * @var bool
239 */
Andrey Andreev50814092012-03-01 12:36:12 +0200240 protected $_safe_mode = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200241
242 /**
243 * Subject header
244 *
245 * @var string
246 */
Andrey Andreev50814092012-03-01 12:36:12 +0200247 protected $_subject = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200248
249 /**
250 * Message body
251 *
252 * @var string
253 */
Andrey Andreev50814092012-03-01 12:36:12 +0200254 protected $_body = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200255
256 /**
257 * Final message body to be sent.
258 *
259 * @var string
260 */
Andrey Andreev50814092012-03-01 12:36:12 +0200261 protected $_finalbody = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200262
263 /**
264 * multipart/alternative boundary
265 *
266 * @var string
267 */
Andrey Andreev50814092012-03-01 12:36:12 +0200268 protected $_alt_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200269
270 /**
271 * Attachment boundary
272 *
273 * @var string
274 */
Andrey Andreev50814092012-03-01 12:36:12 +0200275 protected $_atc_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200276
277 /**
278 * Final headers to send
279 *
280 * @var string
281 */
Andrey Andreev50814092012-03-01 12:36:12 +0200282 protected $_header_str = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200283
284 /**
285 * SMTP Connection socket placeholder
286 *
287 * @var resource
288 */
Andrey Andreev50814092012-03-01 12:36:12 +0200289 protected $_smtp_connect = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200290
291 /**
292 * Mail encoding
293 *
294 * @var string '8bit' or '7bit'
295 */
Andrey Andreev50814092012-03-01 12:36:12 +0200296 protected $_encoding = '8bit';
Andrey Andreev597ea272012-11-01 22:56:26 +0200297
298 /**
299 * Whether to perform SMTP authentication
300 *
301 * @var bool
302 */
Andrey Andreev50814092012-03-01 12:36:12 +0200303 protected $_smtp_auth = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200304
305 /**
306 * Whether to send a Reply-To header
307 *
308 * @var bool
309 */
Andrey Andreev50814092012-03-01 12:36:12 +0200310 protected $_replyto_flag = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200311
312 /**
313 * Debug messages
314 *
315 * @see CI_Email::print_debugger()
316 * @var string
317 */
Andrey Andreev50814092012-03-01 12:36:12 +0200318 protected $_debug_msg = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200319
320 /**
321 * Recipients
322 *
323 * @var string[]
324 */
Andrey Andreev50814092012-03-01 12:36:12 +0200325 protected $_recipients = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200326
327 /**
328 * CC Recipients
329 *
330 * @var string[]
331 */
Andrey Andreev50814092012-03-01 12:36:12 +0200332 protected $_cc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200333
334 /**
335 * BCC Recipients
336 *
337 * @var string[]
338 */
Andrey Andreev50814092012-03-01 12:36:12 +0200339 protected $_bcc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200340
341 /**
342 * Message headers
343 *
344 * @var string[]
345 */
Andrey Andreev50814092012-03-01 12:36:12 +0200346 protected $_headers = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200347
348 /**
349 * Attachment data
350 *
351 * @var array
352 */
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300353 protected $_attachments = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200354
355 /**
356 * Valid $protocol values
357 *
358 * @see CI_Email::$protocol
359 * @var string[]
360 */
Andrey Andreev50814092012-03-01 12:36:12 +0200361 protected $_protocols = array('mail', 'sendmail', 'smtp');
Andrey Andreev597ea272012-11-01 22:56:26 +0200362
363 /**
364 * Base charsets
365 *
366 * Character sets valid for 7-bit encoding,
367 * excluding language suffix.
368 *
369 * @var string[]
370 */
371 protected $_base_charsets = array('us-ascii', 'iso-2022-');
372
373 /**
374 * Bit depths
375 *
376 * Valid mail encodings
377 *
378 * @see CI_Email::$_encoding
379 * @var string[]
380 */
Andrey Andreev50814092012-03-01 12:36:12 +0200381 protected $_bit_depths = array('7bit', '8bit');
Andrey Andreev597ea272012-11-01 22:56:26 +0200382
383 /**
384 * $priority translations
385 *
386 * Actual values to send with the X-Priority header
387 *
388 * @var string[]
389 */
Andrey Andreevdea61772014-02-24 16:36:25 +0200390 protected $_priorities = array(
391 1 => '1 (Highest)',
392 2 => '2 (High)',
393 3 => '3 (Normal)',
394 4 => '4 (Low)',
395 5 => '5 (Lowest)'
396 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000397
Andrey Andreev597ea272012-11-01 22:56:26 +0200398 // --------------------------------------------------------------------
399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 /**
401 * Constructor - Sets Email Preferences
402 *
403 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300404 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300405 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300406 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 */
Andrey Andreev0b1fd2c2015-03-10 20:00:19 +0200408 public function __construct(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300410 $this->charset = config_item('charset');
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300411 $this->initialize($config);
Andrey Andreevf6274742014-02-20 18:05:58 +0200412 $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300413
Andrey Andreev90726b82015-01-20 12:39:22 +0200414 log_message('info', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530416
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530417 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100418
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530419 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * Initialize preferences
421 *
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300422 * @param array $config
Andrew Podner4296a652012-12-17 07:51:15 -0500423 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300425 public function initialize(array $config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300427 $this->clear();
428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 foreach ($config as $key => $val)
430 {
431 if (isset($this->$key))
432 {
433 $method = 'set_'.$key;
434
435 if (method_exists($this, $method))
436 {
437 $this->$method($val);
438 }
439 else
440 {
441 $this->$key = $val;
442 }
443 }
444 }
445
Andrey Andreev8c95c3d2016-05-09 12:35:41 +0300446 $this->charset = strtoupper($this->charset);
447 $this->_smtp_auth = isset($this->smtp_user[0], $this->smtp_pass[0]);
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000448
449 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Initialize the Email Data
456 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800457 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500458 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000460 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200462 $this->_subject = '';
463 $this->_body = '';
464 $this->_finalbody = '';
465 $this->_header_str = '';
466 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500468 $this->_cc_array = array();
469 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 $this->_headers = array();
471 $this->_debug_msg = array();
472
Mickey Wubfc1cad2012-05-31 22:28:40 -0700473 $this->set_header('User-Agent', $this->useragent);
474 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
476 if ($clear_attachments !== FALSE)
477 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300478 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Eric Barnes6113f542010-12-29 13:36:12 -0500480
Greg Akera769deb2010-11-10 15:47:29 -0600481 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
487 * Set FROM
488 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300489 * @param string $from
490 * @param string $name
491 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500492 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300494 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200496 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200498 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 if ($this->validate)
502 {
503 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300504 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200505 {
506 $this->validate_email($this->_str_to_array($return_path));
507 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100511 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 // only use Q encoding if there are characters that would require it
514 if ( ! preg_match('/[\200-\377]/', $name))
515 {
516 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000517 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519 else
520 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300521 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523 }
524
Mickey Wubfc1cad2012-05-31 22:28:40 -0700525 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200526
Andrey Andreev925dd902012-10-19 11:06:31 +0300527 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200528 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500529
Greg Akera769deb2010-11-10 15:47:29 -0600530 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // --------------------------------------------------------------------
534
535 /**
536 * Set Reply-to
537 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string
539 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500540 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000542 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200544 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200546 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 if ($this->validate)
550 {
551 $this->validate_email($this->_str_to_array($replyto));
552 }
553
Andrey Andreevc70216d2016-01-20 17:25:13 +0200554 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Andrey Andreevc70216d2016-01-20 17:25:13 +0200556 // only use Q encoding if there are characters that would require it
557 if ( ! preg_match('/[\200-\377]/', $name))
558 {
559 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
560 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
561 }
562 else
563 {
564 $name = $this->_prep_q_encoding($name);
565 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567
Mickey Wubfc1cad2012-05-31 22:28:40 -0700568 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600570
571 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // --------------------------------------------------------------------
575
576 /**
577 * Set Recipients
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500580 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000582 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 $to = $this->_str_to_array($to);
585 $to = $this->clean_email($to);
586
587 if ($this->validate)
588 {
589 $this->validate_email($to);
590 }
591
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200592 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700594 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
596
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300597 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500598
Greg Akera769deb2010-11-10 15:47:29 -0600599 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 // --------------------------------------------------------------------
603
604 /**
605 * Set CC
606 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500608 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000610 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Andrey Andreev56454792012-05-17 14:32:19 +0300612 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000613
614 if ($this->validate)
615 {
616 $this->validate_email($cc);
617 }
618
Mickey Wubfc1cad2012-05-31 22:28:40 -0700619 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000620
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200621 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 $this->_cc_array = $cc;
624 }
Eric Barnes6113f542010-12-29 13:36:12 -0500625
Greg Akera769deb2010-11-10 15:47:29 -0600626 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * Set BCC
633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param string
635 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500636 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000638 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100640 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 $this->bcc_batch_mode = TRUE;
643 $this->bcc_batch_size = $limit;
644 }
645
Andrey Andreev56454792012-05-17 14:32:19 +0300646 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000647
648 if ($this->validate)
649 {
650 $this->validate_email($bcc);
651 }
652
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200653 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 $this->_bcc_array = $bcc;
656 }
657 else
658 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700659 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Eric Barnes6113f542010-12-29 13:36:12 -0500661
Greg Akera769deb2010-11-10 15:47:29 -0600662 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 // --------------------------------------------------------------------
666
667 /**
668 * Set Email Subject
669 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500671 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000673 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700676 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600677 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Set Body
684 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500686 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000688 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200690 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200691
Andrey Andreevaf728622011-10-20 10:11:59 +0300692 /* strip slashes only if magic quotes is ON
693 if we do it with magic quotes OFF, it strips real, user-inputted chars.
694
695 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
696 it will probably not exist in future versions at all.
697 */
698 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200699 {
diegorivera33c32802011-10-19 02:56:15 -0200700 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200701 }
diegorivera33c32802011-10-19 02:56:15 -0200702
Greg Akera769deb2010-11-10 15:47:29 -0600703 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // --------------------------------------------------------------------
707
708 /**
709 * Assign file attachments
710 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100711 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300712 * @param string $disposition = 'attachment'
713 * @param string $newname = NULL
714 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500715 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100717 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100719 if ($mime === '')
720 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100721 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100722 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100723 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100724 return FALSE;
725 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200726
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200727 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100728 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100729 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100730 return FALSE;
731 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100732
Petr Heralecky300e3f02014-01-10 11:49:11 +0100733 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100734 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100735 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100736 }
737 else
738 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100739 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100740 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200741
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300742 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100743 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300744 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100745 'type' => $mime,
746 'content' => chunk_split(base64_encode($file_content))
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300747 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100748
Greg Akera769deb2010-11-10 15:47:29 -0600749 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200751
Petr Heralecky300e3f02014-01-10 11:49:11 +0100752 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200753
Petr Heralecky300e3f02014-01-10 11:49:11 +0100754 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200755 * Set and return attachment Content-ID
756 *
757 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100758 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100759 * @param string $filename
760 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100761 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200762 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100763 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100764 if ($this->multipart !== 'related')
Petr Heralecky300e3f02014-01-10 11:49:11 +0100765 {
766 $this->multipart = 'related'; // Thunderbird need this for inline images
767 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200768
Petr Heraleckyde886152014-01-10 12:52:56 +0100769 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100770 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100771 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100772 {
Petr Heralecky230fca32014-01-10 12:55:57 +0100773 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100774 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100775 }
776 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200777
Petr Heralecky300e3f02014-01-10 11:49:11 +0100778 return FALSE;
779 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000780
781 // --------------------------------------------------------------------
782
783 /**
784 * Add a Header Item
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string
787 * @param string
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300788 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700790 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
florisluitenf55d5142013-06-07 17:20:06 +0300792 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300793 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 // --------------------------------------------------------------------
797
798 /**
799 * Convert a String to an Array
800 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 * @param string
802 * @return array
803 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600804 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 if ( ! is_array($email))
807 {
Andrey Andreev56454792012-05-17 14:32:19 +0300808 return (strpos($email, ',') !== FALSE)
809 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
810 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
Andrey Andreev56454792012-05-17 14:32:19 +0300812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 return $email;
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 // --------------------------------------------------------------------
817
818 /**
819 * Set Multipart Value
820 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500822 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200824 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400826 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600827 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 }
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 // --------------------------------------------------------------------
831
832 /**
833 * Set Mailtype
834 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500836 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000838 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Andrey Andreev56454792012-05-17 14:32:19 +0300840 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600841 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 }
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 /**
847 * Set Wordwrap
848 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400849 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500850 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000852 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400854 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600855 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 // --------------------------------------------------------------------
859
860 /**
861 * Set Protocol
862 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500864 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000866 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200868 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600869 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 // --------------------------------------------------------------------
873
874 /**
875 * Set Priority
876 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200877 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500878 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000880 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200882 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600883 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
889 * Set Newline Character
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500892 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000894 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200896 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600897 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
901
902 /**
903 * Set CRLF
904 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500906 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000908 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200910 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600911 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
915
916 /**
917 * Set Message Boundary
918 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 * @return void
920 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600921 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200923 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
924 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
930 * Get the Message ID
931 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @return string
933 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600934 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200936 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300937 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 // --------------------------------------------------------------------
941
942 /**
943 * Get Mail Protocol
944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200946 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600948 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200951 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000952
Alex Bilbied261b1e2012-06-02 11:12:16 +0100953 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
955 return $this->protocol;
956 }
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // --------------------------------------------------------------------
960
961 /**
962 * Get Mail Encoding
963 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * @param bool
965 * @return string
966 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600967 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200969 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
971 foreach ($this->_base_charsets as $charset)
972 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300973 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
975 $this->_encoding = '7bit';
976 }
977 }
978
Alex Bilbied261b1e2012-06-02 11:12:16 +0100979 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
981 return $this->_encoding;
982 }
983 }
984
985 // --------------------------------------------------------------------
986
987 /**
988 * Get content type (text/html/attachment)
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @return string
991 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600992 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev56454792012-05-17 14:32:19 +0300994 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300996 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300998 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
1000 return 'plain-attach';
1001 }
1002 else
1003 {
1004 return 'plain';
1005 }
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 // --------------------------------------------------------------------
1009
1010 /**
1011 * Set RFC 822 Date
1012 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 * @return string
1014 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001015 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001017 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001018 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001020 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +00001021
Andrey Andreev59c5b532012-03-01 14:09:51 +02001022 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 }
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Mime message
1029 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * @return string
1031 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001032 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001034 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 +00001035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Validate Email Address
1041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @param string
1043 * @return bool
1044 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001045 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
1047 if ( ! is_array($email))
1048 {
patworkb0707982011-04-08 15:10:05 +02001049 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 return FALSE;
1051 }
1052
1053 foreach ($email as $val)
1054 {
1055 if ( ! $this->valid_email($val))
1056 {
patworkb0707982011-04-08 15:10:05 +02001057 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 return FALSE;
1059 }
1060 }
1061
1062 return TRUE;
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 // --------------------------------------------------------------------
1066
1067 /**
1068 * Email Validation
1069 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 * @param string
1071 * @return bool
1072 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001073 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Andrey Andreev95496662014-06-01 00:00:13 +03001075 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1076 {
1077 $email = substr($email, 0, ++$atpos).idn_to_ascii(substr($email, $atpos));
1078 }
1079
Andrey Andreev580388b2012-06-27 15:43:46 +03001080 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1087 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 * @param string
1089 * @return string
1090 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001091 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
1093 if ( ! is_array($email))
1094 {
Andrey Andreev56454792012-05-17 14:32:19 +03001095 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
1097
1098 $clean_email = array();
1099
1100 foreach ($email as $addy)
1101 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001102 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104
1105 return $clean_email;
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Build alternative plain text message
1112 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001113 * Provides the raw message for use in plain-text headers of
1114 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * If the user hasn't specified his own alternative message
1116 * it creates one by stripping the HTML
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @return string
1119 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001120 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001122 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001124 return ($this->wordwrap)
1125 ? $this->word_wrap($this->alt_message, 76)
1126 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 }
1128
Andrey Andreev56454792012-05-17 14:32:19 +03001129 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001131
1132 for ($i = 20; $i >= 3; $i--)
1133 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001134 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 }
1136
GDmac9bea4be2012-10-30 06:14:19 +01001137 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001138 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001139
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001140 return ($this->wordwrap)
1141 ? $this->word_wrap($body, 76)
1142 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
1146
1147 /**
1148 * Word Wrap
1149 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001151 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @return string
1153 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001154 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001156 // Set the character limit, if not already present
1157 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001159 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
1161
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 // Standardize newlines
1163 if (strpos($str, "\r") !== FALSE)
1164 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001165 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
1167
GDmac9bea4be2012-10-30 06:14:19 +01001168 // Reduce multiple spaces at end of line
1169 $str = preg_replace('| +\n|', "\n", $str);
1170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 // If the current word is surrounded by {unwrap} tags we'll
1172 // strip the entire chunk and replace it with a marker.
1173 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001174 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001176 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001178 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001179 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
1181 }
1182
vlakofff0ab8132014-05-20 10:32:53 +02001183 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001185 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 $str = wordwrap($str, $charlim, "\n", FALSE);
1187
1188 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001189 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 foreach (explode("\n", $str) as $line)
1191 {
1192 // Is the line within the allowed character count?
1193 // If so we'll join it to the output and continue
vlakofff0ab8132014-05-20 10:32:53 +02001194 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $output .= $line.$this->newline;
1197 continue;
1198 }
1199
1200 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001201 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
1203 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001204 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 break;
1207 }
1208
1209 // Trim the word down
vlakofff0ab8132014-05-20 10:32:53 +02001210 $temp .= mb_substr($line, 0, $charlim - 1);
1211 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 }
vlakofff0ab8132014-05-20 10:32:53 +02001213 while (mb_strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001214
1215 // If $temp contains data it means we had to split up an over-length
1216 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001217 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001219 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 }
1221
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001222 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
1224
1225 // Put our markers back
1226 if (count($unwrap) > 0)
1227 {
1228 foreach ($unwrap as $key => $val)
1229 {
Andrey Andreev56454792012-05-17 14:32:19 +03001230 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232 }
1233
1234 return $output;
1235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Build final headers
1241 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 * @return string
1243 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001244 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001246 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1247 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001248 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001249 $this->set_header('Message-ID', $this->_get_message_id());
1250 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // --------------------------------------------------------------------
1254
1255 /**
1256 * Write Headers as a string
1257 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 * @return void
1259 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001260 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001262 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301264 if (isset($this->_headers['Subject']))
1265 {
1266 $this->_subject = $this->_headers['Subject'];
1267 unset($this->_headers['Subject']);
1268 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 }
1270
1271 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001272 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001273
Pascal Kriete14287f32011-02-14 13:39:34 -05001274 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
1276 $val = trim($val);
1277
Alex Bilbied261b1e2012-06-02 11:12:16 +01001278 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreev56454792012-05-17 14:32:19 +03001280 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
1282 }
1283
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001284 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 {
Derek Jones1d890882009-02-10 20:32:14 +00001286 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 }
1288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 // --------------------------------------------------------------------
1291
1292 /**
1293 * Build Final Body and attachments
1294 *
buhayc6da1ef2013-04-18 09:06:49 -07001295 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001297 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001299 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 {
1301 $this->_body = $this->word_wrap($this->_body);
1302 }
1303
1304 $this->_set_boundaries();
1305 $this->_write_headers();
1306
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001307 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001308 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001309
1310 switch ($this->_get_content_type())
1311 {
1312 case 'plain' :
1313
Andrey Andreev56454792012-05-17 14:32:19 +03001314 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1315 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001316
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;
1320 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 }
Brandon Jones485d7412010-11-09 16:38:17 -05001322 else
1323 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001324 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001325 }
Eric Barnes6113f542010-12-29 13:36:12 -05001326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 return;
1328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 case 'html' :
1330
1331 if ($this->send_multipart === FALSE)
1332 {
Andrey Andreev56454792012-05-17 14:32:19 +03001333 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001334 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 }
1336 else
1337 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001338 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001339
Andrey Andreev56454792012-05-17 14:32:19 +03001340 $body .= $this->_get_mime_message().$this->newline.$this->newline
1341 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001342
Andrey Andreev56454792012-05-17 14:32:19 +03001343 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1344 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1345 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001346
Andrey Andreev56454792012-05-17 14:32:19 +03001347 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1348 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 }
Eric Barnes6113f542010-12-29 13:36:12 -05001350
Andrey Andreev56454792012-05-17 14:32:19 +03001351 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001352
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001353 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
1355 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001356 }
1357 else
1358 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001359 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 }
1361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 if ($this->send_multipart !== FALSE)
1363 {
Andrey Andreev56454792012-05-17 14:32:19 +03001364 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 }
1366
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 return;
1368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 case 'plain-attach' :
1370
Andrey Andreev2b956af2013-08-07 14:32:56 +03001371 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001372
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001373 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
1375 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001376 }
1377
Andrey Andreev2b956af2013-08-07 14:32:56 +03001378 $body .= $this->_get_mime_message().$this->newline
1379 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001380 .'--'.$this->_atc_boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001381 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001382 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1383 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001384 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001385
1386 break;
1387 case 'html-attach' :
1388
Andrey Andreev2b956af2013-08-07 14:32:56 +03001389 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Eric Barnes6113f542010-12-29 13:36:12 -05001390
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001391 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 {
1393 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
1395
Andrey Andreev56454792012-05-17 14:32:19 +03001396 $body .= $this->_get_mime_message().$this->newline.$this->newline
1397 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001398
Andrey Andreev56454792012-05-17 14:32:19 +03001399 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1400 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001401
Andrey Andreev56454792012-05-17 14:32:19 +03001402 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1403 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1404 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001405
Andrey Andreev56454792012-05-17 14:32:19 +03001406 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1407 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001408
Andrey Andreev56454792012-05-17 14:32:19 +03001409 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1410 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001411
1412 break;
1413 }
1414
1415 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001416 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001418 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001419 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001420 ? basename($filename) : $this->_attachments[$i]['name'][1];
Andrey Andreev56454792012-05-17 14:32:19 +03001421
1422 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001423 .'Content-type: '.$this->_attachments[$i]['type'].'; '
Andrey Andreev56454792012-05-17 14:32:19 +03001424 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001425 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001426 .'Content-Transfer-Encoding: base64'.$this->newline
Petr Heraleckyde886152014-01-10 12:52:56 +01001427 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
Derek Allard2067d1a2008-11-13 22:59:24 +00001428
Petr Heralecky300e3f02014-01-10 11:49:11 +01001429 $attachment[$z++] = $this->_attachments[$i]['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 }
1431
Andrey Andreev56454792012-05-17 14:32:19 +03001432 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
Andrey Andreev2b956af2013-08-07 14:32:56 +03001433 $this->_finalbody = ($this->_get_protocol() === 'mail')
1434 ? $body
1435 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001436
buhay466e8082013-04-17 14:25:34 -07001437 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 // --------------------------------------------------------------------
1441
1442 /**
1443 * Prep Quoted Printable
1444 *
1445 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1446 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1447 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 * @return string
1450 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001451 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001453 // ASCII code numbers for "safe" characters that can always be
1454 // used literally, without encoding, as described in RFC 2049.
1455 // http://www.ietf.org/rfc/rfc2049.txt
1456 static $ascii_safe_chars = array(
1457 // ' ( ) + , - . / : = ?
1458 39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
1459 // numbers
1460 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
1461 // upper-case letters
1462 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,
1463 // lower-case letters
1464 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
1465 );
1466
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001467 // We are intentionally wrapping so mail servers will encode characters
1468 // properly and MUAs will behave, so {unwrap} must go!
1469 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1470
Andrey Andreev683b34d2012-10-09 15:00:00 +03001471 // RFC 2045 specifies CRLF as "\r\n".
1472 // However, many developers choose to override that and violate
1473 // the RFC rules due to (apparently) a bug in MS Exchange,
1474 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001475 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001476 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001477 if (is_php('5.3'))
1478 {
1479 return quoted_printable_encode($str);
1480 }
1481 elseif (function_exists('imap_8bit'))
1482 {
1483 return imap_8bit($str);
1484 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001485 }
1486
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001487 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001488 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001489
1490 // Standardize newlines
1491 if (strpos($str, "\r") !== FALSE)
1492 {
1493 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1494 }
1495
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 $escape = '=';
1497 $output = '';
1498
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001499 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 {
1501 $length = strlen($line);
1502 $temp = '';
1503
1504 // Loop through each character in the line to add soft-wrap
1505 // characters at the end of a line " =\r\n" and add the newly
1506 // processed line(s) to the output (see comment on $crlf class property)
1507 for ($i = 0; $i < $length; $i++)
1508 {
1509 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001510 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 $ascii = ord($char);
1512
1513 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001514 if ($ascii === 32 OR $ascii === 9)
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001516 if ($i === ($length - 1))
1517 {
1518 $char = $escape.sprintf('%02s', dechex($ascii));
1519 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001521 // DO NOT move this below the $ascii_safe_chars line!
1522 //
1523 // = (equals) signs are allowed by RFC2049, but must be encoded
1524 // as they are the encoding delimiter!
1525 elseif ($ascii === 61)
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001527 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 }
Andrey Andreevb06b5c42015-11-16 13:37:58 +02001529 elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
1530 {
1531 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
1532 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001533
1534 // If we're at the character limit, add the line to the output,
1535 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001536 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 $output .= $temp.$escape.$this->crlf;
1539 $temp = '';
1540 }
1541
1542 // Add the character to our temporary line
1543 $temp .= $char;
1544 }
1545
1546 // Add our completed line to the output
1547 $output .= $temp.$this->crlf;
1548 }
1549
1550 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001551 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 }
1553
1554 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001555
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 /**
1557 * Prep Q Encoding
1558 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001559 * Performs "Q Encoding" on a string for use in email headers.
1560 * It's related but not identical to quoted-printable, so it has its
1561 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001563 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001564 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001566 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001568 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001569
Andrey Andreev925dd902012-10-19 11:06:31 +03001570 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001572 // Note: We used to have mb_encode_mimeheader() as the first choice
1573 // here, but it turned out to be buggy and unreliable. DO NOT
1574 // re-add it! -- Narf
1575 if (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001576 {
1577 $output = @iconv_mime_encode('', $str,
1578 array(
1579 'scheme' => 'Q',
1580 'line-length' => 76,
1581 'input-charset' => $this->charset,
1582 'output-charset' => $this->charset,
1583 'line-break-chars' => $this->crlf
1584 )
1585 );
1586
1587 // There are reports that iconv_mime_encode() might fail and return FALSE
1588 if ($output !== FALSE)
1589 {
1590 // iconv_mime_encode() will always put a header field name.
1591 // We've passed it an empty one, but it still prepends our
1592 // encoded string with ': ', so we need to strip it.
1593 return substr($output, 2);
1594 }
1595
1596 $chars = iconv_strlen($str, 'UTF-8');
1597 }
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001598 elseif (MB_ENABLED === TRUE)
1599 {
1600 $chars = mb_strlen($str, 'UTF-8');
1601 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 }
1603
Andrey Andreev925dd902012-10-19 11:06:31 +03001604 // We might already have this set for UTF-8
1605 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001606
Andrey Andreev925dd902012-10-19 11:06:31 +03001607 $output = '=?'.$this->charset.'?Q?';
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001608 for ($i = 0, $length = strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001610 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001611 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1612 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001613
Andrey Andreev925dd902012-10-19 11:06:31 +03001614 // RFC 2045 sets a limit of 76 characters per line.
1615 // We'll append ?= to the end of each line though.
1616 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001618 $output .= '?='.$this->crlf // EOL
1619 .' =?'.$this->charset.'?Q?'.$chr; // New line
1620 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001622 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001624 $output .= $chr;
1625 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 }
1628
Andrey Andreev925dd902012-10-19 11:06:31 +03001629 // End the header
1630 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 }
1632
1633 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001634
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 /**
1636 * Send Email
1637 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001638 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 * @return bool
1640 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001641 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001643 if ( ! isset($this->_headers['From']))
Michael Granados0b85d6b2014-11-09 02:43:48 -02001644 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001645 $this->_set_error_message('lang:email_no_from');
Michael Granados0b85d6b2014-11-09 02:43:48 -02001646 return FALSE;
1647 }
1648
Alex Bilbied261b1e2012-06-02 11:12:16 +01001649 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
1651 $this->reply_to($this->_headers['From']);
1652 }
1653
Andrey Andreev76f15c92012-03-01 13:05:07 +02001654 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1655 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1656 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
patworkb0707982011-04-08 15:10:05 +02001658 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 return FALSE;
1660 }
1661
1662 $this->_build_headers();
1663
Andrey Andreev76f15c92012-03-01 13:05:07 +02001664 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001666 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001667
Alex Bilbiea87aab32012-07-30 09:50:37 +01001668 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001669 {
1670 $this->clear();
1671 }
1672
Alex Bilbieb901e732012-07-30 09:44:57 +01001673 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 }
1675
buhay466e8082013-04-17 14:25:34 -07001676 if ($this->_build_message() === FALSE)
1677 {
1678 return FALSE;
1679 }
buhayc6da1ef2013-04-18 09:06:49 -07001680
Alex Bilbieb901e732012-07-30 09:44:57 +01001681 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001682
Alex Bilbiea87aab32012-07-30 09:50:37 +01001683 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001684 {
1685 $this->clear();
1686 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001687
Alex Bilbieb901e732012-07-30 09:44:57 +01001688 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 }
Barry Mienydd671972010-10-04 16:33:58 +02001690
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 // --------------------------------------------------------------------
1692
1693 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001694 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001696 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001698 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001700 $float = $this->bcc_batch_size - 1;
1701 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 $chunk = array();
1703
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001704 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 {
1706 if (isset($this->_bcc_array[$i]))
1707 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001708 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 }
1710
Alex Bilbied261b1e2012-06-02 11:12:16 +01001711 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 {
1713 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001714 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001715 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 }
1717
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001718 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
1720 $chunk[] = substr($set, 1);
1721 }
1722 }
1723
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001724 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
1726 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001727
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001728 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001729
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001730 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001732 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 }
1734 else
1735 {
1736 $this->_bcc_array = $bcc;
1737 }
1738
buhay466e8082013-04-17 14:25:34 -07001739 if ($this->_build_message() === FALSE)
1740 {
1741 return FALSE;
1742 }
buhayc6da1ef2013-04-18 09:06:49 -07001743
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 $this->_spool_email();
1745 }
1746 }
Barry Mienydd671972010-10-04 16:33:58 +02001747
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 // --------------------------------------------------------------------
1749
1750 /**
1751 * Unwrap special elements
1752 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 * @return void
1754 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001755 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Andrey Andreev56454792012-05-17 14:32:19 +03001757 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 }
Barry Mienydd671972010-10-04 16:33:58 +02001759
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 // --------------------------------------------------------------------
1761
1762 /**
1763 * Strip line-breaks via callback
1764 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001765 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 * @return string
1767 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001768 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 {
1770 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1771 {
1772 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1773 }
1774
1775 return $matches[1];
1776 }
Barry Mienydd671972010-10-04 16:33:58 +02001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // --------------------------------------------------------------------
1779
1780 /**
1781 * Spool mail to the mail server
1782 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 * @return bool
1784 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001785 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 {
1787 $this->_unwrap_specials();
1788
Andrey Andreev56454792012-05-17 14:32:19 +03001789 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001790 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 {
Andrey Andreev56454792012-05-17 14:32:19 +03001792 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001793 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 }
1795
patworkb0707982011-04-08 15:10:05 +02001796 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 return TRUE;
1798 }
Barry Mienydd671972010-10-04 16:33:58 +02001799
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 // --------------------------------------------------------------------
1801
1802 /**
1803 * Send using mail()
1804 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 * @return bool
1806 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001807 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001809 if (is_array($this->_recipients))
1810 {
1811 $this->_recipients = implode(', ', $this->_recipients);
1812 }
1813
Alex Bilbied261b1e2012-06-02 11:12:16 +01001814 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001816 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 }
1818 else
1819 {
1820 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1821 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001822 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 +00001823 }
1824 }
Barry Mienydd671972010-10-04 16:33:58 +02001825
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 // --------------------------------------------------------------------
1827
1828 /**
1829 * Send using Sendmail
1830 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 * @return bool
1832 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001833 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001835 // is popen() enabled?
1836 if ( ! function_usable('popen')
1837 OR FALSE === ($fp = @popen(
Andrey Andreev9d84d3c2015-12-30 21:50:20 +02001838 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001839 , 'w'))
1840 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001841 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001842 return FALSE;
1843 }
Derek Jones71141ce2010-03-02 16:41:20 -06001844
Derek Jonesc630bcf2008-11-17 21:09:45 +00001845 fputs($fp, $this->_header_str);
1846 fputs($fp, $this->_finalbody);
1847
Barry Mienydd671972010-10-04 16:33:58 +02001848 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001849
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001850 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 {
patworkb0707982011-04-08 15:10:05 +02001852 $this->_set_error_message('lang:email_exit_status', $status);
1853 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 return FALSE;
1855 }
1856
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 return TRUE;
1858 }
Barry Mienydd671972010-10-04 16:33:58 +02001859
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 // --------------------------------------------------------------------
1861
1862 /**
1863 * Send using SMTP
1864 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 * @return bool
1866 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001867 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301868 {
1869 if ($this->smtp_host === '')
1870 {
1871 $this->_set_error_message('lang:email_no_hostname');
1872 return FALSE;
1873 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001874
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301875 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1876 {
1877 return FALSE;
1878 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001879
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001880 if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1881 {
1882 return FALSE;
1883 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001884
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301885 foreach ($this->_recipients as $val)
1886 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001887 if ( ! $this->_send_command('to', $val))
1888 {
1889 return FALSE;
1890 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301891 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001892
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301893 if (count($this->_cc_array) > 0)
1894 {
1895 foreach ($this->_cc_array as $val)
1896 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001897 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301898 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001899 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301900 }
1901 }
1902 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001903
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301904 if (count($this->_bcc_array) > 0)
1905 {
1906 foreach ($this->_bcc_array as $val)
1907 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001908 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301909 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001910 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301911 }
1912 }
1913 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001914
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001915 if ( ! $this->_send_command('data'))
1916 {
1917 return FALSE;
1918 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001919
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301920 // perform dot transformation on any lines that begin with a dot
1921 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001922
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301923 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001924
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301925 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001926
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301927 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001928
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301929 if (strpos($reply, '250') !== 0)
1930 {
1931 $this->_set_error_message('lang:email_smtp_error', $reply);
1932 return FALSE;
1933 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001934
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301935 if ($this->smtp_keepalive)
1936 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301937 $this->_send_command('reset');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301938 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301939 else
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301940 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301941 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301944 return TRUE;
1945 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001946
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301947 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001948
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301949 /**
1950 * SMTP Connect
1951 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301952 * @return string
1953 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301954 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301955 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301956 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301957 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301958 return TRUE;
1959 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001960
Andrey Andreev89b67b42013-03-12 19:34:23 +02001961 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001962
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301963 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1964 $this->smtp_port,
1965 $errno,
1966 $errstr,
1967 $this->smtp_timeout);
1968
1969 if ( ! is_resource($this->_smtp_connect))
1970 {
1971 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1972 return FALSE;
1973 }
1974
1975 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1976 $this->_set_error_message($this->_get_smtp_data());
1977
1978 if ($this->smtp_crypto === 'tls')
1979 {
1980 $this->_send_command('hello');
1981 $this->_send_command('starttls');
1982
1983 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1984
1985 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301986 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301987 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301988 return FALSE;
1989 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301990 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301991
1992 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301993 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001994
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301995 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001996
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301997 /**
1998 * Send SMTP command
1999 *
2000 * @param string
2001 * @param string
2002 * @return string
2003 */
2004 protected function _send_command($cmd, $data = '')
2005 {
2006 switch ($cmd)
2007 {
2008 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002009
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302010 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2011 {
2012 $this->_send_data('EHLO '.$this->_get_hostname());
2013 }
2014 else
2015 {
2016 $this->_send_data('HELO '.$this->_get_hostname());
2017 }
Radu Potopbbf04b02011-09-28 13:57:51 +03002018
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302019 $resp = 250;
2020 break;
2021 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002022
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302023 $this->_send_data('STARTTLS');
2024 $resp = 220;
2025 break;
2026 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03002027
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302028 $this->_send_data('MAIL FROM:<'.$data.'>');
2029 $resp = 250;
2030 break;
2031 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03002032
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302033 if ($this->dsn)
2034 {
2035 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2036 }
2037 else
2038 {
2039 $this->_send_data('RCPT TO:<'.$data.'>');
2040 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002041
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302042 $resp = 250;
2043 break;
2044 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002045
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302046 $this->_send_data('DATA');
2047 $resp = 354;
2048 break;
2049 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002050
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302051 $this->_send_data('RSET');
2052 $resp = 250;
2053 break;
2054 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002055
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302056 $this->_send_data('QUIT');
2057 $resp = 221;
2058 break;
2059 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002060
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302061 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002062
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302063 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002064
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302065 if ((int) substr($reply, 0, 3) !== $resp)
2066 {
2067 $this->_set_error_message('lang:email_smtp_error', $reply);
2068 return FALSE;
2069 }
Barry Mienydd671972010-10-04 16:33:58 +02002070
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302071 if ($cmd === 'quit')
2072 {
2073 fclose($this->_smtp_connect);
2074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002075
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302076 return TRUE;
2077 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002078
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302079 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002080
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302081 /**
2082 * SMTP Authenticate
2083 *
2084 * @return bool
2085 */
2086 protected function _smtp_authenticate()
2087 {
2088 if ( ! $this->_smtp_auth)
2089 {
2090 return TRUE;
2091 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002092
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302093 if ($this->smtp_user === '' && $this->smtp_pass === '')
2094 {
2095 $this->_set_error_message('lang:email_no_smtp_unpw');
2096 return FALSE;
2097 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002098
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302099 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002100
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302101 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002102
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002103 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302104 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302105 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302106 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002107 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302108 {
2109 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2110 return FALSE;
2111 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002112
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302113 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002114
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302115 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002116
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302117 if (strpos($reply, '334') !== 0)
2118 {
2119 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2120 return FALSE;
2121 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002122
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302123 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002124
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302125 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302126
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302127 if (strpos($reply, '235') !== 0)
2128 {
2129 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2130 return FALSE;
2131 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302132
Andrey Andreev84253192016-05-09 12:24:52 +03002133 if ($this->smtp_keepalive)
2134 {
2135 $this->_smtp_auth = FALSE;
2136 }
2137
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302138 return TRUE;
2139 }
Barry Mienydd671972010-10-04 16:33:58 +02002140
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 // --------------------------------------------------------------------
2142
2143 /**
2144 * Send SMTP data
2145 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002146 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 * @return bool
2148 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002149 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002150 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002151 $data .= $this->newline;
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002152 for ($written = $timestamp = 0, $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002153 {
2154 if (($result = fwrite($this->_smtp_connect, substr($data, $written))) === FALSE)
2155 {
2156 break;
2157 }
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002158 // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2159 elseif ($result === 0)
2160 {
2161 if ($timestamp === 0)
2162 {
2163 $timestamp = time();
2164 }
2165 elseif ($timestamp < (time() - $this->smtp_timeout))
2166 {
2167 $result = FALSE;
2168 break;
2169 }
2170
2171 usleep(250000);
2172 continue;
2173 }
2174 else
2175 {
2176 $timestamp = 0;
2177 }
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002178 }
2179
2180 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002181 {
patworkb0707982011-04-08 15:10:05 +02002182 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 return FALSE;
2184 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002185
2186 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 }
Barry Mienydd671972010-10-04 16:33:58 +02002188
Derek Allard2067d1a2008-11-13 22:59:24 +00002189 // --------------------------------------------------------------------
2190
2191 /**
2192 * Get SMTP data
2193 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 * @return string
2195 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002196 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 {
Andrey Andreev56454792012-05-17 14:32:19 +03002198 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002199
2200 while ($str = fgets($this->_smtp_connect, 512))
2201 {
2202 $data .= $str;
2203
Alex Bilbied261b1e2012-06-02 11:12:16 +01002204 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 {
2206 break;
2207 }
2208 }
2209
2210 return $data;
2211 }
Barry Mienydd671972010-10-04 16:33:58 +02002212
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 // --------------------------------------------------------------------
2214
2215 /**
2216 * Get Hostname
Andrey Andreev396eb892015-02-06 14:50:10 +02002217 *
2218 * There are only two legal types of hostname - either a fully
2219 * qualified domain name (eg: "mail.example.com") or an IP literal
2220 * (eg: "[1.2.3.4]").
2221 *
2222 * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
James Wade3245af42015-02-06 11:48:51 +00002223 * @link http://cbl.abuseat.org/namingproblems.html
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 * @return string
2225 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002226 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 {
Andrey Andreev396eb892015-02-06 14:50:10 +02002228 if (isset($_SERVER['SERVER_NAME']))
2229 {
2230 return $_SERVER['SERVER_NAME'];
2231 }
2232
2233 return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 * Get Debug Message
2240 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002241 * @param array $include List of raw data chunks to include in the output
2242 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 * @return string
2244 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002245 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002246 {
2247 $msg = '';
2248
2249 if (count($this->_debug_msg) > 0)
2250 {
2251 foreach ($this->_debug_msg as $val)
2252 {
2253 $msg .= $val;
2254 }
2255 }
2256
Andrey Andreev61797f62012-11-26 16:15:12 +02002257 // Determine which parts of our raw data needs to be printed
2258 $raw_data = '';
2259 is_array($include) OR $include = array($include);
2260
2261 if (in_array('headers', $include, TRUE))
2262 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002263 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002264 }
2265
2266 if (in_array('subject', $include, TRUE))
2267 {
2268 $raw_data .= htmlspecialchars($this->_subject)."\n";
2269 }
2270
2271 if (in_array('body', $include, TRUE))
2272 {
2273 $raw_data .= htmlspecialchars($this->_finalbody);
2274 }
2275
2276 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002277 }
Barry Mienydd671972010-10-04 16:33:58 +02002278
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 // --------------------------------------------------------------------
2280
2281 /**
2282 * Set Message
2283 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002284 * @param string $msg
2285 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002286 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002288 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 {
2290 $CI =& get_instance();
2291 $CI->lang->load('email');
2292
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002293 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 {
Andrey Andreev56454792012-05-17 14:32:19 +03002295 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 }
2297 else
2298 {
Andrey Andreev56454792012-05-17 14:32:19 +03002299 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 }
2301 }
Barry Mienydd671972010-10-04 16:33:58 +02002302
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 // --------------------------------------------------------------------
2304
2305 /**
2306 * Mime Types
2307 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 * @param string
2309 * @return string
2310 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002311 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002313 $ext = strtolower($ext);
2314
vlakoff66c7bb42014-05-19 13:45:12 +02002315 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002316
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002317 if (isset($mimes[$ext]))
2318 {
2319 return is_array($mimes[$ext])
2320 ? current($mimes[$ext])
2321 : $mimes[$ext];
2322 }
2323
2324 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 }
2326
Andrey Andreev8c95c3d2016-05-09 12:35:41 +03002327 // --------------------------------------------------------------------
2328
2329 /**
2330 * Destructor
2331 *
2332 * @return void
2333 */
2334 public function __destruct()
2335 {
2336 is_resource($this->_smtp_connect) && $this->_send_command('quit');
2337 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002338}