blob: 37a628259520909f549947466ee396053a3cb19a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Email Class
31 *
32 * Permits email to be sent using Mail, Sendmail, or SMTP.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/email.html
39 */
trita15dd4f2011-11-23 07:40:05 -050040class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Andrey Andreev597ea272012-11-01 22:56:26 +020042 /**
43 * Used as the User-Agent and X-Mailer headers' value.
44 *
45 * @var string
46 */
Andrey Andreev59c5b532012-03-01 14:09:51 +020047 public $useragent = 'CodeIgniter';
Andrey Andreev50814092012-03-01 12:36:12 +020048
Andrey Andreev597ea272012-11-01 22:56:26 +020049 /**
50 * Path to the Sendmail binary.
51 *
52 * @var string
53 */
54 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
55
56 /**
57 * Which method to use for sending e-mails.
58 *
59 * @var string 'mail', 'sendmail' or 'smtp'
60 */
61 public $protocol = 'mail'; // mail/sendmail/smtp
62
63 /**
64 * STMP Server host
65 *
66 * @var string
67 */
68 public $smtp_host = '';
69
70 /**
71 * SMTP Username
72 *
73 * @var string
74 */
75 public $smtp_user = '';
76
77 /**
78 * SMTP Password
79 *
80 * @var string
81 */
82 public $smtp_pass = '';
83
84 /**
85 * SMTP Server port
86 *
87 * @var int
88 */
89 public $smtp_port = 25;
90
91 /**
92 * SMTP connection timeout in seconds
93 *
94 * @var int
95 */
96 public $smtp_timeout = 5;
vlakofff3994252013-02-19 01:45:23 +010097
nisheeth-barthwalcf225572013-02-18 01:08:04 +053098 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +053099 * SMTP persistent connection
100 *
101 * @var bool
102 */
103 public $smtp_keepalive = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200104
105 /**
106 * SMTP Encryption
107 *
Andrey Andreev89b67b42013-03-12 19:34:23 +0200108 * @var string empty, 'tls' or 'ssl'
Andrey Andreev597ea272012-11-01 22:56:26 +0200109 */
Andrey Andreev89b67b42013-03-12 19:34:23 +0200110 public $smtp_crypto = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200111
112 /**
113 * Whether to apply word-wrapping to the message body.
114 *
115 * @var bool
116 */
117 public $wordwrap = TRUE;
118
119 /**
120 * Number of characters to wrap at.
121 *
122 * @see CI_Email::$wordwrap
123 * @var int
124 */
125 public $wrapchars = 76;
126
127 /**
128 * Message format.
129 *
130 * @var string 'text' or 'html'
131 */
132 public $mailtype = 'text';
133
134 /**
135 * Character set (default: utf-8)
136 *
137 * @var string
138 */
139 public $charset = 'utf-8';
140
141 /**
142 * Multipart message
143 *
144 * @var string 'mixed' (in the body) or 'related' (separate)
145 */
146 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
147
148 /**
149 * Alternative message (for HTML messages only)
150 *
151 * @var string
152 */
153 public $alt_message = '';
154
155 /**
156 * Whether to validate e-mail addresses.
157 *
158 * @var bool
159 */
160 public $validate = FALSE;
161
162 /**
163 * X-Priority header value.
164 *
165 * @var int 1-5
166 */
167 public $priority = 3; // Default priority (1 - 5)
168
169 /**
170 * Newline character sequence.
171 * Use "\r\n" to comply with RFC 822.
172 *
173 * @link http://www.ietf.org/rfc/rfc822.txt
174 * @var string "\r\n" or "\n"
175 */
176 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
177
178 /**
179 * CRLF character sequence
180 *
181 * RFC 2045 specifies that for 'quoted-printable' encoding,
182 * "\r\n" must be used. However, it appears that some servers
183 * (even on the receiving end) don't handle it properly and
184 * switching to "\n", while improper, is the only solution
185 * that seems to work for all environments.
186 *
187 * @link http://www.ietf.org/rfc/rfc822.txt
188 * @var string
189 */
190 public $crlf = "\n";
191
192 /**
193 * Whether to use Delivery Status Notification.
194 *
195 * @var bool
196 */
197 public $dsn = FALSE;
198
199 /**
200 * Whether to send multipart alternatives.
201 * Yahoo! doesn't seem to like these.
202 *
203 * @var bool
204 */
205 public $send_multipart = TRUE;
206
207 /**
208 * Whether to send messages to BCC recipients in batches.
209 *
210 * @var bool
211 */
212 public $bcc_batch_mode = FALSE;
213
214 /**
215 * BCC Batch max number size.
216 *
217 * @see CI_Email::$bcc_batch_mode
218 * @var int
219 */
220 public $bcc_batch_size = 200;
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Whether PHP is running in safe mode. Initialized by the class constructor.
226 *
227 * @var bool
228 */
Andrey Andreev50814092012-03-01 12:36:12 +0200229 protected $_safe_mode = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200230
231 /**
232 * Subject header
233 *
234 * @var string
235 */
Andrey Andreev50814092012-03-01 12:36:12 +0200236 protected $_subject = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200237
238 /**
239 * Message body
240 *
241 * @var string
242 */
Andrey Andreev50814092012-03-01 12:36:12 +0200243 protected $_body = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200244
245 /**
246 * Final message body to be sent.
247 *
248 * @var string
249 */
Andrey Andreev50814092012-03-01 12:36:12 +0200250 protected $_finalbody = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200251
252 /**
253 * multipart/alternative boundary
254 *
255 * @var string
256 */
Andrey Andreev50814092012-03-01 12:36:12 +0200257 protected $_alt_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200258
259 /**
260 * Attachment boundary
261 *
262 * @var string
263 */
Andrey Andreev50814092012-03-01 12:36:12 +0200264 protected $_atc_boundary = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200265
266 /**
267 * Final headers to send
268 *
269 * @var string
270 */
Andrey Andreev50814092012-03-01 12:36:12 +0200271 protected $_header_str = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200272
273 /**
274 * SMTP Connection socket placeholder
275 *
276 * @var resource
277 */
Andrey Andreev50814092012-03-01 12:36:12 +0200278 protected $_smtp_connect = '';
Andrey Andreev597ea272012-11-01 22:56:26 +0200279
280 /**
281 * Mail encoding
282 *
283 * @var string '8bit' or '7bit'
284 */
Andrey Andreev50814092012-03-01 12:36:12 +0200285 protected $_encoding = '8bit';
Andrey Andreev597ea272012-11-01 22:56:26 +0200286
287 /**
288 * Whether to perform SMTP authentication
289 *
290 * @var bool
291 */
Andrey Andreev50814092012-03-01 12:36:12 +0200292 protected $_smtp_auth = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200293
294 /**
295 * Whether to send a Reply-To header
296 *
297 * @var bool
298 */
Andrey Andreev50814092012-03-01 12:36:12 +0200299 protected $_replyto_flag = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +0200300
301 /**
302 * Debug messages
303 *
304 * @see CI_Email::print_debugger()
305 * @var string
306 */
Andrey Andreev50814092012-03-01 12:36:12 +0200307 protected $_debug_msg = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200308
309 /**
310 * Recipients
311 *
312 * @var string[]
313 */
Andrey Andreev50814092012-03-01 12:36:12 +0200314 protected $_recipients = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200315
316 /**
317 * CC Recipients
318 *
319 * @var string[]
320 */
Andrey Andreev50814092012-03-01 12:36:12 +0200321 protected $_cc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200322
323 /**
324 * BCC Recipients
325 *
326 * @var string[]
327 */
Andrey Andreev50814092012-03-01 12:36:12 +0200328 protected $_bcc_array = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200329
330 /**
331 * Message headers
332 *
333 * @var string[]
334 */
Andrey Andreev50814092012-03-01 12:36:12 +0200335 protected $_headers = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200336
337 /**
338 * Attachment data
339 *
340 * @var array
341 */
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300342 protected $_attachments = array();
Andrey Andreev597ea272012-11-01 22:56:26 +0200343
344 /**
345 * Valid $protocol values
346 *
347 * @see CI_Email::$protocol
348 * @var string[]
349 */
Andrey Andreev50814092012-03-01 12:36:12 +0200350 protected $_protocols = array('mail', 'sendmail', 'smtp');
Andrey Andreev597ea272012-11-01 22:56:26 +0200351
352 /**
353 * Base charsets
354 *
355 * Character sets valid for 7-bit encoding,
356 * excluding language suffix.
357 *
358 * @var string[]
359 */
360 protected $_base_charsets = array('us-ascii', 'iso-2022-');
361
362 /**
363 * Bit depths
364 *
365 * Valid mail encodings
366 *
367 * @see CI_Email::$_encoding
368 * @var string[]
369 */
Andrey Andreev50814092012-03-01 12:36:12 +0200370 protected $_bit_depths = array('7bit', '8bit');
Andrey Andreev597ea272012-11-01 22:56:26 +0200371
372 /**
373 * $priority translations
374 *
375 * Actual values to send with the X-Priority header
376 *
377 * @var string[]
378 */
Andrey Andreev50814092012-03-01 12:36:12 +0200379 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
Andrey Andreev597ea272012-11-01 22:56:26 +0200381 // --------------------------------------------------------------------
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 /**
384 * Constructor - Sets Email Preferences
385 *
386 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +0300387 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300388 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +0300389 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000391 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300393 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 if (count($config) > 0)
396 {
397 $this->initialize($config);
398 }
399 else
400 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100401 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
Andrey Andreev5932a732013-09-13 14:45:53 +0300404 $this->_safe_mode = ( ! is_php('5.4') && (bool) @ini_get('safe_mode'));
Andrey Andreev925dd902012-10-19 11:06:31 +0300405 $this->charset = strtoupper($this->charset);
406
Andrey Andreev59c5b532012-03-01 14:09:51 +0200407 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530409
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530410 // --------------------------------------------------------------------
vlakofff3994252013-02-19 01:45:23 +0100411
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530412 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530413 * Destructor - Releases Resources
414 *
415 * @return void
416 */
417 public function __destruct()
418 {
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530419 if (is_resource($this->_smtp_connect))
420 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530421 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +0530422 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530423 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
425 // --------------------------------------------------------------------
426
427 /**
428 * Initialize preferences
429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param array
Andrew Podner4296a652012-12-17 07:51:15 -0500431 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000433 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 foreach ($config as $key => $val)
436 {
437 if (isset($this->$key))
438 {
439 $method = 'set_'.$key;
440
441 if (method_exists($this, $method))
442 {
443 $this->$method($val);
444 }
445 else
446 {
447 $this->$key = $val;
448 }
449 }
450 }
Eric Barnes6113f542010-12-29 13:36:12 -0500451 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000452
Alex Bilbied261b1e2012-06-02 11:12:16 +0100453 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000454
455 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * Initialize the Email Data
462 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800463 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500464 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000466 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200468 $this->_subject = '';
469 $this->_body = '';
470 $this->_finalbody = '';
471 $this->_header_str = '';
472 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500474 $this->_cc_array = array();
475 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 $this->_headers = array();
477 $this->_debug_msg = array();
478
Mickey Wubfc1cad2012-05-31 22:28:40 -0700479 $this->set_header('User-Agent', $this->useragent);
480 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000481
482 if ($clear_attachments !== FALSE)
483 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300484 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Eric Barnes6113f542010-12-29 13:36:12 -0500486
Greg Akera769deb2010-11-10 15:47:29 -0600487 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
491
492 /**
493 * Set FROM
494 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300495 * @param string $from
496 * @param string $name
497 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500498 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300500 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200502 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200504 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506
507 if ($this->validate)
508 {
509 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300510 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200511 {
512 $this->validate_email($this->_str_to_array($return_path));
513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515
516 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100517 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 // only use Q encoding if there are characters that would require it
520 if ( ! preg_match('/[\200-\377]/', $name))
521 {
522 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000523 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525 else
526 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300527 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529 }
530
Mickey Wubfc1cad2012-05-31 22:28:40 -0700531 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200532
Andrey Andreev925dd902012-10-19 11:06:31 +0300533 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200534 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500535
Greg Akera769deb2010-11-10 15:47:29 -0600536 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * Set Reply-to
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string
545 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500546 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000548 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200550 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200552 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554
555 if ($this->validate)
556 {
557 $this->validate_email($this->_str_to_array($replyto));
558 }
559
Alex Bilbied261b1e2012-06-02 11:12:16 +0100560 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 $name = $replyto;
563 }
564
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300565 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
567 $name = '"'.$name.'"';
568 }
569
Mickey Wubfc1cad2012-05-31 22:28:40 -0700570 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600572
573 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
577
578 /**
579 * Set Recipients
580 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500582 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000584 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $to = $this->_str_to_array($to);
587 $to = $this->clean_email($to);
588
589 if ($this->validate)
590 {
591 $this->validate_email($to);
592 }
593
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200594 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700596 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300599 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500600
Greg Akera769deb2010-11-10 15:47:29 -0600601 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * Set CC
608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500610 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000612 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Andrey Andreev56454792012-05-17 14:32:19 +0300614 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 if ($this->validate)
617 {
618 $this->validate_email($cc);
619 }
620
Mickey Wubfc1cad2012-05-31 22:28:40 -0700621 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000622
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200623 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 $this->_cc_array = $cc;
626 }
Eric Barnes6113f542010-12-29 13:36:12 -0500627
Greg Akera769deb2010-11-10 15:47:29 -0600628 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
632
633 /**
634 * Set BCC
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param string
637 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500638 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000640 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100642 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 $this->bcc_batch_mode = TRUE;
645 $this->bcc_batch_size = $limit;
646 }
647
Andrey Andreev56454792012-05-17 14:32:19 +0300648 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000649
650 if ($this->validate)
651 {
652 $this->validate_email($bcc);
653 }
654
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200655 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
657 $this->_bcc_array = $bcc;
658 }
659 else
660 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700661 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
Eric Barnes6113f542010-12-29 13:36:12 -0500663
Greg Akera769deb2010-11-10 15:47:29 -0600664 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Set Email Subject
671 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500673 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000675 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700678 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600679 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // --------------------------------------------------------------------
683
684 /**
685 * Set Body
686 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500688 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000690 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200692 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200693
Andrey Andreevaf728622011-10-20 10:11:59 +0300694 /* strip slashes only if magic quotes is ON
695 if we do it with magic quotes OFF, it strips real, user-inputted chars.
696
697 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
698 it will probably not exist in future versions at all.
699 */
700 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200701 {
diegorivera33c32802011-10-19 02:56:15 -0200702 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200703 }
diegorivera33c32802011-10-19 02:56:15 -0200704
Greg Akera769deb2010-11-10 15:47:29 -0600705 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 // --------------------------------------------------------------------
709
710 /**
711 * Assign file attachments
712 *
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100713 * @param string $file can be path, url or buffered content
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300714 * @param string $disposition = 'attachment'
715 * @param string $newname = NULL
716 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500717 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 */
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100719 public function attach($file, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Petr Heralecky300e3f02014-01-10 11:49:11 +0100721 if ($mime === '')
722 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100723 if (strpos($file,'http://')!==0 && ! file_exists($file) )
Petr Heralecky300e3f02014-01-10 11:49:11 +0100724 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100725 $this->_set_error_message('lang:email_attachment_missing', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100726 return FALSE;
727 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200728
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100729 if ( ! $fp = @fopen($file, FOPEN_READ))
Petr Heralecky300e3f02014-01-10 11:49:11 +0100730 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100731 $this->_set_error_message('lang:email_attachment_unreadable', $file);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100732 return FALSE;
733 }
Petr Heralecky232ea652014-01-10 15:21:18 +0100734
Petr Heralecky300e3f02014-01-10 11:49:11 +0100735 $file_content = stream_get_contents($fp);
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100736 $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
Petr Heraleckyde886152014-01-10 12:52:56 +0100737 fclose($fp);
Petr Heralecky300e3f02014-01-10 11:49:11 +0100738 }
739 else
740 {
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100741 $file_content =& $file; // buffered file
Petr Heralecky300e3f02014-01-10 11:49:11 +0100742 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200743
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300744 $this->_attachments[] = array(
Petr Heraleckyceb03af2014-01-10 16:40:54 +0100745 'name' => array($file, $newname),
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300746 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
Petr Heralecky300e3f02014-01-10 11:49:11 +0100747 'type' => $mime,
748 'content' => chunk_split(base64_encode($file_content))
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300749 );
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100750
Greg Akera769deb2010-11-10 15:47:29 -0600751 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200753
Petr Heralecky300e3f02014-01-10 11:49:11 +0100754 // --------------------------------------------------------------------
Andrey Andreevc8097262014-01-10 14:45:31 +0200755
Petr Heralecky300e3f02014-01-10 11:49:11 +0100756 /**
Andrey Andreevc8097262014-01-10 14:45:31 +0200757 * Set and return attachment Content-ID
758 *
759 * Useful for attached inline pictures
Petr Heralecky300e3f02014-01-10 11:49:11 +0100760 *
Petr Heraleckyde886152014-01-10 12:52:56 +0100761 * @param string $filename
762 * @return string
Petr Heralecky300e3f02014-01-10 11:49:11 +0100763 */
Andrey Andreevc8097262014-01-10 14:45:31 +0200764 public function attachment_cid($filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100765 {
Petr Heraleckyde886152014-01-10 12:52:56 +0100766 if ($this->multipart !== 'related')
Petr Heralecky300e3f02014-01-10 11:49:11 +0100767 {
768 $this->multipart = 'related'; // Thunderbird need this for inline images
769 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200770
Petr Heraleckyde886152014-01-10 12:52:56 +0100771 for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100772 {
Petr Heralecky9ad2fff2014-01-10 13:25:34 +0100773 if ($this->_attachments[$i]['name'][0] === $filename)
Petr Heralecky300e3f02014-01-10 11:49:11 +0100774 {
Petr Heralecky230fca32014-01-10 12:55:57 +0100775 $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
Petr Heraleckyde886152014-01-10 12:52:56 +0100776 return $this->_attachments[$i]['cid'];
Petr Heralecky300e3f02014-01-10 11:49:11 +0100777 }
778 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200779
Petr Heralecky300e3f02014-01-10 11:49:11 +0100780 return FALSE;
781 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000782
783 // --------------------------------------------------------------------
784
785 /**
786 * Add a Header Item
787 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 * @param string
789 * @param string
790 * @return void
791 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700792 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
florisluitenf55d5142013-06-07 17:20:06 +0300794 $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 // --------------------------------------------------------------------
798
799 /**
800 * Convert a String to an Array
801 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 * @param string
803 * @return array
804 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600805 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
807 if ( ! is_array($email))
808 {
Andrey Andreev56454792012-05-17 14:32:19 +0300809 return (strpos($email, ',') !== FALSE)
810 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
811 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
Andrey Andreev56454792012-05-17 14:32:19 +0300813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 return $email;
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // --------------------------------------------------------------------
818
819 /**
820 * Set Multipart Value
821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500823 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000825 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400827 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600828 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 // --------------------------------------------------------------------
832
833 /**
834 * Set Mailtype
835 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500837 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000839 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
Andrey Andreev56454792012-05-17 14:32:19 +0300841 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600842 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 /**
848 * Set Wordwrap
849 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400850 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500851 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000853 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400855 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600856 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 // --------------------------------------------------------------------
860
861 /**
862 * Set Protocol
863 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500865 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000867 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200869 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600870 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
874
875 /**
876 * Set Priority
877 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200878 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500879 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000881 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200883 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600884 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 // --------------------------------------------------------------------
888
889 /**
890 * Set Newline Character
891 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500893 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000895 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200897 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600898 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
902
903 /**
904 * Set CRLF
905 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500907 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000909 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200911 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600912 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 // --------------------------------------------------------------------
916
917 /**
918 * Set Message Boundary
919 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @return void
921 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600922 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200924 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
925 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
929
930 /**
931 * Get the Message ID
932 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @return string
934 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600935 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200937 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300938 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // --------------------------------------------------------------------
942
943 /**
944 * Get Mail Protocol
945 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200947 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600949 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
951 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200952 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000953
Alex Bilbied261b1e2012-06-02 11:12:16 +0100954 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 return $this->protocol;
957 }
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 // --------------------------------------------------------------------
961
962 /**
963 * Get Mail Encoding
964 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 * @param bool
966 * @return string
967 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600968 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200970 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
972 foreach ($this->_base_charsets as $charset)
973 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300974 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 $this->_encoding = '7bit';
977 }
978 }
979
Alex Bilbied261b1e2012-06-02 11:12:16 +0100980 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 return $this->_encoding;
983 }
984 }
985
986 // --------------------------------------------------------------------
987
988 /**
989 * Get content type (text/html/attachment)
990 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @return string
992 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600993 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Andrey Andreev56454792012-05-17 14:32:19 +0300995 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300997 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300999 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
1001 return 'plain-attach';
1002 }
1003 else
1004 {
1005 return 'plain';
1006 }
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // --------------------------------------------------------------------
1010
1011 /**
1012 * Set RFC 822 Date
1013 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @return string
1015 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001016 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001018 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001019 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001021 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
Andrey Andreev59c5b532012-03-01 14:09:51 +02001023 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Mime message
1030 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 * @return string
1032 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001033 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001035 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 +00001036 }
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 // --------------------------------------------------------------------
1039
1040 /**
1041 * Validate Email Address
1042 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 * @param string
1044 * @return bool
1045 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001046 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
1048 if ( ! is_array($email))
1049 {
patworkb0707982011-04-08 15:10:05 +02001050 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 return FALSE;
1052 }
1053
1054 foreach ($email as $val)
1055 {
1056 if ( ! $this->valid_email($val))
1057 {
patworkb0707982011-04-08 15:10:05 +02001058 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 return FALSE;
1060 }
1061 }
1062
1063 return TRUE;
1064 }
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 // --------------------------------------------------------------------
1067
1068 /**
1069 * Email Validation
1070 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @param string
1072 * @return bool
1073 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001074 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001076 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
Barry Mienydd671972010-10-04 16:33:58 +02001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 * @param string
1085 * @return string
1086 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001087 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
1089 if ( ! is_array($email))
1090 {
Andrey Andreev56454792012-05-17 14:32:19 +03001091 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
1093
1094 $clean_email = array();
1095
1096 foreach ($email as $addy)
1097 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001098 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 }
1100
1101 return $clean_email;
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Build alternative plain text message
1108 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001109 * Provides the raw message for use in plain-text headers of
1110 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * If the user hasn't specified his own alternative message
1112 * it creates one by stripping the HTML
1113 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 * @return string
1115 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001116 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001118 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001120 return ($this->wordwrap)
1121 ? $this->word_wrap($this->alt_message, 76)
1122 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
1124
Andrey Andreev56454792012-05-17 14:32:19 +03001125 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001126 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001127
1128 for ($i = 20; $i >= 3; $i--)
1129 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 }
1132
GDmac9bea4be2012-10-30 06:14:19 +01001133 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001134 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001135
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001136 return ($this->wordwrap)
1137 ? $this->word_wrap($body, 76)
1138 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 }
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 // --------------------------------------------------------------------
1142
1143 /**
1144 * Word Wrap
1145 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001147 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 * @return string
1149 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001150 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001152 // Set the character limit, if not already present
1153 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001155 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
1157
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 // Standardize newlines
1159 if (strpos($str, "\r") !== FALSE)
1160 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001161 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
1163
GDmac9bea4be2012-10-30 06:14:19 +01001164 // Reduce multiple spaces at end of line
1165 $str = preg_replace('| +\n|', "\n", $str);
1166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 // If the current word is surrounded by {unwrap} tags we'll
1168 // strip the entire chunk and replace it with a marker.
1169 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +03001170 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001172 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001174 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +03001175 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
1177 }
1178
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001179 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001181 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 $str = wordwrap($str, $charlim, "\n", FALSE);
1183
1184 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001185 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 foreach (explode("\n", $str) as $line)
1187 {
1188 // Is the line within the allowed character count?
1189 // If so we'll join it to the output and continue
1190 if (strlen($line) <= $charlim)
1191 {
1192 $output .= $line.$this->newline;
1193 continue;
1194 }
1195
1196 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001197 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
1199 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +03001200 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
1202 break;
1203 }
1204
1205 // Trim the word down
1206 $temp .= substr($line, 0, $charlim-1);
1207 $line = substr($line, $charlim-1);
1208 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001209 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001210
1211 // If $temp contains data it means we had to split up an over-length
1212 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001213 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001215 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 }
1217
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001218 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
1220
1221 // Put our markers back
1222 if (count($unwrap) > 0)
1223 {
1224 foreach ($unwrap as $key => $val)
1225 {
Andrey Andreev56454792012-05-17 14:32:19 +03001226 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 }
1228 }
1229
1230 return $output;
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 // --------------------------------------------------------------------
1234
1235 /**
1236 * Build final headers
1237 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 * @return string
1239 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001240 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001242 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1243 $this->set_header('X-Mailer', $this->useragent);
1244 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
1245 $this->set_header('Message-ID', $this->_get_message_id());
1246 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 // --------------------------------------------------------------------
1250
1251 /**
1252 * Write Headers as a string
1253 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 * @return void
1255 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001256 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001258 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301260 if (isset($this->_headers['Subject']))
1261 {
1262 $this->_subject = $this->_headers['Subject'];
1263 unset($this->_headers['Subject']);
1264 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 }
1266
1267 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001268 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001269
Pascal Kriete14287f32011-02-14 13:39:34 -05001270 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
1272 $val = trim($val);
1273
Alex Bilbied261b1e2012-06-02 11:12:16 +01001274 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Andrey Andreev56454792012-05-17 14:32:19 +03001276 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
1278 }
1279
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001280 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
Derek Jones1d890882009-02-10 20:32:14 +00001282 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
1284 }
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // --------------------------------------------------------------------
1287
1288 /**
1289 * Build Final Body and attachments
1290 *
buhayc6da1ef2013-04-18 09:06:49 -07001291 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001293 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001295 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
1297 $this->_body = $this->word_wrap($this->_body);
1298 }
1299
1300 $this->_set_boundaries();
1301 $this->_write_headers();
1302
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001303 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001304 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001305
1306 switch ($this->_get_content_type())
1307 {
1308 case 'plain' :
1309
Andrey Andreev56454792012-05-17 14:32:19 +03001310 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1311 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001312
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001313 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 {
1315 $this->_header_str .= $hdr;
1316 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 }
Brandon Jones485d7412010-11-09 16:38:17 -05001318 else
1319 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001320 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
Brandon Jones485d7412010-11-09 16:38:17 -05001321 }
Eric Barnes6113f542010-12-29 13:36:12 -05001322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 return;
1324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 case 'html' :
1326
1327 if ($this->send_multipart === FALSE)
1328 {
Andrey Andreev56454792012-05-17 14:32:19 +03001329 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001330 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 }
1332 else
1333 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001334 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001335
Andrey Andreev56454792012-05-17 14:32:19 +03001336 $body .= $this->_get_mime_message().$this->newline.$this->newline
1337 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001338
Andrey Andreev56454792012-05-17 14:32:19 +03001339 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1340 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1341 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001342
Andrey Andreev56454792012-05-17 14:32:19 +03001343 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1344 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 }
Eric Barnes6113f542010-12-29 13:36:12 -05001346
Andrey Andreev56454792012-05-17 14:32:19 +03001347 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001348
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001349 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
1351 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001352 }
1353 else
1354 {
Andrey Andreev2b956af2013-08-07 14:32:56 +03001355 $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 }
1357
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 if ($this->send_multipart !== FALSE)
1359 {
Andrey Andreev56454792012-05-17 14:32:19 +03001360 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 }
1362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 return;
1364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 case 'plain-attach' :
1366
Andrey Andreev2b956af2013-08-07 14:32:56 +03001367 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +00001368
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001369 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 {
1371 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001372 }
1373
Andrey Andreev2b956af2013-08-07 14:32:56 +03001374 $body .= $this->_get_mime_message().$this->newline
1375 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001376 .'--'.$this->_atc_boundary.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001377 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
Andrey Andreev2b956af2013-08-07 14:32:56 +03001378 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1379 .$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001380 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001381
1382 break;
1383 case 'html-attach' :
1384
Andrey Andreev2b956af2013-08-07 14:32:56 +03001385 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
Eric Barnes6113f542010-12-29 13:36:12 -05001386
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001387 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
1389 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 }
1391
Andrey Andreev56454792012-05-17 14:32:19 +03001392 $body .= $this->_get_mime_message().$this->newline.$this->newline
1393 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001394
Andrey Andreev56454792012-05-17 14:32:19 +03001395 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1396 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001397
Andrey Andreev56454792012-05-17 14:32:19 +03001398 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1399 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1400 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001401
Andrey Andreev56454792012-05-17 14:32:19 +03001402 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1403 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001404
Andrey Andreev56454792012-05-17 14:32:19 +03001405 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1406 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001407
1408 break;
1409 }
1410
1411 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001412 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001414 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001415 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001416 ? basename($filename) : $this->_attachments[$i]['name'][1];
Andrey Andreev56454792012-05-17 14:32:19 +03001417
1418 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001419 .'Content-type: '.$this->_attachments[$i]['type'].'; '
Andrey Andreev56454792012-05-17 14:32:19 +03001420 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001421 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Petr Heralecky300e3f02014-01-10 11:49:11 +01001422 .'Content-Transfer-Encoding: base64'.$this->newline
Petr Heraleckyde886152014-01-10 12:52:56 +01001423 .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
Derek Allard2067d1a2008-11-13 22:59:24 +00001424
Petr Heralecky300e3f02014-01-10 11:49:11 +01001425 $attachment[$z++] = $this->_attachments[$i]['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 }
1427
Andrey Andreev56454792012-05-17 14:32:19 +03001428 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
Andrey Andreev2b956af2013-08-07 14:32:56 +03001429 $this->_finalbody = ($this->_get_protocol() === 'mail')
1430 ? $body
1431 : $hdr.$this->newline.$this->newline.$body;
Andrey Andreevc8097262014-01-10 14:45:31 +02001432
buhay466e8082013-04-17 14:25:34 -07001433 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 }
Barry Mienydd671972010-10-04 16:33:58 +02001435
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 // --------------------------------------------------------------------
1437
1438 /**
1439 * Prep Quoted Printable
1440 *
1441 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1442 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1443 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 * @return string
1446 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001447 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001449 // We are intentionally wrapping so mail servers will encode characters
1450 // properly and MUAs will behave, so {unwrap} must go!
1451 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1452
Andrey Andreev683b34d2012-10-09 15:00:00 +03001453 // RFC 2045 specifies CRLF as "\r\n".
1454 // However, many developers choose to override that and violate
1455 // the RFC rules due to (apparently) a bug in MS Exchange,
1456 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001457 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001458 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001459 if (is_php('5.3'))
1460 {
1461 return quoted_printable_encode($str);
1462 }
1463 elseif (function_exists('imap_8bit'))
1464 {
1465 return imap_8bit($str);
1466 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001467 }
1468
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001469 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001470 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001471
1472 // Standardize newlines
1473 if (strpos($str, "\r") !== FALSE)
1474 {
1475 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1476 }
1477
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 $escape = '=';
1479 $output = '';
1480
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001481 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
1483 $length = strlen($line);
1484 $temp = '';
1485
1486 // Loop through each character in the line to add soft-wrap
1487 // characters at the end of a line " =\r\n" and add the newly
1488 // processed line(s) to the output (see comment on $crlf class property)
1489 for ($i = 0; $i < $length; $i++)
1490 {
1491 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001492 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 $ascii = ord($char);
1494
1495 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001496 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001498 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001500 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001502 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 }
1504
1505 // If we're at the character limit, add the line to the output,
1506 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001507 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
1509 $output .= $temp.$escape.$this->crlf;
1510 $temp = '';
1511 }
1512
1513 // Add the character to our temporary line
1514 $temp .= $char;
1515 }
1516
1517 // Add our completed line to the output
1518 $output .= $temp.$this->crlf;
1519 }
1520
1521 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001522 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 }
1524
1525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 /**
1528 * Prep Q Encoding
1529 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001530 * Performs "Q Encoding" on a string for use in email headers.
1531 * It's related but not identical to quoted-printable, so it has its
1532 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001534 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001535 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001537 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001539 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001540
Andrey Andreev925dd902012-10-19 11:06:31 +03001541 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001543 if (MB_ENABLED === TRUE)
1544 {
1545 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1546 }
1547 elseif (extension_loaded('iconv'))
1548 {
1549 $output = @iconv_mime_encode('', $str,
1550 array(
1551 'scheme' => 'Q',
1552 'line-length' => 76,
1553 'input-charset' => $this->charset,
1554 'output-charset' => $this->charset,
1555 'line-break-chars' => $this->crlf
1556 )
1557 );
1558
1559 // There are reports that iconv_mime_encode() might fail and return FALSE
1560 if ($output !== FALSE)
1561 {
1562 // iconv_mime_encode() will always put a header field name.
1563 // We've passed it an empty one, but it still prepends our
1564 // encoded string with ': ', so we need to strip it.
1565 return substr($output, 2);
1566 }
1567
1568 $chars = iconv_strlen($str, 'UTF-8');
1569 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 }
1571
Andrey Andreev925dd902012-10-19 11:06:31 +03001572 // We might already have this set for UTF-8
1573 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001574
Andrey Andreev925dd902012-10-19 11:06:31 +03001575 $output = '=?'.$this->charset.'?Q?';
1576 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001578 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1579 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1580 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001581
Andrey Andreev925dd902012-10-19 11:06:31 +03001582 // RFC 2045 sets a limit of 76 characters per line.
1583 // We'll append ?= to the end of each line though.
1584 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001586 $output .= '?='.$this->crlf // EOL
1587 .' =?'.$this->charset.'?Q?'.$chr; // New line
1588 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001590 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001592 $output .= $chr;
1593 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 }
1596
Andrey Andreev925dd902012-10-19 11:06:31 +03001597 // End the header
1598 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 }
1600
1601 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001602
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 /**
1604 * Send Email
1605 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001606 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 * @return bool
1608 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001609 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001611 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
1613 $this->reply_to($this->_headers['From']);
1614 }
1615
Andrey Andreev76f15c92012-03-01 13:05:07 +02001616 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1617 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1618 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 {
patworkb0707982011-04-08 15:10:05 +02001620 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 return FALSE;
1622 }
1623
1624 $this->_build_headers();
1625
Andrey Andreev76f15c92012-03-01 13:05:07 +02001626 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001628 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001629
Alex Bilbiea87aab32012-07-30 09:50:37 +01001630 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001631 {
1632 $this->clear();
1633 }
1634
Alex Bilbieb901e732012-07-30 09:44:57 +01001635 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 }
1637
buhay466e8082013-04-17 14:25:34 -07001638 if ($this->_build_message() === FALSE)
1639 {
1640 return FALSE;
1641 }
buhayc6da1ef2013-04-18 09:06:49 -07001642
Alex Bilbieb901e732012-07-30 09:44:57 +01001643 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001644
Alex Bilbiea87aab32012-07-30 09:50:37 +01001645 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001646 {
1647 $this->clear();
1648 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001649
Alex Bilbieb901e732012-07-30 09:44:57 +01001650 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 }
Barry Mienydd671972010-10-04 16:33:58 +02001652
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 // --------------------------------------------------------------------
1654
1655 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001656 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001658 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001660 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001662 $float = $this->bcc_batch_size - 1;
1663 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 $chunk = array();
1665
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001666 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 {
1668 if (isset($this->_bcc_array[$i]))
1669 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001670 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
1672
Alex Bilbied261b1e2012-06-02 11:12:16 +01001673 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 {
1675 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001676 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001677 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 }
1679
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001680 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 {
1682 $chunk[] = substr($set, 1);
1683 }
1684 }
1685
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001686 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 {
1688 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001689
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001690 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001691
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001692 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001694 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 }
1696 else
1697 {
1698 $this->_bcc_array = $bcc;
1699 }
1700
buhay466e8082013-04-17 14:25:34 -07001701 if ($this->_build_message() === FALSE)
1702 {
1703 return FALSE;
1704 }
buhayc6da1ef2013-04-18 09:06:49 -07001705
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 $this->_spool_email();
1707 }
1708 }
Barry Mienydd671972010-10-04 16:33:58 +02001709
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 // --------------------------------------------------------------------
1711
1712 /**
1713 * Unwrap special elements
1714 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 * @return void
1716 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001717 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
Andrey Andreev56454792012-05-17 14:32:19 +03001719 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 }
Barry Mienydd671972010-10-04 16:33:58 +02001721
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 // --------------------------------------------------------------------
1723
1724 /**
1725 * Strip line-breaks via callback
1726 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001727 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 * @return string
1729 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001730 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
1732 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1733 {
1734 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1735 }
1736
1737 return $matches[1];
1738 }
Barry Mienydd671972010-10-04 16:33:58 +02001739
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 // --------------------------------------------------------------------
1741
1742 /**
1743 * Spool mail to the mail server
1744 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 * @return bool
1746 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001747 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 {
1749 $this->_unwrap_specials();
1750
Andrey Andreev56454792012-05-17 14:32:19 +03001751 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001752 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 {
Andrey Andreev56454792012-05-17 14:32:19 +03001754 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001755 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 }
1757
patworkb0707982011-04-08 15:10:05 +02001758 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 return TRUE;
1760 }
Barry Mienydd671972010-10-04 16:33:58 +02001761
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 // --------------------------------------------------------------------
1763
1764 /**
1765 * Send using mail()
1766 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 * @return bool
1768 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001769 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001771 if (is_array($this->_recipients))
1772 {
1773 $this->_recipients = implode(', ', $this->_recipients);
1774 }
1775
Alex Bilbied261b1e2012-06-02 11:12:16 +01001776 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001778 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780 else
1781 {
1782 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1783 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001784 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 +00001785 }
1786 }
Barry Mienydd671972010-10-04 16:33:58 +02001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 // --------------------------------------------------------------------
1789
1790 /**
1791 * Send using Sendmail
1792 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 * @return bool
1794 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001795 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001797 // is popen() enabled?
1798 if ( ! function_usable('popen')
1799 OR FALSE === ($fp = @popen(
1800 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
1801 .' -t -r '.$this->clean_email($this->_headers['Return-Path'])
1802 , 'w'))
1803 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001804 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001805 return FALSE;
1806 }
Derek Jones71141ce2010-03-02 16:41:20 -06001807
Derek Jonesc630bcf2008-11-17 21:09:45 +00001808 fputs($fp, $this->_header_str);
1809 fputs($fp, $this->_finalbody);
1810
Barry Mienydd671972010-10-04 16:33:58 +02001811 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001812
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001813 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 {
patworkb0707982011-04-08 15:10:05 +02001815 $this->_set_error_message('lang:email_exit_status', $status);
1816 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 return FALSE;
1818 }
1819
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 return TRUE;
1821 }
Barry Mienydd671972010-10-04 16:33:58 +02001822
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 // --------------------------------------------------------------------
1824
1825 /**
1826 * Send using SMTP
1827 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 * @return bool
1829 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001830 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301831 {
1832 if ($this->smtp_host === '')
1833 {
1834 $this->_set_error_message('lang:email_no_hostname');
1835 return FALSE;
1836 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001837
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301838 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1839 {
1840 return FALSE;
1841 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001842
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301843 $this->_send_command('from', $this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001844
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301845 foreach ($this->_recipients as $val)
1846 {
1847 $this->_send_command('to', $val);
1848 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001849
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301850 if (count($this->_cc_array) > 0)
1851 {
1852 foreach ($this->_cc_array as $val)
1853 {
1854 if ($val !== '')
1855 {
1856 $this->_send_command('to', $val);
1857 }
1858 }
1859 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001860
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301861 if (count($this->_bcc_array) > 0)
1862 {
1863 foreach ($this->_bcc_array as $val)
1864 {
1865 if ($val !== '')
1866 {
1867 $this->_send_command('to', $val);
1868 }
1869 }
1870 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001871
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301872 $this->_send_command('data');
Derek Allard2067d1a2008-11-13 22:59:24 +00001873
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301874 // perform dot transformation on any lines that begin with a dot
1875 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001876
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301877 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001878
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301879 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001880
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301881 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001882
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301883 if (strpos($reply, '250') !== 0)
1884 {
1885 $this->_set_error_message('lang:email_smtp_error', $reply);
1886 return FALSE;
1887 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001888
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301889 if ($this->smtp_keepalive)
1890 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301891 $this->_send_command('reset');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301892 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301893 else
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301894 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301895 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301896 }
Barry Mienydd671972010-10-04 16:33:58 +02001897
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301898 return TRUE;
1899 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001900
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301901 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001902
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301903 /**
1904 * SMTP Connect
1905 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301906 * @return string
1907 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301908 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301909 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301910 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301911 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301912 return TRUE;
1913 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001914
Andrey Andreev89b67b42013-03-12 19:34:23 +02001915 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001916
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301917 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1918 $this->smtp_port,
1919 $errno,
1920 $errstr,
1921 $this->smtp_timeout);
1922
1923 if ( ! is_resource($this->_smtp_connect))
1924 {
1925 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1926 return FALSE;
1927 }
1928
1929 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1930 $this->_set_error_message($this->_get_smtp_data());
1931
1932 if ($this->smtp_crypto === 'tls')
1933 {
1934 $this->_send_command('hello');
1935 $this->_send_command('starttls');
1936
1937 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1938
1939 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301940 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301941 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301942 return FALSE;
1943 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301944 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301945
1946 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301947 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001948
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301949 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001950
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301951 /**
1952 * Send SMTP command
1953 *
1954 * @param string
1955 * @param string
1956 * @return string
1957 */
1958 protected function _send_command($cmd, $data = '')
1959 {
1960 switch ($cmd)
1961 {
1962 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001963
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301964 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1965 {
1966 $this->_send_data('EHLO '.$this->_get_hostname());
1967 }
1968 else
1969 {
1970 $this->_send_data('HELO '.$this->_get_hostname());
1971 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001972
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301973 $resp = 250;
1974 break;
1975 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001976
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301977 $this->_send_data('STARTTLS');
1978 $resp = 220;
1979 break;
1980 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03001981
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301982 $this->_send_data('MAIL FROM:<'.$data.'>');
1983 $resp = 250;
1984 break;
1985 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03001986
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301987 if ($this->dsn)
1988 {
1989 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
1990 }
1991 else
1992 {
1993 $this->_send_data('RCPT TO:<'.$data.'>');
1994 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001995
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301996 $resp = 250;
1997 break;
1998 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001999
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302000 $this->_send_data('DATA');
2001 $resp = 354;
2002 break;
2003 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00002004
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302005 $this->_send_data('RSET');
2006 $resp = 250;
2007 break;
2008 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00002009
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302010 $this->_send_data('QUIT');
2011 $resp = 221;
2012 break;
2013 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002014
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302015 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002016
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302017 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00002018
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302019 if ((int) substr($reply, 0, 3) !== $resp)
2020 {
2021 $this->_set_error_message('lang:email_smtp_error', $reply);
2022 return FALSE;
2023 }
Barry Mienydd671972010-10-04 16:33:58 +02002024
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302025 if ($cmd === 'quit')
2026 {
2027 fclose($this->_smtp_connect);
2028 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002029
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302030 return TRUE;
2031 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002032
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302033 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002034
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302035 /**
2036 * SMTP Authenticate
2037 *
2038 * @return bool
2039 */
2040 protected function _smtp_authenticate()
2041 {
2042 if ( ! $this->_smtp_auth)
2043 {
2044 return TRUE;
2045 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002046
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302047 if ($this->smtp_user === '' && $this->smtp_pass === '')
2048 {
2049 $this->_set_error_message('lang:email_no_smtp_unpw');
2050 return FALSE;
2051 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002052
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302053 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002054
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302055 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002056
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002057 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302058 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302059 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302060 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002061 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302062 {
2063 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2064 return FALSE;
2065 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002066
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302067 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002068
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302069 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002070
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302071 if (strpos($reply, '334') !== 0)
2072 {
2073 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2074 return FALSE;
2075 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002076
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302077 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002078
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302079 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302080
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302081 if (strpos($reply, '235') !== 0)
2082 {
2083 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2084 return FALSE;
2085 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302086
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302087 return TRUE;
2088 }
Barry Mienydd671972010-10-04 16:33:58 +02002089
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 // --------------------------------------------------------------------
2091
2092 /**
2093 * Send SMTP data
2094 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002095 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 * @return bool
2097 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002098 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002100 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 {
patworkb0707982011-04-08 15:10:05 +02002102 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 return FALSE;
2104 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002105
2106 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 }
Barry Mienydd671972010-10-04 16:33:58 +02002108
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 // --------------------------------------------------------------------
2110
2111 /**
2112 * Get SMTP data
2113 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 * @return string
2115 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002116 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
Andrey Andreev56454792012-05-17 14:32:19 +03002118 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002119
2120 while ($str = fgets($this->_smtp_connect, 512))
2121 {
2122 $data .= $str;
2123
Alex Bilbied261b1e2012-06-02 11:12:16 +01002124 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
2126 break;
2127 }
2128 }
2129
2130 return $data;
2131 }
Barry Mienydd671972010-10-04 16:33:58 +02002132
Derek Allard2067d1a2008-11-13 22:59:24 +00002133 // --------------------------------------------------------------------
2134
2135 /**
2136 * Get Hostname
2137 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 * @return string
2139 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002140 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02002142 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00002143 }
Barry Mienydd671972010-10-04 16:33:58 +02002144
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 // --------------------------------------------------------------------
2146
2147 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 * Get Debug Message
2149 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002150 * @param array $include List of raw data chunks to include in the output
2151 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 * @return string
2153 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002154 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002155 {
2156 $msg = '';
2157
2158 if (count($this->_debug_msg) > 0)
2159 {
2160 foreach ($this->_debug_msg as $val)
2161 {
2162 $msg .= $val;
2163 }
2164 }
2165
Andrey Andreev61797f62012-11-26 16:15:12 +02002166 // Determine which parts of our raw data needs to be printed
2167 $raw_data = '';
2168 is_array($include) OR $include = array($include);
2169
2170 if (in_array('headers', $include, TRUE))
2171 {
Andrey Andreev58f677f2013-07-16 11:01:37 +03002172 $raw_data = htmlspecialchars($this->_header_str)."\n";
Andrey Andreev61797f62012-11-26 16:15:12 +02002173 }
2174
2175 if (in_array('subject', $include, TRUE))
2176 {
2177 $raw_data .= htmlspecialchars($this->_subject)."\n";
2178 }
2179
2180 if (in_array('body', $include, TRUE))
2181 {
2182 $raw_data .= htmlspecialchars($this->_finalbody);
2183 }
2184
2185 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 }
Barry Mienydd671972010-10-04 16:33:58 +02002187
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 // --------------------------------------------------------------------
2189
2190 /**
2191 * Set Message
2192 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002193 * @param string $msg
2194 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002195 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002197 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 {
2199 $CI =& get_instance();
2200 $CI->lang->load('email');
2201
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002202 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 {
Andrey Andreev56454792012-05-17 14:32:19 +03002204 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 }
2206 else
2207 {
Andrey Andreev56454792012-05-17 14:32:19 +03002208 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
2210 }
Barry Mienydd671972010-10-04 16:33:58 +02002211
Derek Allard2067d1a2008-11-13 22:59:24 +00002212 // --------------------------------------------------------------------
2213
2214 /**
2215 * Mime Types
2216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 * @param string
2218 * @return string
2219 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002220 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002221 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002222 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00002223
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002224 $ext = strtolower($ext);
2225
2226 if ( ! is_array($mimes))
2227 {
2228 $mimes =& get_mimes();
2229 }
2230
2231 if (isset($mimes[$ext]))
2232 {
2233 return is_array($mimes[$ext])
2234 ? current($mimes[$ext])
2235 : $mimes[$ext];
2236 }
2237
2238 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 }
2240
2241}
Derek Allard2067d1a2008-11-13 22:59:24 +00002242
2243/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03002244/* Location: ./system/libraries/Email.php */