blob: 8f383c939add8528659bdd0b3c00fec7bc80d834 [file] [log] [blame]
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Email Class
30 *
31 * Permits email to be sent using Mail, Sendmail, or SMTP.
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/email.html
38 */
trita15dd4f2011-11-23 07:40:05 -050039class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev59c5b532012-03-01 14:09:51 +020041 public $useragent = 'CodeIgniter';
42 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
43 public $protocol = 'mail'; // mail/sendmail/smtp
44 public $smtp_host = ''; // SMTP Server. Example: mail.earthlink.net
45 public $smtp_user = ''; // SMTP Username
46 public $smtp_pass = ''; // SMTP Password
47 public $smtp_port = 25; // SMTP Port
48 public $smtp_timeout = 5; // SMTP Timeout in seconds
49 public $smtp_crypto = ''; // SMTP Encryption. Can be null, tls or ssl.
50 public $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
51 public $wrapchars = 76; // Number of characters to wrap at.
52 public $mailtype = 'text'; // text/html Defines email formatting
53 public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
54 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
55 public $alt_message = ''; // Alternative message for HTML emails
56 public $validate = FALSE; // TRUE/FALSE. Enables email validation
57 public $priority = 3; // Default priority (1 - 5)
58 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
59 public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
61 // distasteful, is the only thing that seems to work for all environments.
leandronf7686c122012-03-22 08:07:31 -030062 public $dsn = FALSE; // Delivery Status Notification
Andrey Andreev1bd3d882011-12-22 15:38:20 +020063 public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Andrey Andreev59c5b532012-03-01 14:09:51 +020064 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020065 public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Andrey Andreev50814092012-03-01 12:36:12 +020066
67 protected $_safe_mode = FALSE;
68 protected $_subject = '';
69 protected $_body = '';
70 protected $_finalbody = '';
71 protected $_alt_boundary = '';
72 protected $_atc_boundary = '';
73 protected $_header_str = '';
74 protected $_smtp_connect = '';
75 protected $_encoding = '8bit';
76 protected $_IP = FALSE;
77 protected $_smtp_auth = FALSE;
78 protected $_replyto_flag = FALSE;
79 protected $_debug_msg = array();
80 protected $_recipients = array();
81 protected $_cc_array = array();
82 protected $_bcc_array = array();
83 protected $_headers = array();
84 protected $_attach_name = array();
85 protected $_attach_type = array();
86 protected $_attach_disp = array();
87 protected $_protocols = array('mail', 'sendmail', 'smtp');
88 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
89 protected $_bit_depths = array('7bit', '8bit');
90 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Constructor - Sets Email Preferences
94 *
95 * The constructor can be passed an array of config values
96 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000097 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
99 if (count($config) > 0)
100 {
101 $this->initialize($config);
102 }
103 else
104 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200105 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200106 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
108
Andrey Andreev59c5b532012-03-01 14:09:51 +0200109 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Initialize preferences
116 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * @param array
118 * @return void
119 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000120 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 foreach ($config as $key => $val)
123 {
124 if (isset($this->$key))
125 {
126 $method = 'set_'.$key;
127
128 if (method_exists($this, $method))
129 {
130 $this->$method($val);
131 }
132 else
133 {
134 $this->$key = $val;
135 }
136 }
137 }
Eric Barnes6113f542010-12-29 13:36:12 -0500138 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000139
Andrey Andreev76f15c92012-03-01 13:05:07 +0200140 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200141 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000142
143 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
149 * Initialize the Email Data
150 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800151 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200152 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000154 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200156 $this->_subject = '';
157 $this->_body = '';
158 $this->_finalbody = '';
159 $this->_header_str = '';
160 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500162 $this->_cc_array = array();
163 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 $this->_headers = array();
165 $this->_debug_msg = array();
166
167 $this->_set_header('User-Agent', $this->useragent);
168 $this->_set_header('Date', $this->_set_date());
169
170 if ($clear_attachments !== FALSE)
171 {
172 $this->_attach_name = array();
173 $this->_attach_type = array();
174 $this->_attach_disp = array();
175 }
Eric Barnes6113f542010-12-29 13:36:12 -0500176
Greg Akera769deb2010-11-10 15:47:29 -0600177 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // --------------------------------------------------------------------
181
182 /**
183 * Set FROM
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string
186 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200187 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000189 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200191 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200193 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
195
196 if ($this->validate)
197 {
198 $this->validate_email($this->_str_to_array($from));
199 }
200
201 // prepare the display name
202 if ($name != '')
203 {
204 // only use Q encoding if there are characters that would require it
205 if ( ! preg_match('/[\200-\377]/', $name))
206 {
207 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000208 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210 else
211 {
212 $name = $this->_prep_q_encoding($name, TRUE);
213 }
214 }
215
216 $this->_set_header('From', $name.' <'.$from.'>');
217 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500218
Greg Akera769deb2010-11-10 15:47:29 -0600219 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Set Reply-to
226 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @param string
228 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200229 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000231 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200233 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200235 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 if ($this->validate)
239 {
240 $this->validate_email($this->_str_to_array($replyto));
241 }
242
243 if ($name == '')
244 {
245 $name = $replyto;
246 }
247
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200248 if (strncmp($name, '"', 1) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 $name = '"'.$name.'"';
251 }
252
253 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
254 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600255
256 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 // --------------------------------------------------------------------
260
261 /**
262 * Set Recipients
263 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200265 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000267 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 $to = $this->_str_to_array($to);
270 $to = $this->clean_email($to);
271
272 if ($this->validate)
273 {
274 $this->validate_email($to);
275 }
276
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200277 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200279 $this->_set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
282 switch ($this->_get_protocol())
283 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200284 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000285 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200287 case 'sendmail':
288 case 'mail':
289 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 break;
291 }
Eric Barnes6113f542010-12-29 13:36:12 -0500292
Greg Akera769deb2010-11-10 15:47:29 -0600293 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // --------------------------------------------------------------------
297
298 /**
299 * Set CC
300 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200302 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000304 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 $cc = $this->_str_to_array($cc);
307 $cc = $this->clean_email($cc);
308
309 if ($this->validate)
310 {
311 $this->validate_email($cc);
312 }
313
Andrey Andreev59c5b532012-03-01 14:09:51 +0200314 $this->_set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000315
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200316 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $this->_cc_array = $cc;
319 }
Eric Barnes6113f542010-12-29 13:36:12 -0500320
Greg Akera769deb2010-11-10 15:47:29 -0600321 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
325
326 /**
327 * Set BCC
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @param string
330 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200331 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000333 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 if ($limit != '' && is_numeric($limit))
336 {
337 $this->bcc_batch_mode = TRUE;
338 $this->bcc_batch_size = $limit;
339 }
340
341 $bcc = $this->_str_to_array($bcc);
342 $bcc = $this->clean_email($bcc);
343
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 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200355 $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);
372 $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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200408 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 */
trit10eb14d2011-11-23 15:48:21 -0500410 public function attach($filename, $disposition = '', $newname = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
trit151fc682011-11-23 07:30:06 -0500412 $this->_attach_name[] = array($filename, $newname);
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600413 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
tritd5269982011-11-23 17:22:44 -0500414 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600415 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Add a Header Item
422 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @param string
424 * @param string
425 * @return void
426 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600427 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 $this->_headers[$header] = $value;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
435 * Convert a String to an Array
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string
438 * @return array
439 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600440 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 if ( ! is_array($email))
443 {
444 if (strpos($email, ',') !== FALSE)
445 {
446 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
447 }
448 else
449 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200450 $email = (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452 }
453 return $email;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
457
458 /**
459 * Set Multipart Value
460 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200462 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000464 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400466 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600467 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 // --------------------------------------------------------------------
471
472 /**
473 * Set Mailtype
474 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200476 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000478 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600481 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
487 * Set Wordwrap
488 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400489 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200490 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000492 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400494 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600495 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
499
500 /**
501 * Set Protocol
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200504 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000506 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200508 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600509 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Set Priority
516 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200517 * @param int
518 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000520 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200522 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600523 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
527
528 /**
529 * Set Newline Character
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200532 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000534 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200536 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600537 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 // --------------------------------------------------------------------
541
542 /**
543 * Set CRLF
544 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200546 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000548 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200550 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600551 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
555
556 /**
557 * Set Message Boundary
558 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @return void
560 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600561 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200563 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
564 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // --------------------------------------------------------------------
568
569 /**
570 * Get the Message ID
571 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 * @return string
573 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600574 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200576 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200577 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
581
582 /**
583 * Get Mail Protocol
584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200586 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600588 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200591 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000592
593 if ($return == TRUE)
594 {
595 return $this->protocol;
596 }
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // --------------------------------------------------------------------
600
601 /**
602 * Get Mail Encoding
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param bool
605 * @return string
606 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600607 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200609 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000610
611 foreach ($this->_base_charsets as $charset)
612 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200613 if (strncmp($charset, $this->charset, strlen($charset)) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 $this->_encoding = '7bit';
616 }
617 }
618
619 if ($return == TRUE)
620 {
621 return $this->_encoding;
622 }
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Get content type (text/html/attachment)
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @return string
631 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600632 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200634 if ($this->mailtype === 'html' && count($this->_attach_name) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return 'html';
637 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200638 elseif ($this->mailtype === 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 return 'html-attach';
641 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200642 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return 'plain-attach';
645 }
646 else
647 {
648 return 'plain';
649 }
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
653
654 /**
655 * Set RFC 822 Date
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @return string
658 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600659 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200661 $timezone = date('Z');
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200662 $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200664 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665
Andrey Andreev59c5b532012-03-01 14:09:51 +0200666 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 // --------------------------------------------------------------------
670
671 /**
672 * Mime message
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @return string
675 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600676 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200678 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 +0000679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // --------------------------------------------------------------------
682
683 /**
684 * Validate Email Address
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param string
687 * @return bool
688 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000689 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
691 if ( ! is_array($email))
692 {
patworkb0707982011-04-08 15:10:05 +0200693 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return FALSE;
695 }
696
697 foreach ($email as $val)
698 {
699 if ( ! $this->valid_email($val))
700 {
patworkb0707982011-04-08 15:10:05 +0200701 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 return FALSE;
703 }
704 }
705
706 return TRUE;
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
712 * Email Validation
713 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 * @param string
715 * @return bool
716 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000717 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200719 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 +0000720 }
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 // --------------------------------------------------------------------
723
724 /**
725 * Clean Extended Email Address: Joe Smith <joe@smith.com>
726 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 * @param string
728 * @return string
729 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000730 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
732 if ( ! is_array($email))
733 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200734 return (preg_match('/\<(.*)\>/', $email, $match)) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 }
736
737 $clean_email = array();
738
739 foreach ($email as $addy)
740 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200741 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743
744 return $clean_email;
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 // --------------------------------------------------------------------
748
749 /**
750 * Build alternative plain text message
751 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000752 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * in plain-text headers of HTML-formatted emails.
754 * If the user hasn't specified his own alternative message
755 * it creates one by stripping the HTML
756 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 * @return string
758 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600759 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200761 if ($this->alt_message != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
763 return $this->word_wrap($this->alt_message, '76');
764 }
765
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200766 $body = (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match)) ? $match[1] : $this->_body;
767 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000768
769 for ($i = 20; $i >= 3; $i--)
770 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200771 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200774 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Word Wrap
781 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200783 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * @return string
785 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000786 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
788 // Se the character limit
789 if ($charlim == '')
790 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200791 $charlim = ($this->wrapchars == "") ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
793
794 // Reduce multiple spaces
795 $str = preg_replace("| +|", " ", $str);
796
797 // Standardize newlines
798 if (strpos($str, "\r") !== FALSE)
799 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200800 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
802
803 // If the current word is surrounded by {unwrap} tags we'll
804 // strip the entire chunk and replace it with a marker.
805 $unwrap = array();
806 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
807 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200808 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200810 $unwrap[] = $matches[1][$i];
811 $str = str_replace($matches[1][$i], "{{unwrapped".$i."}}", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813 }
814
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000815 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500817 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 $str = wordwrap($str, $charlim, "\n", FALSE);
819
820 // Split the string into individual lines of text and cycle through them
821 $output = "";
822 foreach (explode("\n", $str) as $line)
823 {
824 // Is the line within the allowed character count?
825 // If so we'll join it to the output and continue
826 if (strlen($line) <= $charlim)
827 {
828 $output .= $line.$this->newline;
829 continue;
830 }
831
832 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200833 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
835 // If the over-length word is a URL we won't wrap it
836 if (preg_match("!\[url.+\]|://|wwww.!", $line))
837 {
838 break;
839 }
840
841 // Trim the word down
842 $temp .= substr($line, 0, $charlim-1);
843 $line = substr($line, $charlim-1);
844 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200845 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000846
847 // If $temp contains data it means we had to split up an over-length
848 // word into smaller chunks so we'll add it back to our current line
849 if ($temp != '')
850 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200851 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
853
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200854 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856
857 // Put our markers back
858 if (count($unwrap) > 0)
859 {
860 foreach ($unwrap as $key => $val)
861 {
862 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
863 }
864 }
865
866 return $output;
867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // --------------------------------------------------------------------
870
871 /**
872 * Build final headers
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @return string
875 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600876 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
878 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
879 $this->_set_header('X-Mailer', $this->useragent);
880 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
881 $this->_set_header('Message-ID', $this->_get_message_id());
882 $this->_set_header('Mime-Version', '1.0');
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // --------------------------------------------------------------------
886
887 /**
888 * Write Headers as a string
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @return void
891 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600892 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200894 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
896 $this->_subject = $this->_headers['Subject'];
897 unset($this->_headers['Subject']);
898 }
899
900 reset($this->_headers);
901 $this->_header_str = "";
902
Pascal Kriete14287f32011-02-14 13:39:34 -0500903 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
905 $val = trim($val);
906
907 if ($val != "")
908 {
909 $this->_header_str .= $key.": ".$val.$this->newline;
910 }
911 }
912
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200913 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
Derek Jones1d890882009-02-10 20:32:14 +0000915 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
920
921 /**
922 * Build Final Body and attachments
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @return void
925 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600926 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200928 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
930 $this->_body = $this->word_wrap($this->_body);
931 }
932
933 $this->_set_boundaries();
934 $this->_write_headers();
935
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200936 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500937 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
939 switch ($this->_get_content_type())
940 {
941 case 'plain' :
942
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200943 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline
944 . "Content-Transfer-Encoding: " . $this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000945
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200946 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
948 $this->_header_str .= $hdr;
949 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Brandon Jones485d7412010-11-09 16:38:17 -0500951 else
952 {
953 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
954 }
Eric Barnes6113f542010-12-29 13:36:12 -0500955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 return;
957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 case 'html' :
959
960 if ($this->send_multipart === FALSE)
961 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200962 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline
963 . "Content-Transfer-Encoding: quoted-printable";
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
965 else
966 {
Derek Jonesa45e7612009-02-10 18:33:01 +0000967 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000968
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200969 $body .= $this->_get_mime_message() . $this->newline . $this->newline
970 . "--" . $this->_alt_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200972 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
973 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
974 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500975
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200976 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
977 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Eric Barnes6113f542010-12-29 13:36:12 -0500979
Brandon Jones485d7412010-11-09 16:38:17 -0500980 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500981
982
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200983 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500986 }
987 else
988 {
989 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 }
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
993 if ($this->send_multipart !== FALSE)
994 {
Brandon Jones485d7412010-11-09 16:38:17 -0500995 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 return;
999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 case 'plain-attach' :
1001
Derek Jonesa45e7612009-02-10 18:33:01 +00001002 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001004 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001007 }
1008
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001009 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1010 . "--" . $this->_atc_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001011
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001012 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
1013 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001015 . $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
1017 break;
1018 case 'html-attach' :
1019
Derek Jonesa45e7612009-02-10 18:33:01 +00001020 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001021
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001022 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
1026
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001027 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1028 . "--" . $this->_atc_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001029
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001030 . "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline
1031 . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001033 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
1034 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
1035 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001036
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001037 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
1038 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001039
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001040 . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline
1041 . "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 break;
1044 }
1045
1046 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001047 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
tritbc4ac992011-11-23 07:19:41 -05001049 $filename = $this->_attach_name[$i][0];
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001050 $basename = (is_null($this->_attach_name[$i][1])) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 $ctype = $this->_attach_type[$i];
1052
tritbc4ac992011-11-23 07:19:41 -05001053 if ( ! file_exists($filename))
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
tritbc4ac992011-11-23 07:19:41 -05001055 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 return FALSE;
1057 }
1058
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001059 $attachment[$z++] = "--".$this->_atc_boundary.$this->newline
1060 . "Content-type: ".$ctype."; "
1061 . "name=\"".$basename."\"".$this->newline
1062 . "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline
1063 . "Content-Transfer-Encoding: base64".$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001064
tritbc4ac992011-11-23 07:19:41 -05001065 $file = filesize($filename) +1;
Derek Allard2067d1a2008-11-13 22:59:24 +00001066
tritbc4ac992011-11-23 07:19:41 -05001067 if ( ! $fp = fopen($filename, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
tritbc4ac992011-11-23 07:19:41 -05001069 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 return FALSE;
1071 }
1072
1073 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1074 fclose($fp);
1075 }
1076
Brandon Jones485d7412010-11-09 16:38:17 -05001077 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001078 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr . $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 return;
1080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Prep Quoted Printable
1086 *
1087 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1088 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001091 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * @return string
1093 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001094 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 // Set the character limit
1097 // Don't allow over 76, as that will make servers and MUAs barf
1098 // all over quoted-printable data
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001099 if ($charlim == '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001101 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 }
1103
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001104 // Reduce multiple spaces & remove nulls
1105 $str = preg_replace(array("| +|", '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107 // Standardize newlines
1108 if (strpos($str, "\r") !== FALSE)
1109 {
1110 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1111 }
1112
1113 // We are intentionally wrapping so mail servers will encode characters
1114 // properly and MUAs will behave, so {unwrap} must go!
1115 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 $escape = '=';
1118 $output = '';
1119
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001120 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
1122 $length = strlen($line);
1123 $temp = '';
1124
1125 // Loop through each character in the line to add soft-wrap
1126 // characters at the end of a line " =\r\n" and add the newly
1127 // processed line(s) to the output (see comment on $crlf class property)
1128 for ($i = 0; $i < $length; $i++)
1129 {
1130 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001131 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 $ascii = ord($char);
1133
1134 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001135 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001137 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001139 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001141 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
1143
1144 // If we're at the character limit, add the line to the output,
1145 // reset our temp variable, and keep on chuggin'
1146 if ((strlen($temp) + strlen($char)) >= $charlim)
1147 {
1148 $output .= $temp.$escape.$this->crlf;
1149 $temp = '';
1150 }
1151
1152 // Add the character to our temporary line
1153 $temp .= $char;
1154 }
1155
1156 // Add our completed line to the output
1157 $output .= $temp.$this->crlf;
1158 }
1159
1160 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001161 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
1163
1164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 /**
1167 * Prep Q Encoding
1168 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001169 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 * but not identical to quoted-printable, so it has its own method
1171 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001172 * @param string
1173 * @param bool set to TRUE for processing From: headers
1174 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001176 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
1178 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1179
1180 // Line length must not exceed 76 characters, so we adjust for
1181 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1182 $limit = 75 - 7 - strlen($this->charset);
1183
1184 // these special characters must be converted too
1185 $convert = array('_', '=', '?');
1186
1187 if ($from === TRUE)
1188 {
1189 $convert[] = ',';
1190 $convert[] = ';';
1191 }
1192
1193 $output = '';
1194 $temp = '';
1195
1196 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1197 {
1198 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001199 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 $ascii = ord($char);
1201
1202 // convert ALL non-printable ASCII characters and our specials
1203 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1204 {
1205 $char = '='.dechex($ascii);
1206 }
1207
1208 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001209 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 $char = '_';
1212 }
1213
1214 // If we're at the character limit, add the line to the output,
1215 // reset our temp variable, and keep on chuggin'
1216 if ((strlen($temp) + strlen($char)) >= $limit)
1217 {
1218 $output .= $temp.$this->crlf;
1219 $temp = '';
1220 }
1221
1222 // Add the character to our temporary line
1223 $temp .= $char;
1224 }
1225
1226 $str = $output.$temp;
1227
1228 // wrap each line with the shebang, charset, and transfer encoding
1229 // the preceding space on successive lines is required for header "folding"
Andrey Andreev59c5b532012-03-01 14:09:51 +02001230 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232
1233 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 /**
1236 * Send Email
1237 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 * @return bool
1239 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001240 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
1242 if ($this->_replyto_flag == FALSE)
1243 {
1244 $this->reply_to($this->_headers['From']);
1245 }
1246
Andrey Andreev76f15c92012-03-01 13:05:07 +02001247 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1248 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1249 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
patworkb0707982011-04-08 15:10:05 +02001251 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 return FALSE;
1253 }
1254
1255 $this->_build_headers();
1256
Andrey Andreev76f15c92012-03-01 13:05:07 +02001257 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001259 return $this->batch_bcc_send();
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 }
1261
1262 $this->_build_message();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001263 return $this->_spool_email();
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 // --------------------------------------------------------------------
1267
1268 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001269 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001271 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001273 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001275 $float = $this->bcc_batch_size - 1;
1276 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 $chunk = array();
1278
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001279 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
1281 if (isset($this->_bcc_array[$i]))
1282 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001283 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
1285
1286 if ($i == $float)
1287 {
1288 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001289 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001290 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 }
1292
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001293 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 {
1295 $chunk[] = substr($set, 1);
1296 }
1297 }
1298
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001299 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 {
1301 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001302
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001303 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001304
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001305 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001307 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 }
1309 else
1310 {
1311 $this->_bcc_array = $bcc;
1312 }
1313
1314 $this->_build_message();
1315 $this->_spool_email();
1316 }
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // --------------------------------------------------------------------
1320
1321 /**
1322 * Unwrap special elements
1323 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 * @return void
1325 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001326 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 {
1328 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1329 }
Barry Mienydd671972010-10-04 16:33:58 +02001330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 // --------------------------------------------------------------------
1332
1333 /**
1334 * Strip line-breaks via callback
1335 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 * @return string
1337 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001338 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 {
1340 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1341 {
1342 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1343 }
1344
1345 return $matches[1];
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Spool mail to the mail server
1352 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * @return bool
1354 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001355 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
1357 $this->_unwrap_specials();
1358
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001359 $method = '_send_with_' . $this->_get_protocol();
1360 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001362 $this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001363 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 }
1365
patworkb0707982011-04-08 15:10:05 +02001366 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 return TRUE;
1368 }
Barry Mienydd671972010-10-04 16:33:58 +02001369
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 // --------------------------------------------------------------------
1371
1372 /**
1373 * Send using mail()
1374 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * @return bool
1376 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001377 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 {
1379 if ($this->_safe_mode == TRUE)
1380 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001381 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 }
1383 else
1384 {
1385 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1386 // we've encountered servers that seem to require it to be in place.
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001387 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 +00001388 }
1389 }
Barry Mienydd671972010-10-04 16:33:58 +02001390
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 // --------------------------------------------------------------------
1392
1393 /**
1394 * Send using Sendmail
1395 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 * @return bool
1397 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001398 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1401
Derek Jones4cefaa42009-04-29 19:13:56 +00001402 if ($fp === FALSE OR $fp === NULL)
1403 {
1404 // server probably has popen disabled, so nothing we can do to get a verbose error.
1405 return FALSE;
1406 }
Derek Jones71141ce2010-03-02 16:41:20 -06001407
Derek Jonesc630bcf2008-11-17 21:09:45 +00001408 fputs($fp, $this->_header_str);
1409 fputs($fp, $this->_finalbody);
1410
Barry Mienydd671972010-10-04 16:33:58 +02001411 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001412
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001413 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 {
patworkb0707982011-04-08 15:10:05 +02001415 $this->_set_error_message('lang:email_exit_status', $status);
1416 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 return FALSE;
1418 }
1419
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 return TRUE;
1421 }
Barry Mienydd671972010-10-04 16:33:58 +02001422
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 // --------------------------------------------------------------------
1424
1425 /**
1426 * Send using SMTP
1427 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 * @return bool
1429 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001430 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 {
1432 if ($this->smtp_host == '')
1433 {
patworkb0707982011-04-08 15:10:05 +02001434 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 return FALSE;
1436 }
1437
Diogo Osório593f7982012-03-02 18:04:17 +00001438 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001439 {
1440 return FALSE;
1441 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001442
1443 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1444
Pascal Kriete14287f32011-02-14 13:39:34 -05001445 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
1447 $this->_send_command('to', $val);
1448 }
1449
1450 if (count($this->_cc_array) > 0)
1451 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001452 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 {
1454 if ($val != "")
1455 {
1456 $this->_send_command('to', $val);
1457 }
1458 }
1459 }
1460
1461 if (count($this->_bcc_array) > 0)
1462 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001463 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
1465 if ($val != "")
1466 {
1467 $this->_send_command('to', $val);
1468 }
1469 }
1470 }
1471
1472 $this->_send_command('data');
1473
1474 // perform dot transformation on any lines that begin with a dot
1475 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1476
1477 $this->_send_data('.');
1478
1479 $reply = $this->_get_smtp_data();
1480
1481 $this->_set_error_message($reply);
1482
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001483 if (strncmp($reply, '250', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
patworkb0707982011-04-08 15:10:05 +02001485 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 return FALSE;
1487 }
1488
1489 $this->_send_command('quit');
1490 return TRUE;
1491 }
Barry Mienydd671972010-10-04 16:33:58 +02001492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 // --------------------------------------------------------------------
1494
1495 /**
1496 * SMTP Connect
1497 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 * @param string
1499 * @return string
1500 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001501 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001503 $ssl = ($this->smtp_crypto == 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001504
Radu Potopbbf04b02011-09-28 13:57:51 +03001505 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 $this->smtp_port,
1507 $errno,
1508 $errstr,
1509 $this->smtp_timeout);
1510
Pascal Kriete14287f32011-02-14 13:39:34 -05001511 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
patworkb0707982011-04-08 15:10:05 +02001513 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 return FALSE;
1515 }
1516
1517 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001518
1519 if ($this->smtp_crypto == 'tls')
1520 {
1521 $this->_send_command('hello');
1522 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001523
Radu Potop4c589ae2011-09-29 10:19:55 +03001524 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001525
Sean Fishere862ddd2011-10-26 22:45:00 -03001526 if ($crypto !== TRUE)
1527 {
1528 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1529 return FALSE;
1530 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001531 }
1532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 return $this->_send_command('hello');
1534 }
Barry Mienydd671972010-10-04 16:33:58 +02001535
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Send SMTP command
1540 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 * @param string
1542 * @param string
1543 * @return string
1544 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001545 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 {
1547 switch ($cmd)
1548 {
1549 case 'hello' :
1550
1551 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1552 $this->_send_data('EHLO '.$this->_get_hostname());
1553 else
1554 $this->_send_data('HELO '.$this->_get_hostname());
1555
1556 $resp = 250;
1557 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001558 case 'starttls' :
1559
1560 $this->_send_data('STARTTLS');
1561
1562 $resp = 220;
1563 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 case 'from' :
1565
1566 $this->_send_data('MAIL FROM:<'.$data.'>');
1567
1568 $resp = 250;
1569 break;
1570 case 'to' :
leandronfb4552942012-03-21 23:54:26 -03001571
leandronf7686c122012-03-22 08:07:31 -03001572 if ($this->dsn)
1573 {
leandronfb4552942012-03-21 23:54:26 -03001574 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001575 }
leandronfb4552942012-03-21 23:54:26 -03001576 else
leandronf7686c122012-03-22 08:07:31 -03001577 {
leandronfb4552942012-03-21 23:54:26 -03001578 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001579 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 $resp = 250;
1581 break;
1582 case 'data' :
1583
1584 $this->_send_data('DATA');
1585
1586 $resp = 354;
1587 break;
1588 case 'quit' :
1589
1590 $this->_send_data('QUIT');
1591
1592 $resp = 221;
1593 break;
1594 }
1595
1596 $reply = $this->_get_smtp_data();
1597
1598 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1599
1600 if (substr($reply, 0, 3) != $resp)
1601 {
patworkb0707982011-04-08 15:10:05 +02001602 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 return FALSE;
1604 }
1605
1606 if ($cmd == 'quit')
1607 {
1608 fclose($this->_smtp_connect);
1609 }
1610
1611 return TRUE;
1612 }
Barry Mienydd671972010-10-04 16:33:58 +02001613
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 // --------------------------------------------------------------------
1615
1616 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001617 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 * @return bool
1620 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001621 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 {
1623 if ( ! $this->_smtp_auth)
1624 {
1625 return TRUE;
1626 }
1627
Andrey Andreev76f15c92012-03-01 13:05:07 +02001628 if ($this->smtp_user == '' && $this->smtp_pass == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 {
patworkb0707982011-04-08 15:10:05 +02001630 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 return FALSE;
1632 }
1633
1634 $this->_send_data('AUTH LOGIN');
1635
1636 $reply = $this->_get_smtp_data();
1637
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001638 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
patworkb0707982011-04-08 15:10:05 +02001640 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 return FALSE;
1642 }
1643
1644 $this->_send_data(base64_encode($this->smtp_user));
1645
1646 $reply = $this->_get_smtp_data();
1647
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001648 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001649 {
patworkb0707982011-04-08 15:10:05 +02001650 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 return FALSE;
1652 }
1653
1654 $this->_send_data(base64_encode($this->smtp_pass));
1655
1656 $reply = $this->_get_smtp_data();
1657
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001658 if (strncmp($reply, '235', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 {
patworkb0707982011-04-08 15:10:05 +02001660 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 return FALSE;
1662 }
1663
1664 return TRUE;
1665 }
Barry Mienydd671972010-10-04 16:33:58 +02001666
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 // --------------------------------------------------------------------
1668
1669 /**
1670 * Send SMTP data
1671 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 * @return bool
1673 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001674 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 {
1676 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1677 {
patworkb0707982011-04-08 15:10:05 +02001678 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 return FALSE;
1680 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001681
1682 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 // --------------------------------------------------------------------
1686
1687 /**
1688 * Get SMTP data
1689 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 * @return string
1691 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001692 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 {
1694 $data = "";
1695
1696 while ($str = fgets($this->_smtp_connect, 512))
1697 {
1698 $data .= $str;
1699
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001700 if ($str[3] == " ")
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 {
1702 break;
1703 }
1704 }
1705
1706 return $data;
1707 }
Barry Mienydd671972010-10-04 16:33:58 +02001708
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 // --------------------------------------------------------------------
1710
1711 /**
1712 * Get Hostname
1713 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 * @return string
1715 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001716 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001718 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 }
Barry Mienydd671972010-10-04 16:33:58 +02001720
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 // --------------------------------------------------------------------
1722
1723 /**
1724 * Get IP
1725 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 * @return string
1727 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001728 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 {
1730 if ($this->_IP !== FALSE)
1731 {
1732 return $this->_IP;
1733 }
1734
Andrey Andreev76f15c92012-03-01 13:05:07 +02001735 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1736 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001737 if ($cip) $this->_IP = $cip;
1738 elseif ($rip) $this->_IP = $rip;
1739 else
1740 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001741 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001742 if ($fip)
1743 {
1744 $this->_IP = $fip;
1745 }
1746 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001747
Robin Sowell76b369e2010-03-19 11:15:28 -04001748 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
1750 $x = explode(',', $this->_IP);
1751 $this->_IP = end($x);
1752 }
1753
Andrey Andreev59c5b532012-03-01 14:09:51 +02001754 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 +00001755 {
1756 $this->_IP = '0.0.0.0';
1757 }
1758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 return $this->_IP;
1760 }
Barry Mienydd671972010-10-04 16:33:58 +02001761
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 // --------------------------------------------------------------------
1763
1764 /**
1765 * Get Debug Message
1766 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 * @return string
1768 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001769 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
1771 $msg = '';
1772
1773 if (count($this->_debug_msg) > 0)
1774 {
1775 foreach ($this->_debug_msg as $val)
1776 {
1777 $msg .= $val;
1778 }
1779 }
1780
Andrey Andreev59c5b532012-03-01 14:09:51 +02001781 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 }
Barry Mienydd671972010-10-04 16:33:58 +02001783
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 // --------------------------------------------------------------------
1785
1786 /**
1787 * Set Message
1788 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001790 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001792 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
1794 $CI =& get_instance();
1795 $CI->lang->load('email');
1796
Andrey Andreev76f15c92012-03-01 13:05:07 +02001797 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 {
1799 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1800 }
1801 else
1802 {
1803 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1804 }
1805 }
Barry Mienydd671972010-10-04 16:33:58 +02001806
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 // --------------------------------------------------------------------
1808
1809 /**
1810 * Mime Types
1811 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 * @param string
1813 * @return string
1814 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001815 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001817 $mimes = array(
1818 'hqx' => 'application/mac-binhex40',
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 'cpt' => 'application/mac-compactpro',
1820 'doc' => 'application/msword',
1821 'bin' => 'application/macbinary',
1822 'dms' => 'application/octet-stream',
1823 'lha' => 'application/octet-stream',
1824 'lzh' => 'application/octet-stream',
1825 'exe' => 'application/octet-stream',
1826 'class' => 'application/octet-stream',
1827 'psd' => 'application/octet-stream',
1828 'so' => 'application/octet-stream',
1829 'sea' => 'application/octet-stream',
1830 'dll' => 'application/octet-stream',
1831 'oda' => 'application/oda',
1832 'pdf' => 'application/pdf',
1833 'ai' => 'application/postscript',
1834 'eps' => 'application/postscript',
1835 'ps' => 'application/postscript',
1836 'smi' => 'application/smil',
1837 'smil' => 'application/smil',
1838 'mif' => 'application/vnd.mif',
1839 'xls' => 'application/vnd.ms-excel',
1840 'ppt' => 'application/vnd.ms-powerpoint',
1841 'wbxml' => 'application/vnd.wap.wbxml',
1842 'wmlc' => 'application/vnd.wap.wmlc',
1843 'dcr' => 'application/x-director',
1844 'dir' => 'application/x-director',
1845 'dxr' => 'application/x-director',
1846 'dvi' => 'application/x-dvi',
1847 'gtar' => 'application/x-gtar',
1848 'php' => 'application/x-httpd-php',
1849 'php4' => 'application/x-httpd-php',
1850 'php3' => 'application/x-httpd-php',
1851 'phtml' => 'application/x-httpd-php',
1852 'phps' => 'application/x-httpd-php-source',
1853 'js' => 'application/x-javascript',
1854 'swf' => 'application/x-shockwave-flash',
1855 'sit' => 'application/x-stuffit',
1856 'tar' => 'application/x-tar',
1857 'tgz' => 'application/x-tar',
1858 'xhtml' => 'application/xhtml+xml',
1859 'xht' => 'application/xhtml+xml',
1860 'zip' => 'application/zip',
1861 'mid' => 'audio/midi',
1862 'midi' => 'audio/midi',
1863 'mpga' => 'audio/mpeg',
1864 'mp2' => 'audio/mpeg',
1865 'mp3' => 'audio/mpeg',
1866 'aif' => 'audio/x-aiff',
1867 'aiff' => 'audio/x-aiff',
1868 'aifc' => 'audio/x-aiff',
1869 'ram' => 'audio/x-pn-realaudio',
1870 'rm' => 'audio/x-pn-realaudio',
1871 'rpm' => 'audio/x-pn-realaudio-plugin',
1872 'ra' => 'audio/x-realaudio',
1873 'rv' => 'video/vnd.rn-realvideo',
1874 'wav' => 'audio/x-wav',
1875 'bmp' => 'image/bmp',
1876 'gif' => 'image/gif',
1877 'jpeg' => 'image/jpeg',
1878 'jpg' => 'image/jpeg',
1879 'jpe' => 'image/jpeg',
1880 'png' => 'image/png',
1881 'tiff' => 'image/tiff',
1882 'tif' => 'image/tiff',
1883 'css' => 'text/css',
1884 'html' => 'text/html',
1885 'htm' => 'text/html',
1886 'shtml' => 'text/html',
1887 'txt' => 'text/plain',
1888 'text' => 'text/plain',
1889 'log' => 'text/plain',
1890 'rtx' => 'text/richtext',
1891 'rtf' => 'text/rtf',
1892 'xml' => 'text/xml',
1893 'xsl' => 'text/xml',
1894 'mpeg' => 'video/mpeg',
1895 'mpg' => 'video/mpeg',
1896 'mpe' => 'video/mpeg',
1897 'qt' => 'video/quicktime',
1898 'mov' => 'video/quicktime',
1899 'avi' => 'video/x-msvideo',
1900 'movie' => 'video/x-sgi-movie',
1901 'doc' => 'application/msword',
1902 'word' => 'application/msword',
1903 'xl' => 'application/excel',
1904 'eml' => 'message/rfc822'
1905 );
1906
Andrey Andreev59c5b532012-03-01 14:09:51 +02001907 return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 }
1909
1910}
Derek Allard2067d1a2008-11-13 22:59:24 +00001911
1912/* End of file Email.php */
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001913/* Location: ./system/libraries/Email.php */