blob: 7ea49a959869cbf310fb0aecb1abe8b34f131a05 [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;
nisheeth-barthwalcf225572013-02-18 01:08:04 +053097
98 /**
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 *
108 * @var string NULL, 'tls' or 'ssl'
109 */
110 public $smtp_crypto = NULL;
111
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 // --------------------------------------------------------------------
411
412 /**
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530413 * Destructor - Releases Resources
414 *
415 * @return void
416 */
417 public function __destruct()
418 {
419 if(is_resource($this->_smtp_connect))
420 $this->_send_command('quit');
421 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000422
423 // --------------------------------------------------------------------
424
425 /**
426 * Initialize preferences
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param array
Andrew Podner4296a652012-12-17 07:51:15 -0500429 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000431 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 foreach ($config as $key => $val)
434 {
435 if (isset($this->$key))
436 {
437 $method = 'set_'.$key;
438
439 if (method_exists($this, $method))
440 {
441 $this->$method($val);
442 }
443 else
444 {
445 $this->$key = $val;
446 }
447 }
448 }
Eric Barnes6113f542010-12-29 13:36:12 -0500449 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
Alex Bilbied261b1e2012-06-02 11:12:16 +0100451 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200452 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000453
454 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // --------------------------------------------------------------------
458
459 /**
460 * Initialize the Email Data
461 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800462 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500463 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000465 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200467 $this->_subject = '';
468 $this->_body = '';
469 $this->_finalbody = '';
470 $this->_header_str = '';
471 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500473 $this->_cc_array = array();
474 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 $this->_headers = array();
476 $this->_debug_msg = array();
477
Mickey Wubfc1cad2012-05-31 22:28:40 -0700478 $this->set_header('User-Agent', $this->useragent);
479 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000480
481 if ($clear_attachments !== FALSE)
482 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300483 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Eric Barnes6113f542010-12-29 13:36:12 -0500485
Greg Akera769deb2010-11-10 15:47:29 -0600486 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Set FROM
493 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300494 * @param string $from
495 * @param string $name
496 * @param string $return_path = NULL Return-Path
Andrew Podner4296a652012-12-17 07:51:15 -0500497 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300499 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200501 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200503 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 if ($this->validate)
507 {
508 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300509 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200510 {
511 $this->validate_email($this->_str_to_array($return_path));
512 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
515 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100516 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 // only use Q encoding if there are characters that would require it
519 if ( ! preg_match('/[\200-\377]/', $name))
520 {
521 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000522 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524 else
525 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300526 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528 }
529
Mickey Wubfc1cad2012-05-31 22:28:40 -0700530 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200531
Andrey Andreev925dd902012-10-19 11:06:31 +0300532 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200533 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500534
Greg Akera769deb2010-11-10 15:47:29 -0600535 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // --------------------------------------------------------------------
539
540 /**
541 * Set Reply-to
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param string
544 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500545 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000547 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200549 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200551 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553
554 if ($this->validate)
555 {
556 $this->validate_email($this->_str_to_array($replyto));
557 }
558
Alex Bilbied261b1e2012-06-02 11:12:16 +0100559 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 $name = $replyto;
562 }
563
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300564 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 $name = '"'.$name.'"';
567 }
568
Mickey Wubfc1cad2012-05-31 22:28:40 -0700569 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600571
572 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
576
577 /**
578 * Set Recipients
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500581 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000583 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 $to = $this->_str_to_array($to);
586 $to = $this->clean_email($to);
587
588 if ($this->validate)
589 {
590 $this->validate_email($to);
591 }
592
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200593 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700595 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300598 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500599
Greg Akera769deb2010-11-10 15:47:29 -0600600 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // --------------------------------------------------------------------
604
605 /**
606 * Set CC
607 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500609 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000611 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Andrey Andreev56454792012-05-17 14:32:19 +0300613 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000614
615 if ($this->validate)
616 {
617 $this->validate_email($cc);
618 }
619
Mickey Wubfc1cad2012-05-31 22:28:40 -0700620 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000621
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200622 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 $this->_cc_array = $cc;
625 }
Eric Barnes6113f542010-12-29 13:36:12 -0500626
Greg Akera769deb2010-11-10 15:47:29 -0600627 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // --------------------------------------------------------------------
631
632 /**
633 * Set BCC
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @param string
636 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500637 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000639 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100641 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 $this->bcc_batch_mode = TRUE;
644 $this->bcc_batch_size = $limit;
645 }
646
Andrey Andreev56454792012-05-17 14:32:19 +0300647 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000648
649 if ($this->validate)
650 {
651 $this->validate_email($bcc);
652 }
653
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200654 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 $this->_bcc_array = $bcc;
657 }
658 else
659 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700660 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
Eric Barnes6113f542010-12-29 13:36:12 -0500662
Greg Akera769deb2010-11-10 15:47:29 -0600663 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // --------------------------------------------------------------------
667
668 /**
669 * Set Email Subject
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500672 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000674 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700677 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600678 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // --------------------------------------------------------------------
682
683 /**
684 * Set Body
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500687 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000689 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200691 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200692
Andrey Andreevaf728622011-10-20 10:11:59 +0300693 /* strip slashes only if magic quotes is ON
694 if we do it with magic quotes OFF, it strips real, user-inputted chars.
695
696 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
697 it will probably not exist in future versions at all.
698 */
699 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200700 {
diegorivera33c32802011-10-19 02:56:15 -0200701 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200702 }
diegorivera33c32802011-10-19 02:56:15 -0200703
Greg Akera769deb2010-11-10 15:47:29 -0600704 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // --------------------------------------------------------------------
708
709 /**
710 * Assign file attachments
711 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300712 * @param string $filename
713 * @param string $disposition = 'attachment'
714 * @param string $newname = NULL
715 * @param string $mime = ''
Andrew Podner4296a652012-12-17 07:51:15 -0500716 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200718 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300720 $this->_attachments[] = array(
721 'name' => array($filename, $newname),
722 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
723 'type' => $mime
724 );
725
Greg Akera769deb2010-11-10 15:47:29 -0600726 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * Add a Header Item
733 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 * @param string
735 * @param string
736 * @return void
737 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700738 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 $this->_headers[$header] = $value;
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
746 * Convert a String to an Array
747 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 * @param string
749 * @return array
750 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600751 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
753 if ( ! is_array($email))
754 {
Andrey Andreev56454792012-05-17 14:32:19 +0300755 return (strpos($email, ',') !== FALSE)
756 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
757 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Andrey Andreev56454792012-05-17 14:32:19 +0300759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 return $email;
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
764
765 /**
766 * Set Multipart Value
767 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500769 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000771 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400773 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600774 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Set Mailtype
781 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500783 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000785 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
Andrey Andreev56454792012-05-17 14:32:19 +0300787 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600788 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 // --------------------------------------------------------------------
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530792
nisheeth-barthwal59209de2013-02-18 17:02:13 +0530793 /**
794 * Set Useragent
795 *
796 * @param string
797 * @return void
798 */
799 public function set_useragent($type = '')
800 {
801 if( ! $type)
802 $this->useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'PHP/'.phpversion();
803 else
804 $this->useragent = $type;
805 return $this;
806 }
807
nisheeth-barthwalcf225572013-02-18 01:08:04 +0530808 // --------------------------------------------------------------------
809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 /**
811 * Set Wordwrap
812 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400813 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500814 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000816 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400818 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600819 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 // --------------------------------------------------------------------
823
824 /**
825 * Set Protocol
826 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500828 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000830 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200832 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600833 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
837
838 /**
839 * Set Priority
840 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200841 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500842 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000844 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200846 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600847 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // --------------------------------------------------------------------
851
852 /**
853 * Set Newline Character
854 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500856 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000858 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200860 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600861 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // --------------------------------------------------------------------
865
866 /**
867 * Set CRLF
868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500870 * @return CI_Email
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000872 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200874 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600875 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
879
880 /**
881 * Set Message Boundary
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @return void
884 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600885 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200887 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
888 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 // --------------------------------------------------------------------
892
893 /**
894 * Get the Message ID
895 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 * @return string
897 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600898 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200900 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300901 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 // --------------------------------------------------------------------
905
906 /**
907 * Get Mail Protocol
908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200910 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600912 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200915 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000916
Alex Bilbied261b1e2012-06-02 11:12:16 +0100917 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
919 return $this->protocol;
920 }
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 // --------------------------------------------------------------------
924
925 /**
926 * Get Mail Encoding
927 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @param bool
929 * @return string
930 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600931 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200933 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000934
935 foreach ($this->_base_charsets as $charset)
936 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300937 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 $this->_encoding = '7bit';
940 }
941 }
942
Alex Bilbied261b1e2012-06-02 11:12:16 +0100943 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 return $this->_encoding;
946 }
947 }
948
949 // --------------------------------------------------------------------
950
951 /**
952 * Get content type (text/html/attachment)
953 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * @return string
955 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600956 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev56454792012-05-17 14:32:19 +0300958 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300960 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300962 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 return 'plain-attach';
965 }
966 else
967 {
968 return 'plain';
969 }
970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
973
974 /**
975 * Set RFC 822 Date
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @return string
978 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600979 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200981 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300982 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200984 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000985
Andrey Andreev59c5b532012-03-01 14:09:51 +0200986 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 // --------------------------------------------------------------------
990
991 /**
992 * Mime message
993 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 * @return string
995 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600996 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200998 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 +0000999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // --------------------------------------------------------------------
1002
1003 /**
1004 * Validate Email Address
1005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @param string
1007 * @return bool
1008 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001009 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
1011 if ( ! is_array($email))
1012 {
patworkb0707982011-04-08 15:10:05 +02001013 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 return FALSE;
1015 }
1016
1017 foreach ($email as $val)
1018 {
1019 if ( ! $this->valid_email($val))
1020 {
patworkb0707982011-04-08 15:10:05 +02001021 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 return FALSE;
1023 }
1024 }
1025
1026 return TRUE;
1027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 // --------------------------------------------------------------------
1030
1031 /**
1032 * Email Validation
1033 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 * @param string
1035 * @return bool
1036 */
Timothy Warrend37f0e92012-06-27 08:21:59 -04001037 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001039 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 // --------------------------------------------------------------------
1043
1044 /**
1045 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1046 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 * @param string
1048 * @return string
1049 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001050 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
1052 if ( ! is_array($email))
1053 {
Andrey Andreev56454792012-05-17 14:32:19 +03001054 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
1056
1057 $clean_email = array();
1058
1059 foreach ($email as $addy)
1060 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001061 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
1063
1064 return $clean_email;
1065 }
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 // --------------------------------------------------------------------
1068
1069 /**
1070 * Build alternative plain text message
1071 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001072 * Provides the raw message for use in plain-text headers of
1073 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 * If the user hasn't specified his own alternative message
1075 * it creates one by stripping the HTML
1076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 * @return string
1078 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001079 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001081 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001083 return ($this->wordwrap)
1084 ? $this->word_wrap($this->alt_message, 76)
1085 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
1087
Andrey Andreev56454792012-05-17 14:32:19 +03001088 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001089 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +00001090
1091 for ($i = 20; $i >= 3; $i--)
1092 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001093 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 }
1095
GDmac9bea4be2012-10-30 06:14:19 +01001096 // Reduce multiple spaces
Andrey Andreev8a203f62012-11-02 20:40:43 +02001097 $body = preg_replace('| +|', ' ', $body);
GDmac9bea4be2012-10-30 06:14:19 +01001098
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001099 return ($this->wordwrap)
1100 ? $this->word_wrap($body, 76)
1101 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Word Wrap
1108 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +03001110 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * @return string
1112 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001113 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001115 // Set the character limit, if not already present
1116 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001118 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 }
1120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 // Standardize newlines
1122 if (strpos($str, "\r") !== FALSE)
1123 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +02001124 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 }
1126
GDmac9bea4be2012-10-30 06:14:19 +01001127 // Reduce multiple spaces at end of line
1128 $str = preg_replace('| +\n|', "\n", $str);
1129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // If the current word is surrounded by {unwrap} tags we'll
1131 // strip the entire chunk and replace it with a marker.
1132 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +03001133 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001135 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001137 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +03001138 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 }
1140 }
1141
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001142 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +03001144 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 $str = wordwrap($str, $charlim, "\n", FALSE);
1146
1147 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +03001148 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 foreach (explode("\n", $str) as $line)
1150 {
1151 // Is the line within the allowed character count?
1152 // If so we'll join it to the output and continue
1153 if (strlen($line) <= $charlim)
1154 {
1155 $output .= $line.$this->newline;
1156 continue;
1157 }
1158
1159 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001160 do
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +03001163 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
1165 break;
1166 }
1167
1168 // Trim the word down
1169 $temp .= substr($line, 0, $charlim-1);
1170 $line = substr($line, $charlim-1);
1171 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001172 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +00001173
1174 // If $temp contains data it means we had to split up an over-length
1175 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +01001176 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001178 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
1180
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001181 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
1183
1184 // Put our markers back
1185 if (count($unwrap) > 0)
1186 {
1187 foreach ($unwrap as $key => $val)
1188 {
Andrey Andreev56454792012-05-17 14:32:19 +03001189 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 }
1191 }
1192
1193 return $output;
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 // --------------------------------------------------------------------
1197
1198 /**
1199 * Build final headers
1200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 * @return string
1202 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001203 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001205 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1206 $this->set_header('X-Mailer', $this->useragent);
1207 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
1208 $this->set_header('Message-ID', $this->_get_message_id());
1209 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 // --------------------------------------------------------------------
1213
1214 /**
1215 * Write Headers as a string
1216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 * @return void
1218 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001219 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001221 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 {
1223 $this->_subject = $this->_headers['Subject'];
1224 unset($this->_headers['Subject']);
1225 }
1226
1227 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +03001228 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001229
Pascal Kriete14287f32011-02-14 13:39:34 -05001230 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 {
1232 $val = trim($val);
1233
Alex Bilbied261b1e2012-06-02 11:12:16 +01001234 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 {
Andrey Andreev56454792012-05-17 14:32:19 +03001236 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
1238 }
1239
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001240 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Derek Jones1d890882009-02-10 20:32:14 +00001242 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 // --------------------------------------------------------------------
1247
1248 /**
1249 * Build Final Body and attachments
1250 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 * @return void
1252 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001253 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001255 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 $this->_body = $this->word_wrap($this->_body);
1258 }
1259
1260 $this->_set_boundaries();
1261 $this->_write_headers();
1262
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001263 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001264 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001265
1266 switch ($this->_get_content_type())
1267 {
1268 case 'plain' :
1269
Andrey Andreev56454792012-05-17 14:32:19 +03001270 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1271 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +00001272
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001273 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 {
1275 $this->_header_str .= $hdr;
1276 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
Brandon Jones485d7412010-11-09 16:38:17 -05001278 else
1279 {
1280 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1281 }
Eric Barnes6113f542010-12-29 13:36:12 -05001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 return;
1284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 case 'html' :
1286
1287 if ($this->send_multipart === FALSE)
1288 {
Andrey Andreev56454792012-05-17 14:32:19 +03001289 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
1290 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 }
1292 else
1293 {
Andrey Andreev56454792012-05-17 14:32:19 +03001294 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001295
Andrey Andreev56454792012-05-17 14:32:19 +03001296 $body .= $this->_get_mime_message().$this->newline.$this->newline
1297 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001298
Andrey Andreev56454792012-05-17 14:32:19 +03001299 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1300 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1301 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001302
Andrey Andreev56454792012-05-17 14:32:19 +03001303 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1304 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 }
Eric Barnes6113f542010-12-29 13:36:12 -05001306
Andrey Andreev56454792012-05-17 14:32:19 +03001307 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001308
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001309 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 {
1311 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001312 }
1313 else
1314 {
Andrey Andreev56454792012-05-17 14:32:19 +03001315 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 }
1317
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 if ($this->send_multipart !== FALSE)
1319 {
Andrey Andreev56454792012-05-17 14:32:19 +03001320 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 }
1322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 return;
1324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 case 'plain-attach' :
1326
Andrey Andreev56454792012-05-17 14:32:19 +03001327 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001328
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001329 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 {
1331 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001332 }
1333
Andrey Andreev56454792012-05-17 14:32:19 +03001334 $body .= $this->_get_mime_message().$this->newline.$this->newline
1335 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001336
Andrey Andreev56454792012-05-17 14:32:19 +03001337 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1338 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001339
Andrey Andreev56454792012-05-17 14:32:19 +03001340 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001341
1342 break;
1343 case 'html-attach' :
1344
Andrey Andreev56454792012-05-17 14:32:19 +03001345 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001346
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001347 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
1349 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 }
1351
Andrey Andreev56454792012-05-17 14:32:19 +03001352 $body .= $this->_get_mime_message().$this->newline.$this->newline
1353 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001354
Andrey Andreev56454792012-05-17 14:32:19 +03001355 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1356 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001357
Andrey Andreev56454792012-05-17 14:32:19 +03001358 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1359 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1360 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001361
Andrey Andreev56454792012-05-17 14:32:19 +03001362 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1363 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001364
Andrey Andreev56454792012-05-17 14:32:19 +03001365 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1366 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001367
1368 break;
1369 }
1370
1371 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001372 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001374 $filename = $this->_attachments[$i]['name'][0];
vlakoff912f1bc2013-01-15 03:34:12 +01001375 $basename = ($this->_attachments[$i]['name'][1] === NULL)
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001376 ? basename($filename) : $this->_attachments[$i]['name'][1];
1377 $ctype = $this->_attachments[$i]['type'];
Matteo Mattei5688d582012-03-13 19:29:29 +01001378 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001379
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001380 if ($ctype === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001382 if ( ! file_exists($filename))
1383 {
1384 $this->_set_error_message('lang:email_attachment_missing', $filename);
1385 return FALSE;
1386 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001387
Matteo Mattei5688d582012-03-13 19:29:29 +01001388 $file = filesize($filename) +1;
1389
1390 if ( ! $fp = fopen($filename, FOPEN_READ))
1391 {
1392 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1393 return FALSE;
1394 }
1395
Matteo Matteic3b36f42012-03-26 10:27:17 +02001396 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001397 $file_content = fread($fp, $file);
1398 fclose($fp);
1399 }
1400 else
1401 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001402 $file_content =& $this->_attachments[$i]['name'][0];
Matteo Mattei5688d582012-03-13 19:29:29 +01001403 }
Andrey Andreev56454792012-05-17 14:32:19 +03001404
1405 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1406 .'Content-type: '.$ctype.'; '
1407 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001408 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001409 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001410
Matteo Mattei5688d582012-03-13 19:29:29 +01001411 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 }
1413
Andrey Andreev56454792012-05-17 14:32:19 +03001414 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1415 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 return;
1417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Prep Quoted Printable
1423 *
1424 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1425 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1426 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 * @return string
1429 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001430 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001432 // We are intentionally wrapping so mail servers will encode characters
1433 // properly and MUAs will behave, so {unwrap} must go!
1434 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1435
Andrey Andreev683b34d2012-10-09 15:00:00 +03001436 // RFC 2045 specifies CRLF as "\r\n".
1437 // However, many developers choose to override that and violate
1438 // the RFC rules due to (apparently) a bug in MS Exchange,
1439 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001440 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001441 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001442 if (is_php('5.3'))
1443 {
1444 return quoted_printable_encode($str);
1445 }
1446 elseif (function_exists('imap_8bit'))
1447 {
1448 return imap_8bit($str);
1449 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001450 }
1451
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001452 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001453 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001454
1455 // Standardize newlines
1456 if (strpos($str, "\r") !== FALSE)
1457 {
1458 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1459 }
1460
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 $escape = '=';
1462 $output = '';
1463
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001464 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 {
1466 $length = strlen($line);
1467 $temp = '';
1468
1469 // Loop through each character in the line to add soft-wrap
1470 // characters at the end of a line " =\r\n" and add the newly
1471 // processed line(s) to the output (see comment on $crlf class property)
1472 for ($i = 0; $i < $length; $i++)
1473 {
1474 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001475 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 $ascii = ord($char);
1477
1478 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001479 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001481 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001483 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001485 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 }
1487
1488 // If we're at the character limit, add the line to the output,
1489 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001490 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 {
1492 $output .= $temp.$escape.$this->crlf;
1493 $temp = '';
1494 }
1495
1496 // Add the character to our temporary line
1497 $temp .= $char;
1498 }
1499
1500 // Add our completed line to the output
1501 $output .= $temp.$this->crlf;
1502 }
1503
1504 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001505 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 }
1507
1508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001509
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 /**
1511 * Prep Q Encoding
1512 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001513 * Performs "Q Encoding" on a string for use in email headers.
1514 * It's related but not identical to quoted-printable, so it has its
1515 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001517 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001518 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001520 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001522 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001523
Andrey Andreev925dd902012-10-19 11:06:31 +03001524 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001526 if (MB_ENABLED === TRUE)
1527 {
1528 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1529 }
1530 elseif (extension_loaded('iconv'))
1531 {
1532 $output = @iconv_mime_encode('', $str,
1533 array(
1534 'scheme' => 'Q',
1535 'line-length' => 76,
1536 'input-charset' => $this->charset,
1537 'output-charset' => $this->charset,
1538 'line-break-chars' => $this->crlf
1539 )
1540 );
1541
1542 // There are reports that iconv_mime_encode() might fail and return FALSE
1543 if ($output !== FALSE)
1544 {
1545 // iconv_mime_encode() will always put a header field name.
1546 // We've passed it an empty one, but it still prepends our
1547 // encoded string with ': ', so we need to strip it.
1548 return substr($output, 2);
1549 }
1550
1551 $chars = iconv_strlen($str, 'UTF-8');
1552 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 }
1554
Andrey Andreev925dd902012-10-19 11:06:31 +03001555 // We might already have this set for UTF-8
1556 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001557
Andrey Andreev925dd902012-10-19 11:06:31 +03001558 $output = '=?'.$this->charset.'?Q?';
1559 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001561 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1562 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1563 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001564
Andrey Andreev925dd902012-10-19 11:06:31 +03001565 // RFC 2045 sets a limit of 76 characters per line.
1566 // We'll append ?= to the end of each line though.
1567 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001569 $output .= '?='.$this->crlf // EOL
1570 .' =?'.$this->charset.'?Q?'.$chr; // New line
1571 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001573 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001575 $output .= $chr;
1576 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 }
1579
Andrey Andreev925dd902012-10-19 11:06:31 +03001580 // End the header
1581 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 }
1583
1584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001585
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 /**
1587 * Send Email
1588 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001589 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 * @return bool
1591 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001592 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001594 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 {
1596 $this->reply_to($this->_headers['From']);
1597 }
1598
Andrey Andreev76f15c92012-03-01 13:05:07 +02001599 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1600 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1601 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 {
patworkb0707982011-04-08 15:10:05 +02001603 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 return FALSE;
1605 }
1606
1607 $this->_build_headers();
1608
Andrey Andreev76f15c92012-03-01 13:05:07 +02001609 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001611 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001612
Alex Bilbiea87aab32012-07-30 09:50:37 +01001613 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001614 {
1615 $this->clear();
1616 }
1617
Alex Bilbieb901e732012-07-30 09:44:57 +01001618 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 }
1620
1621 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001622 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001623
Alex Bilbiea87aab32012-07-30 09:50:37 +01001624 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001625 {
1626 $this->clear();
1627 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001628
Alex Bilbieb901e732012-07-30 09:44:57 +01001629 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 }
Barry Mienydd671972010-10-04 16:33:58 +02001631
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 // --------------------------------------------------------------------
1633
1634 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001635 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001637 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001639 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001640 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001641 $float = $this->bcc_batch_size - 1;
1642 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 $chunk = array();
1644
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001645 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001646 {
1647 if (isset($this->_bcc_array[$i]))
1648 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001649 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 }
1651
Alex Bilbied261b1e2012-06-02 11:12:16 +01001652 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 {
1654 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001655 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001656 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 }
1658
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001659 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 {
1661 $chunk[] = substr($set, 1);
1662 }
1663 }
1664
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001665 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 {
1667 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001668
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001669 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001670
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001671 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001673 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 }
1675 else
1676 {
1677 $this->_bcc_array = $bcc;
1678 }
1679
1680 $this->_build_message();
1681 $this->_spool_email();
1682 }
1683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 // --------------------------------------------------------------------
1686
1687 /**
1688 * Unwrap special elements
1689 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 * @return void
1691 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001692 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 {
Andrey Andreev56454792012-05-17 14:32:19 +03001694 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 }
Barry Mienydd671972010-10-04 16:33:58 +02001696
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 // --------------------------------------------------------------------
1698
1699 /**
1700 * Strip line-breaks via callback
1701 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001702 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 * @return string
1704 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001705 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 {
1707 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1708 {
1709 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1710 }
1711
1712 return $matches[1];
1713 }
Barry Mienydd671972010-10-04 16:33:58 +02001714
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 // --------------------------------------------------------------------
1716
1717 /**
1718 * Spool mail to the mail server
1719 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 * @return bool
1721 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001722 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
1724 $this->_unwrap_specials();
1725
Andrey Andreev56454792012-05-17 14:32:19 +03001726 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001727 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 {
Andrey Andreev56454792012-05-17 14:32:19 +03001729 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001730 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
1732
patworkb0707982011-04-08 15:10:05 +02001733 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 return TRUE;
1735 }
Barry Mienydd671972010-10-04 16:33:58 +02001736
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 // --------------------------------------------------------------------
1738
1739 /**
1740 * Send using mail()
1741 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 * @return bool
1743 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001744 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001746 if (is_array($this->_recipients))
1747 {
1748 $this->_recipients = implode(', ', $this->_recipients);
1749 }
1750
Alex Bilbied261b1e2012-06-02 11:12:16 +01001751 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001753 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 }
1755 else
1756 {
1757 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1758 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001759 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 +00001760 }
1761 }
Barry Mienydd671972010-10-04 16:33:58 +02001762
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 // --------------------------------------------------------------------
1764
1765 /**
1766 * Send using Sendmail
1767 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 * @return bool
1769 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001770 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 {
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001772 // is popen() enabled?
1773 if ( ! function_usable('popen')
1774 OR FALSE === ($fp = @popen(
1775 $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From'])
1776 .' -t -r '.$this->clean_email($this->_headers['Return-Path'])
1777 , 'w'))
1778 ) // server probably has popen disabled, so nothing we can do to get a verbose error.
Derek Jones4cefaa42009-04-29 19:13:56 +00001779 {
Derek Jones4cefaa42009-04-29 19:13:56 +00001780 return FALSE;
1781 }
Derek Jones71141ce2010-03-02 16:41:20 -06001782
Derek Jonesc630bcf2008-11-17 21:09:45 +00001783 fputs($fp, $this->_header_str);
1784 fputs($fp, $this->_finalbody);
1785
Barry Mienydd671972010-10-04 16:33:58 +02001786 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001787
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001788 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
patworkb0707982011-04-08 15:10:05 +02001790 $this->_set_error_message('lang:email_exit_status', $status);
1791 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 return FALSE;
1793 }
1794
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 return TRUE;
1796 }
Barry Mienydd671972010-10-04 16:33:58 +02001797
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 // --------------------------------------------------------------------
1799
1800 /**
1801 * Send using SMTP
1802 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 * @return bool
1804 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001805 protected function _send_with_smtp()
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301806 {
1807 if ($this->smtp_host === '')
1808 {
1809 $this->_set_error_message('lang:email_no_hostname');
1810 return FALSE;
1811 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001812
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301813 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1814 {
1815 return FALSE;
1816 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001817
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301818 $this->_send_command('from', $this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001819
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301820 foreach ($this->_recipients as $val)
1821 {
1822 $this->_send_command('to', $val);
1823 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001824
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301825 if (count($this->_cc_array) > 0)
1826 {
1827 foreach ($this->_cc_array as $val)
1828 {
1829 if ($val !== '')
1830 {
1831 $this->_send_command('to', $val);
1832 }
1833 }
1834 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001835
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301836 if (count($this->_bcc_array) > 0)
1837 {
1838 foreach ($this->_bcc_array as $val)
1839 {
1840 if ($val !== '')
1841 {
1842 $this->_send_command('to', $val);
1843 }
1844 }
1845 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001846
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301847 $this->_send_command('data');
Derek Allard2067d1a2008-11-13 22:59:24 +00001848
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301849 // perform dot transformation on any lines that begin with a dot
1850 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001851
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301852 $this->_send_data('.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001853
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301854 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00001855
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301856 $this->_set_error_message($reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001857
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301858 if (strpos($reply, '250') !== 0)
1859 {
1860 $this->_set_error_message('lang:email_smtp_error', $reply);
1861 return FALSE;
1862 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001863
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301864 if($this->smtp_keepalive)
1865 $this->_send_command('reset');
1866 else
1867 $this->_send_command('quit');
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 *
1877 * @param bool
1878 * @return string
1879 */
1880 protected function _smtp_connect($force=FALSE)
1881 {
1882 if( ! is_resource($this->_smtp_connect) || $force)
1883 {
1884 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +00001885
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301886 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
1887 $this->smtp_port,
1888 $errno,
1889 $errstr,
1890 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001891
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301892 if ( ! is_resource($this->_smtp_connect))
1893 {
1894 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
1895 return FALSE;
1896 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001897
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301898 stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
1899 $this->_set_error_message($this->_get_smtp_data());
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001900
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301901 if ($this->smtp_crypto === 'tls')
1902 {
1903 $this->_send_command('hello');
1904 $this->_send_command('starttls');
Radu Potop4c589ae2011-09-29 10:19:55 +03001905
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301906 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potopbbf04b02011-09-28 13:57:51 +03001907
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301908 if ($crypto !== TRUE)
1909 {
1910 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1911 return FALSE;
1912 }
1913 }
Barry Mienydd671972010-10-04 16:33:58 +02001914
nisheeth-barthwal59209de2013-02-18 17:02:13 +05301915 return $this->_send_command('hello');
1916 }
1917 return TRUE;
1918 }
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
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302028 if (strpos($reply, '503') !== 0) // Already authenticated
2029 return TRUE;
2030
2031 if (strpos($reply, '334') !== 0)
2032 {
2033 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
2034 return FALSE;
2035 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002036
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302037 $this->_send_data(base64_encode($this->smtp_user));
Derek Allard2067d1a2008-11-13 22:59:24 +00002038
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302039 $reply = $this->_get_smtp_data();
Derek Allard2067d1a2008-11-13 22:59:24 +00002040
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302041 if (strpos($reply, '334') !== 0)
2042 {
2043 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
2044 return FALSE;
2045 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002046
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302047 $this->_send_data(base64_encode($this->smtp_pass));
Derek Allard2067d1a2008-11-13 22:59:24 +00002048
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302049 $reply = $this->_get_smtp_data();
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302050
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302051 if (strpos($reply, '235') !== 0)
2052 {
2053 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2054 return FALSE;
2055 }
nisheeth-barthwalcf225572013-02-18 01:08:04 +05302056
nisheeth-barthwal59209de2013-02-18 17:02:13 +05302057 return TRUE;
2058 }
Barry Mienydd671972010-10-04 16:33:58 +02002059
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 // --------------------------------------------------------------------
2061
2062 /**
2063 * Send SMTP data
2064 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002065 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 * @return bool
2067 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002068 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002070 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 {
patworkb0707982011-04-08 15:10:05 +02002072 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 return FALSE;
2074 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02002075
2076 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
Barry Mienydd671972010-10-04 16:33:58 +02002078
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 // --------------------------------------------------------------------
2080
2081 /**
2082 * Get SMTP data
2083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 * @return string
2085 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002086 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 {
Andrey Andreev56454792012-05-17 14:32:19 +03002088 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00002089
2090 while ($str = fgets($this->_smtp_connect, 512))
2091 {
2092 $data .= $str;
2093
Alex Bilbied261b1e2012-06-02 11:12:16 +01002094 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 {
2096 break;
2097 }
2098 }
2099
2100 return $data;
2101 }
Barry Mienydd671972010-10-04 16:33:58 +02002102
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 // --------------------------------------------------------------------
2104
2105 /**
2106 * Get Hostname
2107 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 * @return string
2109 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002110 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00002111 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02002112 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 }
Barry Mienydd671972010-10-04 16:33:58 +02002114
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 // --------------------------------------------------------------------
2116
2117 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 * Get Debug Message
2119 *
Andrey Andreev61797f62012-11-26 16:15:12 +02002120 * @param array $include List of raw data chunks to include in the output
2121 * Valid options are: 'headers', 'subject', 'body'
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 * @return string
2123 */
Andrey Andreev61797f62012-11-26 16:15:12 +02002124 public function print_debugger($include = array('headers', 'subject', 'body'))
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
2126 $msg = '';
2127
2128 if (count($this->_debug_msg) > 0)
2129 {
2130 foreach ($this->_debug_msg as $val)
2131 {
2132 $msg .= $val;
2133 }
2134 }
2135
Andrey Andreev61797f62012-11-26 16:15:12 +02002136 // Determine which parts of our raw data needs to be printed
2137 $raw_data = '';
2138 is_array($include) OR $include = array($include);
2139
2140 if (in_array('headers', $include, TRUE))
2141 {
2142 $raw_data = $this->_header_str."\n";
2143 }
2144
2145 if (in_array('subject', $include, TRUE))
2146 {
2147 $raw_data .= htmlspecialchars($this->_subject)."\n";
2148 }
2149
2150 if (in_array('body', $include, TRUE))
2151 {
2152 $raw_data .= htmlspecialchars($this->_finalbody);
2153 }
2154
2155 return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
Derek Allard2067d1a2008-11-13 22:59:24 +00002156 }
Barry Mienydd671972010-10-04 16:33:58 +02002157
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 // --------------------------------------------------------------------
2159
2160 /**
2161 * Set Message
2162 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002163 * @param string $msg
2164 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02002165 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002167 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 {
2169 $CI =& get_instance();
2170 $CI->lang->load('email');
2171
Andrey Andreev7a7ad782012-11-12 17:21:01 +02002172 if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 {
Andrey Andreev56454792012-05-17 14:32:19 +03002174 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 }
2176 else
2177 {
Andrey Andreev56454792012-05-17 14:32:19 +03002178 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 }
2180 }
Barry Mienydd671972010-10-04 16:33:58 +02002181
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 // --------------------------------------------------------------------
2183
2184 /**
2185 * Mime Types
2186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 * @param string
2188 * @return string
2189 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02002190 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002192 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00002193
Andrey Andreev6ef498b2012-06-05 22:01:58 +03002194 $ext = strtolower($ext);
2195
2196 if ( ! is_array($mimes))
2197 {
2198 $mimes =& get_mimes();
2199 }
2200
2201 if (isset($mimes[$ext]))
2202 {
2203 return is_array($mimes[$ext])
2204 ? current($mimes[$ext])
2205 : $mimes[$ext];
2206 }
2207
2208 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
2210
2211}
Derek Allard2067d1a2008-11-13 22:59:24 +00002212
2213/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03002214/* Location: ./system/libraries/Email.php */