blob: 83b442f580963a020ece25c57926dd1f123613e5 [file] [log] [blame]
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Email Class
30 *
31 * Permits email to be sent using Mail, Sendmail, or SMTP.
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/email.html
38 */
trita15dd4f2011-11-23 07:40:05 -050039class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev59c5b532012-03-01 14:09:51 +020041 public $useragent = 'CodeIgniter';
42 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
43 public $protocol = 'mail'; // mail/sendmail/smtp
44 public $smtp_host = ''; // SMTP Server. Example: mail.earthlink.net
45 public $smtp_user = ''; // SMTP Username
46 public $smtp_pass = ''; // SMTP Password
47 public $smtp_port = 25; // SMTP Port
48 public $smtp_timeout = 5; // SMTP Timeout in seconds
49 public $smtp_crypto = ''; // SMTP Encryption. Can be null, tls or ssl.
50 public $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
51 public $wrapchars = 76; // Number of characters to wrap at.
52 public $mailtype = 'text'; // text/html Defines email formatting
53 public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
54 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
55 public $alt_message = ''; // Alternative message for HTML emails
56 public $validate = FALSE; // TRUE/FALSE. Enables email validation
57 public $priority = 3; // Default priority (1 - 5)
58 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
59 public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
61 // distasteful, is the only thing that seems to work for all environments.
leandronf7686c122012-03-22 08:07:31 -030062 public $dsn = FALSE; // Delivery Status Notification
Andrey Andreev1bd3d882011-12-22 15:38:20 +020063 public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Andrey Andreev59c5b532012-03-01 14:09:51 +020064 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020065 public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Andrey Andreev50814092012-03-01 12:36:12 +020066
67 protected $_safe_mode = FALSE;
68 protected $_subject = '';
69 protected $_body = '';
70 protected $_finalbody = '';
71 protected $_alt_boundary = '';
72 protected $_atc_boundary = '';
73 protected $_header_str = '';
74 protected $_smtp_connect = '';
75 protected $_encoding = '8bit';
76 protected $_IP = FALSE;
77 protected $_smtp_auth = FALSE;
78 protected $_replyto_flag = FALSE;
79 protected $_debug_msg = array();
80 protected $_recipients = array();
81 protected $_cc_array = array();
82 protected $_bcc_array = array();
83 protected $_headers = array();
84 protected $_attach_name = array();
85 protected $_attach_type = array();
86 protected $_attach_disp = array();
87 protected $_protocols = array('mail', 'sendmail', 'smtp');
88 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
89 protected $_bit_depths = array('7bit', '8bit');
90 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Constructor - Sets Email Preferences
94 *
95 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +030096 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030097 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +030098 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000099 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000100 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300102 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 if (count($config) > 0)
105 {
106 $this->initialize($config);
107 }
108 else
109 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100110 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200111 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113
Andrey Andreev925dd902012-10-19 11:06:31 +0300114 $this->charset = strtoupper($this->charset);
115
Andrey Andreev59c5b532012-03-01 14:09:51 +0200116 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Initialize preferences
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param array
125 * @return void
126 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000127 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 foreach ($config as $key => $val)
130 {
131 if (isset($this->$key))
132 {
133 $method = 'set_'.$key;
134
135 if (method_exists($this, $method))
136 {
137 $this->$method($val);
138 }
139 else
140 {
141 $this->$key = $val;
142 }
143 }
144 }
Eric Barnes6113f542010-12-29 13:36:12 -0500145 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000146
Alex Bilbied261b1e2012-06-02 11:12:16 +0100147 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200148 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000149
150 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
154
155 /**
156 * Initialize the Email Data
157 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800158 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200159 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000161 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200163 $this->_subject = '';
164 $this->_body = '';
165 $this->_finalbody = '';
166 $this->_header_str = '';
167 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500169 $this->_cc_array = array();
170 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $this->_headers = array();
172 $this->_debug_msg = array();
173
Mickey Wubfc1cad2012-05-31 22:28:40 -0700174 $this->set_header('User-Agent', $this->useragent);
175 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000176
177 if ($clear_attachments !== FALSE)
178 {
179 $this->_attach_name = array();
180 $this->_attach_type = array();
181 $this->_attach_disp = array();
182 }
Eric Barnes6113f542010-12-29 13:36:12 -0500183
Greg Akera769deb2010-11-10 15:47:29 -0600184 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // --------------------------------------------------------------------
188
189 /**
190 * Set FROM
191 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300192 * @param string $from
193 * @param string $name
194 * @param string $return_path = NULL Return-Path
Andrey Andreev081c9462012-03-01 12:58:11 +0200195 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300197 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200199 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200201 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 if ($this->validate)
205 {
206 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300207 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200208 {
209 $this->validate_email($this->_str_to_array($return_path));
210 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100214 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 // only use Q encoding if there are characters that would require it
217 if ( ! preg_match('/[\200-\377]/', $name))
218 {
219 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000220 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222 else
223 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300224 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226 }
227
Mickey Wubfc1cad2012-05-31 22:28:40 -0700228 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200229
Andrey Andreev925dd902012-10-19 11:06:31 +0300230 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200231 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500232
Greg Akera769deb2010-11-10 15:47:29 -0600233 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // --------------------------------------------------------------------
237
238 /**
239 * Set Reply-to
240 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * @param string
242 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200243 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000245 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200247 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200249 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 if ($this->validate)
253 {
254 $this->validate_email($this->_str_to_array($replyto));
255 }
256
Alex Bilbied261b1e2012-06-02 11:12:16 +0100257 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $name = $replyto;
260 }
261
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300262 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $name = '"'.$name.'"';
265 }
266
Mickey Wubfc1cad2012-05-31 22:28:40 -0700267 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600269
270 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Set Recipients
277 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200279 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000281 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 $to = $this->_str_to_array($to);
284 $to = $this->clean_email($to);
285
286 if ($this->validate)
287 {
288 $this->validate_email($to);
289 }
290
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200291 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700293 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300296 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500297
Greg Akera769deb2010-11-10 15:47:29 -0600298 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // --------------------------------------------------------------------
302
303 /**
304 * Set CC
305 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200307 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000309 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreev56454792012-05-17 14:32:19 +0300311 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000312
313 if ($this->validate)
314 {
315 $this->validate_email($cc);
316 }
317
Mickey Wubfc1cad2012-05-31 22:28:40 -0700318 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200320 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 $this->_cc_array = $cc;
323 }
Eric Barnes6113f542010-12-29 13:36:12 -0500324
Greg Akera769deb2010-11-10 15:47:29 -0600325 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
329
330 /**
331 * Set BCC
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200335 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000337 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100339 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 $this->bcc_batch_mode = TRUE;
342 $this->bcc_batch_size = $limit;
343 }
344
Andrey Andreev56454792012-05-17 14:32:19 +0300345 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
347 if ($this->validate)
348 {
349 $this->validate_email($bcc);
350 }
351
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200352 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 $this->_bcc_array = $bcc;
355 }
356 else
357 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700358 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Eric Barnes6113f542010-12-29 13:36:12 -0500360
Greg Akera769deb2010-11-10 15:47:29 -0600361 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
365
366 /**
367 * Set Email Subject
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200370 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000372 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700375 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600376 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
380
381 /**
382 * Set Body
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200385 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000387 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200389 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200390
Andrey Andreevaf728622011-10-20 10:11:59 +0300391 /* strip slashes only if magic quotes is ON
392 if we do it with magic quotes OFF, it strips real, user-inputted chars.
393
394 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
395 it will probably not exist in future versions at all.
396 */
397 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200398 {
diegorivera33c32802011-10-19 02:56:15 -0200399 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200400 }
diegorivera33c32802011-10-19 02:56:15 -0200401
Greg Akera769deb2010-11-10 15:47:29 -0600402 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // --------------------------------------------------------------------
406
407 /**
408 * Assign file attachments
409 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300410 * @param string $filename
411 * @param string $disposition = 'attachment'
412 * @param string $newname = NULL
413 * @param string $mime = ''
Andrey Andreev081c9462012-03-01 12:58:11 +0200414 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200416 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
trit151fc682011-11-23 07:30:06 -0500418 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500419 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200420 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600421 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Add a Header Item
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param string
430 * @param string
431 * @return void
432 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700433 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 $this->_headers[$header] = $value;
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
439
440 /**
441 * Convert a String to an Array
442 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @param string
444 * @return array
445 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600446 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 if ( ! is_array($email))
449 {
Andrey Andreev56454792012-05-17 14:32:19 +0300450 return (strpos($email, ',') !== FALSE)
451 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
452 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Andrey Andreev56454792012-05-17 14:32:19 +0300454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 return $email;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * Set Multipart Value
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200464 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000466 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400468 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600469 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Set Mailtype
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200478 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000480 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreev56454792012-05-17 14:32:19 +0300482 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600483 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // --------------------------------------------------------------------
487
488 /**
489 * Set Wordwrap
490 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400491 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200492 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000494 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400496 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600497 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // --------------------------------------------------------------------
501
502 /**
503 * Set Protocol
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200506 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000508 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200510 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600511 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
517 * Set Priority
518 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200519 * @param int
520 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000522 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200524 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600525 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
529
530 /**
531 * Set Newline Character
532 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200534 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000536 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200538 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600539 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
543
544 /**
545 * Set CRLF
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200548 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000550 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200552 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600553 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
559 * Set Message Boundary
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @return void
562 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600563 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200565 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
566 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // --------------------------------------------------------------------
570
571 /**
572 * Get the Message ID
573 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @return string
575 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600576 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200578 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300579 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // --------------------------------------------------------------------
583
584 /**
585 * Get Mail Protocol
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200588 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600590 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200593 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000594
Alex Bilbied261b1e2012-06-02 11:12:16 +0100595 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 return $this->protocol;
598 }
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // --------------------------------------------------------------------
602
603 /**
604 * Get Mail Encoding
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @param bool
607 * @return string
608 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600609 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200611 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000612
613 foreach ($this->_base_charsets as $charset)
614 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300615 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 $this->_encoding = '7bit';
618 }
619 }
620
Alex Bilbied261b1e2012-06-02 11:12:16 +0100621 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 return $this->_encoding;
624 }
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Get content type (text/html/attachment)
631 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @return string
633 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600634 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Andrey Andreev56454792012-05-17 14:32:19 +0300636 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100638 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200640 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 return 'plain-attach';
643 }
644 else
645 {
646 return 'plain';
647 }
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // --------------------------------------------------------------------
651
652 /**
653 * Set RFC 822 Date
654 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @return string
656 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600657 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200659 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300660 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200662 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000663
Andrey Andreev59c5b532012-03-01 14:09:51 +0200664 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Mime message
671 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @return string
673 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600674 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200676 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 +0000677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
680
681 /**
682 * Validate Email Address
683 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * @param string
685 * @return bool
686 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000687 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 if ( ! is_array($email))
690 {
patworkb0707982011-04-08 15:10:05 +0200691 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 return FALSE;
693 }
694
695 foreach ($email as $val)
696 {
697 if ( ! $this->valid_email($val))
698 {
patworkb0707982011-04-08 15:10:05 +0200699 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 return FALSE;
701 }
702 }
703
704 return TRUE;
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // --------------------------------------------------------------------
708
709 /**
710 * Email Validation
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string
713 * @return bool
714 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400715 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300717 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 }
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // --------------------------------------------------------------------
721
722 /**
723 * Clean Extended Email Address: Joe Smith <joe@smith.com>
724 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 * @param string
726 * @return string
727 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000728 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 {
730 if ( ! is_array($email))
731 {
Andrey Andreev56454792012-05-17 14:32:19 +0300732 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734
735 $clean_email = array();
736
737 foreach ($email as $addy)
738 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200739 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
742 return $clean_email;
743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 // --------------------------------------------------------------------
746
747 /**
748 * Build alternative plain text message
749 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300750 * Provides the raw message for use in plain-text headers of
751 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * If the user hasn't specified his own alternative message
753 * it creates one by stripping the HTML
754 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 * @return string
756 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600757 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300759 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300761 return ($this->wordwrap)
762 ? $this->word_wrap($this->alt_message, 76)
763 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
Andrey Andreev56454792012-05-17 14:32:19 +0300766 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200767 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000768
769 for ($i = 20; $i >= 3; $i--)
770 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200771 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300774 return ($this->wordwrap)
775 ? $this->word_wrap($body, 76)
776 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Word Wrap
783 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300785 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @return string
787 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300788 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300790 // Set the character limit, if not already present
791 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300793 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 }
795
796 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300797 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000798
799 // Standardize newlines
800 if (strpos($str, "\r") !== FALSE)
801 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200802 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804
805 // If the current word is surrounded by {unwrap} tags we'll
806 // strip the entire chunk and replace it with a marker.
807 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300808 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200810 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200812 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300813 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 }
815 }
816
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000817 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300819 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 $str = wordwrap($str, $charlim, "\n", FALSE);
821
822 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300823 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 foreach (explode("\n", $str) as $line)
825 {
826 // Is the line within the allowed character count?
827 // If so we'll join it to the output and continue
828 if (strlen($line) <= $charlim)
829 {
830 $output .= $line.$this->newline;
831 continue;
832 }
833
834 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200835 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
837 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300838 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
840 break;
841 }
842
843 // Trim the word down
844 $temp .= substr($line, 0, $charlim-1);
845 $line = substr($line, $charlim-1);
846 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200847 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000848
849 // If $temp contains data it means we had to split up an over-length
850 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100851 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200853 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
855
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200856 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
858
859 // Put our markers back
860 if (count($unwrap) > 0)
861 {
862 foreach ($unwrap as $key => $val)
863 {
Andrey Andreev56454792012-05-17 14:32:19 +0300864 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
866 }
867
868 return $output;
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // --------------------------------------------------------------------
872
873 /**
874 * Build final headers
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @return string
877 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600878 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700880 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
881 $this->set_header('X-Mailer', $this->useragent);
882 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
883 $this->set_header('Message-ID', $this->_get_message_id());
884 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 // --------------------------------------------------------------------
888
889 /**
890 * Write Headers as a string
891 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * @return void
893 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600894 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200896 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
898 $this->_subject = $this->_headers['Subject'];
899 unset($this->_headers['Subject']);
900 }
901
902 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300903 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000904
Pascal Kriete14287f32011-02-14 13:39:34 -0500905 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
907 $val = trim($val);
908
Alex Bilbied261b1e2012-06-02 11:12:16 +0100909 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Andrey Andreev56454792012-05-17 14:32:19 +0300911 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
913 }
914
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200915 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Derek Jones1d890882009-02-10 20:32:14 +0000917 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 // --------------------------------------------------------------------
922
923 /**
924 * Build Final Body and attachments
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return void
927 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600928 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200930 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 $this->_body = $this->word_wrap($this->_body);
933 }
934
935 $this->_set_boundaries();
936 $this->_write_headers();
937
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200938 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500939 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000940
941 switch ($this->_get_content_type())
942 {
943 case 'plain' :
944
Andrey Andreev56454792012-05-17 14:32:19 +0300945 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
946 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000947
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200948 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $this->_header_str .= $hdr;
951 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
Brandon Jones485d7412010-11-09 16:38:17 -0500953 else
954 {
955 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
956 }
Eric Barnes6113f542010-12-29 13:36:12 -0500957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 return;
959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 case 'html' :
961
962 if ($this->send_multipart === FALSE)
963 {
Andrey Andreev56454792012-05-17 14:32:19 +0300964 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
965 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
967 else
968 {
Andrey Andreev56454792012-05-17 14:32:19 +0300969 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
Andrey Andreev56454792012-05-17 14:32:19 +0300971 $body .= $this->_get_mime_message().$this->newline.$this->newline
972 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000973
Andrey Andreev56454792012-05-17 14:32:19 +0300974 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
975 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
976 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500977
Andrey Andreev56454792012-05-17 14:32:19 +0300978 .'Content-Type: text/html; charset='.$this->charset.$this->newline
979 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Eric Barnes6113f542010-12-29 13:36:12 -0500981
Andrey Andreev56454792012-05-17 14:32:19 +0300982 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500983
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200984 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500987 }
988 else
989 {
Andrey Andreev56454792012-05-17 14:32:19 +0300990 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
994 if ($this->send_multipart !== FALSE)
995 {
Andrey Andreev56454792012-05-17 14:32:19 +0300996 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return;
1000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 case 'plain-attach' :
1002
Andrey Andreev56454792012-05-17 14:32:19 +03001003 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001005 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001008 }
1009
Andrey Andreev56454792012-05-17 14:32:19 +03001010 $body .= $this->_get_mime_message().$this->newline.$this->newline
1011 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
Andrey Andreev56454792012-05-17 14:32:19 +03001013 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1014 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
Andrey Andreev56454792012-05-17 14:32:19 +03001016 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017
1018 break;
1019 case 'html-attach' :
1020
Andrey Andreev56454792012-05-17 14:32:19 +03001021 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001022
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001023 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027
Andrey Andreev56454792012-05-17 14:32:19 +03001028 $body .= $this->_get_mime_message().$this->newline.$this->newline
1029 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001030
Andrey Andreev56454792012-05-17 14:32:19 +03001031 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1032 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001033
Andrey Andreev56454792012-05-17 14:32:19 +03001034 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1035 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1036 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001037
Andrey Andreev56454792012-05-17 14:32:19 +03001038 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1039 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001040
Andrey Andreev56454792012-05-17 14:32:19 +03001041 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1042 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043
1044 break;
1045 }
1046
1047 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001048 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 {
tritbc4ac992011-11-23 07:19:41 -05001050 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001051 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001053 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
Alex Bilbied261b1e2012-06-02 11:12:16 +01001055 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001057 if ( ! file_exists($filename))
1058 {
1059 $this->_set_error_message('lang:email_attachment_missing', $filename);
1060 return FALSE;
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062
Matteo Mattei5688d582012-03-13 19:29:29 +01001063 $file = filesize($filename) +1;
1064
1065 if ( ! $fp = fopen($filename, FOPEN_READ))
1066 {
1067 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1068 return FALSE;
1069 }
1070
Matteo Matteic3b36f42012-03-26 10:27:17 +02001071 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001072 $file_content = fread($fp, $file);
1073 fclose($fp);
1074 }
1075 else
1076 {
1077 $file_content =& $this->_attach_content[$i];
1078 }
Andrey Andreev56454792012-05-17 14:32:19 +03001079
1080 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1081 .'Content-type: '.$ctype.'; '
1082 .'name="'.$basename.'"'.$this->newline
1083 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1084 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001085
Matteo Mattei5688d582012-03-13 19:29:29 +01001086 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
1088
Andrey Andreev56454792012-05-17 14:32:19 +03001089 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1090 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 return;
1092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Prep Quoted Printable
1098 *
1099 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1100 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1101 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 * @return string
1104 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001105 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001107 // We are intentionally wrapping so mail servers will encode characters
1108 // properly and MUAs will behave, so {unwrap} must go!
1109 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1110
Andrey Andreev683b34d2012-10-09 15:00:00 +03001111 // RFC 2045 specifies CRLF as "\r\n".
1112 // However, many developers choose to override that and violate
1113 // the RFC rules due to (apparently) a bug in MS Exchange,
1114 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001115 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001116 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001117 if (is_php('5.3'))
1118 {
1119 return quoted_printable_encode($str);
1120 }
1121 elseif (function_exists('imap_8bit'))
1122 {
1123 return imap_8bit($str);
1124 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001125 }
1126
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001127 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001128 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001129
1130 // Standardize newlines
1131 if (strpos($str, "\r") !== FALSE)
1132 {
1133 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1134 }
1135
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $escape = '=';
1137 $output = '';
1138
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001139 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 $length = strlen($line);
1142 $temp = '';
1143
1144 // Loop through each character in the line to add soft-wrap
1145 // characters at the end of a line " =\r\n" and add the newly
1146 // processed line(s) to the output (see comment on $crlf class property)
1147 for ($i = 0; $i < $length; $i++)
1148 {
1149 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001150 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 $ascii = ord($char);
1152
1153 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001154 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001156 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001158 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001160 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 }
1162
1163 // If we're at the character limit, add the line to the output,
1164 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001165 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 {
1167 $output .= $temp.$escape.$this->crlf;
1168 $temp = '';
1169 }
1170
1171 // Add the character to our temporary line
1172 $temp .= $char;
1173 }
1174
1175 // Add our completed line to the output
1176 $output .= $temp.$this->crlf;
1177 }
1178
1179 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001180 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 }
1182
1183 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 /**
1186 * Prep Q Encoding
1187 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001188 * Performs "Q Encoding" on a string for use in email headers.
1189 * It's related but not identical to quoted-printable, so it has its
1190 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001192 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001193 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001195 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001197 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
Andrey Andreev925dd902012-10-19 11:06:31 +03001199 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001201 if (MB_ENABLED === TRUE)
1202 {
1203 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1204 }
1205 elseif (extension_loaded('iconv'))
1206 {
1207 $output = @iconv_mime_encode('', $str,
1208 array(
1209 'scheme' => 'Q',
1210 'line-length' => 76,
1211 'input-charset' => $this->charset,
1212 'output-charset' => $this->charset,
1213 'line-break-chars' => $this->crlf
1214 )
1215 );
1216
1217 // There are reports that iconv_mime_encode() might fail and return FALSE
1218 if ($output !== FALSE)
1219 {
1220 // iconv_mime_encode() will always put a header field name.
1221 // We've passed it an empty one, but it still prepends our
1222 // encoded string with ': ', so we need to strip it.
1223 return substr($output, 2);
1224 }
1225
1226 $chars = iconv_strlen($str, 'UTF-8');
1227 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 }
1229
Andrey Andreev925dd902012-10-19 11:06:31 +03001230 // We might already have this set for UTF-8
1231 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001232
Andrey Andreev925dd902012-10-19 11:06:31 +03001233 $output = '=?'.$this->charset.'?Q?';
1234 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001236 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1237 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1238 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001239
Andrey Andreev925dd902012-10-19 11:06:31 +03001240 // RFC 2045 sets a limit of 76 characters per line.
1241 // We'll append ?= to the end of each line though.
1242 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001244 $output .= '?='.$this->crlf // EOL
1245 .' =?'.$this->charset.'?Q?'.$chr; // New line
1246 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001248 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001250 $output .= $chr;
1251 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 }
1254
Andrey Andreev925dd902012-10-19 11:06:31 +03001255 // End the header
1256 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 }
1258
1259 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 /**
1262 * Send Email
1263 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001264 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 * @return bool
1266 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001267 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001269 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
1271 $this->reply_to($this->_headers['From']);
1272 }
1273
Andrey Andreev76f15c92012-03-01 13:05:07 +02001274 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1275 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1276 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
patworkb0707982011-04-08 15:10:05 +02001278 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 return FALSE;
1280 }
1281
1282 $this->_build_headers();
1283
Andrey Andreev76f15c92012-03-01 13:05:07 +02001284 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001286 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001287
Alex Bilbiea87aab32012-07-30 09:50:37 +01001288 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001289 {
1290 $this->clear();
1291 }
1292
Alex Bilbieb901e732012-07-30 09:44:57 +01001293 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
1295
1296 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001297 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001298
Alex Bilbiea87aab32012-07-30 09:50:37 +01001299 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001300 {
1301 $this->clear();
1302 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001303
Alex Bilbieb901e732012-07-30 09:44:57 +01001304 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 }
Barry Mienydd671972010-10-04 16:33:58 +02001306
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 // --------------------------------------------------------------------
1308
1309 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001310 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001312 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001314 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001316 $float = $this->bcc_batch_size - 1;
1317 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 $chunk = array();
1319
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001320 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 {
1322 if (isset($this->_bcc_array[$i]))
1323 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001324 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 }
1326
Alex Bilbied261b1e2012-06-02 11:12:16 +01001327 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
1329 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001330 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001331 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 }
1333
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001334 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
1336 $chunk[] = substr($set, 1);
1337 }
1338 }
1339
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001340 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 {
1342 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001343
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001344 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001345
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001346 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001348 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 }
1350 else
1351 {
1352 $this->_bcc_array = $bcc;
1353 }
1354
1355 $this->_build_message();
1356 $this->_spool_email();
1357 }
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 // --------------------------------------------------------------------
1361
1362 /**
1363 * Unwrap special elements
1364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 * @return void
1366 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001367 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 {
Andrey Andreev56454792012-05-17 14:32:19 +03001369 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 // --------------------------------------------------------------------
1373
1374 /**
1375 * Strip line-breaks via callback
1376 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001377 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @return string
1379 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001380 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
1382 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1383 {
1384 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1385 }
1386
1387 return $matches[1];
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 // --------------------------------------------------------------------
1391
1392 /**
1393 * Spool mail to the mail server
1394 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 * @return bool
1396 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001397 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 {
1399 $this->_unwrap_specials();
1400
Andrey Andreev56454792012-05-17 14:32:19 +03001401 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001402 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
Andrey Andreev56454792012-05-17 14:32:19 +03001404 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001405 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 }
1407
patworkb0707982011-04-08 15:10:05 +02001408 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 return TRUE;
1410 }
Barry Mienydd671972010-10-04 16:33:58 +02001411
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 // --------------------------------------------------------------------
1413
1414 /**
1415 * Send using mail()
1416 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 * @return bool
1418 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001419 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001421 if (is_array($this->_recipients))
1422 {
1423 $this->_recipients = implode(', ', $this->_recipients);
1424 }
1425
Alex Bilbied261b1e2012-06-02 11:12:16 +01001426 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001428 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 }
1430 else
1431 {
1432 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1433 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001434 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 +00001435 }
1436 }
Barry Mienydd671972010-10-04 16:33:58 +02001437
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 // --------------------------------------------------------------------
1439
1440 /**
1441 * Send using Sendmail
1442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 * @return bool
1444 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001445 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
Melounek58dfc082012-06-29 08:43:47 +02001447 $fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'.' -r '.$this->clean_email($this->_headers['Return-Path']), 'w');
Derek Allard2067d1a2008-11-13 22:59:24 +00001448
Derek Jones4cefaa42009-04-29 19:13:56 +00001449 if ($fp === FALSE OR $fp === NULL)
1450 {
1451 // server probably has popen disabled, so nothing we can do to get a verbose error.
1452 return FALSE;
1453 }
Derek Jones71141ce2010-03-02 16:41:20 -06001454
Derek Jonesc630bcf2008-11-17 21:09:45 +00001455 fputs($fp, $this->_header_str);
1456 fputs($fp, $this->_finalbody);
1457
Barry Mienydd671972010-10-04 16:33:58 +02001458 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001459
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001460 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 {
patworkb0707982011-04-08 15:10:05 +02001462 $this->_set_error_message('lang:email_exit_status', $status);
1463 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 return FALSE;
1465 }
1466
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 return TRUE;
1468 }
Barry Mienydd671972010-10-04 16:33:58 +02001469
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 // --------------------------------------------------------------------
1471
1472 /**
1473 * Send using SMTP
1474 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 * @return bool
1476 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001477 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001479 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
patworkb0707982011-04-08 15:10:05 +02001481 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 return FALSE;
1483 }
1484
Diogo Osório593f7982012-03-02 18:04:17 +00001485 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001486 {
1487 return FALSE;
1488 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001489
1490 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1491
Pascal Kriete14287f32011-02-14 13:39:34 -05001492 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
1494 $this->_send_command('to', $val);
1495 }
1496
1497 if (count($this->_cc_array) > 0)
1498 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001499 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001501 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
1503 $this->_send_command('to', $val);
1504 }
1505 }
1506 }
1507
1508 if (count($this->_bcc_array) > 0)
1509 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001510 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001512 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
1514 $this->_send_command('to', $val);
1515 }
1516 }
1517 }
1518
1519 $this->_send_command('data');
1520
1521 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001522 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001523
1524 $this->_send_data('.');
1525
1526 $reply = $this->_get_smtp_data();
1527
1528 $this->_set_error_message($reply);
1529
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001530 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
patworkb0707982011-04-08 15:10:05 +02001532 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 return FALSE;
1534 }
1535
1536 $this->_send_command('quit');
1537 return TRUE;
1538 }
Barry Mienydd671972010-10-04 16:33:58 +02001539
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 // --------------------------------------------------------------------
1541
1542 /**
1543 * SMTP Connect
1544 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 * @return string
1546 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001547 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001549 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001550
Radu Potopbbf04b02011-09-28 13:57:51 +03001551 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001552 $this->smtp_port,
1553 $errno,
1554 $errstr,
1555 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001556
Pascal Kriete14287f32011-02-14 13:39:34 -05001557 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 {
Andrey Andreev56454792012-05-17 14:32:19 +03001559 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 return FALSE;
1561 }
1562
1563 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001564
Alex Bilbied261b1e2012-06-02 11:12:16 +01001565 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001566 {
1567 $this->_send_command('hello');
1568 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001569
Radu Potop4c589ae2011-09-29 10:19:55 +03001570 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001571
Sean Fishere862ddd2011-10-26 22:45:00 -03001572 if ($crypto !== TRUE)
1573 {
1574 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1575 return FALSE;
1576 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001577 }
1578
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 return $this->_send_command('hello');
1580 }
Barry Mienydd671972010-10-04 16:33:58 +02001581
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 // --------------------------------------------------------------------
1583
1584 /**
1585 * Send SMTP command
1586 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 * @param string
1588 * @param string
1589 * @return string
1590 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001591 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
1593 switch ($cmd)
1594 {
1595 case 'hello' :
1596
Andrey Andreev56454792012-05-17 14:32:19 +03001597 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1598 {
1599 $this->_send_data('EHLO '.$this->_get_hostname());
1600 }
1601 else
1602 {
1603 $this->_send_data('HELO '.$this->_get_hostname());
1604 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001605
1606 $resp = 250;
1607 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001608 case 'starttls' :
1609
1610 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001611 $resp = 220;
1612 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 case 'from' :
1614
1615 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 $resp = 250;
1617 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001618 case 'to' :
1619
leandronf7686c122012-03-22 08:07:31 -03001620 if ($this->dsn)
1621 {
leandronfb4552942012-03-21 23:54:26 -03001622 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001623 }
leandronfb4552942012-03-21 23:54:26 -03001624 else
leandronf7686c122012-03-22 08:07:31 -03001625 {
leandronfb4552942012-03-21 23:54:26 -03001626 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001627 }
Andrey Andreev56454792012-05-17 14:32:19 +03001628
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 $resp = 250;
1630 break;
1631 case 'data' :
1632
1633 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 $resp = 354;
1635 break;
1636 case 'quit' :
1637
1638 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 $resp = 221;
1640 break;
1641 }
1642
1643 $reply = $this->_get_smtp_data();
1644
Andrey Andreev56454792012-05-17 14:32:19 +03001645 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001646
Andrey Andreevba261762012-06-11 14:11:35 +03001647 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
patworkb0707982011-04-08 15:10:05 +02001649 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 return FALSE;
1651 }
1652
Alex Bilbied261b1e2012-06-02 11:12:16 +01001653 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 {
1655 fclose($this->_smtp_connect);
1656 }
1657
1658 return TRUE;
1659 }
Barry Mienydd671972010-10-04 16:33:58 +02001660
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 // --------------------------------------------------------------------
1662
1663 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001664 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 * @return bool
1667 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001668 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 {
1670 if ( ! $this->_smtp_auth)
1671 {
1672 return TRUE;
1673 }
1674
Alex Bilbied261b1e2012-06-02 11:12:16 +01001675 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 {
patworkb0707982011-04-08 15:10:05 +02001677 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 return FALSE;
1679 }
1680
1681 $this->_send_data('AUTH LOGIN');
1682
1683 $reply = $this->_get_smtp_data();
1684
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001685 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 {
patworkb0707982011-04-08 15:10:05 +02001687 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 return FALSE;
1689 }
1690
1691 $this->_send_data(base64_encode($this->smtp_user));
1692
1693 $reply = $this->_get_smtp_data();
1694
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001695 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 {
patworkb0707982011-04-08 15:10:05 +02001697 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 return FALSE;
1699 }
1700
1701 $this->_send_data(base64_encode($this->smtp_pass));
1702
1703 $reply = $this->_get_smtp_data();
1704
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001705 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 {
patworkb0707982011-04-08 15:10:05 +02001707 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 return FALSE;
1709 }
1710
1711 return TRUE;
1712 }
Barry Mienydd671972010-10-04 16:33:58 +02001713
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // --------------------------------------------------------------------
1715
1716 /**
1717 * Send SMTP data
1718 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001719 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 * @return bool
1721 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001722 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001724 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
patworkb0707982011-04-08 15:10:05 +02001726 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 return FALSE;
1728 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001729
1730 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
Barry Mienydd671972010-10-04 16:33:58 +02001732
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 // --------------------------------------------------------------------
1734
1735 /**
1736 * Get SMTP data
1737 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 * @return string
1739 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001740 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Andrey Andreev56454792012-05-17 14:32:19 +03001742 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001743
1744 while ($str = fgets($this->_smtp_connect, 512))
1745 {
1746 $data .= $str;
1747
Alex Bilbied261b1e2012-06-02 11:12:16 +01001748 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
1750 break;
1751 }
1752 }
1753
1754 return $data;
1755 }
Barry Mienydd671972010-10-04 16:33:58 +02001756
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 // --------------------------------------------------------------------
1758
1759 /**
1760 * Get Hostname
1761 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 * @return string
1763 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001764 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001766 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
Barry Mienydd671972010-10-04 16:33:58 +02001768
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 // --------------------------------------------------------------------
1770
1771 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 * Get Debug Message
1773 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 * @return string
1775 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001776 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 {
1778 $msg = '';
1779
1780 if (count($this->_debug_msg) > 0)
1781 {
1782 foreach ($this->_debug_msg as $val)
1783 {
1784 $msg .= $val;
1785 }
1786 }
1787
Andrey Andreev59c5b532012-03-01 14:09:51 +02001788 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 }
Barry Mienydd671972010-10-04 16:33:58 +02001790
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 // --------------------------------------------------------------------
1792
1793 /**
1794 * Set Message
1795 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001796 * @param string $msg
1797 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02001798 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001800 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 {
1802 $CI =& get_instance();
1803 $CI->lang->load('email');
1804
Andrey Andreev76f15c92012-03-01 13:05:07 +02001805 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 {
Andrey Andreev56454792012-05-17 14:32:19 +03001807 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 }
1809 else
1810 {
Andrey Andreev56454792012-05-17 14:32:19 +03001811 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 }
1813 }
Barry Mienydd671972010-10-04 16:33:58 +02001814
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 // --------------------------------------------------------------------
1816
1817 /**
1818 * Mime Types
1819 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @param string
1821 * @return string
1822 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001823 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001825 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001826
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001827 $ext = strtolower($ext);
1828
1829 if ( ! is_array($mimes))
1830 {
1831 $mimes =& get_mimes();
1832 }
1833
1834 if (isset($mimes[$ext]))
1835 {
1836 return is_array($mimes[$ext])
1837 ? current($mimes[$ext])
1838 : $mimes[$ext];
1839 }
1840
1841 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 }
1843
1844}
Derek Allard2067d1a2008-11-13 22:59:24 +00001845
1846/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001847/* Location: ./system/libraries/Email.php */