blob: c8a5b41af0370eeb7f1bdb81eb5c0c919f6dbc80 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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();
86 protected $_protocols = array('mail', 'sendmail', 'smtp');
87 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
88 protected $_bit_depths = array('7bit', '8bit');
89 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 /**
92 * Constructor - Sets Email Preferences
93 *
94 * The constructor can be passed an array of config values
95 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000096 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 if (count($config) > 0)
99 {
100 $this->initialize($config);
101 }
102 else
103 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200104 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200105 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107
Andrey Andreev59c5b532012-03-01 14:09:51 +0200108 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Initialize preferences
115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @param array
117 * @return void
118 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000119 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 foreach ($config as $key => $val)
122 {
123 if (isset($this->$key))
124 {
125 $method = 'set_'.$key;
126
127 if (method_exists($this, $method))
128 {
129 $this->$method($val);
130 }
131 else
132 {
133 $this->$key = $val;
134 }
135 }
136 }
Eric Barnes6113f542010-12-29 13:36:12 -0500137 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000138
Andrey Andreev76f15c92012-03-01 13:05:07 +0200139 $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200140 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000141
142 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
146
147 /**
148 * Initialize the Email Data
149 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800150 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200151 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000153 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200155 $this->_subject = '';
156 $this->_body = '';
157 $this->_finalbody = '';
158 $this->_header_str = '';
159 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500161 $this->_cc_array = array();
162 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $this->_headers = array();
164 $this->_debug_msg = array();
165
166 $this->_set_header('User-Agent', $this->useragent);
167 $this->_set_header('Date', $this->_set_date());
168
169 if ($clear_attachments !== FALSE)
170 {
171 $this->_attach_name = array();
172 $this->_attach_type = array();
173 $this->_attach_disp = array();
174 }
Eric Barnes6113f542010-12-29 13:36:12 -0500175
Greg Akera769deb2010-11-10 15:47:29 -0600176 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // --------------------------------------------------------------------
180
181 /**
182 * Set FROM
183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @param string
185 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200186 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000188 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200190 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200192 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194
195 if ($this->validate)
196 {
197 $this->validate_email($this->_str_to_array($from));
198 }
199
200 // prepare the display name
201 if ($name != '')
202 {
203 // only use Q encoding if there are characters that would require it
204 if ( ! preg_match('/[\200-\377]/', $name))
205 {
206 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000207 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209 else
210 {
211 $name = $this->_prep_q_encoding($name, TRUE);
212 }
213 }
214
215 $this->_set_header('From', $name.' <'.$from.'>');
216 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500217
Greg Akera769deb2010-11-10 15:47:29 -0600218 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // --------------------------------------------------------------------
222
223 /**
224 * Set Reply-to
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string
227 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200228 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000230 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200232 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200234 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 if ($this->validate)
238 {
239 $this->validate_email($this->_str_to_array($replyto));
240 }
241
242 if ($name == '')
243 {
244 $name = $replyto;
245 }
246
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200247 if (strncmp($name, '"', 1) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 $name = '"'.$name.'"';
250 }
251
252 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
253 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600254
255 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Set Recipients
262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200264 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000266 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $to = $this->_str_to_array($to);
269 $to = $this->clean_email($to);
270
271 if ($this->validate)
272 {
273 $this->validate_email($to);
274 }
275
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200276 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200278 $this->_set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 switch ($this->_get_protocol())
282 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200283 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000284 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200286 case 'sendmail':
287 case 'mail':
288 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 break;
290 }
Eric Barnes6113f542010-12-29 13:36:12 -0500291
Greg Akera769deb2010-11-10 15:47:29 -0600292 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // --------------------------------------------------------------------
296
297 /**
298 * Set CC
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200301 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000303 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 $cc = $this->_str_to_array($cc);
306 $cc = $this->clean_email($cc);
307
308 if ($this->validate)
309 {
310 $this->validate_email($cc);
311 }
312
Andrey Andreev59c5b532012-03-01 14:09:51 +0200313 $this->_set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000314
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200315 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 $this->_cc_array = $cc;
318 }
Eric Barnes6113f542010-12-29 13:36:12 -0500319
Greg Akera769deb2010-11-10 15:47:29 -0600320 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // --------------------------------------------------------------------
324
325 /**
326 * Set BCC
327 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @param string
329 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200330 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000332 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 if ($limit != '' && is_numeric($limit))
335 {
336 $this->bcc_batch_mode = TRUE;
337 $this->bcc_batch_size = $limit;
338 }
339
340 $bcc = $this->_str_to_array($bcc);
341 $bcc = $this->clean_email($bcc);
342
343 if ($this->validate)
344 {
345 $this->validate_email($bcc);
346 }
347
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200348 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $this->_bcc_array = $bcc;
351 }
352 else
353 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200354 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Eric Barnes6113f542010-12-29 13:36:12 -0500356
Greg Akera769deb2010-11-10 15:47:29 -0600357 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
361
362 /**
363 * Set Email Subject
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200366 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000368 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $subject = $this->_prep_q_encoding($subject);
371 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600372 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // --------------------------------------------------------------------
376
377 /**
378 * Set Body
379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200381 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000383 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200385 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200386
Andrey Andreevaf728622011-10-20 10:11:59 +0300387 /* strip slashes only if magic quotes is ON
388 if we do it with magic quotes OFF, it strips real, user-inputted chars.
389
390 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
391 it will probably not exist in future versions at all.
392 */
393 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200394 {
diegorivera33c32802011-10-19 02:56:15 -0200395 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200396 }
diegorivera33c32802011-10-19 02:56:15 -0200397
Greg Akera769deb2010-11-10 15:47:29 -0600398 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 // --------------------------------------------------------------------
402
403 /**
404 * Assign file attachments
405 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200407 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 */
trit10eb14d2011-11-23 15:48:21 -0500409 public function attach($filename, $disposition = '', $newname = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
trit151fc682011-11-23 07:30:06 -0500411 $this->_attach_name[] = array($filename, $newname);
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600412 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
tritd5269982011-11-23 17:22:44 -0500413 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600414 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Add a Header Item
421 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * @param string
423 * @param string
424 * @return void
425 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600426 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 $this->_headers[$header] = $value;
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
432
433 /**
434 * Convert a String to an Array
435 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @param string
437 * @return array
438 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600439 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 if ( ! is_array($email))
442 {
443 if (strpos($email, ',') !== FALSE)
444 {
445 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
446 }
447 else
448 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200449 $email = (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451 }
452 return $email;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * Set Multipart Value
459 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200461 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000463 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400465 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600466 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
470
471 /**
472 * Set Mailtype
473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200475 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000477 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600480 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
484
485 /**
486 * Set Wordwrap
487 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400488 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200489 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000491 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400493 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600494 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Set Protocol
501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200503 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000505 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200507 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600508 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // --------------------------------------------------------------------
512
513 /**
514 * Set Priority
515 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200516 * @param int
517 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000519 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200521 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600522 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
526
527 /**
528 * Set Newline Character
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200531 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000533 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200535 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600536 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * Set CRLF
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200545 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000547 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200549 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600550 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Set Message Boundary
557 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @return void
559 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600560 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200562 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
563 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // --------------------------------------------------------------------
567
568 /**
569 * Get the Message ID
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @return string
572 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600573 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200575 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200576 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
580
581 /**
582 * Get Mail Protocol
583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200585 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600587 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200590 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000591
592 if ($return == TRUE)
593 {
594 return $this->protocol;
595 }
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // --------------------------------------------------------------------
599
600 /**
601 * Get Mail Encoding
602 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @param bool
604 * @return string
605 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600606 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200608 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000609
610 foreach ($this->_base_charsets as $charset)
611 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200612 if (strncmp($charset, $this->charset, strlen($charset)) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 $this->_encoding = '7bit';
615 }
616 }
617
618 if ($return == TRUE)
619 {
620 return $this->_encoding;
621 }
622 }
623
624 // --------------------------------------------------------------------
625
626 /**
627 * Get content type (text/html/attachment)
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @return string
630 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600631 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200633 if ($this->mailtype === 'html' && count($this->_attach_name) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 return 'html';
636 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200637 elseif ($this->mailtype === 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 return 'html-attach';
640 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200641 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 return 'plain-attach';
644 }
645 else
646 {
647 return 'plain';
648 }
649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
652
653 /**
654 * Set RFC 822 Date
655 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * @return string
657 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600658 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200660 $timezone = date('Z');
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200661 $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200663 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664
Andrey Andreev59c5b532012-03-01 14:09:51 +0200665 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // --------------------------------------------------------------------
669
670 /**
671 * Mime message
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @return string
674 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600675 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200677 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 +0000678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Validate Email Address
684 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * @param string
686 * @return bool
687 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000688 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
690 if ( ! is_array($email))
691 {
patworkb0707982011-04-08 15:10:05 +0200692 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 return FALSE;
694 }
695
696 foreach ($email as $val)
697 {
698 if ( ! $this->valid_email($val))
699 {
patworkb0707982011-04-08 15:10:05 +0200700 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 return FALSE;
702 }
703 }
704
705 return TRUE;
706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 // --------------------------------------------------------------------
709
710 /**
711 * Email Validation
712 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 * @param string
714 * @return bool
715 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000716 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200718 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 +0000719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 // --------------------------------------------------------------------
722
723 /**
724 * Clean Extended Email Address: Joe Smith <joe@smith.com>
725 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 * @param string
727 * @return string
728 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000729 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
731 if ( ! is_array($email))
732 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200733 return (preg_match('/\<(.*)\>/', $email, $match)) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
735
736 $clean_email = array();
737
738 foreach ($email as $addy)
739 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200740 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
742
743 return $clean_email;
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
747
748 /**
749 * Build alternative plain text message
750 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000751 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * in plain-text headers of HTML-formatted emails.
753 * If the user hasn't specified his own alternative message
754 * it creates one by stripping the HTML
755 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 * @return string
757 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600758 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200760 if ($this->alt_message != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
762 return $this->word_wrap($this->alt_message, '76');
763 }
764
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200765 $body = (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match)) ? $match[1] : $this->_body;
766 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000767
768 for ($i = 20; $i >= 3; $i--)
769 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200770 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200773 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 // --------------------------------------------------------------------
777
778 /**
779 * Word Wrap
780 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200782 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 * @return string
784 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000785 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 // Se the character limit
788 if ($charlim == '')
789 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200790 $charlim = ($this->wrapchars == "") ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
792
793 // Reduce multiple spaces
794 $str = preg_replace("| +|", " ", $str);
795
796 // Standardize newlines
797 if (strpos($str, "\r") !== FALSE)
798 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200799 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 }
801
802 // If the current word is surrounded by {unwrap} tags we'll
803 // strip the entire chunk and replace it with a marker.
804 $unwrap = array();
805 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
806 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200807 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200809 $unwrap[] = $matches[1][$i];
810 $str = str_replace($matches[1][$i], "{{unwrapped".$i."}}", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
812 }
813
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000814 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500816 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 $str = wordwrap($str, $charlim, "\n", FALSE);
818
819 // Split the string into individual lines of text and cycle through them
820 $output = "";
821 foreach (explode("\n", $str) as $line)
822 {
823 // Is the line within the allowed character count?
824 // If so we'll join it to the output and continue
825 if (strlen($line) <= $charlim)
826 {
827 $output .= $line.$this->newline;
828 continue;
829 }
830
831 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200832 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 {
834 // If the over-length word is a URL we won't wrap it
835 if (preg_match("!\[url.+\]|://|wwww.!", $line))
836 {
837 break;
838 }
839
840 // Trim the word down
841 $temp .= substr($line, 0, $charlim-1);
842 $line = substr($line, $charlim-1);
843 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200844 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000845
846 // If $temp contains data it means we had to split up an over-length
847 // word into smaller chunks so we'll add it back to our current line
848 if ($temp != '')
849 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200850 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
852
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200853 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 }
855
856 // Put our markers back
857 if (count($unwrap) > 0)
858 {
859 foreach ($unwrap as $key => $val)
860 {
861 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
862 }
863 }
864
865 return $output;
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 // --------------------------------------------------------------------
869
870 /**
871 * Build final headers
872 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @return string
874 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600875 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 {
877 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
878 $this->_set_header('X-Mailer', $this->useragent);
879 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
880 $this->_set_header('Message-ID', $this->_get_message_id());
881 $this->_set_header('Mime-Version', '1.0');
882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 // --------------------------------------------------------------------
885
886 /**
887 * Write Headers as a string
888 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * @return void
890 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600891 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200893 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
895 $this->_subject = $this->_headers['Subject'];
896 unset($this->_headers['Subject']);
897 }
898
899 reset($this->_headers);
900 $this->_header_str = "";
901
Pascal Kriete14287f32011-02-14 13:39:34 -0500902 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
904 $val = trim($val);
905
906 if ($val != "")
907 {
908 $this->_header_str .= $key.": ".$val.$this->newline;
909 }
910 }
911
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200912 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Derek Jones1d890882009-02-10 20:32:14 +0000914 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 // --------------------------------------------------------------------
919
920 /**
921 * Build Final Body and attachments
922 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * @return void
924 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600925 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200927 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
929 $this->_body = $this->word_wrap($this->_body);
930 }
931
932 $this->_set_boundaries();
933 $this->_write_headers();
934
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200935 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500936 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000937
938 switch ($this->_get_content_type())
939 {
940 case 'plain' :
941
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200942 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline
943 . "Content-Transfer-Encoding: " . $this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000944
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200945 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
947 $this->_header_str .= $hdr;
948 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Brandon Jones485d7412010-11-09 16:38:17 -0500950 else
951 {
952 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
953 }
Eric Barnes6113f542010-12-29 13:36:12 -0500954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 return;
956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 case 'html' :
958
959 if ($this->send_multipart === FALSE)
960 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200961 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline
962 . "Content-Transfer-Encoding: quoted-printable";
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
964 else
965 {
Derek Jonesa45e7612009-02-10 18:33:01 +0000966 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000967
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200968 $body .= $this->_get_mime_message() . $this->newline . $this->newline
969 . "--" . $this->_alt_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200971 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
972 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
973 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500974
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200975 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
976 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
Eric Barnes6113f542010-12-29 13:36:12 -0500978
Brandon Jones485d7412010-11-09 16:38:17 -0500979 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500980
981
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200982 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500985 }
986 else
987 {
988 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991
992 if ($this->send_multipart !== FALSE)
993 {
Brandon Jones485d7412010-11-09 16:38:17 -0500994 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 return;
998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 case 'plain-attach' :
1000
Derek Jonesa45e7612009-02-10 18:33:01 +00001001 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001002
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;
Eric Barnes6113f542010-12-29 13:36:12 -05001006 }
1007
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001008 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1009 . "--" . $this->_atc_boundary . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001010
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001011 . "Content-Type: text/plain; charset=" . $this->charset . $this->newline
1012 . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001013
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001014 . $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
1016 break;
1017 case 'html-attach' :
1018
Derek Jonesa45e7612009-02-10 18:33:01 +00001019 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001020
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001021 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
1023 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
1025
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001026 $body .= $this->_get_mime_message() . $this->newline . $this->newline
1027 . "--" . $this->_atc_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001028
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001029 . "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline
1030 . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001031
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
1034 . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001035
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001036 . "Content-Type: text/html; charset=" . $this->charset . $this->newline
1037 . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001038
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001039 . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline
1040 . "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001041
1042 break;
1043 }
1044
1045 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001046 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
tritbc4ac992011-11-23 07:19:41 -05001048 $filename = $this->_attach_name[$i][0];
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001049 $basename = (is_null($this->_attach_name[$i][1])) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 $ctype = $this->_attach_type[$i];
1051
tritbc4ac992011-11-23 07:19:41 -05001052 if ( ! file_exists($filename))
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 {
tritbc4ac992011-11-23 07:19:41 -05001054 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 return FALSE;
1056 }
1057
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001058 $attachment[$z++] = "--".$this->_atc_boundary.$this->newline
1059 . "Content-type: ".$ctype."; "
1060 . "name=\"".$basename."\"".$this->newline
1061 . "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline
1062 . "Content-Transfer-Encoding: base64".$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001063
tritbc4ac992011-11-23 07:19:41 -05001064 $file = filesize($filename) +1;
Derek Allard2067d1a2008-11-13 22:59:24 +00001065
tritbc4ac992011-11-23 07:19:41 -05001066 if ( ! $fp = fopen($filename, FOPEN_READ))
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
tritbc4ac992011-11-23 07:19:41 -05001068 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 return FALSE;
1070 }
1071
1072 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1073 fclose($fp);
1074 }
1075
Brandon Jones485d7412010-11-09 16:38:17 -05001076 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001077 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr . $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 return;
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 // --------------------------------------------------------------------
1082
1083 /**
1084 * Prep Quoted Printable
1085 *
1086 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1087 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001090 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @return string
1092 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001093 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 // Set the character limit
1096 // Don't allow over 76, as that will make servers and MUAs barf
1097 // all over quoted-printable data
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001098 if ($charlim == '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001100 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 }
1102
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001103 // Reduce multiple spaces & remove nulls
1104 $str = preg_replace(array("| +|", '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001105
1106 // Standardize newlines
1107 if (strpos($str, "\r") !== FALSE)
1108 {
1109 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1110 }
1111
1112 // We are intentionally wrapping so mail servers will encode characters
1113 // properly and MUAs will behave, so {unwrap} must go!
1114 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $escape = '=';
1117 $output = '';
1118
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001119 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
1121 $length = strlen($line);
1122 $temp = '';
1123
1124 // Loop through each character in the line to add soft-wrap
1125 // characters at the end of a line " =\r\n" and add the newly
1126 // processed line(s) to the output (see comment on $crlf class property)
1127 for ($i = 0; $i < $length; $i++)
1128 {
1129 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $ascii = ord($char);
1132
1133 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001134 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001136 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001138 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001140 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
1142
1143 // If we're at the character limit, add the line to the output,
1144 // reset our temp variable, and keep on chuggin'
1145 if ((strlen($temp) + strlen($char)) >= $charlim)
1146 {
1147 $output .= $temp.$escape.$this->crlf;
1148 $temp = '';
1149 }
1150
1151 // Add the character to our temporary line
1152 $temp .= $char;
1153 }
1154
1155 // Add our completed line to the output
1156 $output .= $temp.$this->crlf;
1157 }
1158
1159 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001160 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 }
1162
1163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 /**
1166 * Prep Q Encoding
1167 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001168 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * but not identical to quoted-printable, so it has its own method
1170 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001171 * @param string
1172 * @param bool set to TRUE for processing From: headers
1173 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001175 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
1177 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1178
1179 // Line length must not exceed 76 characters, so we adjust for
1180 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1181 $limit = 75 - 7 - strlen($this->charset);
1182
1183 // these special characters must be converted too
1184 $convert = array('_', '=', '?');
1185
1186 if ($from === TRUE)
1187 {
1188 $convert[] = ',';
1189 $convert[] = ';';
1190 }
1191
1192 $output = '';
1193 $temp = '';
1194
1195 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1196 {
1197 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001198 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 $ascii = ord($char);
1200
1201 // convert ALL non-printable ASCII characters and our specials
1202 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1203 {
1204 $char = '='.dechex($ascii);
1205 }
1206
1207 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001208 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 $char = '_';
1211 }
1212
1213 // If we're at the character limit, add the line to the output,
1214 // reset our temp variable, and keep on chuggin'
1215 if ((strlen($temp) + strlen($char)) >= $limit)
1216 {
1217 $output .= $temp.$this->crlf;
1218 $temp = '';
1219 }
1220
1221 // Add the character to our temporary line
1222 $temp .= $char;
1223 }
1224
1225 $str = $output.$temp;
1226
1227 // wrap each line with the shebang, charset, and transfer encoding
1228 // the preceding space on successive lines is required for header "folding"
Andrey Andreev59c5b532012-03-01 14:09:51 +02001229 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
1231
1232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 /**
1235 * Send Email
1236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 * @return bool
1238 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001239 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 {
1241 if ($this->_replyto_flag == FALSE)
1242 {
1243 $this->reply_to($this->_headers['From']);
1244 }
1245
Andrey Andreev76f15c92012-03-01 13:05:07 +02001246 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1247 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1248 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
patworkb0707982011-04-08 15:10:05 +02001250 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 return FALSE;
1252 }
1253
1254 $this->_build_headers();
1255
Andrey Andreev76f15c92012-03-01 13:05:07 +02001256 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001258 return $this->batch_bcc_send();
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 }
1260
1261 $this->_build_message();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001262 return $this->_spool_email();
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 }
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 // --------------------------------------------------------------------
1266
1267 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001268 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001270 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001272 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001274 $float = $this->bcc_batch_size - 1;
1275 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 $chunk = array();
1277
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001278 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
1280 if (isset($this->_bcc_array[$i]))
1281 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001282 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
1284
1285 if ($i == $float)
1286 {
1287 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001288 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001289 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 }
1291
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001292 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
1294 $chunk[] = substr($set, 1);
1295 }
1296 }
1297
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001298 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
1300 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001301
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001302 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001303
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001304 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001306 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
1308 else
1309 {
1310 $this->_bcc_array = $bcc;
1311 }
1312
1313 $this->_build_message();
1314 $this->_spool_email();
1315 }
1316 }
Barry Mienydd671972010-10-04 16:33:58 +02001317
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 // --------------------------------------------------------------------
1319
1320 /**
1321 * Unwrap special elements
1322 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 * @return void
1324 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001325 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
1327 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 // --------------------------------------------------------------------
1331
1332 /**
1333 * Strip line-breaks via callback
1334 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 * @return string
1336 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001337 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 {
1339 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1340 {
1341 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1342 }
1343
1344 return $matches[1];
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 // --------------------------------------------------------------------
1348
1349 /**
1350 * Spool mail to the mail server
1351 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 * @return bool
1353 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001354 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 {
1356 $this->_unwrap_specials();
1357
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001358 $method = '_send_with_' . $this->_get_protocol();
1359 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001361 $this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 }
1363
patworkb0707982011-04-08 15:10:05 +02001364 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 return TRUE;
1366 }
Barry Mienydd671972010-10-04 16:33:58 +02001367
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 // --------------------------------------------------------------------
1369
1370 /**
1371 * Send using mail()
1372 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 * @return bool
1374 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001375 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
1377 if ($this->_safe_mode == TRUE)
1378 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001379 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 }
1381 else
1382 {
1383 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1384 // we've encountered servers that seem to require it to be in place.
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001385 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 +00001386 }
1387 }
Barry Mienydd671972010-10-04 16:33:58 +02001388
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 // --------------------------------------------------------------------
1390
1391 /**
1392 * Send using Sendmail
1393 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 * @return bool
1395 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001396 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
1398 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1399
Derek Jones4cefaa42009-04-29 19:13:56 +00001400 if ($fp === FALSE OR $fp === NULL)
1401 {
1402 // server probably has popen disabled, so nothing we can do to get a verbose error.
1403 return FALSE;
1404 }
Derek Jones71141ce2010-03-02 16:41:20 -06001405
Derek Jonesc630bcf2008-11-17 21:09:45 +00001406 fputs($fp, $this->_header_str);
1407 fputs($fp, $this->_finalbody);
1408
Barry Mienydd671972010-10-04 16:33:58 +02001409 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001410
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001411 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 {
patworkb0707982011-04-08 15:10:05 +02001413 $this->_set_error_message('lang:email_exit_status', $status);
1414 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 return FALSE;
1416 }
1417
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 return TRUE;
1419 }
Barry Mienydd671972010-10-04 16:33:58 +02001420
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 // --------------------------------------------------------------------
1422
1423 /**
1424 * Send using SMTP
1425 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 * @return bool
1427 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001428 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 {
1430 if ($this->smtp_host == '')
1431 {
patworkb0707982011-04-08 15:10:05 +02001432 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 return FALSE;
1434 }
1435
1436 $this->_smtp_connect();
1437 $this->_smtp_authenticate();
1438
1439 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1440
Pascal Kriete14287f32011-02-14 13:39:34 -05001441 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
1443 $this->_send_command('to', $val);
1444 }
1445
1446 if (count($this->_cc_array) > 0)
1447 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001448 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 {
1450 if ($val != "")
1451 {
1452 $this->_send_command('to', $val);
1453 }
1454 }
1455 }
1456
1457 if (count($this->_bcc_array) > 0)
1458 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001459 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 {
1461 if ($val != "")
1462 {
1463 $this->_send_command('to', $val);
1464 }
1465 }
1466 }
1467
1468 $this->_send_command('data');
1469
1470 // perform dot transformation on any lines that begin with a dot
1471 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1472
1473 $this->_send_data('.');
1474
1475 $reply = $this->_get_smtp_data();
1476
1477 $this->_set_error_message($reply);
1478
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001479 if (strncmp($reply, '250', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
patworkb0707982011-04-08 15:10:05 +02001481 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 return FALSE;
1483 }
1484
1485 $this->_send_command('quit');
1486 return TRUE;
1487 }
Barry Mienydd671972010-10-04 16:33:58 +02001488
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 // --------------------------------------------------------------------
1490
1491 /**
1492 * SMTP Connect
1493 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 * @param string
1495 * @return string
1496 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001497 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 {
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001499 $ssl = ($this->smtp_crypto == 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001500
Radu Potopbbf04b02011-09-28 13:57:51 +03001501 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 $this->smtp_port,
1503 $errno,
1504 $errstr,
1505 $this->smtp_timeout);
1506
Pascal Kriete14287f32011-02-14 13:39:34 -05001507 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
patworkb0707982011-04-08 15:10:05 +02001509 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 return FALSE;
1511 }
1512
1513 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001514
1515 if ($this->smtp_crypto == 'tls')
1516 {
1517 $this->_send_command('hello');
1518 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001519
Radu Potop4c589ae2011-09-29 10:19:55 +03001520 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001521
Sean Fishere862ddd2011-10-26 22:45:00 -03001522 if ($crypto !== TRUE)
1523 {
1524 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1525 return FALSE;
1526 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001527 }
1528
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 return $this->_send_command('hello');
1530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 // --------------------------------------------------------------------
1533
1534 /**
1535 * Send SMTP command
1536 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 * @param string
1538 * @param string
1539 * @return string
1540 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001541 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
1543 switch ($cmd)
1544 {
1545 case 'hello' :
1546
1547 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1548 $this->_send_data('EHLO '.$this->_get_hostname());
1549 else
1550 $this->_send_data('HELO '.$this->_get_hostname());
1551
1552 $resp = 250;
1553 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001554 case 'starttls' :
1555
1556 $this->_send_data('STARTTLS');
1557
1558 $resp = 220;
1559 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 case 'from' :
1561
1562 $this->_send_data('MAIL FROM:<'.$data.'>');
1563
1564 $resp = 250;
1565 break;
1566 case 'to' :
1567
1568 $this->_send_data('RCPT TO:<'.$data.'>');
1569
1570 $resp = 250;
1571 break;
1572 case 'data' :
1573
1574 $this->_send_data('DATA');
1575
1576 $resp = 354;
1577 break;
1578 case 'quit' :
1579
1580 $this->_send_data('QUIT');
1581
1582 $resp = 221;
1583 break;
1584 }
1585
1586 $reply = $this->_get_smtp_data();
1587
1588 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1589
1590 if (substr($reply, 0, 3) != $resp)
1591 {
patworkb0707982011-04-08 15:10:05 +02001592 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 return FALSE;
1594 }
1595
1596 if ($cmd == 'quit')
1597 {
1598 fclose($this->_smtp_connect);
1599 }
1600
1601 return TRUE;
1602 }
Barry Mienydd671972010-10-04 16:33:58 +02001603
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 // --------------------------------------------------------------------
1605
1606 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001607 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 * @return bool
1610 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001611 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
1613 if ( ! $this->_smtp_auth)
1614 {
1615 return TRUE;
1616 }
1617
Andrey Andreev76f15c92012-03-01 13:05:07 +02001618 if ($this->smtp_user == '' && $this->smtp_pass == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 {
patworkb0707982011-04-08 15:10:05 +02001620 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 return FALSE;
1622 }
1623
1624 $this->_send_data('AUTH LOGIN');
1625
1626 $reply = $this->_get_smtp_data();
1627
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001628 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 {
patworkb0707982011-04-08 15:10:05 +02001630 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 return FALSE;
1632 }
1633
1634 $this->_send_data(base64_encode($this->smtp_user));
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_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 return FALSE;
1642 }
1643
1644 $this->_send_data(base64_encode($this->smtp_pass));
1645
1646 $reply = $this->_get_smtp_data();
1647
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001648 if (strncmp($reply, '235', 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_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 return FALSE;
1652 }
1653
1654 return TRUE;
1655 }
Barry Mienydd671972010-10-04 16:33:58 +02001656
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 // --------------------------------------------------------------------
1658
1659 /**
1660 * Send SMTP data
1661 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 * @return bool
1663 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001664 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
1666 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1667 {
patworkb0707982011-04-08 15:10:05 +02001668 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 return FALSE;
1670 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001671
1672 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 }
Barry Mienydd671972010-10-04 16:33:58 +02001674
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 // --------------------------------------------------------------------
1676
1677 /**
1678 * Get SMTP data
1679 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 * @return string
1681 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001682 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 {
1684 $data = "";
1685
1686 while ($str = fgets($this->_smtp_connect, 512))
1687 {
1688 $data .= $str;
1689
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001690 if ($str[3] == " ")
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
1692 break;
1693 }
1694 }
1695
1696 return $data;
1697 }
Barry Mienydd671972010-10-04 16:33:58 +02001698
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 // --------------------------------------------------------------------
1700
1701 /**
1702 * Get Hostname
1703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 * @return string
1705 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001706 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001708 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 // --------------------------------------------------------------------
1712
1713 /**
1714 * Get IP
1715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 * @return string
1717 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001718 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
1720 if ($this->_IP !== FALSE)
1721 {
1722 return $this->_IP;
1723 }
1724
Andrey Andreev76f15c92012-03-01 13:05:07 +02001725 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1726 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001727 if ($cip) $this->_IP = $cip;
1728 elseif ($rip) $this->_IP = $rip;
1729 else
1730 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001731 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001732 if ($fip)
1733 {
1734 $this->_IP = $fip;
1735 }
1736 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001737
Robin Sowell76b369e2010-03-19 11:15:28 -04001738 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
1740 $x = explode(',', $this->_IP);
1741 $this->_IP = end($x);
1742 }
1743
Andrey Andreev59c5b532012-03-01 14:09:51 +02001744 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 +00001745 {
1746 $this->_IP = '0.0.0.0';
1747 }
1748
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 return $this->_IP;
1750 }
Barry Mienydd671972010-10-04 16:33:58 +02001751
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 // --------------------------------------------------------------------
1753
1754 /**
1755 * Get Debug Message
1756 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 * @return string
1758 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001759 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 {
1761 $msg = '';
1762
1763 if (count($this->_debug_msg) > 0)
1764 {
1765 foreach ($this->_debug_msg as $val)
1766 {
1767 $msg .= $val;
1768 }
1769 }
1770
Andrey Andreev59c5b532012-03-01 14:09:51 +02001771 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 }
Barry Mienydd671972010-10-04 16:33:58 +02001773
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 // --------------------------------------------------------------------
1775
1776 /**
1777 * Set Message
1778 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001780 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001782 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 {
1784 $CI =& get_instance();
1785 $CI->lang->load('email');
1786
Andrey Andreev76f15c92012-03-01 13:05:07 +02001787 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 {
1789 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1790 }
1791 else
1792 {
1793 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1794 }
1795 }
Barry Mienydd671972010-10-04 16:33:58 +02001796
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 // --------------------------------------------------------------------
1798
1799 /**
1800 * Mime Types
1801 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 * @param string
1803 * @return string
1804 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001805 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001807 $mimes = array(
1808 'hqx' => 'application/mac-binhex40',
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 'cpt' => 'application/mac-compactpro',
1810 'doc' => 'application/msword',
1811 'bin' => 'application/macbinary',
1812 'dms' => 'application/octet-stream',
1813 'lha' => 'application/octet-stream',
1814 'lzh' => 'application/octet-stream',
1815 'exe' => 'application/octet-stream',
1816 'class' => 'application/octet-stream',
1817 'psd' => 'application/octet-stream',
1818 'so' => 'application/octet-stream',
1819 'sea' => 'application/octet-stream',
1820 'dll' => 'application/octet-stream',
1821 'oda' => 'application/oda',
1822 'pdf' => 'application/pdf',
1823 'ai' => 'application/postscript',
1824 'eps' => 'application/postscript',
1825 'ps' => 'application/postscript',
1826 'smi' => 'application/smil',
1827 'smil' => 'application/smil',
1828 'mif' => 'application/vnd.mif',
1829 'xls' => 'application/vnd.ms-excel',
1830 'ppt' => 'application/vnd.ms-powerpoint',
1831 'wbxml' => 'application/vnd.wap.wbxml',
1832 'wmlc' => 'application/vnd.wap.wmlc',
1833 'dcr' => 'application/x-director',
1834 'dir' => 'application/x-director',
1835 'dxr' => 'application/x-director',
1836 'dvi' => 'application/x-dvi',
1837 'gtar' => 'application/x-gtar',
1838 'php' => 'application/x-httpd-php',
1839 'php4' => 'application/x-httpd-php',
1840 'php3' => 'application/x-httpd-php',
1841 'phtml' => 'application/x-httpd-php',
1842 'phps' => 'application/x-httpd-php-source',
1843 'js' => 'application/x-javascript',
1844 'swf' => 'application/x-shockwave-flash',
1845 'sit' => 'application/x-stuffit',
1846 'tar' => 'application/x-tar',
1847 'tgz' => 'application/x-tar',
1848 'xhtml' => 'application/xhtml+xml',
1849 'xht' => 'application/xhtml+xml',
1850 'zip' => 'application/zip',
1851 'mid' => 'audio/midi',
1852 'midi' => 'audio/midi',
1853 'mpga' => 'audio/mpeg',
1854 'mp2' => 'audio/mpeg',
1855 'mp3' => 'audio/mpeg',
1856 'aif' => 'audio/x-aiff',
1857 'aiff' => 'audio/x-aiff',
1858 'aifc' => 'audio/x-aiff',
1859 'ram' => 'audio/x-pn-realaudio',
1860 'rm' => 'audio/x-pn-realaudio',
1861 'rpm' => 'audio/x-pn-realaudio-plugin',
1862 'ra' => 'audio/x-realaudio',
1863 'rv' => 'video/vnd.rn-realvideo',
1864 'wav' => 'audio/x-wav',
1865 'bmp' => 'image/bmp',
1866 'gif' => 'image/gif',
1867 'jpeg' => 'image/jpeg',
1868 'jpg' => 'image/jpeg',
1869 'jpe' => 'image/jpeg',
1870 'png' => 'image/png',
1871 'tiff' => 'image/tiff',
1872 'tif' => 'image/tiff',
1873 'css' => 'text/css',
1874 'html' => 'text/html',
1875 'htm' => 'text/html',
1876 'shtml' => 'text/html',
1877 'txt' => 'text/plain',
1878 'text' => 'text/plain',
1879 'log' => 'text/plain',
1880 'rtx' => 'text/richtext',
1881 'rtf' => 'text/rtf',
1882 'xml' => 'text/xml',
1883 'xsl' => 'text/xml',
1884 'mpeg' => 'video/mpeg',
1885 'mpg' => 'video/mpeg',
1886 'mpe' => 'video/mpeg',
1887 'qt' => 'video/quicktime',
1888 'mov' => 'video/quicktime',
1889 'avi' => 'video/x-msvideo',
1890 'movie' => 'video/x-sgi-movie',
1891 'doc' => 'application/msword',
1892 'word' => 'application/msword',
1893 'xl' => 'application/excel',
1894 'eml' => 'message/rfc822'
1895 );
1896
Andrey Andreev59c5b532012-03-01 14:09:51 +02001897 return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 }
1899
1900}
Derek Allard2067d1a2008-11-13 22:59:24 +00001901
1902/* End of file Email.php */
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001903/* Location: ./system/libraries/Email.php */