blob: c1130e9155701830289ee06947279cf004f5acce [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 *
97 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000099 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300101 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 if (count($config) > 0)
104 {
105 $this->initialize($config);
106 }
107 else
108 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100109 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200110 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
Andrey Andreev925dd902012-10-19 11:06:31 +0300113 $this->charset = strtoupper($this->charset);
114
Andrey Andreev59c5b532012-03-01 14:09:51 +0200115 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Initialize preferences
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param array
124 * @return void
125 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000126 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 foreach ($config as $key => $val)
129 {
130 if (isset($this->$key))
131 {
132 $method = 'set_'.$key;
133
134 if (method_exists($this, $method))
135 {
136 $this->$method($val);
137 }
138 else
139 {
140 $this->$key = $val;
141 }
142 }
143 }
Eric Barnes6113f542010-12-29 13:36:12 -0500144 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000145
Alex Bilbied261b1e2012-06-02 11:12:16 +0100146 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200147 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000148
149 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
153
154 /**
155 * Initialize the Email Data
156 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800157 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200158 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000160 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200162 $this->_subject = '';
163 $this->_body = '';
164 $this->_finalbody = '';
165 $this->_header_str = '';
166 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500168 $this->_cc_array = array();
169 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $this->_headers = array();
171 $this->_debug_msg = array();
172
Mickey Wubfc1cad2012-05-31 22:28:40 -0700173 $this->set_header('User-Agent', $this->useragent);
174 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000175
176 if ($clear_attachments !== FALSE)
177 {
178 $this->_attach_name = array();
179 $this->_attach_type = array();
180 $this->_attach_disp = array();
181 }
Eric Barnes6113f542010-12-29 13:36:12 -0500182
Greg Akera769deb2010-11-10 15:47:29 -0600183 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // --------------------------------------------------------------------
187
188 /**
189 * Set FROM
190 *
Andrey Andreev925dd902012-10-19 11:06:31 +0300191 * @param string From
192 * @param string Return-Path
Andrey Andreev081c9462012-03-01 12:58:11 +0200193 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300195 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200197 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200199 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
202 if ($this->validate)
203 {
204 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300205 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200206 {
207 $this->validate_email($this->_str_to_array($return_path));
208 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100212 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 // only use Q encoding if there are characters that would require it
215 if ( ! preg_match('/[\200-\377]/', $name))
216 {
217 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000218 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220 else
221 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300222 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224 }
225
Mickey Wubfc1cad2012-05-31 22:28:40 -0700226 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200227
Andrey Andreev925dd902012-10-19 11:06:31 +0300228 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200229 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500230
Greg Akera769deb2010-11-10 15:47:29 -0600231 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
235
236 /**
237 * Set Reply-to
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @param string
240 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200241 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000243 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200245 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200247 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250 if ($this->validate)
251 {
252 $this->validate_email($this->_str_to_array($replyto));
253 }
254
Alex Bilbied261b1e2012-06-02 11:12:16 +0100255 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 $name = $replyto;
258 }
259
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300260 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 $name = '"'.$name.'"';
263 }
264
Mickey Wubfc1cad2012-05-31 22:28:40 -0700265 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600267
268 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
272
273 /**
274 * Set Recipients
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200277 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000279 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 $to = $this->_str_to_array($to);
282 $to = $this->clean_email($to);
283
284 if ($this->validate)
285 {
286 $this->validate_email($to);
287 }
288
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200289 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700291 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300294 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500295
Greg Akera769deb2010-11-10 15:47:29 -0600296 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
300
301 /**
302 * Set CC
303 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200305 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000307 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev56454792012-05-17 14:32:19 +0300309 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000310
311 if ($this->validate)
312 {
313 $this->validate_email($cc);
314 }
315
Mickey Wubfc1cad2012-05-31 22:28:40 -0700316 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200318 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $this->_cc_array = $cc;
321 }
Eric Barnes6113f542010-12-29 13:36:12 -0500322
Greg Akera769deb2010-11-10 15:47:29 -0600323 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Set BCC
330 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @param string
332 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200333 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000335 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100337 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $this->bcc_batch_mode = TRUE;
340 $this->bcc_batch_size = $limit;
341 }
342
Andrey Andreev56454792012-05-17 14:32:19 +0300343 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000344
345 if ($this->validate)
346 {
347 $this->validate_email($bcc);
348 }
349
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200350 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 $this->_bcc_array = $bcc;
353 }
354 else
355 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700356 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
Eric Barnes6113f542010-12-29 13:36:12 -0500358
Greg Akera769deb2010-11-10 15:47:29 -0600359 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // --------------------------------------------------------------------
363
364 /**
365 * Set Email Subject
366 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200368 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000370 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700373 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600374 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
378
379 /**
380 * Set Body
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200383 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000385 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200387 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200388
Andrey Andreevaf728622011-10-20 10:11:59 +0300389 /* strip slashes only if magic quotes is ON
390 if we do it with magic quotes OFF, it strips real, user-inputted chars.
391
392 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
393 it will probably not exist in future versions at all.
394 */
395 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200396 {
diegorivera33c32802011-10-19 02:56:15 -0200397 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200398 }
diegorivera33c32802011-10-19 02:56:15 -0200399
Greg Akera769deb2010-11-10 15:47:29 -0600400 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // --------------------------------------------------------------------
404
405 /**
406 * Assign file attachments
407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200409 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200411 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
trit151fc682011-11-23 07:30:06 -0500413 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500414 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200415 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600416 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Add a Header Item
423 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @param string
425 * @param string
426 * @return void
427 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700428 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 $this->_headers[$header] = $value;
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
434
435 /**
436 * Convert a String to an Array
437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param string
439 * @return array
440 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600441 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ( ! is_array($email))
444 {
Andrey Andreev56454792012-05-17 14:32:19 +0300445 return (strpos($email, ',') !== FALSE)
446 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
447 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Andrey Andreev56454792012-05-17 14:32:19 +0300449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return $email;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // --------------------------------------------------------------------
454
455 /**
456 * Set Multipart Value
457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200459 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000461 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400463 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600464 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
468
469 /**
470 * Set Mailtype
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200473 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000475 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreev56454792012-05-17 14:32:19 +0300477 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600478 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
484 * Set Wordwrap
485 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400486 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200487 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000489 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400491 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600492 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // --------------------------------------------------------------------
496
497 /**
498 * Set Protocol
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200501 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000503 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200505 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600506 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
511 /**
512 * Set Priority
513 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200514 * @param int
515 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000517 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200519 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600520 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // --------------------------------------------------------------------
524
525 /**
526 * Set Newline Character
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200529 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000531 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200533 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600534 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 // --------------------------------------------------------------------
538
539 /**
540 * Set CRLF
541 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200543 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000545 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200547 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600548 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 // --------------------------------------------------------------------
552
553 /**
554 * Set Message Boundary
555 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @return void
557 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600558 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200560 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
561 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // --------------------------------------------------------------------
565
566 /**
567 * Get the Message ID
568 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 * @return string
570 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600571 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200573 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300574 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // --------------------------------------------------------------------
578
579 /**
580 * Get Mail Protocol
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200583 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600585 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200588 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000589
Alex Bilbied261b1e2012-06-02 11:12:16 +0100590 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 return $this->protocol;
593 }
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // --------------------------------------------------------------------
597
598 /**
599 * Get Mail Encoding
600 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @param bool
602 * @return string
603 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600604 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200606 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000607
608 foreach ($this->_base_charsets as $charset)
609 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300610 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $this->_encoding = '7bit';
613 }
614 }
615
Alex Bilbied261b1e2012-06-02 11:12:16 +0100616 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 return $this->_encoding;
619 }
620 }
621
622 // --------------------------------------------------------------------
623
624 /**
625 * Get content type (text/html/attachment)
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return string
628 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600629 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreev56454792012-05-17 14:32:19 +0300631 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100633 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200635 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 return 'plain-attach';
638 }
639 else
640 {
641 return 'plain';
642 }
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // --------------------------------------------------------------------
646
647 /**
648 * Set RFC 822 Date
649 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 * @return string
651 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600652 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200654 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300655 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200657 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658
Andrey Andreev59c5b532012-03-01 14:09:51 +0200659 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
663
664 /**
665 * Mime message
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @return string
668 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600669 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200671 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 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
675
676 /**
677 * Validate Email Address
678 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 * @param string
680 * @return bool
681 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000682 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 if ( ! is_array($email))
685 {
patworkb0707982011-04-08 15:10:05 +0200686 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 return FALSE;
688 }
689
690 foreach ($email as $val)
691 {
692 if ( ! $this->valid_email($val))
693 {
patworkb0707982011-04-08 15:10:05 +0200694 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 return FALSE;
696 }
697 }
698
699 return TRUE;
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 // --------------------------------------------------------------------
703
704 /**
705 * Email Validation
706 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @param string
708 * @return bool
709 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400710 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300712 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 // --------------------------------------------------------------------
716
717 /**
718 * Clean Extended Email Address: Joe Smith <joe@smith.com>
719 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 * @param string
721 * @return string
722 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000723 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
725 if ( ! is_array($email))
726 {
Andrey Andreev56454792012-05-17 14:32:19 +0300727 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
729
730 $clean_email = array();
731
732 foreach ($email as $addy)
733 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200734 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 }
736
737 return $clean_email;
738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 // --------------------------------------------------------------------
741
742 /**
743 * Build alternative plain text message
744 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300745 * Provides the raw message for use in plain-text headers of
746 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * If the user hasn't specified his own alternative message
748 * it creates one by stripping the HTML
749 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @return string
751 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600752 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300754 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300756 return ($this->wordwrap)
757 ? $this->word_wrap($this->alt_message, 76)
758 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
Andrey Andreev56454792012-05-17 14:32:19 +0300761 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200762 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000763
764 for ($i = 20; $i >= 3; $i--)
765 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200766 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
768
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300769 return ($this->wordwrap)
770 ? $this->word_wrap($body, 76)
771 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 // --------------------------------------------------------------------
775
776 /**
777 * Word Wrap
778 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300780 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 * @return string
782 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300783 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300785 // Set the character limit, if not already present
786 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300788 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790
791 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300792 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000793
794 // Standardize newlines
795 if (strpos($str, "\r") !== FALSE)
796 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200797 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
799
800 // If the current word is surrounded by {unwrap} tags we'll
801 // strip the entire chunk and replace it with a marker.
802 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300803 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200805 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200807 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300808 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
810 }
811
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000812 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300814 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $str = wordwrap($str, $charlim, "\n", FALSE);
816
817 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300818 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 foreach (explode("\n", $str) as $line)
820 {
821 // Is the line within the allowed character count?
822 // If so we'll join it to the output and continue
823 if (strlen($line) <= $charlim)
824 {
825 $output .= $line.$this->newline;
826 continue;
827 }
828
829 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200830 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
832 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300833 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
835 break;
836 }
837
838 // Trim the word down
839 $temp .= substr($line, 0, $charlim-1);
840 $line = substr($line, $charlim-1);
841 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200842 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000843
844 // If $temp contains data it means we had to split up an over-length
845 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100846 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200848 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
850
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200851 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
853
854 // Put our markers back
855 if (count($unwrap) > 0)
856 {
857 foreach ($unwrap as $key => $val)
858 {
Andrey Andreev56454792012-05-17 14:32:19 +0300859 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
861 }
862
863 return $output;
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // --------------------------------------------------------------------
867
868 /**
869 * Build final headers
870 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 * @return string
872 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600873 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700875 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
876 $this->set_header('X-Mailer', $this->useragent);
877 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
878 $this->set_header('Message-ID', $this->_get_message_id());
879 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 // --------------------------------------------------------------------
883
884 /**
885 * Write Headers as a string
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @return void
888 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600889 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200891 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
893 $this->_subject = $this->_headers['Subject'];
894 unset($this->_headers['Subject']);
895 }
896
897 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300898 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000899
Pascal Kriete14287f32011-02-14 13:39:34 -0500900 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
902 $val = trim($val);
903
Alex Bilbied261b1e2012-06-02 11:12:16 +0100904 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
Andrey Andreev56454792012-05-17 14:32:19 +0300906 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
908 }
909
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200910 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Derek Jones1d890882009-02-10 20:32:14 +0000912 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
917
918 /**
919 * Build Final Body and attachments
920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @return void
922 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600923 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200925 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 $this->_body = $this->word_wrap($this->_body);
928 }
929
930 $this->_set_boundaries();
931 $this->_write_headers();
932
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200933 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500934 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000935
936 switch ($this->_get_content_type())
937 {
938 case 'plain' :
939
Andrey Andreev56454792012-05-17 14:32:19 +0300940 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
941 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000942
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200943 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 $this->_header_str .= $hdr;
946 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Brandon Jones485d7412010-11-09 16:38:17 -0500948 else
949 {
950 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
951 }
Eric Barnes6113f542010-12-29 13:36:12 -0500952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 return;
954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 case 'html' :
956
957 if ($this->send_multipart === FALSE)
958 {
Andrey Andreev56454792012-05-17 14:32:19 +0300959 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
960 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
962 else
963 {
Andrey Andreev56454792012-05-17 14:32:19 +0300964 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Andrey Andreev56454792012-05-17 14:32:19 +0300966 $body .= $this->_get_mime_message().$this->newline.$this->newline
967 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000968
Andrey Andreev56454792012-05-17 14:32:19 +0300969 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
970 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
971 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500972
Andrey Andreev56454792012-05-17 14:32:19 +0300973 .'Content-Type: text/html; charset='.$this->charset.$this->newline
974 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
Eric Barnes6113f542010-12-29 13:36:12 -0500976
Andrey Andreev56454792012-05-17 14:32:19 +0300977 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500978
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200979 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
981 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500982 }
983 else
984 {
Andrey Andreev56454792012-05-17 14:32:19 +0300985 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988
989 if ($this->send_multipart !== FALSE)
990 {
Andrey Andreev56454792012-05-17 14:32:19 +0300991 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 return;
995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 case 'plain-attach' :
997
Andrey Andreev56454792012-05-17 14:32:19 +0300998 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001000 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
1002 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001003 }
1004
Andrey Andreev56454792012-05-17 14:32:19 +03001005 $body .= $this->_get_mime_message().$this->newline.$this->newline
1006 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001007
Andrey Andreev56454792012-05-17 14:32:19 +03001008 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1009 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001010
Andrey Andreev56454792012-05-17 14:32:19 +03001011 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
1013 break;
1014 case 'html-attach' :
1015
Andrey Andreev56454792012-05-17 14:32:19 +03001016 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001017
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001018 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
1020 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 }
1022
Andrey Andreev56454792012-05-17 14:32:19 +03001023 $body .= $this->_get_mime_message().$this->newline.$this->newline
1024 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001025
Andrey Andreev56454792012-05-17 14:32:19 +03001026 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1027 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001028
Andrey Andreev56454792012-05-17 14:32:19 +03001029 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1030 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1031 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev56454792012-05-17 14:32:19 +03001033 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1034 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001035
Andrey Andreev56454792012-05-17 14:32:19 +03001036 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1037 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001038
1039 break;
1040 }
1041
1042 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001043 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
tritbc4ac992011-11-23 07:19:41 -05001045 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001046 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001048 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001049
Alex Bilbied261b1e2012-06-02 11:12:16 +01001050 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001052 if ( ! file_exists($filename))
1053 {
1054 $this->_set_error_message('lang:email_attachment_missing', $filename);
1055 return FALSE;
1056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001057
Matteo Mattei5688d582012-03-13 19:29:29 +01001058 $file = filesize($filename) +1;
1059
1060 if ( ! $fp = fopen($filename, FOPEN_READ))
1061 {
1062 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1063 return FALSE;
1064 }
1065
Matteo Matteic3b36f42012-03-26 10:27:17 +02001066 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001067 $file_content = fread($fp, $file);
1068 fclose($fp);
1069 }
1070 else
1071 {
1072 $file_content =& $this->_attach_content[$i];
1073 }
Andrey Andreev56454792012-05-17 14:32:19 +03001074
1075 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1076 .'Content-type: '.$ctype.'; '
1077 .'name="'.$basename.'"'.$this->newline
1078 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1079 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001080
Matteo Mattei5688d582012-03-13 19:29:29 +01001081 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
1083
Andrey Andreev56454792012-05-17 14:32:19 +03001084 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1085 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 return;
1087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 // --------------------------------------------------------------------
1090
1091 /**
1092 * Prep Quoted Printable
1093 *
1094 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1095 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 * @return string
1099 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001100 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001102 // We are intentionally wrapping so mail servers will encode characters
1103 // properly and MUAs will behave, so {unwrap} must go!
1104 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1105
Andrey Andreev683b34d2012-10-09 15:00:00 +03001106 // RFC 2045 specifies CRLF as "\r\n".
1107 // However, many developers choose to override that and violate
1108 // the RFC rules due to (apparently) a bug in MS Exchange,
1109 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001110 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001111 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001112 if (is_php('5.3'))
1113 {
1114 return quoted_printable_encode($str);
1115 }
1116 elseif (function_exists('imap_8bit'))
1117 {
1118 return imap_8bit($str);
1119 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001120 }
1121
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001122 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001123 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001124
1125 // Standardize newlines
1126 if (strpos($str, "\r") !== FALSE)
1127 {
1128 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1129 }
1130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $escape = '=';
1132 $output = '';
1133
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001134 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
1136 $length = strlen($line);
1137 $temp = '';
1138
1139 // Loop through each character in the line to add soft-wrap
1140 // characters at the end of a line " =\r\n" and add the newly
1141 // processed line(s) to the output (see comment on $crlf class property)
1142 for ($i = 0; $i < $length; $i++)
1143 {
1144 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001145 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 $ascii = ord($char);
1147
1148 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001149 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001151 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001153 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001155 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
1157
1158 // If we're at the character limit, add the line to the output,
1159 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001160 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 $output .= $temp.$escape.$this->crlf;
1163 $temp = '';
1164 }
1165
1166 // Add the character to our temporary line
1167 $temp .= $char;
1168 }
1169
1170 // Add our completed line to the output
1171 $output .= $temp.$this->crlf;
1172 }
1173
1174 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001175 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
1177
1178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 /**
1181 * Prep Q Encoding
1182 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001183 * Performs "Q Encoding" on a string for use in email headers.
1184 * It's related but not identical to quoted-printable, so it has its
1185 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001187 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001188 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001190 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001192 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001193
Andrey Andreev925dd902012-10-19 11:06:31 +03001194 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001196 if (MB_ENABLED === TRUE)
1197 {
1198 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1199 }
1200 elseif (extension_loaded('iconv'))
1201 {
1202 $output = @iconv_mime_encode('', $str,
1203 array(
1204 'scheme' => 'Q',
1205 'line-length' => 76,
1206 'input-charset' => $this->charset,
1207 'output-charset' => $this->charset,
1208 'line-break-chars' => $this->crlf
1209 )
1210 );
1211
1212 // There are reports that iconv_mime_encode() might fail and return FALSE
1213 if ($output !== FALSE)
1214 {
1215 // iconv_mime_encode() will always put a header field name.
1216 // We've passed it an empty one, but it still prepends our
1217 // encoded string with ': ', so we need to strip it.
1218 return substr($output, 2);
1219 }
1220
1221 $chars = iconv_strlen($str, 'UTF-8');
1222 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
1224
Andrey Andreev925dd902012-10-19 11:06:31 +03001225 // We might already have this set for UTF-8
1226 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001227
Andrey Andreev925dd902012-10-19 11:06:31 +03001228 $output = '=?'.$this->charset.'?Q?';
1229 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001231 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1232 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1233 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001234
Andrey Andreev925dd902012-10-19 11:06:31 +03001235 // RFC 2045 sets a limit of 76 characters per line.
1236 // We'll append ?= to the end of each line though.
1237 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001239 $output .= '?='.$this->crlf // EOL
1240 .' =?'.$this->charset.'?Q?'.$chr; // New line
1241 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001243 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001245 $output .= $chr;
1246 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 }
1249
Andrey Andreev925dd902012-10-19 11:06:31 +03001250 // End the header
1251 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 }
1253
1254 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001255
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 /**
1257 * Send Email
1258 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 * @return bool
1260 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001261 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001263 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 {
1265 $this->reply_to($this->_headers['From']);
1266 }
1267
Andrey Andreev76f15c92012-03-01 13:05:07 +02001268 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1269 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1270 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
patworkb0707982011-04-08 15:10:05 +02001272 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 return FALSE;
1274 }
1275
1276 $this->_build_headers();
1277
Andrey Andreev76f15c92012-03-01 13:05:07 +02001278 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001280 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001281
Alex Bilbiea87aab32012-07-30 09:50:37 +01001282 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001283 {
1284 $this->clear();
1285 }
1286
Alex Bilbieb901e732012-07-30 09:44:57 +01001287 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
1289
1290 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001291 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001292
Alex Bilbiea87aab32012-07-30 09:50:37 +01001293 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001294 {
1295 $this->clear();
1296 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001297
Alex Bilbieb901e732012-07-30 09:44:57 +01001298 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 }
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 // --------------------------------------------------------------------
1302
1303 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001304 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001306 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001308 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001310 $float = $this->bcc_batch_size - 1;
1311 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 $chunk = array();
1313
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001314 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
1316 if (isset($this->_bcc_array[$i]))
1317 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001318 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 }
1320
Alex Bilbied261b1e2012-06-02 11:12:16 +01001321 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 {
1323 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001324 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001325 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 }
1327
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001328 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
1330 $chunk[] = substr($set, 1);
1331 }
1332 }
1333
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001334 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
1336 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001337
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001338 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001339
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001340 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001342 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 }
1344 else
1345 {
1346 $this->_bcc_array = $bcc;
1347 }
1348
1349 $this->_build_message();
1350 $this->_spool_email();
1351 }
1352 }
Barry Mienydd671972010-10-04 16:33:58 +02001353
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 // --------------------------------------------------------------------
1355
1356 /**
1357 * Unwrap special elements
1358 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 * @return void
1360 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001361 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 {
Andrey Andreev56454792012-05-17 14:32:19 +03001363 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 }
Barry Mienydd671972010-10-04 16:33:58 +02001365
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 // --------------------------------------------------------------------
1367
1368 /**
1369 * Strip line-breaks via callback
1370 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 * @return string
1372 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001373 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
1375 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1376 {
1377 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1378 }
1379
1380 return $matches[1];
1381 }
Barry Mienydd671972010-10-04 16:33:58 +02001382
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 // --------------------------------------------------------------------
1384
1385 /**
1386 * Spool mail to the mail server
1387 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 * @return bool
1389 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001390 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
1392 $this->_unwrap_specials();
1393
Andrey Andreev56454792012-05-17 14:32:19 +03001394 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001395 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 {
Andrey Andreev56454792012-05-17 14:32:19 +03001397 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001398 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 }
1400
patworkb0707982011-04-08 15:10:05 +02001401 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 return TRUE;
1403 }
Barry Mienydd671972010-10-04 16:33:58 +02001404
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 // --------------------------------------------------------------------
1406
1407 /**
1408 * Send using mail()
1409 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 * @return bool
1411 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001412 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001414 if (is_array($this->_recipients))
1415 {
1416 $this->_recipients = implode(', ', $this->_recipients);
1417 }
1418
Alex Bilbied261b1e2012-06-02 11:12:16 +01001419 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001421 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
1423 else
1424 {
1425 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1426 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001427 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 +00001428 }
1429 }
Barry Mienydd671972010-10-04 16:33:58 +02001430
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 // --------------------------------------------------------------------
1432
1433 /**
1434 * Send using Sendmail
1435 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 * @return bool
1437 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001438 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 {
Melounek58dfc082012-06-29 08:43:47 +02001440 $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 +00001441
Derek Jones4cefaa42009-04-29 19:13:56 +00001442 if ($fp === FALSE OR $fp === NULL)
1443 {
1444 // server probably has popen disabled, so nothing we can do to get a verbose error.
1445 return FALSE;
1446 }
Derek Jones71141ce2010-03-02 16:41:20 -06001447
Derek Jonesc630bcf2008-11-17 21:09:45 +00001448 fputs($fp, $this->_header_str);
1449 fputs($fp, $this->_finalbody);
1450
Barry Mienydd671972010-10-04 16:33:58 +02001451 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001452
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001453 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
patworkb0707982011-04-08 15:10:05 +02001455 $this->_set_error_message('lang:email_exit_status', $status);
1456 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 return FALSE;
1458 }
1459
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 return TRUE;
1461 }
Barry Mienydd671972010-10-04 16:33:58 +02001462
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 // --------------------------------------------------------------------
1464
1465 /**
1466 * Send using SMTP
1467 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 * @return bool
1469 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001470 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001472 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 {
patworkb0707982011-04-08 15:10:05 +02001474 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 return FALSE;
1476 }
1477
Diogo Osório593f7982012-03-02 18:04:17 +00001478 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001479 {
1480 return FALSE;
1481 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001482
1483 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1484
Pascal Kriete14287f32011-02-14 13:39:34 -05001485 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 {
1487 $this->_send_command('to', $val);
1488 }
1489
1490 if (count($this->_cc_array) > 0)
1491 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001492 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001494 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 {
1496 $this->_send_command('to', $val);
1497 }
1498 }
1499 }
1500
1501 if (count($this->_bcc_array) > 0)
1502 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001503 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001505 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 {
1507 $this->_send_command('to', $val);
1508 }
1509 }
1510 }
1511
1512 $this->_send_command('data');
1513
1514 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001515 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001516
1517 $this->_send_data('.');
1518
1519 $reply = $this->_get_smtp_data();
1520
1521 $this->_set_error_message($reply);
1522
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001523 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 {
patworkb0707982011-04-08 15:10:05 +02001525 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 return FALSE;
1527 }
1528
1529 $this->_send_command('quit');
1530 return TRUE;
1531 }
Barry Mienydd671972010-10-04 16:33:58 +02001532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 // --------------------------------------------------------------------
1534
1535 /**
1536 * SMTP Connect
1537 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 * @param string
1539 * @return string
1540 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001541 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001543 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001544
Radu Potopbbf04b02011-09-28 13:57:51 +03001545 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001546 $this->smtp_port,
1547 $errno,
1548 $errstr,
1549 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001550
Pascal Kriete14287f32011-02-14 13:39:34 -05001551 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 {
Andrey Andreev56454792012-05-17 14:32:19 +03001553 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 return FALSE;
1555 }
1556
1557 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001558
Alex Bilbied261b1e2012-06-02 11:12:16 +01001559 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001560 {
1561 $this->_send_command('hello');
1562 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001563
Radu Potop4c589ae2011-09-29 10:19:55 +03001564 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001565
Sean Fishere862ddd2011-10-26 22:45:00 -03001566 if ($crypto !== TRUE)
1567 {
1568 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1569 return FALSE;
1570 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001571 }
1572
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 return $this->_send_command('hello');
1574 }
Barry Mienydd671972010-10-04 16:33:58 +02001575
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 // --------------------------------------------------------------------
1577
1578 /**
1579 * Send SMTP command
1580 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 * @param string
1582 * @param string
1583 * @return string
1584 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001585 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 {
1587 switch ($cmd)
1588 {
1589 case 'hello' :
1590
Andrey Andreev56454792012-05-17 14:32:19 +03001591 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1592 {
1593 $this->_send_data('EHLO '.$this->_get_hostname());
1594 }
1595 else
1596 {
1597 $this->_send_data('HELO '.$this->_get_hostname());
1598 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001599
1600 $resp = 250;
1601 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001602 case 'starttls' :
1603
1604 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001605 $resp = 220;
1606 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 case 'from' :
1608
1609 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 $resp = 250;
1611 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001612 case 'to' :
1613
leandronf7686c122012-03-22 08:07:31 -03001614 if ($this->dsn)
1615 {
leandronfb4552942012-03-21 23:54:26 -03001616 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001617 }
leandronfb4552942012-03-21 23:54:26 -03001618 else
leandronf7686c122012-03-22 08:07:31 -03001619 {
leandronfb4552942012-03-21 23:54:26 -03001620 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001621 }
Andrey Andreev56454792012-05-17 14:32:19 +03001622
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 $resp = 250;
1624 break;
1625 case 'data' :
1626
1627 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 $resp = 354;
1629 break;
1630 case 'quit' :
1631
1632 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 $resp = 221;
1634 break;
1635 }
1636
1637 $reply = $this->_get_smtp_data();
1638
Andrey Andreev56454792012-05-17 14:32:19 +03001639 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001640
Andrey Andreevba261762012-06-11 14:11:35 +03001641 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 {
patworkb0707982011-04-08 15:10:05 +02001643 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 return FALSE;
1645 }
1646
Alex Bilbied261b1e2012-06-02 11:12:16 +01001647 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
1649 fclose($this->_smtp_connect);
1650 }
1651
1652 return TRUE;
1653 }
Barry Mienydd671972010-10-04 16:33:58 +02001654
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 // --------------------------------------------------------------------
1656
1657 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001658 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 * @return bool
1661 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001662 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 {
1664 if ( ! $this->_smtp_auth)
1665 {
1666 return TRUE;
1667 }
1668
Alex Bilbied261b1e2012-06-02 11:12:16 +01001669 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 {
patworkb0707982011-04-08 15:10:05 +02001671 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 return FALSE;
1673 }
1674
1675 $this->_send_data('AUTH LOGIN');
1676
1677 $reply = $this->_get_smtp_data();
1678
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001679 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 {
patworkb0707982011-04-08 15:10:05 +02001681 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 return FALSE;
1683 }
1684
1685 $this->_send_data(base64_encode($this->smtp_user));
1686
1687 $reply = $this->_get_smtp_data();
1688
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001689 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 {
patworkb0707982011-04-08 15:10:05 +02001691 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 return FALSE;
1693 }
1694
1695 $this->_send_data(base64_encode($this->smtp_pass));
1696
1697 $reply = $this->_get_smtp_data();
1698
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001699 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 {
patworkb0707982011-04-08 15:10:05 +02001701 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 return FALSE;
1703 }
1704
1705 return TRUE;
1706 }
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 // --------------------------------------------------------------------
1709
1710 /**
1711 * Send SMTP data
1712 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 * @return bool
1714 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001715 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 {
1717 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1718 {
patworkb0707982011-04-08 15:10:05 +02001719 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 return FALSE;
1721 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001722
1723 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 }
Barry Mienydd671972010-10-04 16:33:58 +02001725
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 // --------------------------------------------------------------------
1727
1728 /**
1729 * Get SMTP data
1730 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 * @return string
1732 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001733 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 {
Andrey Andreev56454792012-05-17 14:32:19 +03001735 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001736
1737 while ($str = fgets($this->_smtp_connect, 512))
1738 {
1739 $data .= $str;
1740
Alex Bilbied261b1e2012-06-02 11:12:16 +01001741 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
1743 break;
1744 }
1745 }
1746
1747 return $data;
1748 }
Barry Mienydd671972010-10-04 16:33:58 +02001749
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 // --------------------------------------------------------------------
1751
1752 /**
1753 * Get Hostname
1754 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 * @return string
1756 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001757 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001759 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 }
Barry Mienydd671972010-10-04 16:33:58 +02001761
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 // --------------------------------------------------------------------
1763
1764 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 * Get Debug Message
1766 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 * @return string
1768 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001769 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
1771 $msg = '';
1772
1773 if (count($this->_debug_msg) > 0)
1774 {
1775 foreach ($this->_debug_msg as $val)
1776 {
1777 $msg .= $val;
1778 }
1779 }
1780
Andrey Andreev59c5b532012-03-01 14:09:51 +02001781 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 }
Barry Mienydd671972010-10-04 16:33:58 +02001783
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 // --------------------------------------------------------------------
1785
1786 /**
1787 * Set Message
1788 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001790 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001792 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
1794 $CI =& get_instance();
1795 $CI->lang->load('email');
1796
Andrey Andreev76f15c92012-03-01 13:05:07 +02001797 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 {
Andrey Andreev56454792012-05-17 14:32:19 +03001799 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 }
1801 else
1802 {
Andrey Andreev56454792012-05-17 14:32:19 +03001803 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 }
1805 }
Barry Mienydd671972010-10-04 16:33:58 +02001806
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 // --------------------------------------------------------------------
1808
1809 /**
1810 * Mime Types
1811 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 * @param string
1813 * @return string
1814 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001815 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001817 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001818
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001819 $ext = strtolower($ext);
1820
1821 if ( ! is_array($mimes))
1822 {
1823 $mimes =& get_mimes();
1824 }
1825
1826 if (isset($mimes[$ext]))
1827 {
1828 return is_array($mimes[$ext])
1829 ? current($mimes[$ext])
1830 : $mimes[$ext];
1831 }
1832
1833 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 }
1835
1836}
Derek Allard2067d1a2008-11-13 22:59:24 +00001837
1838/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001839/* Location: ./system/libraries/Email.php */