blob: ebff7567a137297b5f5aa5f6600825db9c87a7f9 [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/email.html
50 */
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 */
150 public $charset = 'utf-8';
151
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 Andreev9f44c212012-10-10 16:07:17 +0300411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 if (count($config) > 0)
413 {
414 $this->initialize($config);
415 }
416 else
417 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100418 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
Andrey Andreevf6274742014-02-20 18:05:58 +0200421 $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300422 $this->charset = strtoupper($this->charset);
423
Andrey Andreev90726b82015-01-20 12:39:22 +0200424 log_message('info', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530426
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530427 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100428
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530429 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530430 * Destructor - Releases Resources
431 *
432 * @return void
433 */
434 public function __destruct()
435 {
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530436 if (is_resource($this->_smtp_connect))
437 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530438 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530439 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530440 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000441
442 // --------------------------------------------------------------------
443
444 /**
445 * Initialize preferences
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param array
Andrew Podner4296a652012-12-17 07:51:15 -0500448 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000450 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 foreach ($config as $key => $val)
453 {
454 if (isset($this->$key))
455 {
456 $method = 'set_'.$key;
457
458 if (method_exists($this, $method))
459 {
460 $this->$method($val);
461 }
462 else
463 {
464 $this->$key = $val;
465 }
466 }
467 }
Eric Barnes6113f542010-12-29 13:36:12 -0500468 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Alex Bilbied261b1e2012-06-02 11:12:16 +0100470 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000471
472 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Initialize the Email Data
479 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800480 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500481 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000483 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200485 $this->_subject = '';
486 $this->_body = '';
487 $this->_finalbody = '';
488 $this->_header_str = '';
489 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500491 $this->_cc_array = array();
492 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $this->_headers = array();
494 $this->_debug_msg = array();
495
Mickey Wubfc1cad2012-05-31 22:28:40 -0700496 $this->set_header('User-Agent', $this->useragent);
497 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000498
499 if ($clear_attachments !== FALSE)
500 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300501 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Eric Barnes6113f542010-12-29 13:36:12 -0500503
Greg Akera769deb2010-11-10 15:47:29 -0600504 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 // --------------------------------------------------------------------
508
509 /**
510 * Set FROM
511 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300512 * @param string $from
513 * @param string $name
514 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500515 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300517 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200519 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200521 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523
524 if ($this->validate)
525 {
526 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300527 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200528 {
529 $this->validate_email($this->_str_to_array($return_path));
530 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
533 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100534 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 // only use Q encoding if there are characters that would require it
537 if ( ! preg_match('/[\200-\377]/', $name))
538 {
539 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000540 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542 else
543 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300544 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546 }
547
Mickey Wubfc1cad2012-05-31 22:28:40 -0700548 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200549
Andrey Andreev925dd902012-10-19 11:06:31 +0300550 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200551 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500552
Greg Akera769deb2010-11-10 15:47:29 -0600553 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
559 * Set Reply-to
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param string
562 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500563 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000565 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200567 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200569 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571
572 if ($this->validate)
573 {
574 $this->validate_email($this->_str_to_array($replyto));
575 }
576
Alex Bilbied261b1e2012-06-02 11:12:16 +0100577 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 $name = $replyto;
580 }
581
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300582 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 $name = '"'.$name.'"';
585 }
586
Mickey Wubfc1cad2012-05-31 22:28:40 -0700587 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600589
590 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // --------------------------------------------------------------------
594
595 /**
596 * Set Recipients
597 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500599 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000601 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 $to = $this->_str_to_array($to);
604 $to = $this->clean_email($to);
605
606 if ($this->validate)
607 {
608 $this->validate_email($to);
609 }
610
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200611 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700613 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300616 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500617
Greg Akera769deb2010-11-10 15:47:29 -0600618 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Set CC
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500627 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000629 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreev56454792012-05-17 14:32:19 +0300631 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000632
633 if ($this->validate)
634 {
635 $this->validate_email($cc);
636 }
637
Mickey Wubfc1cad2012-05-31 22:28:40 -0700638 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000639
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200640 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 $this->_cc_array = $cc;
643 }
Eric Barnes6113f542010-12-29 13:36:12 -0500644
Greg Akera769deb2010-11-10 15:47:29 -0600645 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
649
650 /**
651 * Set BCC
652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param string
654 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500655 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000657 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100659 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 $this->bcc_batch_mode = TRUE;
662 $this->bcc_batch_size = $limit;
663 }
664
Andrey Andreev56454792012-05-17 14:32:19 +0300665 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000666
667 if ($this->validate)
668 {
669 $this->validate_email($bcc);
670 }
671
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200672 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
674 $this->_bcc_array = $bcc;
675 }
676 else
677 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700678 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
Eric Barnes6113f542010-12-29 13:36:12 -0500680
Greg Akera769deb2010-11-10 15:47:29 -0600681 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // --------------------------------------------------------------------
685
686 /**
687 * Set Email Subject
688 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500690 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000692 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700695 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600696 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 // --------------------------------------------------------------------
700
701 /**
702 * Set Body
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500705 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000707 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200709 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200710
Andrey Andreevaf728622011-10-20 10:11:59 +0300711 /* strip slashes only if magic quotes is ON
712 if we do it with magic quotes OFF, it strips real, user-inputted chars.
713
714 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
715 it will probably not exist in future versions at all.
716 */
717 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200718 {
diegorivera33c32802011-10-19 02:56:15 -0200719 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200720 }
diegorivera33c32802011-10-19 02:56:15 -0200721
Greg Akera769deb2010-11-10 15:47:29 -0600722 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 // --------------------------------------------------------------------
726
727 /**
728 * Assign file attachments
729 *
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100730 * @param string $file Can be local path, URL or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300731 * @param string $disposition = 'attachment'
732 * @param string $newname = NULL
733 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500734 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100736 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100738 if ($mime === '')
739 {
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100740 if (strpos($file, '://') === FALSE && ! file_exists($file))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100741 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100742 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100743 return FALSE;
744 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200745
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200746 if ( ! $fp = @fopen($file, 'rb'))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100747 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100748 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100749 return FALSE;
750 }
Petr Heralecky1dbdf72c2014-01-10 16:54:51 +0100751
Petr Heralecky300e3f02014-01-10 11:49:11 +0100752 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100753 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100754 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100755 }
756 else
757 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100758 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100759 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200760
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300761 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100762 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300763 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100764 'type' => $mime,
765 'content' => chunk_split(base64_encode($file_content))
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300766 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100767
Greg Akera769deb2010-11-10 15:47:29 -0600768 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200770
Petr Heralecky300e3f02014-01-10 11:49:11 +0100771 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200772
Petr Heralecky300e3f02014-01-10 11:49:11 +0100773 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200774 * Set and return attachment Content-ID
775 *
776 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100777 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100778 * @param string $filename
779 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100780 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200781 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100782 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100783 if ($this->multipart !== 'related')
Petr Heralecky300e3f02014-01-10 11:49:11 +0100784 {
785 $this->multipart = 'related'; // Thunderbird need this for inline images
786 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200787
Petr Heraleckyde886152014-01-10 12:52:56 +0100788 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100789 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100790 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100791 {
Petr Heralecky230fca32014-01-10 12:55:57 +0100792 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100793 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100794 }
795 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200796
Petr Heralecky300e3f02014-01-10 11:49:11 +0100797 return FALSE;
798 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000799
800 // --------------------------------------------------------------------
801
802 /**
803 * Add a Header Item
804 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 * @param string
806 * @param string
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300807 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700809 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
florisluitenf55d5142013-06-07 17:20:06 +0300811 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Andrey Andreev6cefc6b2015-08-03 10:22:19 +0300812 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 // --------------------------------------------------------------------
816
817 /**
818 * Convert a String to an Array
819 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * @param string
821 * @return array
822 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600823 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
825 if ( ! is_array($email))
826 {
Andrey Andreev56454792012-05-17 14:32:19 +0300827 return (strpos($email, ',') !== FALSE)
828 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
829 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
Andrey Andreev56454792012-05-17 14:32:19 +0300831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 return $email;
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
836
837 /**
838 * Set Multipart Value
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500841 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 */
Andrey Andreev505b3d62014-02-08 18:24:00 +0200843 public function set_alt_message($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400845 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600846 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // --------------------------------------------------------------------
850
851 /**
852 * Set Mailtype
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500855 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000857 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreev56454792012-05-17 14:32:19 +0300859 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600860 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 /**
866 * Set Wordwrap
867 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400868 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500869 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000871 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400873 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600874 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Set Protocol
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500883 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000885 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200887 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600888 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 // --------------------------------------------------------------------
892
893 /**
894 * Set Priority
895 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200896 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500897 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000899 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200901 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600902 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
906
907 /**
908 * Set Newline Character
909 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500911 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000913 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200915 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600916 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
920
921 /**
922 * Set CRLF
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500925 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000927 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200929 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600930 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // --------------------------------------------------------------------
934
935 /**
936 * Set Message Boundary
937 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * @return void
939 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600940 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200942 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
943 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 // --------------------------------------------------------------------
947
948 /**
949 * Get the Message ID
950 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 * @return string
952 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600953 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200955 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300956 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // --------------------------------------------------------------------
960
961 /**
962 * Get Mail Protocol
963 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200965 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600967 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
969 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200970 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
Alex Bilbied261b1e2012-06-02 11:12:16 +0100972 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 return $this->protocol;
975 }
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 // --------------------------------------------------------------------
979
980 /**
981 * Get Mail Encoding
982 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * @param bool
984 * @return string
985 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600986 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200988 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000989
990 foreach ($this->_base_charsets as $charset)
991 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300992 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
994 $this->_encoding = '7bit';
995 }
996 }
997
Alex Bilbied261b1e2012-06-02 11:12:16 +0100998 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
1000 return $this->_encoding;
1001 }
1002 }
1003
1004 // --------------------------------------------------------------------
1005
1006 /**
1007 * Get content type (text/html/attachment)
1008 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 * @return string
1010 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001011 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreev56454792012-05-17 14:32:19 +03001013 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001015 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001017 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 return 'plain-attach';
1020 }
1021 else
1022 {
1023 return 'plain';
1024 }
1025 }
Barry Mienydd671972010-10-04 16:33:58 +02001026
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 // --------------------------------------------------------------------
1028
1029 /**
1030 * Set RFC 822 Date
1031 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @return string
1033 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001034 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001036 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001037 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001039 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +00001040
Andrey Andreev59c5b532012-03-01 14:09:51 +02001041 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 }
Barry Mienydd671972010-10-04 16:33:58 +02001043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 // --------------------------------------------------------------------
1045
1046 /**
1047 * Mime message
1048 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 * @return string
1050 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001051 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001053 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 +00001054 }
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Validate Email Address
1060 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 * @param string
1062 * @return bool
1063 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001064 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
1066 if ( ! is_array($email))
1067 {
patworkb0707982011-04-08 15:10:05 +02001068 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 return FALSE;
1070 }
1071
1072 foreach ($email as $val)
1073 {
1074 if ( ! $this->valid_email($val))
1075 {
patworkb0707982011-04-08 15:10:05 +02001076 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 return FALSE;
1078 }
1079 }
1080
1081 return TRUE;
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 // --------------------------------------------------------------------
1085
1086 /**
1087 * Email Validation
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @param string
1090 * @return bool
1091 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001092 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev95496662014-06-01 00:00:13 +03001094 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1095 {
1096 $email = substr($email, 0, ++$atpos).idn_to_ascii(substr($email, $atpos));
1097 }
1098
Andrey Andreev580388b2012-06-27 15:43:46 +03001099 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1106 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 * @param string
1108 * @return string
1109 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001110 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
1112 if ( ! is_array($email))
1113 {
Andrey Andreev56454792012-05-17 14:32:19 +03001114 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
1116
1117 $clean_email = array();
1118
1119 foreach ($email as $addy)
1120 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001121 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 }
1123
1124 return $clean_email;
1125 }
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 // --------------------------------------------------------------------
1128
1129 /**
1130 * Build alternative plain text message
1131 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001132 * Provides the raw message for use in plain-text headers of
1133 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 * If the user hasn't specified his own alternative message
1135 * it creates one by stripping the HTML
1136 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @return string
1138 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001139 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001141 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001143 return ($this->wordwrap)
1144 ? $this->word_wrap($this->alt_message, 76)
1145 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
1147
Andrey Andreev56454792012-05-17 14:32:19 +03001148 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001149 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001150
1151 for ($i = 20; $i >= 3; $i--)
1152 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001153 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 }
1155
GDmac9bea4be2012-10-30 06:14:19 +01001156 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001157 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001158
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001159 return ($this->wordwrap)
1160 ? $this->word_wrap($body, 76)
1161 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
1165
1166 /**
1167 * Word Wrap
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001170 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 * @return string
1172 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001173 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001175 // Set the character limit, if not already present
1176 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001178 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
1180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // Standardize newlines
1182 if (strpos($str, "\r") !== FALSE)
1183 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001184 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 }
1186
GDmac9bea4be2012-10-30 06:14:19 +01001187 // Reduce multiple spaces at end of line
1188 $str = preg_replace('| +\n|', "\n", $str);
1189
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 // If the current word is surrounded by {unwrap} tags we'll
1191 // strip the entire chunk and replace it with a marker.
1192 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +02001193 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001195 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001197 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +02001198 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
1200 }
1201
vlakofff0ab8132014-05-20 10:32:53 +02001202 // Use PHP's native function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001204 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 $str = wordwrap($str, $charlim, "\n", FALSE);
1206
1207 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001208 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 foreach (explode("\n", $str) as $line)
1210 {
1211 // Is the line within the allowed character count?
1212 // If so we'll join it to the output and continue
vlakofff0ab8132014-05-20 10:32:53 +02001213 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
1215 $output .= $line.$this->newline;
1216 continue;
1217 }
1218
1219 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001220 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
1222 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +02001223 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 break;
1226 }
1227
1228 // Trim the word down
vlakofff0ab8132014-05-20 10:32:53 +02001229 $temp .= mb_substr($line, 0, $charlim - 1);
1230 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
vlakofff0ab8132014-05-20 10:32:53 +02001232 while (mb_strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001233
1234 // If $temp contains data it means we had to split up an over-length
1235 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001236 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001238 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 }
1240
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001241 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
1243
1244 // Put our markers back
1245 if (count($unwrap) > 0)
1246 {
1247 foreach ($unwrap as $key => $val)
1248 {
Andrey Andreev56454792012-05-17 14:32:19 +03001249 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
1251 }
1252
1253 return $output;
1254 }
Barry Mienydd671972010-10-04 16:33:58 +02001255
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 // --------------------------------------------------------------------
1257
1258 /**
1259 * Build final headers
1260 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 * @return string
1262 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001263 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001265 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1266 $this->set_header('X-Mailer', $this->useragent);
Andrey Andreevdea61772014-02-24 16:36:25 +02001267 $this->set_header('X-Priority', $this->_priorities[$this->priority]);
Mickey Wubfc1cad2012-05-31 22:28:40 -07001268 $this->set_header('Message-ID', $this->_get_message_id());
1269 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 // --------------------------------------------------------------------
1273
1274 /**
1275 * Write Headers as a string
1276 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 * @return void
1278 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001279 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001281 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301283 if (isset($this->_headers['Subject']))
1284 {
1285 $this->_subject = $this->_headers['Subject'];
1286 unset($this->_headers['Subject']);
1287 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
1289
1290 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001291 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001292
Pascal Kriete14287f32011-02-14 13:39:34 -05001293 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
1295 $val = trim($val);
1296
Alex Bilbied261b1e2012-06-02 11:12:16 +01001297 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreev56454792012-05-17 14:32:19 +03001299 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 }
1301 }
1302
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001303 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
Derek Jones1d890882009-02-10 20:32:14 +00001305 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
1307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // --------------------------------------------------------------------
1310
1311 /**
1312 * Build Final Body and attachments
1313 *
buhayc6da1ef2013-04-18 09:06:49 -07001314 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001316 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001318 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 {
1320 $this->_body = $this->word_wrap($this->_body);
1321 }
1322
1323 $this->_set_boundaries();
1324 $this->_write_headers();
1325
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001326 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001327 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001328
1329 switch ($this->_get_content_type())
1330 {
1331 case 'plain' :
1332
Andrey Andreev56454792012-05-17 14:32:19 +03001333 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1334 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001335
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001336 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $this->_header_str .= $hdr;
1339 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 }
Brandon Jones485d7412010-11-09 16:38:17 -05001341 else
1342 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001343 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001344 }
Eric Barnes6113f542010-12-29 13:36:12 -05001345
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 return;
1347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 case 'html' :
1349
1350 if ($this->send_multipart === FALSE)
1351 {
Andrey Andreev56454792012-05-17 14:32:19 +03001352 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001353 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 }
1355 else
1356 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001357 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001358
Andrey Andreev56454792012-05-17 14:32:19 +03001359 $body .= $this->_get_mime_message().$this->newline.$this->newline
1360 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001361
Andrey Andreev56454792012-05-17 14:32:19 +03001362 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1363 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1364 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001365
Andrey Andreev56454792012-05-17 14:32:19 +03001366 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1367 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 }
Eric Barnes6113f542010-12-29 13:36:12 -05001369
Andrey Andreev56454792012-05-17 14:32:19 +03001370 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001371
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001372 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
1374 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001375 }
1376 else
1377 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001378 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 }
1380
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 if ($this->send_multipart !== FALSE)
1382 {
Andrey Andreev56454792012-05-17 14:32:19 +03001383 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 }
1385
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 return;
1387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 case 'plain-attach' :
1389
Andrey Andreev2b956af2013-08-07 14:32:56 +03001390 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001391
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001392 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 {
1394 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001395 }
1396
Andrey Andreev2b956af2013-08-07 14:32:56 +03001397 $body .= $this->_get_mime_message().$this->newline
1398 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001399 .'--'.$this->_atc_boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001400 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001401 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1402 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001403 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001404
1405 break;
1406 case 'html-attach' :
1407
Andrey Andreev2b956af2013-08-07 14:32:56 +03001408 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Eric Barnes6113f542010-12-29 13:36:12 -05001409
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001410 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
1412 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414
Andrey Andreev56454792012-05-17 14:32:19 +03001415 $body .= $this->_get_mime_message().$this->newline.$this->newline
1416 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001417
Andrey Andreev56454792012-05-17 14:32:19 +03001418 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1419 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001420
Andrey Andreev56454792012-05-17 14:32:19 +03001421 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1422 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1423 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001424
Andrey Andreev56454792012-05-17 14:32:19 +03001425 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1426 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001427
Andrey Andreev56454792012-05-17 14:32:19 +03001428 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1429 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001430
1431 break;
1432 }
1433
1434 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001435 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001437 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001438 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001439 ? basename($filename) : $this->_attachments[$i]['name'][1];
Andrey Andreev56454792012-05-17 14:32:19 +03001440
1441 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001442 .'Content-type: '.$this->_attachments[$i]['type'].'; '
Andrey Andreev56454792012-05-17 14:32:19 +03001443 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001444 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001445 .'Content-Transfer-Encoding: base64'.$this->newline
Petr Heraleckyde886152014-01-10 12:52:56 +01001446 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
Derek Allard2067d1a2008-11-13 22:59:24 +00001447
Petr Heralecky300e3f02014-01-10 11:49:11 +01001448 $attachment[$z++] = $this->_attachments[$i]['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 }
1450
Andrey Andreev56454792012-05-17 14:32:19 +03001451 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
Andrey Andreev2b956af2013-08-07 14:32:56 +03001452 $this->_finalbody = ($this->_get_protocol() === 'mail')
1453 ? $body
1454 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001455
buhay466e8082013-04-17 14:25:34 -07001456 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 }
Barry Mienydd671972010-10-04 16:33:58 +02001458
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 // --------------------------------------------------------------------
1460
1461 /**
1462 * Prep Quoted Printable
1463 *
1464 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1465 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1466 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 * @return string
1469 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001470 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001472 // We are intentionally wrapping so mail servers will encode characters
1473 // properly and MUAs will behave, so {unwrap} must go!
1474 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1475
Andrey Andreev683b34d2012-10-09 15:00:00 +03001476 // RFC 2045 specifies CRLF as "\r\n".
1477 // However, many developers choose to override that and violate
1478 // the RFC rules due to (apparently) a bug in MS Exchange,
1479 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001480 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001481 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001482 if (is_php('5.3'))
1483 {
1484 return quoted_printable_encode($str);
1485 }
1486 elseif (function_exists('imap_8bit'))
1487 {
1488 return imap_8bit($str);
1489 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001490 }
1491
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001492 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001493 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001494
1495 // Standardize newlines
1496 if (strpos($str, "\r") !== FALSE)
1497 {
1498 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1499 }
1500
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 $escape = '=';
1502 $output = '';
1503
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001504 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 {
1506 $length = strlen($line);
1507 $temp = '';
1508
1509 // Loop through each character in the line to add soft-wrap
1510 // characters at the end of a line " =\r\n" and add the newly
1511 // processed line(s) to the output (see comment on $crlf class property)
1512 for ($i = 0; $i < $length; $i++)
1513 {
1514 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001515 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 $ascii = ord($char);
1517
1518 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001519 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001521 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001523 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001525 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
1527
1528 // If we're at the character limit, add the line to the output,
1529 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001530 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
1532 $output .= $temp.$escape.$this->crlf;
1533 $temp = '';
1534 }
1535
1536 // Add the character to our temporary line
1537 $temp .= $char;
1538 }
1539
1540 // Add our completed line to the output
1541 $output .= $temp.$this->crlf;
1542 }
1543
1544 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001545 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 }
1547
1548 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001549
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 /**
1551 * Prep Q Encoding
1552 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001553 * Performs "Q Encoding" on a string for use in email headers.
1554 * It's related but not identical to quoted-printable, so it has its
1555 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001557 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001558 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001560 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001562 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001563
Andrey Andreev925dd902012-10-19 11:06:31 +03001564 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 {
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001566 // Note: We used to have mb_encode_mimeheader() as the first choice
1567 // here, but it turned out to be buggy and unreliable. DO NOT
1568 // re-add it! -- Narf
1569 if (ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001570 {
1571 $output = @iconv_mime_encode('', $str,
1572 array(
1573 'scheme' => 'Q',
1574 'line-length' => 76,
1575 'input-charset' => $this->charset,
1576 'output-charset' => $this->charset,
1577 'line-break-chars' => $this->crlf
1578 )
1579 );
1580
1581 // There are reports that iconv_mime_encode() might fail and return FALSE
1582 if ($output !== FALSE)
1583 {
1584 // iconv_mime_encode() will always put a header field name.
1585 // We've passed it an empty one, but it still prepends our
1586 // encoded string with ': ', so we need to strip it.
1587 return substr($output, 2);
1588 }
1589
1590 $chars = iconv_strlen($str, 'UTF-8');
1591 }
Andrey Andreev3368ceb2015-10-30 12:25:15 +02001592 elseif (MB_ENABLED === TRUE)
1593 {
1594 $chars = mb_strlen($str, 'UTF-8');
1595 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
1597
Andrey Andreev925dd902012-10-19 11:06:31 +03001598 // We might already have this set for UTF-8
1599 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001600
Andrey Andreev925dd902012-10-19 11:06:31 +03001601 $output = '=?'.$this->charset.'?Q?';
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001602 for ($i = 0, $length = strlen($output); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +02001604 $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
Andrey Andreev925dd902012-10-19 11:06:31 +03001605 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1606 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001607
Andrey Andreev925dd902012-10-19 11:06:31 +03001608 // RFC 2045 sets a limit of 76 characters per line.
1609 // We'll append ?= to the end of each line though.
1610 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001612 $output .= '?='.$this->crlf // EOL
1613 .' =?'.$this->charset.'?Q?'.$chr; // New line
1614 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001616 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001618 $output .= $chr;
1619 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 }
1622
Andrey Andreev925dd902012-10-19 11:06:31 +03001623 // End the header
1624 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 }
1626
1627 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001628
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 /**
1630 * Send Email
1631 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001632 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 * @return bool
1634 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001635 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001637 if ( ! isset($this->_headers['From']))
Michael Granados0b85d6b2014-11-09 02:43:48 -02001638 {
Michael Granados92e2fd22014-11-11 21:49:26 -02001639 $this->_set_error_message('lang:email_no_from');
Michael Granados0b85d6b2014-11-09 02:43:48 -02001640 return FALSE;
1641 }
1642
Alex Bilbied261b1e2012-06-02 11:12:16 +01001643 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 {
1645 $this->reply_to($this->_headers['From']);
1646 }
1647
Andrey Andreev76f15c92012-03-01 13:05:07 +02001648 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1649 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1650 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
patworkb0707982011-04-08 15:10:05 +02001652 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 return FALSE;
1654 }
1655
1656 $this->_build_headers();
1657
Andrey Andreev76f15c92012-03-01 13:05:07 +02001658 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001660 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001661
Alex Bilbiea87aab32012-07-30 09:50:37 +01001662 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001663 {
1664 $this->clear();
1665 }
1666
Alex Bilbieb901e732012-07-30 09:44:57 +01001667 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 }
1669
buhay466e8082013-04-17 14:25:34 -07001670 if ($this->_build_message() === FALSE)
1671 {
1672 return FALSE;
1673 }
buhayc6da1ef2013-04-18 09:06:49 -07001674
Alex Bilbieb901e732012-07-30 09:44:57 +01001675 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001676
Alex Bilbiea87aab32012-07-30 09:50:37 +01001677 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001678 {
1679 $this->clear();
1680 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001681
Alex Bilbieb901e732012-07-30 09:44:57 +01001682 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 // --------------------------------------------------------------------
1686
1687 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001688 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001690 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001692 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001694 $float = $this->bcc_batch_size - 1;
1695 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 $chunk = array();
1697
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001698 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 {
1700 if (isset($this->_bcc_array[$i]))
1701 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001702 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 }
1704
Alex Bilbied261b1e2012-06-02 11:12:16 +01001705 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 {
1707 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001708 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001709 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 }
1711
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001712 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 {
1714 $chunk[] = substr($set, 1);
1715 }
1716 }
1717
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001718 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
1720 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001721
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001722 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001723
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001724 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001726 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 }
1728 else
1729 {
1730 $this->_bcc_array = $bcc;
1731 }
1732
buhay466e8082013-04-17 14:25:34 -07001733 if ($this->_build_message() === FALSE)
1734 {
1735 return FALSE;
1736 }
buhayc6da1ef2013-04-18 09:06:49 -07001737
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 $this->_spool_email();
1739 }
1740 }
Barry Mienydd671972010-10-04 16:33:58 +02001741
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 // --------------------------------------------------------------------
1743
1744 /**
1745 * Unwrap special elements
1746 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 * @return void
1748 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001749 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
Andrey Andreev56454792012-05-17 14:32:19 +03001751 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 }
Barry Mienydd671972010-10-04 16:33:58 +02001753
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 // --------------------------------------------------------------------
1755
1756 /**
1757 * Strip line-breaks via callback
1758 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001759 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 * @return string
1761 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001762 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 {
1764 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1765 {
1766 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1767 }
1768
1769 return $matches[1];
1770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 // --------------------------------------------------------------------
1773
1774 /**
1775 * Spool mail to the mail server
1776 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 * @return bool
1778 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001779 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 {
1781 $this->_unwrap_specials();
1782
Andrey Andreev56454792012-05-17 14:32:19 +03001783 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001784 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
Andrey Andreev56454792012-05-17 14:32:19 +03001786 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001787 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 }
1789
patworkb0707982011-04-08 15:10:05 +02001790 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 return TRUE;
1792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Send using mail()
1798 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @return bool
1800 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001801 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001803 if (is_array($this->_recipients))
1804 {
1805 $this->_recipients = implode(', ', $this->_recipients);
1806 }
1807
Alex Bilbied261b1e2012-06-02 11:12:16 +01001808 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001810 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
1812 else
1813 {
1814 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1815 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001816 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 +00001817 }
1818 }
Barry Mienydd671972010-10-04 16:33:58 +02001819
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 // --------------------------------------------------------------------
1821
1822 /**
1823 * Send using Sendmail
1824 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 * @return bool
1826 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001827 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001829 // is popen() enabled?
1830 if ( ! function_usable('popen')
1831 OR FALSE === ($fp = @popen(
1832 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
1833 .' -t -r '.$this->clean_email($this->_headers['Return-Path'])
1834 , 'w'))
1835 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001836 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001837 return FALSE;
1838 }
Derek Jones71141ce2010-03-02 16:41:20 -06001839
Derek Jonesc630bcf2008-11-17 21:09:45 +00001840 fputs($fp, $this->_header_str);
1841 fputs($fp, $this->_finalbody);
1842
Barry Mienydd671972010-10-04 16:33:58 +02001843 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001844
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001845 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 {
patworkb0707982011-04-08 15:10:05 +02001847 $this->_set_error_message('lang:email_exit_status', $status);
1848 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 return FALSE;
1850 }
1851
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 return TRUE;
1853 }
Barry Mienydd671972010-10-04 16:33:58 +02001854
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 // --------------------------------------------------------------------
1856
1857 /**
1858 * Send using SMTP
1859 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 * @return bool
1861 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001862 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301863 {
1864 if ($this->smtp_host === '')
1865 {
1866 $this->_set_error_message('lang:email_no_hostname');
1867 return FALSE;
1868 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001869
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301870 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1871 {
1872 return FALSE;
1873 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001874
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001875 if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1876 {
1877 return FALSE;
1878 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001879
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301880 foreach ($this->_recipients as $val)
1881 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001882 if ( ! $this->_send_command('to', $val))
1883 {
1884 return FALSE;
1885 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301886 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001887
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301888 if (count($this->_cc_array) > 0)
1889 {
1890 foreach ($this->_cc_array as $val)
1891 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001892 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301893 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001894 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301895 }
1896 }
1897 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001898
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301899 if (count($this->_bcc_array) > 0)
1900 {
1901 foreach ($this->_bcc_array as $val)
1902 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001903 if ($val !== '' && ! $this->_send_command('to', $val))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301904 {
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001905 return FALSE;
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301906 }
1907 }
1908 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001909
Andrey Andreev8e138ec2015-08-31 15:23:42 +03001910 if ( ! $this->_send_command('data'))
1911 {
1912 return FALSE;
1913 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001914
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301915 // perform dot transformation on any lines that begin with a dot
1916 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001917
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301918 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001919
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301920 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001921
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301922 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001923
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301924 if (strpos($reply, '250') !== 0)
1925 {
1926 $this->_set_error_message('lang:email_smtp_error', $reply);
1927 return FALSE;
1928 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001929
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301930 if ($this->smtp_keepalive)
1931 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301932 $this->_send_command('reset');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301933 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301934 else
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301935 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301936 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301937 }
Barry Mienydd671972010-10-04 16:33:58 +02001938
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301939 return TRUE;
1940 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001941
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301942 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001943
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301944 /**
1945 * SMTP Connect
1946 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301947 * @return string
1948 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301949 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301950 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301951 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301952 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301953 return TRUE;
1954 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001955
Andrey Andreev89b67b42013-03-12 19:34:23 +02001956 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001957
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301958 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1959 $this->smtp_port,
1960 $errno,
1961 $errstr,
1962 $this->smtp_timeout);
1963
1964 if ( ! is_resource($this->_smtp_connect))
1965 {
1966 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1967 return FALSE;
1968 }
1969
1970 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1971 $this->_set_error_message($this->_get_smtp_data());
1972
1973 if ($this->smtp_crypto === 'tls')
1974 {
1975 $this->_send_command('hello');
1976 $this->_send_command('starttls');
1977
1978 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1979
1980 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301981 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301982 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301983 return FALSE;
1984 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301985 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301986
1987 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301988 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001989
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301990 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001991
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301992 /**
1993 * Send SMTP command
1994 *
1995 * @param string
1996 * @param string
1997 * @return string
1998 */
1999 protected function _send_command($cmd, $data = '')
2000 {
2001 switch ($cmd)
2002 {
2003 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002004
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302005 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2006 {
2007 $this->_send_data('EHLO '.$this->_get_hostname());
2008 }
2009 else
2010 {
2011 $this->_send_data('HELO '.$this->_get_hostname());
2012 }
Radu Potopbbf04b02011-09-28 13:57:51 +03002013
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302014 $resp = 250;
2015 break;
2016 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002017
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302018 $this->_send_data('STARTTLS');
2019 $resp = 220;
2020 break;
2021 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03002022
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302023 $this->_send_data('MAIL FROM:<'.$data.'>');
2024 $resp = 250;
2025 break;
2026 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03002027
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302028 if ($this->dsn)
2029 {
2030 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2031 }
2032 else
2033 {
2034 $this->_send_data('RCPT TO:<'.$data.'>');
2035 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002036
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302037 $resp = 250;
2038 break;
2039 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002040
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302041 $this->_send_data('DATA');
2042 $resp = 354;
2043 break;
2044 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002045
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302046 $this->_send_data('RSET');
2047 $resp = 250;
2048 break;
2049 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002050
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302051 $this->_send_data('QUIT');
2052 $resp = 221;
2053 break;
2054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002055
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302056 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002057
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302058 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002059
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302060 if ((int) substr($reply, 0, 3) !== $resp)
2061 {
2062 $this->_set_error_message('lang:email_smtp_error', $reply);
2063 return FALSE;
2064 }
Barry Mienydd671972010-10-04 16:33:58 +02002065
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302066 if ($cmd === 'quit')
2067 {
2068 fclose($this->_smtp_connect);
2069 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002070
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302071 return TRUE;
2072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002073
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302074 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002075
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302076 /**
2077 * SMTP Authenticate
2078 *
2079 * @return bool
2080 */
2081 protected function _smtp_authenticate()
2082 {
2083 if ( ! $this->_smtp_auth)
2084 {
2085 return TRUE;
2086 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002087
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302088 if ($this->smtp_user === '' && $this->smtp_pass === '')
2089 {
2090 $this->_set_error_message('lang:email_no_smtp_unpw');
2091 return FALSE;
2092 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002093
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302094 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002095
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302096 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002097
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002098 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302099 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302100 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302101 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002102 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302103 {
2104 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2105 return FALSE;
2106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002107
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302108 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002109
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302110 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002111
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302112 if (strpos($reply, '334') !== 0)
2113 {
2114 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2115 return FALSE;
2116 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002117
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302118 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002119
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302120 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302121
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302122 if (strpos($reply, '235') !== 0)
2123 {
2124 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2125 return FALSE;
2126 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302127
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302128 return TRUE;
2129 }
Barry Mienydd671972010-10-04 16:33:58 +02002130
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 // --------------------------------------------------------------------
2132
2133 /**
2134 * Send SMTP data
2135 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002136 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002137 * @return bool
2138 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002139 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 {
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002141 $data .= $this->newline;
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002142 for ($written = $timestamp = 0, $length = strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002143 {
2144 if (($result = fwrite($this->_smtp_connect, substr($data, $written))) === FALSE)
2145 {
2146 break;
2147 }
Andrey Andreev4e0496e2015-06-22 12:34:38 +03002148 // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2149 elseif ($result === 0)
2150 {
2151 if ($timestamp === 0)
2152 {
2153 $timestamp = time();
2154 }
2155 elseif ($timestamp < (time() - $this->smtp_timeout))
2156 {
2157 $result = FALSE;
2158 break;
2159 }
2160
2161 usleep(250000);
2162 continue;
2163 }
2164 else
2165 {
2166 $timestamp = 0;
2167 }
Andrey Andreevd8b1ad32014-01-15 17:42:52 +02002168 }
2169
2170 if ($result === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 {
patworkb0707982011-04-08 15:10:05 +02002172 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 return FALSE;
2174 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002175
2176 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002177 }
Barry Mienydd671972010-10-04 16:33:58 +02002178
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 // --------------------------------------------------------------------
2180
2181 /**
2182 * Get SMTP data
2183 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 * @return string
2185 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002186 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
Andrey Andreev56454792012-05-17 14:32:19 +03002188 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002189
2190 while ($str = fgets($this->_smtp_connect, 512))
2191 {
2192 $data .= $str;
2193
Alex Bilbied261b1e2012-06-02 11:12:16 +01002194 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 {
2196 break;
2197 }
2198 }
2199
2200 return $data;
2201 }
Barry Mienydd671972010-10-04 16:33:58 +02002202
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 // --------------------------------------------------------------------
2204
2205 /**
2206 * Get Hostname
Andrey Andreev396eb892015-02-06 14:50:10 +02002207 *
2208 * There are only two legal types of hostname - either a fully
2209 * qualified domain name (eg: "mail.example.com") or an IP literal
2210 * (eg: "[1.2.3.4]").
2211 *
2212 * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
James Wade3245af42015-02-06 11:48:51 +00002213 * @link http://cbl.abuseat.org/namingproblems.html
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 * @return string
2215 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002216 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 {
Andrey Andreev396eb892015-02-06 14:50:10 +02002218 if (isset($_SERVER['SERVER_NAME']))
2219 {
2220 return $_SERVER['SERVER_NAME'];
2221 }
2222
2223 return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 }
Barry Mienydd671972010-10-04 16:33:58 +02002225
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 // --------------------------------------------------------------------
2227
2228 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 * Get Debug Message
2230 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002231 * @param array $include List of raw data chunks to include in the output
2232 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 * @return string
2234 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002235 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 {
2237 $msg = '';
2238
2239 if (count($this->_debug_msg) > 0)
2240 {
2241 foreach ($this->_debug_msg as $val)
2242 {
2243 $msg .= $val;
2244 }
2245 }
2246
Andrey Andreev61797f62012-11-26 16:15:12 +02002247 // Determine which parts of our raw data needs to be printed
2248 $raw_data = '';
2249 is_array($include) OR $include = array($include);
2250
2251 if (in_array('headers', $include, TRUE))
2252 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002253 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002254 }
2255
2256 if (in_array('subject', $include, TRUE))
2257 {
2258 $raw_data .= htmlspecialchars($this->_subject)."\n";
2259 }
2260
2261 if (in_array('body', $include, TRUE))
2262 {
2263 $raw_data .= htmlspecialchars($this->_finalbody);
2264 }
2265
2266 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 }
Barry Mienydd671972010-10-04 16:33:58 +02002268
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 // --------------------------------------------------------------------
2270
2271 /**
2272 * Set Message
2273 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002274 * @param string $msg
2275 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002276 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002277 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002278 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 {
2280 $CI =& get_instance();
2281 $CI->lang->load('email');
2282
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002283 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 {
Andrey Andreev56454792012-05-17 14:32:19 +03002285 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 }
2287 else
2288 {
Andrey Andreev56454792012-05-17 14:32:19 +03002289 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 }
2291 }
Barry Mienydd671972010-10-04 16:33:58 +02002292
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 // --------------------------------------------------------------------
2294
2295 /**
2296 * Mime Types
2297 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 * @param string
2299 * @return string
2300 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002301 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002303 $ext = strtolower($ext);
2304
vlakoff66c7bb42014-05-19 13:45:12 +02002305 $mimes =& get_mimes();
vlakoff69550c52014-05-19 13:45:02 +02002306
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002307 if (isset($mimes[$ext]))
2308 {
2309 return is_array($mimes[$ext])
2310 ? current($mimes[$ext])
2311 : $mimes[$ext];
2312 }
2313
2314 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 }
2316
2317}