blob: 10253c79604115eb65fdaffdb7692f87ea2eb4e2 [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 === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200402 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
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 === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200454 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000455
456 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
462 * Initialize the Email Data
463 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800464 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500465 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000467 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200469 $this->_subject = '';
470 $this->_body = '';
471 $this->_finalbody = '';
472 $this->_header_str = '';
473 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500475 $this->_cc_array = array();
476 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 $this->_headers = array();
478 $this->_debug_msg = array();
479
Mickey Wubfc1cad2012-05-31 22:28:40 -0700480 $this->set_header('User-Agent', $this->useragent);
481 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
483 if ($clear_attachments !== FALSE)
484 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300485 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
Eric Barnes6113f542010-12-29 13:36:12 -0500487
Greg Akera769deb2010-11-10 15:47:29 -0600488 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Set FROM
495 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300496 * @param string $from
497 * @param string $name
498 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500499 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300501 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200503 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200505 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
508 if ($this->validate)
509 {
510 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300511 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200512 {
513 $this->validate_email($this->_str_to_array($return_path));
514 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516
517 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100518 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 // only use Q encoding if there are characters that would require it
521 if ( ! preg_match('/[\200-\377]/', $name))
522 {
523 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000524 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526 else
527 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300528 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530 }
531
Mickey Wubfc1cad2012-05-31 22:28:40 -0700532 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200533
Andrey Andreev925dd902012-10-19 11:06:31 +0300534 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200535 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500536
Greg Akera769deb2010-11-10 15:47:29 -0600537 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 // --------------------------------------------------------------------
541
542 /**
543 * Set Reply-to
544 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @param string
546 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500547 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000549 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200551 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200553 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
556 if ($this->validate)
557 {
558 $this->validate_email($this->_str_to_array($replyto));
559 }
560
Alex Bilbied261b1e2012-06-02 11:12:16 +0100561 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 $name = $replyto;
564 }
565
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300566 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $name = '"'.$name.'"';
569 }
570
Mickey Wubfc1cad2012-05-31 22:28:40 -0700571 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600573
574 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // --------------------------------------------------------------------
578
579 /**
580 * Set Recipients
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500583 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000585 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $to = $this->_str_to_array($to);
588 $to = $this->clean_email($to);
589
590 if ($this->validate)
591 {
592 $this->validate_email($to);
593 }
594
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200595 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700597 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300600 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500601
Greg Akera769deb2010-11-10 15:47:29 -0600602 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // --------------------------------------------------------------------
606
607 /**
608 * Set CC
609 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500611 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000613 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
Andrey Andreev56454792012-05-17 14:32:19 +0300615 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
617 if ($this->validate)
618 {
619 $this->validate_email($cc);
620 }
621
Mickey Wubfc1cad2012-05-31 22:28:40 -0700622 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000623
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200624 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 $this->_cc_array = $cc;
627 }
Eric Barnes6113f542010-12-29 13:36:12 -0500628
Greg Akera769deb2010-11-10 15:47:29 -0600629 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // --------------------------------------------------------------------
633
634 /**
635 * Set BCC
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @param string
638 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500639 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000641 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 $this->bcc_batch_mode = TRUE;
646 $this->bcc_batch_size = $limit;
647 }
648
Andrey Andreev56454792012-05-17 14:32:19 +0300649 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000650
651 if ($this->validate)
652 {
653 $this->validate_email($bcc);
654 }
655
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200656 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
658 $this->_bcc_array = $bcc;
659 }
660 else
661 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700662 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
Eric Barnes6113f542010-12-29 13:36:12 -0500664
Greg Akera769deb2010-11-10 15:47:29 -0600665 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // --------------------------------------------------------------------
669
670 /**
671 * Set Email Subject
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500674 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000676 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700679 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600680 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 // --------------------------------------------------------------------
684
685 /**
686 * Set Body
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500689 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000691 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200693 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200694
Andrey Andreevaf728622011-10-20 10:11:59 +0300695 /* strip slashes only if magic quotes is ON
696 if we do it with magic quotes OFF, it strips real, user-inputted chars.
697
698 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
699 it will probably not exist in future versions at all.
700 */
701 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200702 {
diegorivera33c32802011-10-19 02:56:15 -0200703 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200704 }
diegorivera33c32802011-10-19 02:56:15 -0200705
Greg Akera769deb2010-11-10 15:47:29 -0600706 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
712 * Assign file attachments
713 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300714 * @param string $filename
715 * @param string $disposition = 'attachment'
716 * @param string $newname = NULL
717 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500718 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200720 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300722 $this->_attachments[] = array(
723 'name' => array($filename, $newname),
724 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
725 'type' => $mime
726 );
727
Greg Akera769deb2010-11-10 15:47:29 -0600728 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Add a Header Item
735 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 * @param string
737 * @param string
738 * @return void
739 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700740 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
742 $this->_headers[$header] = $value;
743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 // --------------------------------------------------------------------
746
747 /**
748 * Convert a String to an Array
749 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @param string
751 * @return array
752 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600753 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 if ( ! is_array($email))
756 {
Andrey Andreev56454792012-05-17 14:32:19 +0300757 return (strpos($email, ',') !== FALSE)
758 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
759 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
Andrey Andreev56454792012-05-17 14:32:19 +0300761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 return $email;
763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 // --------------------------------------------------------------------
766
767 /**
768 * Set Multipart Value
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500771 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000773 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400775 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600776 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Set Mailtype
783 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500785 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000787 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Andrey Andreev56454792012-05-17 14:32:19 +0300789 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600790 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 /**
796 * Set Wordwrap
797 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400798 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500799 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000801 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400803 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600804 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // --------------------------------------------------------------------
808
809 /**
810 * Set Protocol
811 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500813 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000815 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200817 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600818 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // --------------------------------------------------------------------
822
823 /**
824 * Set Priority
825 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200826 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500827 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000829 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200831 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600832 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
836
837 /**
838 * Set Newline Character
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500841 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000843 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200845 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600846 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // --------------------------------------------------------------------
850
851 /**
852 * Set CRLF
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500855 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000857 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200859 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600860 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
864
865 /**
866 * Set Message Boundary
867 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * @return void
869 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600870 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200872 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
873 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 // --------------------------------------------------------------------
877
878 /**
879 * Get the Message ID
880 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * @return string
882 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600883 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200885 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300886 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * Get Mail Protocol
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200895 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600897 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200900 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000901
Alex Bilbied261b1e2012-06-02 11:12:16 +0100902 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
904 return $this->protocol;
905 }
906 }
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 // --------------------------------------------------------------------
909
910 /**
911 * Get Mail Encoding
912 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 * @param bool
914 * @return string
915 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600916 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200918 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919
920 foreach ($this->_base_charsets as $charset)
921 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300922 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 $this->_encoding = '7bit';
925 }
926 }
927
Alex Bilbied261b1e2012-06-02 11:12:16 +0100928 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
930 return $this->_encoding;
931 }
932 }
933
934 // --------------------------------------------------------------------
935
936 /**
937 * Get content type (text/html/attachment)
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @return string
940 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600941 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
Andrey Andreev56454792012-05-17 14:32:19 +0300943 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300945 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300947 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 return 'plain-attach';
950 }
951 else
952 {
953 return 'plain';
954 }
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 // --------------------------------------------------------------------
958
959 /**
960 * Set RFC 822 Date
961 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 * @return string
963 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600964 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200966 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300967 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200969 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
Andrey Andreev59c5b532012-03-01 14:09:51 +0200971 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // --------------------------------------------------------------------
975
976 /**
977 * Mime message
978 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @return string
980 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600981 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200983 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 +0000984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // --------------------------------------------------------------------
987
988 /**
989 * Validate Email Address
990 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @param string
992 * @return bool
993 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000994 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 if ( ! is_array($email))
997 {
patworkb0707982011-04-08 15:10:05 +0200998 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return FALSE;
1000 }
1001
1002 foreach ($email as $val)
1003 {
1004 if ( ! $this->valid_email($val))
1005 {
patworkb0707982011-04-08 15:10:05 +02001006 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 return FALSE;
1008 }
1009 }
1010
1011 return TRUE;
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Email Validation
1018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string
1020 * @return bool
1021 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001022 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001024 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
Barry Mienydd671972010-10-04 16:33:58 +02001026
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 // --------------------------------------------------------------------
1028
1029 /**
1030 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1031 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @param string
1033 * @return string
1034 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001035 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
1037 if ( ! is_array($email))
1038 {
Andrey Andreev56454792012-05-17 14:32:19 +03001039 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
1041
1042 $clean_email = array();
1043
1044 foreach ($email as $addy)
1045 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001046 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
1048
1049 return $clean_email;
1050 }
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 // --------------------------------------------------------------------
1053
1054 /**
1055 * Build alternative plain text message
1056 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001057 * Provides the raw message for use in plain-text headers of
1058 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * If the user hasn't specified his own alternative message
1060 * it creates one by stripping the HTML
1061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @return string
1063 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001064 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001066 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001068 return ($this->wordwrap)
1069 ? $this->word_wrap($this->alt_message, 76)
1070 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
1072
Andrey Andreev56454792012-05-17 14:32:19 +03001073 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001074 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
1076 for ($i = 20; $i >= 3; $i--)
1077 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001078 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080
GDmac9bea4be2012-10-30 06:14:19 +01001081 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001082 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001083
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001084 return ($this->wordwrap)
1085 ? $this->word_wrap($body, 76)
1086 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 // --------------------------------------------------------------------
1090
1091 /**
1092 * Word Wrap
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001095 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @return string
1097 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001098 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001100 // Set the character limit, if not already present
1101 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001103 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
1105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 // Standardize newlines
1107 if (strpos($str, "\r") !== FALSE)
1108 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001109 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
1111
GDmac9bea4be2012-10-30 06:14:19 +01001112 // Reduce multiple spaces at end of line
1113 $str = preg_replace('| +\n|', "\n", $str);
1114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // If the current word is surrounded by {unwrap} tags we'll
1116 // strip the entire chunk and replace it with a marker.
1117 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +03001118 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001120 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001122 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +03001123 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
1125 }
1126
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001127 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001129 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 $str = wordwrap($str, $charlim, "\n", FALSE);
1131
1132 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001133 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 foreach (explode("\n", $str) as $line)
1135 {
1136 // Is the line within the allowed character count?
1137 // If so we'll join it to the output and continue
1138 if (strlen($line) <= $charlim)
1139 {
1140 $output .= $line.$this->newline;
1141 continue;
1142 }
1143
1144 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001145 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
1147 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +03001148 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
1150 break;
1151 }
1152
1153 // Trim the word down
1154 $temp .= substr($line, 0, $charlim-1);
1155 $line = substr($line, $charlim-1);
1156 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001157 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001158
1159 // If $temp contains data it means we had to split up an over-length
1160 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001161 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001163 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001166 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
1168
1169 // Put our markers back
1170 if (count($unwrap) > 0)
1171 {
1172 foreach ($unwrap as $key => $val)
1173 {
Andrey Andreev56454792012-05-17 14:32:19 +03001174 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
1176 }
1177
1178 return $output;
1179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // --------------------------------------------------------------------
1182
1183 /**
1184 * Build final headers
1185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * @return string
1187 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001188 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001190 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1191 $this->set_header('X-Mailer', $this->useragent);
1192 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
1193 $this->set_header('Message-ID', $this->_get_message_id());
1194 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 // --------------------------------------------------------------------
1198
1199 /**
1200 * Write Headers as a string
1201 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 * @return void
1203 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001204 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001206 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
nisheeth-barthwal6bb98902013-02-19 18:11:14 +05301208 if (isset($this->_headers['Subject']))
1209 {
1210 $this->_subject = $this->_headers['Subject'];
1211 unset($this->_headers['Subject']);
1212 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 }
1214
1215 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001216 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001217
Pascal Kriete14287f32011-02-14 13:39:34 -05001218 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
1220 $val = trim($val);
1221
Alex Bilbied261b1e2012-06-02 11:12:16 +01001222 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Andrey Andreev56454792012-05-17 14:32:19 +03001224 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
1226 }
1227
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001228 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
Derek Jones1d890882009-02-10 20:32:14 +00001230 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 // --------------------------------------------------------------------
1235
1236 /**
1237 * Build Final Body and attachments
1238 *
buhayc6da1ef2013-04-18 09:06:49 -07001239 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001241 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001243 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 $this->_body = $this->word_wrap($this->_body);
1246 }
1247
1248 $this->_set_boundaries();
1249 $this->_write_headers();
1250
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001251 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001252 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001253
1254 switch ($this->_get_content_type())
1255 {
1256 case 'plain' :
1257
Andrey Andreev56454792012-05-17 14:32:19 +03001258 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1259 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001260
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001261 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
1263 $this->_header_str .= $hdr;
1264 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 }
Brandon Jones485d7412010-11-09 16:38:17 -05001266 else
1267 {
1268 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1269 }
Eric Barnes6113f542010-12-29 13:36:12 -05001270
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 return;
1272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 case 'html' :
1274
1275 if ($this->send_multipart === FALSE)
1276 {
Andrey Andreev56454792012-05-17 14:32:19 +03001277 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
1278 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 }
1280 else
1281 {
Andrey Andreev56454792012-05-17 14:32:19 +03001282 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001283
Andrey Andreev56454792012-05-17 14:32:19 +03001284 $body .= $this->_get_mime_message().$this->newline.$this->newline
1285 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001286
Andrey Andreev56454792012-05-17 14:32:19 +03001287 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1288 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1289 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001290
Andrey Andreev56454792012-05-17 14:32:19 +03001291 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1292 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 }
Eric Barnes6113f542010-12-29 13:36:12 -05001294
Andrey Andreev56454792012-05-17 14:32:19 +03001295 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001296
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001297 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
1299 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001300 }
1301 else
1302 {
Andrey Andreev56454792012-05-17 14:32:19 +03001303 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 }
1305
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 if ($this->send_multipart !== FALSE)
1307 {
Andrey Andreev56454792012-05-17 14:32:19 +03001308 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 }
1310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 return;
1312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 case 'plain-attach' :
1314
Andrey Andreev56454792012-05-17 14:32:19 +03001315 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001316
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001317 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
1319 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001320 }
1321
Andrey Andreev56454792012-05-17 14:32:19 +03001322 $body .= $this->_get_mime_message().$this->newline.$this->newline
1323 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001324
Andrey Andreev56454792012-05-17 14:32:19 +03001325 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1326 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001327
Andrey Andreev56454792012-05-17 14:32:19 +03001328 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001329
1330 break;
1331 case 'html-attach' :
1332
Andrey Andreev56454792012-05-17 14:32:19 +03001333 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001334
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001335 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 {
1337 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 }
1339
Andrey Andreev56454792012-05-17 14:32:19 +03001340 $body .= $this->_get_mime_message().$this->newline.$this->newline
1341 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001342
Andrey Andreev56454792012-05-17 14:32:19 +03001343 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1344 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001345
Andrey Andreev56454792012-05-17 14:32:19 +03001346 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1347 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1348 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001349
Andrey Andreev56454792012-05-17 14:32:19 +03001350 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1351 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001352
Andrey Andreev56454792012-05-17 14:32:19 +03001353 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1354 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001355
1356 break;
1357 }
1358
1359 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001360 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001362 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001363 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001364 ? basename($filename) : $this->_attachments[$i]['name'][1];
1365 $ctype = $this->_attachments[$i]['type'];
Matteo Mattei5688d582012-03-13 19:29:29 +01001366 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001367
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001368 if ($ctype === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001370 if ( ! file_exists($filename))
1371 {
1372 $this->_set_error_message('lang:email_attachment_missing', $filename);
1373 return FALSE;
1374 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001375
Matteo Mattei5688d582012-03-13 19:29:29 +01001376 $file = filesize($filename) +1;
1377
1378 if ( ! $fp = fopen($filename, FOPEN_READ))
1379 {
1380 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1381 return FALSE;
1382 }
1383
Matteo Matteic3b36f42012-03-26 10:27:17 +02001384 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001385 $file_content = fread($fp, $file);
1386 fclose($fp);
1387 }
1388 else
1389 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001390 $file_content =& $this->_attachments[$i]['name'][0];
Matteo Mattei5688d582012-03-13 19:29:29 +01001391 }
Andrey Andreev56454792012-05-17 14:32:19 +03001392
1393 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1394 .'Content-type: '.$ctype.'; '
1395 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001396 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001397 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001398
Matteo Mattei5688d582012-03-13 19:29:29 +01001399 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 }
1401
Andrey Andreev56454792012-05-17 14:32:19 +03001402 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1403 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
buhay466e8082013-04-17 14:25:34 -07001404 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 }
Barry Mienydd671972010-10-04 16:33:58 +02001406
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 // --------------------------------------------------------------------
1408
1409 /**
1410 * Prep Quoted Printable
1411 *
1412 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1413 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1414 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 * @return string
1417 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001418 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001420 // We are intentionally wrapping so mail servers will encode characters
1421 // properly and MUAs will behave, so {unwrap} must go!
1422 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1423
Andrey Andreev683b34d2012-10-09 15:00:00 +03001424 // RFC 2045 specifies CRLF as "\r\n".
1425 // However, many developers choose to override that and violate
1426 // the RFC rules due to (apparently) a bug in MS Exchange,
1427 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001428 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001429 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001430 if (is_php('5.3'))
1431 {
1432 return quoted_printable_encode($str);
1433 }
1434 elseif (function_exists('imap_8bit'))
1435 {
1436 return imap_8bit($str);
1437 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001438 }
1439
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001440 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001441 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001442
1443 // Standardize newlines
1444 if (strpos($str, "\r") !== FALSE)
1445 {
1446 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1447 }
1448
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 $escape = '=';
1450 $output = '';
1451
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001452 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 {
1454 $length = strlen($line);
1455 $temp = '';
1456
1457 // Loop through each character in the line to add soft-wrap
1458 // characters at the end of a line " =\r\n" and add the newly
1459 // processed line(s) to the output (see comment on $crlf class property)
1460 for ($i = 0; $i < $length; $i++)
1461 {
1462 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001463 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 $ascii = ord($char);
1465
1466 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001467 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001469 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001471 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001473 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 }
1475
1476 // If we're at the character limit, add the line to the output,
1477 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001478 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 {
1480 $output .= $temp.$escape.$this->crlf;
1481 $temp = '';
1482 }
1483
1484 // Add the character to our temporary line
1485 $temp .= $char;
1486 }
1487
1488 // Add our completed line to the output
1489 $output .= $temp.$this->crlf;
1490 }
1491
1492 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001493 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 }
1495
1496 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001497
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 /**
1499 * Prep Q Encoding
1500 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001501 * Performs "Q Encoding" on a string for use in email headers.
1502 * It's related but not identical to quoted-printable, so it has its
1503 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001505 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001506 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001508 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001510 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001511
Andrey Andreev925dd902012-10-19 11:06:31 +03001512 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001514 if (MB_ENABLED === TRUE)
1515 {
1516 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1517 }
1518 elseif (extension_loaded('iconv'))
1519 {
1520 $output = @iconv_mime_encode('', $str,
1521 array(
1522 'scheme' => 'Q',
1523 'line-length' => 76,
1524 'input-charset' => $this->charset,
1525 'output-charset' => $this->charset,
1526 'line-break-chars' => $this->crlf
1527 )
1528 );
1529
1530 // There are reports that iconv_mime_encode() might fail and return FALSE
1531 if ($output !== FALSE)
1532 {
1533 // iconv_mime_encode() will always put a header field name.
1534 // We've passed it an empty one, but it still prepends our
1535 // encoded string with ': ', so we need to strip it.
1536 return substr($output, 2);
1537 }
1538
1539 $chars = iconv_strlen($str, 'UTF-8');
1540 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 }
1542
Andrey Andreev925dd902012-10-19 11:06:31 +03001543 // We might already have this set for UTF-8
1544 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001545
Andrey Andreev925dd902012-10-19 11:06:31 +03001546 $output = '=?'.$this->charset.'?Q?';
1547 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001549 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1550 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1551 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001552
Andrey Andreev925dd902012-10-19 11:06:31 +03001553 // RFC 2045 sets a limit of 76 characters per line.
1554 // We'll append ?= to the end of each line though.
1555 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001557 $output .= '?='.$this->crlf // EOL
1558 .' =?'.$this->charset.'?Q?'.$chr; // New line
1559 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001561 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001563 $output .= $chr;
1564 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 }
1567
Andrey Andreev925dd902012-10-19 11:06:31 +03001568 // End the header
1569 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 }
1571
1572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001573
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 /**
1575 * Send Email
1576 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001577 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 * @return bool
1579 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001580 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001582 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 {
1584 $this->reply_to($this->_headers['From']);
1585 }
1586
Andrey Andreev76f15c92012-03-01 13:05:07 +02001587 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1588 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1589 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
patworkb0707982011-04-08 15:10:05 +02001591 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 return FALSE;
1593 }
1594
1595 $this->_build_headers();
1596
Andrey Andreev76f15c92012-03-01 13:05:07 +02001597 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001599 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001600
Alex Bilbiea87aab32012-07-30 09:50:37 +01001601 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001602 {
1603 $this->clear();
1604 }
1605
Alex Bilbieb901e732012-07-30 09:44:57 +01001606 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 }
1608
buhay466e8082013-04-17 14:25:34 -07001609 if ($this->_build_message() === FALSE)
1610 {
1611 return FALSE;
1612 }
buhayc6da1ef2013-04-18 09:06:49 -07001613
Alex Bilbieb901e732012-07-30 09:44:57 +01001614 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001615
Alex Bilbiea87aab32012-07-30 09:50:37 +01001616 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001617 {
1618 $this->clear();
1619 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001620
Alex Bilbieb901e732012-07-30 09:44:57 +01001621 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 }
Barry Mienydd671972010-10-04 16:33:58 +02001623
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 // --------------------------------------------------------------------
1625
1626 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001627 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001629 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001631 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001633 $float = $this->bcc_batch_size - 1;
1634 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 $chunk = array();
1636
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001637 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 {
1639 if (isset($this->_bcc_array[$i]))
1640 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001641 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 }
1643
Alex Bilbied261b1e2012-06-02 11:12:16 +01001644 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
1646 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001647 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001648 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001649 }
1650
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001651 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
1653 $chunk[] = substr($set, 1);
1654 }
1655 }
1656
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001657 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 {
1659 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001660
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001661 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001662
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001663 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001665 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 }
1667 else
1668 {
1669 $this->_bcc_array = $bcc;
1670 }
1671
buhay466e8082013-04-17 14:25:34 -07001672 if ($this->_build_message() === FALSE)
1673 {
1674 return FALSE;
1675 }
buhayc6da1ef2013-04-18 09:06:49 -07001676
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 $this->_spool_email();
1678 }
1679 }
Barry Mienydd671972010-10-04 16:33:58 +02001680
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 // --------------------------------------------------------------------
1682
1683 /**
1684 * Unwrap special elements
1685 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 * @return void
1687 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001688 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 {
Andrey Andreev56454792012-05-17 14:32:19 +03001690 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 }
Barry Mienydd671972010-10-04 16:33:58 +02001692
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 // --------------------------------------------------------------------
1694
1695 /**
1696 * Strip line-breaks via callback
1697 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001698 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 * @return string
1700 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001701 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 {
1703 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1704 {
1705 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1706 }
1707
1708 return $matches[1];
1709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 // --------------------------------------------------------------------
1712
1713 /**
1714 * Spool mail to the mail server
1715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 * @return bool
1717 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001718 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
1720 $this->_unwrap_specials();
1721
Andrey Andreev56454792012-05-17 14:32:19 +03001722 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001723 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 {
Andrey Andreev56454792012-05-17 14:32:19 +03001725 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001726 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 }
1728
patworkb0707982011-04-08 15:10:05 +02001729 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 return TRUE;
1731 }
Barry Mienydd671972010-10-04 16:33:58 +02001732
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 // --------------------------------------------------------------------
1734
1735 /**
1736 * Send using mail()
1737 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 * @return bool
1739 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001740 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001742 if (is_array($this->_recipients))
1743 {
1744 $this->_recipients = implode(', ', $this->_recipients);
1745 }
1746
Alex Bilbied261b1e2012-06-02 11:12:16 +01001747 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001749 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 }
1751 else
1752 {
1753 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1754 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001755 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 +00001756 }
1757 }
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 // --------------------------------------------------------------------
1760
1761 /**
1762 * Send using Sendmail
1763 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 * @return bool
1765 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001766 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001768 // is popen() enabled?
1769 if ( ! function_usable('popen')
1770 OR FALSE === ($fp = @popen(
1771 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
1772 .' -t -r '.$this->clean_email($this->_headers['Return-Path'])
1773 , 'w'))
1774 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001775 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001776 return FALSE;
1777 }
Derek Jones71141ce2010-03-02 16:41:20 -06001778
Derek Jonesc630bcf2008-11-17 21:09:45 +00001779 fputs($fp, $this->_header_str);
1780 fputs($fp, $this->_finalbody);
1781
Barry Mienydd671972010-10-04 16:33:58 +02001782 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001783
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001784 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
patworkb0707982011-04-08 15:10:05 +02001786 $this->_set_error_message('lang:email_exit_status', $status);
1787 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 return FALSE;
1789 }
1790
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 return TRUE;
1792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Send using SMTP
1798 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @return bool
1800 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001801 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301802 {
1803 if ($this->smtp_host === '')
1804 {
1805 $this->_set_error_message('lang:email_no_hostname');
1806 return FALSE;
1807 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001808
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301809 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1810 {
1811 return FALSE;
1812 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001813
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301814 $this->_send_command('from', $this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001815
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301816 foreach ($this->_recipients as $val)
1817 {
1818 $this->_send_command('to', $val);
1819 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001820
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301821 if (count($this->_cc_array) > 0)
1822 {
1823 foreach ($this->_cc_array as $val)
1824 {
1825 if ($val !== '')
1826 {
1827 $this->_send_command('to', $val);
1828 }
1829 }
1830 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001831
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301832 if (count($this->_bcc_array) > 0)
1833 {
1834 foreach ($this->_bcc_array as $val)
1835 {
1836 if ($val !== '')
1837 {
1838 $this->_send_command('to', $val);
1839 }
1840 }
1841 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001842
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301843 $this->_send_command('data');
Derek Allard2067d1a2008-11-13 22:59:24 +00001844
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301845 // perform dot transformation on any lines that begin with a dot
1846 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001847
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301848 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001849
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301850 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001851
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301852 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001853
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301854 if (strpos($reply, '250') !== 0)
1855 {
1856 $this->_set_error_message('lang:email_smtp_error', $reply);
1857 return FALSE;
1858 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001859
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301860 if ($this->smtp_keepalive)
1861 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301862 $this->_send_command('reset');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301863 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301864 else
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301865 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301866 $this->_send_command('quit');
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05301867 }
Barry Mienydd671972010-10-04 16:33:58 +02001868
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301869 return TRUE;
1870 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001871
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301872 // --------------------------------------------------------------------
Radu Potop4c589ae2011-09-29 10:19:55 +03001873
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301874 /**
1875 * SMTP Connect
1876 *
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301877 * @return string
1878 */
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301879 protected function _smtp_connect()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301880 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301881 if (is_resource($this->_smtp_connect))
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301882 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301883 return TRUE;
1884 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001885
Andrey Andreev89b67b42013-03-12 19:34:23 +02001886 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001887
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301888 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1889 $this->smtp_port,
1890 $errno,
1891 $errstr,
1892 $this->smtp_timeout);
1893
1894 if ( ! is_resource($this->_smtp_connect))
1895 {
1896 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1897 return FALSE;
1898 }
1899
1900 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1901 $this->_set_error_message($this->_get_smtp_data());
1902
1903 if ($this->smtp_crypto === 'tls')
1904 {
1905 $this->_send_command('hello');
1906 $this->_send_command('starttls');
1907
1908 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1909
1910 if ($crypto !== TRUE)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301911 {
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301912 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301913 return FALSE;
1914 }
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301915 }
nisheeth-barthwala44e6912013-02-18 18:07:03 +05301916
1917 return $this->_send_command('hello');
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301918 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001919
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301920 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001921
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301922 /**
1923 * Send SMTP command
1924 *
1925 * @param string
1926 * @param string
1927 * @return string
1928 */
1929 protected function _send_command($cmd, $data = '')
1930 {
1931 switch ($cmd)
1932 {
1933 case 'hello' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001934
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301935 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1936 {
1937 $this->_send_data('EHLO '.$this->_get_hostname());
1938 }
1939 else
1940 {
1941 $this->_send_data('HELO '.$this->_get_hostname());
1942 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001943
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301944 $resp = 250;
1945 break;
1946 case 'starttls' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001947
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301948 $this->_send_data('STARTTLS');
1949 $resp = 220;
1950 break;
1951 case 'from' :
Andrey Andreev56454792012-05-17 14:32:19 +03001952
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301953 $this->_send_data('MAIL FROM:<'.$data.'>');
1954 $resp = 250;
1955 break;
1956 case 'to' :
Andrey Andreev56454792012-05-17 14:32:19 +03001957
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301958 if ($this->dsn)
1959 {
1960 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
1961 }
1962 else
1963 {
1964 $this->_send_data('RCPT TO:<'.$data.'>');
1965 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001966
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301967 $resp = 250;
1968 break;
1969 case 'data' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001970
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301971 $this->_send_data('DATA');
1972 $resp = 354;
1973 break;
1974 case 'reset':
Derek Allard2067d1a2008-11-13 22:59:24 +00001975
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301976 $this->_send_data('RSET');
1977 $resp = 250;
1978 break;
1979 case 'quit' :
Derek Allard2067d1a2008-11-13 22:59:24 +00001980
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301981 $this->_send_data('QUIT');
1982 $resp = 221;
1983 break;
1984 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001985
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301986 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001987
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301988 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001989
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301990 if ((int) substr($reply, 0, 3) !== $resp)
1991 {
1992 $this->_set_error_message('lang:email_smtp_error', $reply);
1993 return FALSE;
1994 }
Barry Mienydd671972010-10-04 16:33:58 +02001995
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301996 if ($cmd === 'quit')
1997 {
1998 fclose($this->_smtp_connect);
1999 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002000
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302001 return TRUE;
2002 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002003
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302004 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00002005
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302006 /**
2007 * SMTP Authenticate
2008 *
2009 * @return bool
2010 */
2011 protected function _smtp_authenticate()
2012 {
2013 if ( ! $this->_smtp_auth)
2014 {
2015 return TRUE;
2016 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002017
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302018 if ($this->smtp_user === '' && $this->smtp_pass === '')
2019 {
2020 $this->_set_error_message('lang:email_no_smtp_unpw');
2021 return FALSE;
2022 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002023
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302024 $this->_send_data('AUTH LOGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +00002025
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302026 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002027
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002028 if (strpos($reply, '503') === 0) // Already authenticated
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302029 {
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302030 return TRUE;
nisheeth-barthwal758f40c2013-02-18 17:18:51 +05302031 }
Andrey Andreevfa01ae42013-03-04 14:40:18 +02002032 elseif (strpos($reply, '334') !== 0)
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302033 {
2034 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2035 return FALSE;
2036 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002037
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302038 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002039
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302040 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002041
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302042 if (strpos($reply, '334') !== 0)
2043 {
2044 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2045 return FALSE;
2046 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002047
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302048 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002049
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302050 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302051
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302052 if (strpos($reply, '235') !== 0)
2053 {
2054 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2055 return FALSE;
2056 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302057
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302058 return TRUE;
2059 }
Barry Mienydd671972010-10-04 16:33:58 +02002060
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 // --------------------------------------------------------------------
2062
2063 /**
2064 * Send SMTP data
2065 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002066 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 * @return bool
2068 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002069 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002071 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 {
patworkb0707982011-04-08 15:10:05 +02002073 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 return FALSE;
2075 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002076
2077 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 }
Barry Mienydd671972010-10-04 16:33:58 +02002079
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 // --------------------------------------------------------------------
2081
2082 /**
2083 * Get SMTP data
2084 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 * @return string
2086 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002087 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 {
Andrey Andreev56454792012-05-17 14:32:19 +03002089 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002090
2091 while ($str = fgets($this->_smtp_connect, 512))
2092 {
2093 $data .= $str;
2094
Alex Bilbied261b1e2012-06-02 11:12:16 +01002095 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 {
2097 break;
2098 }
2099 }
2100
2101 return $data;
2102 }
Barry Mienydd671972010-10-04 16:33:58 +02002103
Derek Allard2067d1a2008-11-13 22:59:24 +00002104 // --------------------------------------------------------------------
2105
2106 /**
2107 * Get Hostname
2108 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 * @return string
2110 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002111 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02002113 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 }
Barry Mienydd671972010-10-04 16:33:58 +02002115
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 // --------------------------------------------------------------------
2117
2118 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 * Get Debug Message
2120 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002121 * @param array $include List of raw data chunks to include in the output
2122 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 * @return string
2124 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002125 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 {
2127 $msg = '';
2128
2129 if (count($this->_debug_msg) > 0)
2130 {
2131 foreach ($this->_debug_msg as $val)
2132 {
2133 $msg .= $val;
2134 }
2135 }
2136
Andrey Andreev61797f62012-11-26 16:15:12 +02002137 // Determine which parts of our raw data needs to be printed
2138 $raw_data = '';
2139 is_array($include) OR $include = array($include);
2140
2141 if (in_array('headers', $include, TRUE))
2142 {
2143 $raw_data = $this->_header_str."\n";
2144 }
2145
2146 if (in_array('subject', $include, TRUE))
2147 {
2148 $raw_data .= htmlspecialchars($this->_subject)."\n";
2149 }
2150
2151 if (in_array('body', $include, TRUE))
2152 {
2153 $raw_data .= htmlspecialchars($this->_finalbody);
2154 }
2155
2156 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 }
Barry Mienydd671972010-10-04 16:33:58 +02002158
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 // --------------------------------------------------------------------
2160
2161 /**
2162 * Set Message
2163 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002164 * @param string $msg
2165 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002166 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002168 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
2170 $CI =& get_instance();
2171 $CI->lang->load('email');
2172
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002173 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 {
Andrey Andreev56454792012-05-17 14:32:19 +03002175 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002176 }
2177 else
2178 {
Andrey Andreev56454792012-05-17 14:32:19 +03002179 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 }
2181 }
Barry Mienydd671972010-10-04 16:33:58 +02002182
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 // --------------------------------------------------------------------
2184
2185 /**
2186 * Mime Types
2187 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 * @param string
2189 * @return string
2190 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002191 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002193 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00002194
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002195 $ext = strtolower($ext);
2196
2197 if ( ! is_array($mimes))
2198 {
2199 $mimes =& get_mimes();
2200 }
2201
2202 if (isset($mimes[$ext]))
2203 {
2204 return is_array($mimes[$ext])
2205 ? current($mimes[$ext])
2206 : $mimes[$ext];
2207 }
2208
2209 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 }
2211
2212}
Derek Allard2067d1a2008-11-13 22:59:24 +00002213
2214/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03002215/* Location: ./system/libraries/Email.php */