blob: aec4957dea223bec977f89a976e6377069f979e3 [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.
Andrey Andreev1bd3d882011-12-22 15:38:20 +020062 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 +020063 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020064 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 +020065
66 protected $_safe_mode = FALSE;
67 protected $_subject = '';
68 protected $_body = '';
69 protected $_finalbody = '';
70 protected $_alt_boundary = '';
71 protected $_atc_boundary = '';
72 protected $_header_str = '';
73 protected $_smtp_connect = '';
74 protected $_encoding = '8bit';
75 protected $_IP = FALSE;
76 protected $_smtp_auth = FALSE;
77 protected $_replyto_flag = FALSE;
78 protected $_debug_msg = array();
79 protected $_recipients = array();
80 protected $_cc_array = array();
81 protected $_bcc_array = array();
82 protected $_headers = array();
83 protected $_attach_name = array();
84 protected $_attach_type = array();
85 protected $_attach_disp = array();
Matteo Mattei5688d582012-03-13 19:29:29 +010086 protected $_attach_source = array();
87 protected $_attach_content = array();
Andrey Andreev50814092012-03-01 12:36:12 +020088 protected $_protocols = array('mail', 'sendmail', 'smtp');
89 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
90 protected $_bit_depths = array('7bit', '8bit');
91 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 /**
94 * Constructor - Sets Email Preferences
95 *
96 * The constructor can be passed an array of config values
97 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000098 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
100 if (count($config) > 0)
101 {
102 $this->initialize($config);
103 }
104 else
105 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200106 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200107 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
Andrey Andreev59c5b532012-03-01 14:09:51 +0200110 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Initialize preferences
117 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * @param array
119 * @return void
120 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000121 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 foreach ($config as $key => $val)
124 {
125 if (isset($this->$key))
126 {
127 $method = 'set_'.$key;
128
129 if (method_exists($this, $method))
130 {
131 $this->$method($val);
132 }
133 else
134 {
135 $this->$key = $val;
136 }
137 }
138 }
Eric Barnes6113f542010-12-29 13:36:12 -0500139 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000140
Andrey Andreev76f15c92012-03-01 13:05:07 +0200141 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200142 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000143
144 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
148
149 /**
150 * Initialize the Email Data
151 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800152 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200153 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000155 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200157 $this->_subject = '';
158 $this->_body = '';
159 $this->_finalbody = '';
160 $this->_header_str = '';
161 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500163 $this->_cc_array = array();
164 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 $this->_headers = array();
166 $this->_debug_msg = array();
167
168 $this->_set_header('User-Agent', $this->useragent);
169 $this->_set_header('Date', $this->_set_date());
170
171 if ($clear_attachments !== FALSE)
172 {
173 $this->_attach_name = array();
174 $this->_attach_type = array();
175 $this->_attach_disp = array();
Matteo Mattei5688d582012-03-13 19:29:29 +0100176 $this->_attach_source = array();
177 $this->_attach_content = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Eric Barnes6113f542010-12-29 13:36:12 -0500179
Greg Akera769deb2010-11-10 15:47:29 -0600180 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Set FROM
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string
189 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200190 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000192 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200194 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200196 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 if ($this->validate)
200 {
201 $this->validate_email($this->_str_to_array($from));
202 }
203
204 // prepare the display name
205 if ($name != '')
206 {
207 // only use Q encoding if there are characters that would require it
208 if ( ! preg_match('/[\200-\377]/', $name))
209 {
210 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000211 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213 else
214 {
215 $name = $this->_prep_q_encoding($name, TRUE);
216 }
217 }
218
219 $this->_set_header('From', $name.' <'.$from.'>');
220 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500221
Greg Akera769deb2010-11-10 15:47:29 -0600222 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Set Reply-to
229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string
231 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200232 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000234 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200236 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200238 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240
241 if ($this->validate)
242 {
243 $this->validate_email($this->_str_to_array($replyto));
244 }
245
246 if ($name == '')
247 {
248 $name = $replyto;
249 }
250
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200251 if (strncmp($name, '"', 1) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 $name = '"'.$name.'"';
254 }
255
256 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
257 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600258
259 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * Set Recipients
266 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200268 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000270 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 $to = $this->_str_to_array($to);
273 $to = $this->clean_email($to);
274
275 if ($this->validate)
276 {
277 $this->validate_email($to);
278 }
279
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200280 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200282 $this->_set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 switch ($this->_get_protocol())
286 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200287 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000288 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200290 case 'sendmail':
291 case 'mail':
292 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 break;
294 }
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 {
309 $cc = $this->_str_to_array($cc);
310 $cc = $this->clean_email($cc);
311
312 if ($this->validate)
313 {
314 $this->validate_email($cc);
315 }
316
Andrey Andreev59c5b532012-03-01 14:09:51 +0200317 $this->_set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200319 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->_cc_array = $cc;
322 }
Eric Barnes6113f542010-12-29 13:36:12 -0500323
Greg Akera769deb2010-11-10 15:47:29 -0600324 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Set BCC
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string
333 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200334 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000336 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 if ($limit != '' && is_numeric($limit))
339 {
340 $this->bcc_batch_mode = TRUE;
341 $this->bcc_batch_size = $limit;
342 }
343
344 $bcc = $this->_str_to_array($bcc);
345 $bcc = $this->clean_email($bcc);
346
347 if ($this->validate)
348 {
349 $this->validate_email($bcc);
350 }
351
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200352 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 $this->_bcc_array = $bcc;
355 }
356 else
357 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200358 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Eric Barnes6113f542010-12-29 13:36:12 -0500360
Greg Akera769deb2010-11-10 15:47:29 -0600361 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
365
366 /**
367 * Set Email Subject
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200370 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000372 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $subject = $this->_prep_q_encoding($subject);
375 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600376 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
380
381 /**
382 * Set Body
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200385 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000387 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200389 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200390
Andrey Andreevaf728622011-10-20 10:11:59 +0300391 /* strip slashes only if magic quotes is ON
392 if we do it with magic quotes OFF, it strips real, user-inputted chars.
393
394 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
395 it will probably not exist in future versions at all.
396 */
397 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200398 {
diegorivera33c32802011-10-19 02:56:15 -0200399 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200400 }
diegorivera33c32802011-10-19 02:56:15 -0200401
Greg Akera769deb2010-11-10 15:47:29 -0600402 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // --------------------------------------------------------------------
406
407 /**
408 * Assign file attachments
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200411 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
trit10eb14d2011-11-23 15:48:21 -0500413 public function attach($filename, $disposition = '', $newname = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
trit151fc682011-11-23 07:30:06 -0500415 $this->_attach_name[] = array($filename, $newname);
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600416 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
tritd5269982011-11-23 17:22:44 -0500417 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Mattei5688d582012-03-13 19:29:29 +0100418 $this->_attach_source[] = 'file';
419 return $this;
420 }
421
422 /**
423 * Assign string attachments
424 *
425 * @param string
426 * @return object
427 */
428 public function string_attach($str, $filename, $mime, $disposition = 'attachment')
429 {
430 $this->_attach_name[] = $filename;
431 $this->_attach_type[] = $mime;
432 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
433 $this->_attach_source[] = 'string';
434 $this->_attach_content[] = $str;
Greg Akera769deb2010-11-10 15:47:29 -0600435 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Add a Header Item
442 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @param string
444 * @param string
445 * @return void
446 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600447 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 $this->_headers[$header] = $value;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Convert a String to an Array
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param string
458 * @return array
459 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600460 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 if ( ! is_array($email))
463 {
464 if (strpos($email, ',') !== FALSE)
465 {
466 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
467 }
468 else
469 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200470 $email = (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472 }
473 return $email;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Set Multipart Value
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200482 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000484 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400486 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600487 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
491
492 /**
493 * Set Mailtype
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200496 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000498 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600501 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
505
506 /**
507 * Set Wordwrap
508 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400509 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200510 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000512 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400514 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600515 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Set Protocol
522 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200524 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000526 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200528 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600529 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // --------------------------------------------------------------------
533
534 /**
535 * Set Priority
536 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200537 * @param int
538 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000540 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200542 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600543 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
547
548 /**
549 * Set Newline Character
550 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200552 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000554 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200556 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600557 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // --------------------------------------------------------------------
561
562 /**
563 * Set CRLF
564 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200566 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000568 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200570 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600571 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // --------------------------------------------------------------------
575
576 /**
577 * Set Message Boundary
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @return void
580 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600581 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200583 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
584 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // --------------------------------------------------------------------
588
589 /**
590 * Get the Message ID
591 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @return string
593 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600594 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200596 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200597 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // --------------------------------------------------------------------
601
602 /**
603 * Get Mail Protocol
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200606 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600608 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200611 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000612
613 if ($return == TRUE)
614 {
615 return $this->protocol;
616 }
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
622 * Get Mail Encoding
623 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param bool
625 * @return string
626 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600627 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200629 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000630
631 foreach ($this->_base_charsets as $charset)
632 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200633 if (strncmp($charset, $this->charset, strlen($charset)) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 $this->_encoding = '7bit';
636 }
637 }
638
639 if ($return == TRUE)
640 {
641 return $this->_encoding;
642 }
643 }
644
645 // --------------------------------------------------------------------
646
647 /**
648 * Get content type (text/html/attachment)
649 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 * @return string
651 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600652 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200654 if ($this->mailtype === 'html' && count($this->_attach_name) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 return 'html';
657 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200658 elseif ($this->mailtype === 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return 'html-attach';
661 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200662 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 return 'plain-attach';
665 }
666 else
667 {
668 return 'plain';
669 }
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // --------------------------------------------------------------------
673
674 /**
675 * Set RFC 822 Date
676 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 * @return string
678 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600679 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200681 $timezone = date('Z');
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200682 $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200684 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000685
Andrey Andreev59c5b532012-03-01 14:09:51 +0200686 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
692 * Mime message
693 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 * @return string
695 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600696 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200698 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 +0000699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Validate Email Address
705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param string
707 * @return bool
708 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000709 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 if ( ! is_array($email))
712 {
patworkb0707982011-04-08 15:10:05 +0200713 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 return FALSE;
715 }
716
717 foreach ($email as $val)
718 {
719 if ( ! $this->valid_email($val))
720 {
patworkb0707982011-04-08 15:10:05 +0200721 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 return FALSE;
723 }
724 }
725
726 return TRUE;
727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 // --------------------------------------------------------------------
730
731 /**
732 * Email Validation
733 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 * @param string
735 * @return bool
736 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000737 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200739 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $address);
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 // --------------------------------------------------------------------
743
744 /**
745 * Clean Extended Email Address: Joe Smith <joe@smith.com>
746 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * @param string
748 * @return string
749 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000750 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
752 if ( ! is_array($email))
753 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200754 return (preg_match('/\<(.*)\>/', $email, $match)) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756
757 $clean_email = array();
758
759 foreach ($email as $addy)
760 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200761 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763
764 return $clean_email;
765 }
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 // --------------------------------------------------------------------
768
769 /**
770 * Build alternative plain text message
771 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000772 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 * in plain-text headers of HTML-formatted emails.
774 * If the user hasn't specified his own alternative message
775 * it creates one by stripping the HTML
776 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 * @return string
778 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600779 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200781 if ($this->alt_message != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
783 return $this->word_wrap($this->alt_message, '76');
784 }
785
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200786 $body = (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match)) ? $match[1] : $this->_body;
787 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 for ($i = 20; $i >= 3; $i--)
790 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200791 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
793
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200794 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 // --------------------------------------------------------------------
798
799 /**
800 * Word Wrap
801 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200803 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 * @return string
805 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000806 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
808 // Se the character limit
809 if ($charlim == '')
810 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200811 $charlim = ($this->wrapchars == "") ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813
814 // Reduce multiple spaces
815 $str = preg_replace("| +|", " ", $str);
816
817 // Standardize newlines
818 if (strpos($str, "\r") !== FALSE)
819 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200820 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
822
823 // If the current word is surrounded by {unwrap} tags we'll
824 // strip the entire chunk and replace it with a marker.
825 $unwrap = array();
826 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
827 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200828 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200830 $unwrap[] = $matches[1][$i];
831 $str = str_replace($matches[1][$i], "{{unwrapped".$i."}}", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
833 }
834
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000835 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500837 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 $str = wordwrap($str, $charlim, "\n", FALSE);
839
840 // Split the string into individual lines of text and cycle through them
841 $output = "";
842 foreach (explode("\n", $str) as $line)
843 {
844 // Is the line within the allowed character count?
845 // If so we'll join it to the output and continue
846 if (strlen($line) <= $charlim)
847 {
848 $output .= $line.$this->newline;
849 continue;
850 }
851
852 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200853 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 // If the over-length word is a URL we won't wrap it
856 if (preg_match("!\[url.+\]|://|wwww.!", $line))
857 {
858 break;
859 }
860
861 // Trim the word down
862 $temp .= substr($line, 0, $charlim-1);
863 $line = substr($line, $charlim-1);
864 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200865 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000866
867 // If $temp contains data it means we had to split up an over-length
868 // word into smaller chunks so we'll add it back to our current line
869 if ($temp != '')
870 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200871 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
873
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200874 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
876
877 // Put our markers back
878 if (count($unwrap) > 0)
879 {
880 foreach ($unwrap as $key => $val)
881 {
882 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
883 }
884 }
885
886 return $output;
887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * Build final headers
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @return string
895 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600896 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
898 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
899 $this->_set_header('X-Mailer', $this->useragent);
900 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
901 $this->_set_header('Message-ID', $this->_get_message_id());
902 $this->_set_header('Mime-Version', '1.0');
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
906
907 /**
908 * Write Headers as a string
909 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @return void
911 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600912 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200914 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 $this->_subject = $this->_headers['Subject'];
917 unset($this->_headers['Subject']);
918 }
919
920 reset($this->_headers);
921 $this->_header_str = "";
922
Pascal Kriete14287f32011-02-14 13:39:34 -0500923 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 $val = trim($val);
926
927 if ($val != "")
928 {
929 $this->_header_str .= $key.": ".$val.$this->newline;
930 }
931 }
932
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200933 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Derek Jones1d890882009-02-10 20:32:14 +0000935 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 }
937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 // --------------------------------------------------------------------
940
941 /**
942 * Build Final Body and attachments
943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @return void
945 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600946 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200948 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $this->_body = $this->word_wrap($this->_body);
951 }
952
953 $this->_set_boundaries();
954 $this->_write_headers();
955
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200956 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500957 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000958
959 switch ($this->_get_content_type())
960 {
961 case 'plain' :
962
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200963 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline
964 . "Content-Transfer-Encoding: " . $this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200966 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $this->_header_str .= $hdr;
969 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Brandon Jones485d7412010-11-09 16:38:17 -0500971 else
972 {
973 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
974 }
Eric Barnes6113f542010-12-29 13:36:12 -0500975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 return;
977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 case 'html' :
979
980 if ($this->send_multipart === FALSE)
981 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200982 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline
983 . "Content-Transfer-Encoding: quoted-printable";
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 }
985 else
986 {
Derek Jonesa45e7612009-02-10 18:33:01 +0000987 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000988
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200989 $body .= $this->_get_mime_message() . $this->newline . $this->newline
990 . "--" . $this->_alt_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000991
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200992 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
993 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
994 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500995
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200996 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
997 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Eric Barnes6113f542010-12-29 13:36:12 -0500999
Brandon Jones485d7412010-11-09 16:38:17 -05001000 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001001
1002
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001003 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
1005 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001006 }
1007 else
1008 {
1009 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
1013 if ($this->send_multipart !== FALSE)
1014 {
Brandon Jones485d7412010-11-09 16:38:17 -05001015 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
1017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 return;
1019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 case 'plain-attach' :
1021
Derek Jonesa45e7612009-02-10 18:33:01 +00001022 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001023
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001024 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
1026 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001027 }
1028
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001029 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1030 . "--" . $this->_atc_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001031
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001032 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
1033 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001035 . $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001036
1037 break;
1038 case 'html-attach' :
1039
Derek Jonesa45e7612009-02-10 18:33:01 +00001040 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001041
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001042 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
1044 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
1046
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001047 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1048 . "--" . $this->_atc_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001049
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001050 . "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline
1051 . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001052
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001053 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
1054 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
1055 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001056
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001057 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
1058 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001059
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001060 . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline
1061 . "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001062
1063 break;
1064 }
1065
1066 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001067 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
tritbc4ac992011-11-23 07:19:41 -05001069 $filename = $this->_attach_name[$i][0];
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001070 $basename = (is_null($this->_attach_name[$i][1])) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001072 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001073
Matteo Mattei5688d582012-03-13 19:29:29 +01001074 if($this->_attach_source[$i] == 'file')
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001076 if ( ! file_exists($filename))
1077 {
1078 $this->_set_error_message('lang:email_attachment_missing', $filename);
1079 return FALSE;
1080 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001081
Matteo Mattei5688d582012-03-13 19:29:29 +01001082 $file = filesize($filename) +1;
1083
1084 if ( ! $fp = fopen($filename, FOPEN_READ))
1085 {
1086 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1087 return FALSE;
1088 }
1089
1090 $file_content = fread($fp, $file);
1091 fclose($fp);
1092 }
1093 else
1094 {
1095 $file_content =& $this->_attach_content[$i];
1096 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001097 $attachment[$z++] = "--".$this->_atc_boundary.$this->newline
1098 . "Content-type: ".$ctype."; "
1099 . "name=\"".$basename."\"".$this->newline
1100 . "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline
1101 . "Content-Transfer-Encoding: base64".$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001102
Matteo Mattei5688d582012-03-13 19:29:29 +01001103 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
1105
Brandon Jones485d7412010-11-09 16:38:17 -05001106 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001107 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr . $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 return;
1109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 // --------------------------------------------------------------------
1112
1113 /**
1114 * Prep Quoted Printable
1115 *
1116 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1117 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001120 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @return string
1122 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001123 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 // Set the character limit
1126 // Don't allow over 76, as that will make servers and MUAs barf
1127 // all over quoted-printable data
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001128 if ($charlim == '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 }
1132
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001133 // Reduce multiple spaces & remove nulls
1134 $str = preg_replace(array("| +|", '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001135
1136 // Standardize newlines
1137 if (strpos($str, "\r") !== FALSE)
1138 {
1139 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1140 }
1141
1142 // We are intentionally wrapping so mail servers will encode characters
1143 // properly and MUAs will behave, so {unwrap} must go!
1144 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 $escape = '=';
1147 $output = '';
1148
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001149 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
1151 $length = strlen($line);
1152 $temp = '';
1153
1154 // Loop through each character in the line to add soft-wrap
1155 // characters at the end of a line " =\r\n" and add the newly
1156 // processed line(s) to the output (see comment on $crlf class property)
1157 for ($i = 0; $i < $length; $i++)
1158 {
1159 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001160 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 $ascii = ord($char);
1162
1163 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001164 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001166 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001168 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001170 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
1172
1173 // If we're at the character limit, add the line to the output,
1174 // reset our temp variable, and keep on chuggin'
1175 if ((strlen($temp) + strlen($char)) >= $charlim)
1176 {
1177 $output .= $temp.$escape.$this->crlf;
1178 $temp = '';
1179 }
1180
1181 // Add the character to our temporary line
1182 $temp .= $char;
1183 }
1184
1185 // Add our completed line to the output
1186 $output .= $temp.$this->crlf;
1187 }
1188
1189 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001190 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 }
1192
1193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 /**
1196 * Prep Q Encoding
1197 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001198 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * but not identical to quoted-printable, so it has its own method
1200 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001201 * @param string
1202 * @param bool set to TRUE for processing From: headers
1203 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001205 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
1207 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1208
1209 // Line length must not exceed 76 characters, so we adjust for
1210 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1211 $limit = 75 - 7 - strlen($this->charset);
1212
1213 // these special characters must be converted too
1214 $convert = array('_', '=', '?');
1215
1216 if ($from === TRUE)
1217 {
1218 $convert[] = ',';
1219 $convert[] = ';';
1220 }
1221
1222 $output = '';
1223 $temp = '';
1224
1225 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1226 {
1227 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001228 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 $ascii = ord($char);
1230
1231 // convert ALL non-printable ASCII characters and our specials
1232 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1233 {
1234 $char = '='.dechex($ascii);
1235 }
1236
1237 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001238 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 {
1240 $char = '_';
1241 }
1242
1243 // If we're at the character limit, add the line to the output,
1244 // reset our temp variable, and keep on chuggin'
1245 if ((strlen($temp) + strlen($char)) >= $limit)
1246 {
1247 $output .= $temp.$this->crlf;
1248 $temp = '';
1249 }
1250
1251 // Add the character to our temporary line
1252 $temp .= $char;
1253 }
1254
1255 $str = $output.$temp;
1256
1257 // wrap each line with the shebang, charset, and transfer encoding
1258 // the preceding space on successive lines is required for header "folding"
Andrey Andreev59c5b532012-03-01 14:09:51 +02001259 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 * @return bool
1268 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001269 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
1271 if ($this->_replyto_flag == FALSE)
1272 {
1273 $this->reply_to($this->_headers['From']);
1274 }
1275
Andrey Andreev76f15c92012-03-01 13:05:07 +02001276 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1277 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1278 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
patworkb0707982011-04-08 15:10:05 +02001280 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 return FALSE;
1282 }
1283
1284 $this->_build_headers();
1285
Andrey Andreev76f15c92012-03-01 13:05:07 +02001286 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001288 return $this->batch_bcc_send();
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 }
1290
1291 $this->_build_message();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001292 return $this->_spool_email();
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // --------------------------------------------------------------------
1296
1297 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001298 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001300 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001302 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001304 $float = $this->bcc_batch_size - 1;
1305 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 $chunk = array();
1307
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001308 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
1310 if (isset($this->_bcc_array[$i]))
1311 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001312 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 }
1314
1315 if ($i == $float)
1316 {
1317 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001318 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001319 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 }
1321
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001322 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
1324 $chunk[] = substr($set, 1);
1325 }
1326 }
1327
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001328 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
1330 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001331
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001332 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001333
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001334 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001336 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 }
1338 else
1339 {
1340 $this->_bcc_array = $bcc;
1341 }
1342
1343 $this->_build_message();
1344 $this->_spool_email();
1345 }
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Unwrap special elements
1352 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * @return void
1354 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001355 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
1357 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 // --------------------------------------------------------------------
1361
1362 /**
1363 * Strip line-breaks via callback
1364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 * @return string
1366 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001367 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 {
1369 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1370 {
1371 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1372 }
1373
1374 return $matches[1];
1375 }
Barry Mienydd671972010-10-04 16:33:58 +02001376
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 // --------------------------------------------------------------------
1378
1379 /**
1380 * Spool mail to the mail server
1381 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 * @return bool
1383 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001384 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 {
1386 $this->_unwrap_specials();
1387
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001388 $method = '_send_with_' . $this->_get_protocol();
1389 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001391 $this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001392 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 }
1394
patworkb0707982011-04-08 15:10:05 +02001395 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 return TRUE;
1397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 // --------------------------------------------------------------------
1400
1401 /**
1402 * Send using mail()
1403 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 * @return bool
1405 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001406 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 {
1408 if ($this->_safe_mode == TRUE)
1409 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001410 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 }
1412 else
1413 {
1414 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1415 // we've encountered servers that seem to require it to be in place.
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001416 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
1418 }
Barry Mienydd671972010-10-04 16:33:58 +02001419
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 // --------------------------------------------------------------------
1421
1422 /**
1423 * Send using Sendmail
1424 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 * @return bool
1426 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001427 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 {
1429 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1430
Derek Jones4cefaa42009-04-29 19:13:56 +00001431 if ($fp === FALSE OR $fp === NULL)
1432 {
1433 // server probably has popen disabled, so nothing we can do to get a verbose error.
1434 return FALSE;
1435 }
Derek Jones71141ce2010-03-02 16:41:20 -06001436
Derek Jonesc630bcf2008-11-17 21:09:45 +00001437 fputs($fp, $this->_header_str);
1438 fputs($fp, $this->_finalbody);
1439
Barry Mienydd671972010-10-04 16:33:58 +02001440 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001441
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001442 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 {
patworkb0707982011-04-08 15:10:05 +02001444 $this->_set_error_message('lang:email_exit_status', $status);
1445 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 return FALSE;
1447 }
1448
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 return TRUE;
1450 }
Barry Mienydd671972010-10-04 16:33:58 +02001451
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 // --------------------------------------------------------------------
1453
1454 /**
1455 * Send using SMTP
1456 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 * @return bool
1458 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001459 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 {
1461 if ($this->smtp_host == '')
1462 {
patworkb0707982011-04-08 15:10:05 +02001463 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 return FALSE;
1465 }
1466
Diogo Osório593f7982012-03-02 18:04:17 +00001467 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001468 {
1469 return FALSE;
1470 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001471
1472 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1473
Pascal Kriete14287f32011-02-14 13:39:34 -05001474 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 {
1476 $this->_send_command('to', $val);
1477 }
1478
1479 if (count($this->_cc_array) > 0)
1480 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001481 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
1483 if ($val != "")
1484 {
1485 $this->_send_command('to', $val);
1486 }
1487 }
1488 }
1489
1490 if (count($this->_bcc_array) > 0)
1491 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001492 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 {
1494 if ($val != "")
1495 {
1496 $this->_send_command('to', $val);
1497 }
1498 }
1499 }
1500
1501 $this->_send_command('data');
1502
1503 // perform dot transformation on any lines that begin with a dot
1504 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1505
1506 $this->_send_data('.');
1507
1508 $reply = $this->_get_smtp_data();
1509
1510 $this->_set_error_message($reply);
1511
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001512 if (strncmp($reply, '250', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
patworkb0707982011-04-08 15:10:05 +02001514 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 return FALSE;
1516 }
1517
1518 $this->_send_command('quit');
1519 return TRUE;
1520 }
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 // --------------------------------------------------------------------
1523
1524 /**
1525 * SMTP Connect
1526 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 * @param string
1528 * @return string
1529 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001530 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001532 $ssl = ($this->smtp_crypto == 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001533
Radu Potopbbf04b02011-09-28 13:57:51 +03001534 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 $this->smtp_port,
1536 $errno,
1537 $errstr,
1538 $this->smtp_timeout);
1539
Pascal Kriete14287f32011-02-14 13:39:34 -05001540 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 {
patworkb0707982011-04-08 15:10:05 +02001542 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 return FALSE;
1544 }
1545
1546 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001547
1548 if ($this->smtp_crypto == 'tls')
1549 {
1550 $this->_send_command('hello');
1551 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001552
Radu Potop4c589ae2011-09-29 10:19:55 +03001553 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001554
Sean Fishere862ddd2011-10-26 22:45:00 -03001555 if ($crypto !== TRUE)
1556 {
1557 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1558 return FALSE;
1559 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001560 }
1561
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 return $this->_send_command('hello');
1563 }
Barry Mienydd671972010-10-04 16:33:58 +02001564
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 // --------------------------------------------------------------------
1566
1567 /**
1568 * Send SMTP command
1569 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 * @param string
1571 * @param string
1572 * @return string
1573 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001574 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001575 {
1576 switch ($cmd)
1577 {
1578 case 'hello' :
1579
1580 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1581 $this->_send_data('EHLO '.$this->_get_hostname());
1582 else
1583 $this->_send_data('HELO '.$this->_get_hostname());
1584
1585 $resp = 250;
1586 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001587 case 'starttls' :
1588
1589 $this->_send_data('STARTTLS');
1590
1591 $resp = 220;
1592 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 case 'from' :
1594
1595 $this->_send_data('MAIL FROM:<'.$data.'>');
1596
1597 $resp = 250;
1598 break;
1599 case 'to' :
1600
1601 $this->_send_data('RCPT TO:<'.$data.'>');
1602
1603 $resp = 250;
1604 break;
1605 case 'data' :
1606
1607 $this->_send_data('DATA');
1608
1609 $resp = 354;
1610 break;
1611 case 'quit' :
1612
1613 $this->_send_data('QUIT');
1614
1615 $resp = 221;
1616 break;
1617 }
1618
1619 $reply = $this->_get_smtp_data();
1620
1621 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1622
1623 if (substr($reply, 0, 3) != $resp)
1624 {
patworkb0707982011-04-08 15:10:05 +02001625 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 return FALSE;
1627 }
1628
1629 if ($cmd == 'quit')
1630 {
1631 fclose($this->_smtp_connect);
1632 }
1633
1634 return TRUE;
1635 }
Barry Mienydd671972010-10-04 16:33:58 +02001636
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 // --------------------------------------------------------------------
1638
1639 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001640 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 * @return bool
1643 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001644 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
1646 if ( ! $this->_smtp_auth)
1647 {
1648 return TRUE;
1649 }
1650
Andrey Andreev76f15c92012-03-01 13:05:07 +02001651 if ($this->smtp_user == '' && $this->smtp_pass == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
patworkb0707982011-04-08 15:10:05 +02001653 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 return FALSE;
1655 }
1656
1657 $this->_send_data('AUTH LOGIN');
1658
1659 $reply = $this->_get_smtp_data();
1660
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001661 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 {
patworkb0707982011-04-08 15:10:05 +02001663 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 return FALSE;
1665 }
1666
1667 $this->_send_data(base64_encode($this->smtp_user));
1668
1669 $reply = $this->_get_smtp_data();
1670
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001671 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 {
patworkb0707982011-04-08 15:10:05 +02001673 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 return FALSE;
1675 }
1676
1677 $this->_send_data(base64_encode($this->smtp_pass));
1678
1679 $reply = $this->_get_smtp_data();
1680
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001681 if (strncmp($reply, '235', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 {
patworkb0707982011-04-08 15:10:05 +02001683 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 return FALSE;
1685 }
1686
1687 return TRUE;
1688 }
Barry Mienydd671972010-10-04 16:33:58 +02001689
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 // --------------------------------------------------------------------
1691
1692 /**
1693 * Send SMTP data
1694 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 * @return bool
1696 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001697 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 {
1699 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1700 {
patworkb0707982011-04-08 15:10:05 +02001701 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 return FALSE;
1703 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001704
1705 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 }
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 // --------------------------------------------------------------------
1709
1710 /**
1711 * Get SMTP data
1712 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 * @return string
1714 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001715 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 {
1717 $data = "";
1718
1719 while ($str = fgets($this->_smtp_connect, 512))
1720 {
1721 $data .= $str;
1722
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001723 if ($str[3] == " ")
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 {
1725 break;
1726 }
1727 }
1728
1729 return $data;
1730 }
Barry Mienydd671972010-10-04 16:33:58 +02001731
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 // --------------------------------------------------------------------
1733
1734 /**
1735 * Get Hostname
1736 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 * @return string
1738 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001739 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001741 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
Barry Mienydd671972010-10-04 16:33:58 +02001743
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 // --------------------------------------------------------------------
1745
1746 /**
1747 * Get IP
1748 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 * @return string
1750 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001751 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
1753 if ($this->_IP !== FALSE)
1754 {
1755 return $this->_IP;
1756 }
1757
Andrey Andreev76f15c92012-03-01 13:05:07 +02001758 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1759 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001760 if ($cip) $this->_IP = $cip;
1761 elseif ($rip) $this->_IP = $rip;
1762 else
1763 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001764 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001765 if ($fip)
1766 {
1767 $this->_IP = $fip;
1768 }
1769 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001770
Robin Sowell76b369e2010-03-19 11:15:28 -04001771 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 {
1773 $x = explode(',', $this->_IP);
1774 $this->_IP = end($x);
1775 }
1776
Andrey Andreev59c5b532012-03-01 14:09:51 +02001777 if ( ! preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->_IP))
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 {
1779 $this->_IP = '0.0.0.0';
1780 }
1781
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 return $this->_IP;
1783 }
Barry Mienydd671972010-10-04 16:33:58 +02001784
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 // --------------------------------------------------------------------
1786
1787 /**
1788 * Get Debug Message
1789 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 * @return string
1791 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001792 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
1794 $msg = '';
1795
1796 if (count($this->_debug_msg) > 0)
1797 {
1798 foreach ($this->_debug_msg as $val)
1799 {
1800 $msg .= $val;
1801 }
1802 }
1803
Andrey Andreev59c5b532012-03-01 14:09:51 +02001804 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 }
Barry Mienydd671972010-10-04 16:33:58 +02001806
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 // --------------------------------------------------------------------
1808
1809 /**
1810 * Set Message
1811 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001813 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001815 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
1817 $CI =& get_instance();
1818 $CI->lang->load('email');
1819
Andrey Andreev76f15c92012-03-01 13:05:07 +02001820 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 {
1822 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1823 }
1824 else
1825 {
1826 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1827 }
1828 }
Barry Mienydd671972010-10-04 16:33:58 +02001829
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 // --------------------------------------------------------------------
1831
1832 /**
1833 * Mime Types
1834 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 * @param string
1836 * @return string
1837 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001838 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001840 $mimes = array(
1841 'hqx' => 'application/mac-binhex40',
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 'cpt' => 'application/mac-compactpro',
1843 'doc' => 'application/msword',
1844 'bin' => 'application/macbinary',
1845 'dms' => 'application/octet-stream',
1846 'lha' => 'application/octet-stream',
1847 'lzh' => 'application/octet-stream',
1848 'exe' => 'application/octet-stream',
1849 'class' => 'application/octet-stream',
1850 'psd' => 'application/octet-stream',
1851 'so' => 'application/octet-stream',
1852 'sea' => 'application/octet-stream',
1853 'dll' => 'application/octet-stream',
1854 'oda' => 'application/oda',
1855 'pdf' => 'application/pdf',
1856 'ai' => 'application/postscript',
1857 'eps' => 'application/postscript',
1858 'ps' => 'application/postscript',
1859 'smi' => 'application/smil',
1860 'smil' => 'application/smil',
1861 'mif' => 'application/vnd.mif',
1862 'xls' => 'application/vnd.ms-excel',
1863 'ppt' => 'application/vnd.ms-powerpoint',
1864 'wbxml' => 'application/vnd.wap.wbxml',
1865 'wmlc' => 'application/vnd.wap.wmlc',
1866 'dcr' => 'application/x-director',
1867 'dir' => 'application/x-director',
1868 'dxr' => 'application/x-director',
1869 'dvi' => 'application/x-dvi',
1870 'gtar' => 'application/x-gtar',
1871 'php' => 'application/x-httpd-php',
1872 'php4' => 'application/x-httpd-php',
1873 'php3' => 'application/x-httpd-php',
1874 'phtml' => 'application/x-httpd-php',
1875 'phps' => 'application/x-httpd-php-source',
1876 'js' => 'application/x-javascript',
1877 'swf' => 'application/x-shockwave-flash',
1878 'sit' => 'application/x-stuffit',
1879 'tar' => 'application/x-tar',
1880 'tgz' => 'application/x-tar',
1881 'xhtml' => 'application/xhtml+xml',
1882 'xht' => 'application/xhtml+xml',
1883 'zip' => 'application/zip',
1884 'mid' => 'audio/midi',
1885 'midi' => 'audio/midi',
1886 'mpga' => 'audio/mpeg',
1887 'mp2' => 'audio/mpeg',
1888 'mp3' => 'audio/mpeg',
1889 'aif' => 'audio/x-aiff',
1890 'aiff' => 'audio/x-aiff',
1891 'aifc' => 'audio/x-aiff',
1892 'ram' => 'audio/x-pn-realaudio',
1893 'rm' => 'audio/x-pn-realaudio',
1894 'rpm' => 'audio/x-pn-realaudio-plugin',
1895 'ra' => 'audio/x-realaudio',
1896 'rv' => 'video/vnd.rn-realvideo',
1897 'wav' => 'audio/x-wav',
1898 'bmp' => 'image/bmp',
1899 'gif' => 'image/gif',
1900 'jpeg' => 'image/jpeg',
1901 'jpg' => 'image/jpeg',
1902 'jpe' => 'image/jpeg',
1903 'png' => 'image/png',
1904 'tiff' => 'image/tiff',
1905 'tif' => 'image/tiff',
1906 'css' => 'text/css',
1907 'html' => 'text/html',
1908 'htm' => 'text/html',
1909 'shtml' => 'text/html',
1910 'txt' => 'text/plain',
1911 'text' => 'text/plain',
1912 'log' => 'text/plain',
1913 'rtx' => 'text/richtext',
1914 'rtf' => 'text/rtf',
1915 'xml' => 'text/xml',
1916 'xsl' => 'text/xml',
1917 'mpeg' => 'video/mpeg',
1918 'mpg' => 'video/mpeg',
1919 'mpe' => 'video/mpeg',
1920 'qt' => 'video/quicktime',
1921 'mov' => 'video/quicktime',
1922 'avi' => 'video/x-msvideo',
1923 'movie' => 'video/x-sgi-movie',
1924 'doc' => 'application/msword',
1925 'word' => 'application/msword',
1926 'xl' => 'application/excel',
1927 'eml' => 'message/rfc822'
1928 );
1929
Andrey Andreev59c5b532012-03-01 14:09:51 +02001930 return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 }
1932
1933}
Derek Allard2067d1a2008-11-13 22:59:24 +00001934
1935/* End of file Email.php */
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001936/* Location: ./system/libraries/Email.php */