blob: 0109e28901fbf24d078409f6c673094ad1a7423b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Email Class
31 *
32 * Permits email to be sent using Mail, Sendmail, or SMTP.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/email.html
39 */
trita15dd4f2011-11-23 07:40:05 -050040class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Andrey Andreev59c5b532012-03-01 14:09:51 +020042 public $useragent = 'CodeIgniter';
43 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
44 public $protocol = 'mail'; // mail/sendmail/smtp
45 public $smtp_host = ''; // SMTP Server. Example: mail.earthlink.net
46 public $smtp_user = ''; // SMTP Username
47 public $smtp_pass = ''; // SMTP Password
48 public $smtp_port = 25; // SMTP Port
49 public $smtp_timeout = 5; // SMTP Timeout in seconds
50 public $smtp_crypto = ''; // SMTP Encryption. Can be null, tls or ssl.
vkeranov7e4356b2012-10-27 18:41:00 +030051 public $wordwrap = TRUE; // TRUE/FALSE - Turns word-wrap on/off
Andrey Andreev59c5b532012-03-01 14:09:51 +020052 public $wrapchars = 76; // Number of characters to wrap at.
vkeranov7e4356b2012-10-27 18:41:00 +030053 public $mailtype = 'text'; // text/html - Defines email formatting
Andrey Andreev59c5b532012-03-01 14:09:51 +020054 public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
55 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
56 public $alt_message = ''; // Alternative message for HTML emails
vkeranov7e4356b2012-10-27 18:41:00 +030057 public $validate = FALSE; // TRUE/FALSE - Enables email validation
Andrey Andreev59c5b532012-03-01 14:09:51 +020058 public $priority = 3; // Default priority (1 - 5)
59 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
vkeranov7e4356b2012-10-27 18:41:00 +030060 public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
62 // distasteful, is the only thing that seems to work for all environments.
leandronf7686c122012-03-22 08:07:31 -030063 public $dsn = FALSE; // Delivery Status Notification
vkeranov7e4356b2012-10-27 18:41:00 +030064 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 +020065 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020066 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 +020067
68 protected $_safe_mode = FALSE;
69 protected $_subject = '';
70 protected $_body = '';
71 protected $_finalbody = '';
72 protected $_alt_boundary = '';
73 protected $_atc_boundary = '';
74 protected $_header_str = '';
75 protected $_smtp_connect = '';
76 protected $_encoding = '8bit';
77 protected $_IP = FALSE;
78 protected $_smtp_auth = FALSE;
79 protected $_replyto_flag = FALSE;
80 protected $_debug_msg = array();
81 protected $_recipients = array();
82 protected $_cc_array = array();
83 protected $_bcc_array = array();
84 protected $_headers = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +030085 protected $_attachments = array();
Andrey Andreev50814092012-03-01 12:36:12 +020086 protected $_protocols = array('mail', 'sendmail', 'smtp');
87 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
88 protected $_bit_depths = array('7bit', '8bit');
89 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 /**
92 * Constructor - Sets Email Preferences
93 *
94 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +030095 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030096 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +030097 * @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 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300178 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Eric Barnes6113f542010-12-29 13:36:12 -0500180
Greg Akera769deb2010-11-10 15:47:29 -0600181 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Set FROM
188 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300189 * @param string $from
190 * @param string $name
191 * @param string $return_path = NULL Return-Path
Andrey Andreev081c9462012-03-01 12:58:11 +0200192 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300194 public function from($from, $name = '', $return_path = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200196 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200198 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 if ($this->validate)
202 {
203 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300204 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200205 {
206 $this->validate_email($this->_str_to_array($return_path));
207 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100211 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 // only use Q encoding if there are characters that would require it
214 if ( ! preg_match('/[\200-\377]/', $name))
215 {
216 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000217 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219 else
220 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300221 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223 }
224
Mickey Wubfc1cad2012-05-31 22:28:40 -0700225 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200226
Andrey Andreev925dd902012-10-19 11:06:31 +0300227 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200228 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500229
Greg Akera769deb2010-11-10 15:47:29 -0600230 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Set Reply-to
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string
239 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200240 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000242 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200244 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200246 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249 if ($this->validate)
250 {
251 $this->validate_email($this->_str_to_array($replyto));
252 }
253
Alex Bilbied261b1e2012-06-02 11:12:16 +0100254 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 $name = $replyto;
257 }
258
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300259 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $name = '"'.$name.'"';
262 }
263
Mickey Wubfc1cad2012-05-31 22:28:40 -0700264 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600266
267 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Set Recipients
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200276 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000278 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $to = $this->_str_to_array($to);
281 $to = $this->clean_email($to);
282
283 if ($this->validate)
284 {
285 $this->validate_email($to);
286 }
287
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200288 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700290 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300293 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500294
Greg Akera769deb2010-11-10 15:47:29 -0600295 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Set CC
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200304 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000306 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreev56454792012-05-17 14:32:19 +0300308 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310 if ($this->validate)
311 {
312 $this->validate_email($cc);
313 }
314
Mickey Wubfc1cad2012-05-31 22:28:40 -0700315 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200317 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->_cc_array = $cc;
320 }
Eric Barnes6113f542010-12-29 13:36:12 -0500321
Greg Akera769deb2010-11-10 15:47:29 -0600322 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Set BCC
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string
331 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200332 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000334 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100336 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->bcc_batch_mode = TRUE;
339 $this->bcc_batch_size = $limit;
340 }
341
Andrey Andreev56454792012-05-17 14:32:19 +0300342 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
344 if ($this->validate)
345 {
346 $this->validate_email($bcc);
347 }
348
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200349 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $this->_bcc_array = $bcc;
352 }
353 else
354 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700355 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Eric Barnes6113f542010-12-29 13:36:12 -0500357
Greg Akera769deb2010-11-10 15:47:29 -0600358 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Set Email Subject
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200367 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000369 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700372 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600373 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Set Body
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200382 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000384 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200386 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200387
Andrey Andreevaf728622011-10-20 10:11:59 +0300388 /* strip slashes only if magic quotes is ON
389 if we do it with magic quotes OFF, it strips real, user-inputted chars.
390
391 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
392 it will probably not exist in future versions at all.
393 */
394 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200395 {
diegorivera33c32802011-10-19 02:56:15 -0200396 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200397 }
diegorivera33c32802011-10-19 02:56:15 -0200398
Greg Akera769deb2010-11-10 15:47:29 -0600399 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // --------------------------------------------------------------------
403
404 /**
405 * Assign file attachments
406 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300407 * @param string $filename
408 * @param string $disposition = 'attachment'
409 * @param string $newname = NULL
410 * @param string $mime = ''
Andrey Andreev081c9462012-03-01 12:58:11 +0200411 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200413 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300415 $this->_attachments[] = array(
416 'name' => array($filename, $newname),
417 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
418 'type' => $mime
419 );
420
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 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300638 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300640 elseif ($this->mailtype === 'text' && count($this->_attachments) > 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
GDmac9bea4be2012-10-30 06:14:19 +0100774 // Reduce multiple spaces
775 $str = preg_replace('| +|', ' ', $str);
776
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300777 return ($this->wordwrap)
778 ? $this->word_wrap($body, 76)
779 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 // --------------------------------------------------------------------
783
784 /**
785 * Word Wrap
786 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300788 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 * @return string
790 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300791 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300793 // Set the character limit, if not already present
794 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300796 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // 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
GDmac9bea4be2012-10-30 06:14:19 +0100805 // Reduce multiple spaces at end of line
806 $str = preg_replace('| +\n|', "\n", $str);
807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 // If the current word is surrounded by {unwrap} tags we'll
809 // strip the entire chunk and replace it with a marker.
810 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300811 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200813 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200815 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300816 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 }
818 }
819
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000820 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300822 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $str = wordwrap($str, $charlim, "\n", FALSE);
824
825 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300826 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 foreach (explode("\n", $str) as $line)
828 {
829 // Is the line within the allowed character count?
830 // If so we'll join it to the output and continue
831 if (strlen($line) <= $charlim)
832 {
833 $output .= $line.$this->newline;
834 continue;
835 }
836
837 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200838 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
840 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300841 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
843 break;
844 }
845
846 // Trim the word down
847 $temp .= substr($line, 0, $charlim-1);
848 $line = substr($line, $charlim-1);
849 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200850 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000851
852 // If $temp contains data it means we had to split up an over-length
853 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100854 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200856 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
858
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200859 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
861
862 // Put our markers back
863 if (count($unwrap) > 0)
864 {
865 foreach ($unwrap as $key => $val)
866 {
Andrey Andreev56454792012-05-17 14:32:19 +0300867 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
869 }
870
871 return $output;
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
875
876 /**
877 * Build final headers
878 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 * @return string
880 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600881 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700883 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
884 $this->set_header('X-Mailer', $this->useragent);
885 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
886 $this->set_header('Message-ID', $this->_get_message_id());
887 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
891
892 /**
893 * Write Headers as a string
894 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @return void
896 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600897 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200899 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
901 $this->_subject = $this->_headers['Subject'];
902 unset($this->_headers['Subject']);
903 }
904
905 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300906 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000907
Pascal Kriete14287f32011-02-14 13:39:34 -0500908 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 $val = trim($val);
911
Alex Bilbied261b1e2012-06-02 11:12:16 +0100912 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev56454792012-05-17 14:32:19 +0300914 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916 }
917
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200918 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Derek Jones1d890882009-02-10 20:32:14 +0000920 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
925
926 /**
927 * Build Final Body and attachments
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @return void
930 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600931 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200933 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
935 $this->_body = $this->word_wrap($this->_body);
936 }
937
938 $this->_set_boundaries();
939 $this->_write_headers();
940
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200941 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500942 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000943
944 switch ($this->_get_content_type())
945 {
946 case 'plain' :
947
Andrey Andreev56454792012-05-17 14:32:19 +0300948 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
949 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000950
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200951 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 $this->_header_str .= $hdr;
954 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Brandon Jones485d7412010-11-09 16:38:17 -0500956 else
957 {
958 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
959 }
Eric Barnes6113f542010-12-29 13:36:12 -0500960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 return;
962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 case 'html' :
964
965 if ($this->send_multipart === FALSE)
966 {
Andrey Andreev56454792012-05-17 14:32:19 +0300967 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
968 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
970 else
971 {
Andrey Andreev56454792012-05-17 14:32:19 +0300972 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000973
Andrey Andreev56454792012-05-17 14:32:19 +0300974 $body .= $this->_get_mime_message().$this->newline.$this->newline
975 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000976
Andrey Andreev56454792012-05-17 14:32:19 +0300977 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
978 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
979 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500980
Andrey Andreev56454792012-05-17 14:32:19 +0300981 .'Content-Type: text/html; charset='.$this->charset.$this->newline
982 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Eric Barnes6113f542010-12-29 13:36:12 -0500984
Andrey Andreev56454792012-05-17 14:32:19 +0300985 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500986
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200987 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
989 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500990 }
991 else
992 {
Andrey Andreev56454792012-05-17 14:32:19 +0300993 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 if ($this->send_multipart !== FALSE)
997 {
Andrey Andreev56454792012-05-17 14:32:19 +0300998 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
1000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 return;
1002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 case 'plain-attach' :
1004
Andrey Andreev56454792012-05-17 14:32:19 +03001005 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001006
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001007 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
1009 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001010 }
1011
Andrey Andreev56454792012-05-17 14:32:19 +03001012 $body .= $this->_get_mime_message().$this->newline.$this->newline
1013 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
Andrey Andreev56454792012-05-17 14:32:19 +03001015 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1016 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001017
Andrey Andreev56454792012-05-17 14:32:19 +03001018 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001019
1020 break;
1021 case 'html-attach' :
1022
Andrey Andreev56454792012-05-17 14:32:19 +03001023 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001024
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001025 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
1027 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 }
1029
Andrey Andreev56454792012-05-17 14:32:19 +03001030 $body .= $this->_get_mime_message().$this->newline.$this->newline
1031 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev56454792012-05-17 14:32:19 +03001033 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1034 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001035
Andrey Andreev56454792012-05-17 14:32:19 +03001036 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1037 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1038 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001039
Andrey Andreev56454792012-05-17 14:32:19 +03001040 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1041 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001042
Andrey Andreev56454792012-05-17 14:32:19 +03001043 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1044 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001045
1046 break;
1047 }
1048
1049 $attachment = array();
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001050 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001052 $filename = $this->_attachments[$i]['name'][0];
1053 $basename = is_null($this->_attachments[$i]['name'][1])
1054 ? basename($filename) : $this->_attachments[$i]['name'][1];
1055 $ctype = $this->_attachments[$i]['type'];
Matteo Mattei5688d582012-03-13 19:29:29 +01001056 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001057
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001058 if ($ctype === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001060 if ( ! file_exists($filename))
1061 {
1062 $this->_set_error_message('lang:email_attachment_missing', $filename);
1063 return FALSE;
1064 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001065
Matteo Mattei5688d582012-03-13 19:29:29 +01001066 $file = filesize($filename) +1;
1067
1068 if ( ! $fp = fopen($filename, FOPEN_READ))
1069 {
1070 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1071 return FALSE;
1072 }
1073
Matteo Matteic3b36f42012-03-26 10:27:17 +02001074 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001075 $file_content = fread($fp, $file);
1076 fclose($fp);
1077 }
1078 else
1079 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001080 $file_content =& $this->_attachments[$i]['name'][0];
Matteo Mattei5688d582012-03-13 19:29:29 +01001081 }
Andrey Andreev56454792012-05-17 14:32:19 +03001082
1083 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1084 .'Content-type: '.$ctype.'; '
1085 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001086 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001087 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001088
Matteo Mattei5688d582012-03-13 19:29:29 +01001089 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
1091
Andrey Andreev56454792012-05-17 14:32:19 +03001092 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1093 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 return;
1095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 // --------------------------------------------------------------------
1098
1099 /**
1100 * Prep Quoted Printable
1101 *
1102 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1103 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1104 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @return string
1107 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001108 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001110 // We are intentionally wrapping so mail servers will encode characters
1111 // properly and MUAs will behave, so {unwrap} must go!
1112 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1113
Andrey Andreev683b34d2012-10-09 15:00:00 +03001114 // RFC 2045 specifies CRLF as "\r\n".
1115 // However, many developers choose to override that and violate
1116 // the RFC rules due to (apparently) a bug in MS Exchange,
1117 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001118 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001119 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001120 if (is_php('5.3'))
1121 {
1122 return quoted_printable_encode($str);
1123 }
1124 elseif (function_exists('imap_8bit'))
1125 {
1126 return imap_8bit($str);
1127 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001128 }
1129
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001131 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001132
1133 // Standardize newlines
1134 if (strpos($str, "\r") !== FALSE)
1135 {
1136 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1137 }
1138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 $escape = '=';
1140 $output = '';
1141
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001142 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
1144 $length = strlen($line);
1145 $temp = '';
1146
1147 // Loop through each character in the line to add soft-wrap
1148 // characters at the end of a line " =\r\n" and add the newly
1149 // processed line(s) to the output (see comment on $crlf class property)
1150 for ($i = 0; $i < $length; $i++)
1151 {
1152 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001153 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $ascii = ord($char);
1155
1156 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001157 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001159 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001161 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001163 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
1166 // If we're at the character limit, add the line to the output,
1167 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001168 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
1170 $output .= $temp.$escape.$this->crlf;
1171 $temp = '';
1172 }
1173
1174 // Add the character to our temporary line
1175 $temp .= $char;
1176 }
1177
1178 // Add our completed line to the output
1179 $output .= $temp.$this->crlf;
1180 }
1181
1182 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001183 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 }
1185
1186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001187
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 /**
1189 * Prep Q Encoding
1190 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001191 * Performs "Q Encoding" on a string for use in email headers.
1192 * It's related but not identical to quoted-printable, so it has its
1193 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001195 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001196 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001198 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001200 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001201
Andrey Andreev925dd902012-10-19 11:06:31 +03001202 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001204 if (MB_ENABLED === TRUE)
1205 {
1206 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1207 }
1208 elseif (extension_loaded('iconv'))
1209 {
1210 $output = @iconv_mime_encode('', $str,
1211 array(
1212 'scheme' => 'Q',
1213 'line-length' => 76,
1214 'input-charset' => $this->charset,
1215 'output-charset' => $this->charset,
1216 'line-break-chars' => $this->crlf
1217 )
1218 );
1219
1220 // There are reports that iconv_mime_encode() might fail and return FALSE
1221 if ($output !== FALSE)
1222 {
1223 // iconv_mime_encode() will always put a header field name.
1224 // We've passed it an empty one, but it still prepends our
1225 // encoded string with ': ', so we need to strip it.
1226 return substr($output, 2);
1227 }
1228
1229 $chars = iconv_strlen($str, 'UTF-8');
1230 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232
Andrey Andreev925dd902012-10-19 11:06:31 +03001233 // We might already have this set for UTF-8
1234 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001235
Andrey Andreev925dd902012-10-19 11:06:31 +03001236 $output = '=?'.$this->charset.'?Q?';
1237 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001239 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1240 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1241 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001242
Andrey Andreev925dd902012-10-19 11:06:31 +03001243 // RFC 2045 sets a limit of 76 characters per line.
1244 // We'll append ?= to the end of each line though.
1245 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001247 $output .= '?='.$this->crlf // EOL
1248 .' =?'.$this->charset.'?Q?'.$chr; // New line
1249 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001251 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001253 $output .= $chr;
1254 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 }
1257
Andrey Andreev925dd902012-10-19 11:06:31 +03001258 // End the header
1259 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 }
1261
1262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 /**
1265 * Send Email
1266 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001267 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 * @return bool
1269 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001270 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001272 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
1274 $this->reply_to($this->_headers['From']);
1275 }
1276
Andrey Andreev76f15c92012-03-01 13:05:07 +02001277 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1278 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1279 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
patworkb0707982011-04-08 15:10:05 +02001281 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 return FALSE;
1283 }
1284
1285 $this->_build_headers();
1286
Andrey Andreev76f15c92012-03-01 13:05:07 +02001287 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001289 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001290
Alex Bilbiea87aab32012-07-30 09:50:37 +01001291 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001292 {
1293 $this->clear();
1294 }
1295
Alex Bilbieb901e732012-07-30 09:44:57 +01001296 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 }
1298
1299 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001300 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001301
Alex Bilbiea87aab32012-07-30 09:50:37 +01001302 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001303 {
1304 $this->clear();
1305 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001306
Alex Bilbieb901e732012-07-30 09:44:57 +01001307 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // --------------------------------------------------------------------
1311
1312 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001313 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001315 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001317 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001319 $float = $this->bcc_batch_size - 1;
1320 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 $chunk = array();
1322
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001323 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 if (isset($this->_bcc_array[$i]))
1326 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001327 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 }
1329
Alex Bilbied261b1e2012-06-02 11:12:16 +01001330 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
1332 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001333 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001334 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 }
1336
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001337 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 {
1339 $chunk[] = substr($set, 1);
1340 }
1341 }
1342
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001343 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 {
1345 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001346
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001347 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001348
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001349 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001351 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 }
1353 else
1354 {
1355 $this->_bcc_array = $bcc;
1356 }
1357
1358 $this->_build_message();
1359 $this->_spool_email();
1360 }
1361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 // --------------------------------------------------------------------
1364
1365 /**
1366 * Unwrap special elements
1367 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * @return void
1369 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001370 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreev56454792012-05-17 14:32:19 +03001372 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 }
Barry Mienydd671972010-10-04 16:33:58 +02001374
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 // --------------------------------------------------------------------
1376
1377 /**
1378 * Strip line-breaks via callback
1379 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001380 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 * @return string
1382 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001383 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
1385 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1386 {
1387 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1388 }
1389
1390 return $matches[1];
1391 }
Barry Mienydd671972010-10-04 16:33:58 +02001392
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 // --------------------------------------------------------------------
1394
1395 /**
1396 * Spool mail to the mail server
1397 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 * @return bool
1399 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001400 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 {
1402 $this->_unwrap_specials();
1403
Andrey Andreev56454792012-05-17 14:32:19 +03001404 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001405 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 {
Andrey Andreev56454792012-05-17 14:32:19 +03001407 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001408 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 }
1410
patworkb0707982011-04-08 15:10:05 +02001411 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 return TRUE;
1413 }
Barry Mienydd671972010-10-04 16:33:58 +02001414
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 // --------------------------------------------------------------------
1416
1417 /**
1418 * Send using mail()
1419 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 * @return bool
1421 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001422 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001424 if (is_array($this->_recipients))
1425 {
1426 $this->_recipients = implode(', ', $this->_recipients);
1427 }
1428
Alex Bilbied261b1e2012-06-02 11:12:16 +01001429 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001431 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 }
1433 else
1434 {
1435 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1436 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001437 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 +00001438 }
1439 }
Barry Mienydd671972010-10-04 16:33:58 +02001440
Derek Allard2067d1a2008-11-13 22:59:24 +00001441 // --------------------------------------------------------------------
1442
1443 /**
1444 * Send using Sendmail
1445 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 * @return bool
1447 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001448 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 {
Melounek58dfc082012-06-29 08:43:47 +02001450 $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 +00001451
Derek Jones4cefaa42009-04-29 19:13:56 +00001452 if ($fp === FALSE OR $fp === NULL)
1453 {
1454 // server probably has popen disabled, so nothing we can do to get a verbose error.
1455 return FALSE;
1456 }
Derek Jones71141ce2010-03-02 16:41:20 -06001457
Derek Jonesc630bcf2008-11-17 21:09:45 +00001458 fputs($fp, $this->_header_str);
1459 fputs($fp, $this->_finalbody);
1460
Barry Mienydd671972010-10-04 16:33:58 +02001461 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001462
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001463 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
patworkb0707982011-04-08 15:10:05 +02001465 $this->_set_error_message('lang:email_exit_status', $status);
1466 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 return FALSE;
1468 }
1469
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 return TRUE;
1471 }
Barry Mienydd671972010-10-04 16:33:58 +02001472
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 // --------------------------------------------------------------------
1474
1475 /**
1476 * Send using SMTP
1477 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 * @return bool
1479 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001480 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001482 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 {
patworkb0707982011-04-08 15:10:05 +02001484 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 return FALSE;
1486 }
1487
Diogo Osório593f7982012-03-02 18:04:17 +00001488 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001489 {
1490 return FALSE;
1491 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001492
1493 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1494
Pascal Kriete14287f32011-02-14 13:39:34 -05001495 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 {
1497 $this->_send_command('to', $val);
1498 }
1499
1500 if (count($this->_cc_array) > 0)
1501 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001502 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001504 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 {
1506 $this->_send_command('to', $val);
1507 }
1508 }
1509 }
1510
1511 if (count($this->_bcc_array) > 0)
1512 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001513 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001515 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 {
1517 $this->_send_command('to', $val);
1518 }
1519 }
1520 }
1521
1522 $this->_send_command('data');
1523
1524 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001525 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001526
1527 $this->_send_data('.');
1528
1529 $reply = $this->_get_smtp_data();
1530
1531 $this->_set_error_message($reply);
1532
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001533 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
patworkb0707982011-04-08 15:10:05 +02001535 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 return FALSE;
1537 }
1538
1539 $this->_send_command('quit');
1540 return TRUE;
1541 }
Barry Mienydd671972010-10-04 16:33:58 +02001542
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 // --------------------------------------------------------------------
1544
1545 /**
1546 * SMTP Connect
1547 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 * @return string
1549 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001550 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001552 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001553
Radu Potopbbf04b02011-09-28 13:57:51 +03001554 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001555 $this->smtp_port,
1556 $errno,
1557 $errstr,
1558 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001559
Pascal Kriete14287f32011-02-14 13:39:34 -05001560 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 {
Andrey Andreev56454792012-05-17 14:32:19 +03001562 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 return FALSE;
1564 }
1565
1566 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001567
Alex Bilbied261b1e2012-06-02 11:12:16 +01001568 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001569 {
1570 $this->_send_command('hello');
1571 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001572
Radu Potop4c589ae2011-09-29 10:19:55 +03001573 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001574
Sean Fishere862ddd2011-10-26 22:45:00 -03001575 if ($crypto !== TRUE)
1576 {
1577 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1578 return FALSE;
1579 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001580 }
1581
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 return $this->_send_command('hello');
1583 }
Barry Mienydd671972010-10-04 16:33:58 +02001584
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 // --------------------------------------------------------------------
1586
1587 /**
1588 * Send SMTP command
1589 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 * @param string
1591 * @param string
1592 * @return string
1593 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001594 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 {
1596 switch ($cmd)
1597 {
1598 case 'hello' :
1599
Andrey Andreev56454792012-05-17 14:32:19 +03001600 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1601 {
1602 $this->_send_data('EHLO '.$this->_get_hostname());
1603 }
1604 else
1605 {
1606 $this->_send_data('HELO '.$this->_get_hostname());
1607 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001608
1609 $resp = 250;
1610 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001611 case 'starttls' :
1612
1613 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001614 $resp = 220;
1615 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 case 'from' :
1617
1618 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 $resp = 250;
1620 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001621 case 'to' :
1622
leandronf7686c122012-03-22 08:07:31 -03001623 if ($this->dsn)
1624 {
leandronfb4552942012-03-21 23:54:26 -03001625 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001626 }
leandronfb4552942012-03-21 23:54:26 -03001627 else
leandronf7686c122012-03-22 08:07:31 -03001628 {
leandronfb4552942012-03-21 23:54:26 -03001629 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001630 }
Andrey Andreev56454792012-05-17 14:32:19 +03001631
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 $resp = 250;
1633 break;
1634 case 'data' :
1635
1636 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 $resp = 354;
1638 break;
1639 case 'quit' :
1640
1641 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 $resp = 221;
1643 break;
1644 }
1645
1646 $reply = $this->_get_smtp_data();
1647
Andrey Andreev56454792012-05-17 14:32:19 +03001648 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001649
Andrey Andreevba261762012-06-11 14:11:35 +03001650 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
patworkb0707982011-04-08 15:10:05 +02001652 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 return FALSE;
1654 }
1655
Alex Bilbied261b1e2012-06-02 11:12:16 +01001656 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
1658 fclose($this->_smtp_connect);
1659 }
1660
1661 return TRUE;
1662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 // --------------------------------------------------------------------
1665
1666 /**
vkeranov7e4356b2012-10-27 18:41:00 +03001667 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 * @return bool
1670 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001671 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 {
1673 if ( ! $this->_smtp_auth)
1674 {
1675 return TRUE;
1676 }
1677
Alex Bilbied261b1e2012-06-02 11:12:16 +01001678 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 {
patworkb0707982011-04-08 15:10:05 +02001680 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 return FALSE;
1682 }
1683
1684 $this->_send_data('AUTH LOGIN');
1685
1686 $reply = $this->_get_smtp_data();
1687
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001688 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 {
patworkb0707982011-04-08 15:10:05 +02001690 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 return FALSE;
1692 }
1693
1694 $this->_send_data(base64_encode($this->smtp_user));
1695
1696 $reply = $this->_get_smtp_data();
1697
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001698 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 {
patworkb0707982011-04-08 15:10:05 +02001700 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 return FALSE;
1702 }
1703
1704 $this->_send_data(base64_encode($this->smtp_pass));
1705
1706 $reply = $this->_get_smtp_data();
1707
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001708 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 {
patworkb0707982011-04-08 15:10:05 +02001710 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 return FALSE;
1712 }
1713
1714 return TRUE;
1715 }
Barry Mienydd671972010-10-04 16:33:58 +02001716
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 // --------------------------------------------------------------------
1718
1719 /**
1720 * Send SMTP data
1721 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001722 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 * @return bool
1724 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001725 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001727 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 {
patworkb0707982011-04-08 15:10:05 +02001729 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 return FALSE;
1731 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001732
1733 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 }
Barry Mienydd671972010-10-04 16:33:58 +02001735
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 // --------------------------------------------------------------------
1737
1738 /**
1739 * Get SMTP data
1740 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 * @return string
1742 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001743 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 {
Andrey Andreev56454792012-05-17 14:32:19 +03001745 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001746
1747 while ($str = fgets($this->_smtp_connect, 512))
1748 {
1749 $data .= $str;
1750
Alex Bilbied261b1e2012-06-02 11:12:16 +01001751 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
1753 break;
1754 }
1755 }
1756
1757 return $data;
1758 }
Barry Mienydd671972010-10-04 16:33:58 +02001759
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 // --------------------------------------------------------------------
1761
1762 /**
1763 * Get Hostname
1764 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 * @return string
1766 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001767 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001769 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 // --------------------------------------------------------------------
1773
1774 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 * Get Debug Message
1776 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 * @return string
1778 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001779 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 {
1781 $msg = '';
1782
1783 if (count($this->_debug_msg) > 0)
1784 {
1785 foreach ($this->_debug_msg as $val)
1786 {
1787 $msg .= $val;
1788 }
1789 }
1790
Andrey Andreev59c5b532012-03-01 14:09:51 +02001791 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Set Message
1798 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001799 * @param string $msg
1800 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02001801 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001803 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 {
1805 $CI =& get_instance();
1806 $CI->lang->load('email');
1807
Andrey Andreev76f15c92012-03-01 13:05:07 +02001808 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
Andrey Andreev56454792012-05-17 14:32:19 +03001810 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
1812 else
1813 {
Andrey Andreev56454792012-05-17 14:32:19 +03001814 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 }
1816 }
Barry Mienydd671972010-10-04 16:33:58 +02001817
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 // --------------------------------------------------------------------
1819
1820 /**
1821 * Mime Types
1822 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 * @param string
1824 * @return string
1825 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001826 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001828 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001829
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001830 $ext = strtolower($ext);
1831
1832 if ( ! is_array($mimes))
1833 {
1834 $mimes =& get_mimes();
1835 }
1836
1837 if (isset($mimes[$ext]))
1838 {
1839 return is_array($mimes[$ext])
1840 ? current($mimes[$ext])
1841 : $mimes[$ext];
1842 }
1843
1844 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 }
1846
1847}
Derek Allard2067d1a2008-11-13 22:59:24 +00001848
1849/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001850/* Location: ./system/libraries/Email.php */