blob: 48c3bf3ab16d5d3fc1adcac6af1c09c095dc06b8 [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 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200410 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
trit151fc682011-11-23 07:30:06 -0500412 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500413 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200414 $this->_attach_type[] = $mime;
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];
Matteo Mattei5688d582012-03-13 19:29:29 +01001052 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Matteo Matteic3b36f42012-03-26 10:27:17 +02001054 if ($this->_attach_type[$i] == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001056 if ( ! file_exists($filename))
1057 {
1058 $this->_set_error_message('lang:email_attachment_missing', $filename);
1059 return FALSE;
1060 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001061
Matteo Mattei5688d582012-03-13 19:29:29 +01001062 $file = filesize($filename) +1;
1063
1064 if ( ! $fp = fopen($filename, FOPEN_READ))
1065 {
1066 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1067 return FALSE;
1068 }
1069
Matteo Matteic3b36f42012-03-26 10:27:17 +02001070 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001071 $file_content = fread($fp, $file);
1072 fclose($fp);
1073 }
1074 else
1075 {
1076 $file_content =& $this->_attach_content[$i];
1077 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001078 $attachment[$z++] = "--".$this->_atc_boundary.$this->newline
1079 . "Content-type: ".$ctype."; "
1080 . "name=\"".$basename."\"".$this->newline
1081 . "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline
1082 . "Content-Transfer-Encoding: base64".$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001083
Matteo Mattei5688d582012-03-13 19:29:29 +01001084 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
1086
Brandon Jones485d7412010-11-09 16:38:17 -05001087 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001088 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr . $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 return;
1090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // --------------------------------------------------------------------
1093
1094 /**
1095 * Prep Quoted Printable
1096 *
1097 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1098 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1099 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001101 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @return string
1103 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001104 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
1106 // Set the character limit
1107 // Don't allow over 76, as that will make servers and MUAs barf
1108 // all over quoted-printable data
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001109 if ($charlim == '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001111 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 }
1113
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001114 // Reduce multiple spaces & remove nulls
1115 $str = preg_replace(array("| +|", '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116
1117 // Standardize newlines
1118 if (strpos($str, "\r") !== FALSE)
1119 {
1120 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1121 }
1122
1123 // We are intentionally wrapping so mail servers will encode characters
1124 // properly and MUAs will behave, so {unwrap} must go!
1125 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 $escape = '=';
1128 $output = '';
1129
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001130 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 $length = strlen($line);
1133 $temp = '';
1134
1135 // Loop through each character in the line to add soft-wrap
1136 // characters at the end of a line " =\r\n" and add the newly
1137 // processed line(s) to the output (see comment on $crlf class property)
1138 for ($i = 0; $i < $length; $i++)
1139 {
1140 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001141 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $ascii = ord($char);
1143
1144 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001145 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001147 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001149 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001151 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
1153
1154 // If we're at the character limit, add the line to the output,
1155 // reset our temp variable, and keep on chuggin'
1156 if ((strlen($temp) + strlen($char)) >= $charlim)
1157 {
1158 $output .= $temp.$escape.$this->crlf;
1159 $temp = '';
1160 }
1161
1162 // Add the character to our temporary line
1163 $temp .= $char;
1164 }
1165
1166 // Add our completed line to the output
1167 $output .= $temp.$this->crlf;
1168 }
1169
1170 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001171 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
1173
1174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 /**
1177 * Prep Q Encoding
1178 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001179 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 * but not identical to quoted-printable, so it has its own method
1181 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001182 * @param string
1183 * @param bool set to TRUE for processing From: headers
1184 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001186 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
1188 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1189
1190 // Line length must not exceed 76 characters, so we adjust for
1191 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1192 $limit = 75 - 7 - strlen($this->charset);
1193
1194 // these special characters must be converted too
1195 $convert = array('_', '=', '?');
1196
1197 if ($from === TRUE)
1198 {
1199 $convert[] = ',';
1200 $convert[] = ';';
1201 }
1202
1203 $output = '';
1204 $temp = '';
1205
1206 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1207 {
1208 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001209 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 $ascii = ord($char);
1211
1212 // convert ALL non-printable ASCII characters and our specials
1213 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1214 {
1215 $char = '='.dechex($ascii);
1216 }
1217
1218 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001219 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
1221 $char = '_';
1222 }
1223
1224 // If we're at the character limit, add the line to the output,
1225 // reset our temp variable, and keep on chuggin'
1226 if ((strlen($temp) + strlen($char)) >= $limit)
1227 {
1228 $output .= $temp.$this->crlf;
1229 $temp = '';
1230 }
1231
1232 // Add the character to our temporary line
1233 $temp .= $char;
1234 }
1235
1236 $str = $output.$temp;
1237
1238 // wrap each line with the shebang, charset, and transfer encoding
1239 // the preceding space on successive lines is required for header "folding"
Andrey Andreev59c5b532012-03-01 14:09:51 +02001240 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 }
1242
1243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 /**
1246 * Send Email
1247 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 * @return bool
1249 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001250 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
1252 if ($this->_replyto_flag == FALSE)
1253 {
1254 $this->reply_to($this->_headers['From']);
1255 }
1256
Andrey Andreev76f15c92012-03-01 13:05:07 +02001257 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1258 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1259 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
patworkb0707982011-04-08 15:10:05 +02001261 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 return FALSE;
1263 }
1264
1265 $this->_build_headers();
1266
Andrey Andreev76f15c92012-03-01 13:05:07 +02001267 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001269 return $this->batch_bcc_send();
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
1271
1272 $this->_build_message();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001273 return $this->_spool_email();
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 // --------------------------------------------------------------------
1277
1278 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001279 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001281 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001283 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001285 $float = $this->bcc_batch_size - 1;
1286 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 $chunk = array();
1288
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001289 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
1291 if (isset($this->_bcc_array[$i]))
1292 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001293 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
1295
1296 if ($i == $float)
1297 {
1298 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001299 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001300 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 }
1302
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001303 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
1305 $chunk[] = substr($set, 1);
1306 }
1307 }
1308
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001309 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 {
1311 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001312
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001313 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001314
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001315 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001317 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 }
1319 else
1320 {
1321 $this->_bcc_array = $bcc;
1322 }
1323
1324 $this->_build_message();
1325 $this->_spool_email();
1326 }
1327 }
Barry Mienydd671972010-10-04 16:33:58 +02001328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 // --------------------------------------------------------------------
1330
1331 /**
1332 * Unwrap special elements
1333 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 * @return void
1335 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001336 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 // --------------------------------------------------------------------
1342
1343 /**
1344 * Strip line-breaks via callback
1345 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 * @return string
1347 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001348 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
1350 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1351 {
1352 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1353 }
1354
1355 return $matches[1];
1356 }
Barry Mienydd671972010-10-04 16:33:58 +02001357
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 // --------------------------------------------------------------------
1359
1360 /**
1361 * Spool mail to the mail server
1362 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 * @return bool
1364 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001365 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 {
1367 $this->_unwrap_specials();
1368
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001369 $method = '_send_with_' . $this->_get_protocol();
1370 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001372 $this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001373 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 }
1375
patworkb0707982011-04-08 15:10:05 +02001376 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 return TRUE;
1378 }
Barry Mienydd671972010-10-04 16:33:58 +02001379
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 // --------------------------------------------------------------------
1381
1382 /**
1383 * Send using mail()
1384 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 * @return bool
1386 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001387 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
1389 if ($this->_safe_mode == TRUE)
1390 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001391 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 }
1393 else
1394 {
1395 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1396 // we've encountered servers that seem to require it to be in place.
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001397 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 +00001398 }
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 // --------------------------------------------------------------------
1402
1403 /**
1404 * Send using Sendmail
1405 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 * @return bool
1407 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001408 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
1410 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1411
Derek Jones4cefaa42009-04-29 19:13:56 +00001412 if ($fp === FALSE OR $fp === NULL)
1413 {
1414 // server probably has popen disabled, so nothing we can do to get a verbose error.
1415 return FALSE;
1416 }
Derek Jones71141ce2010-03-02 16:41:20 -06001417
Derek Jonesc630bcf2008-11-17 21:09:45 +00001418 fputs($fp, $this->_header_str);
1419 fputs($fp, $this->_finalbody);
1420
Barry Mienydd671972010-10-04 16:33:58 +02001421 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001422
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001423 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 {
patworkb0707982011-04-08 15:10:05 +02001425 $this->_set_error_message('lang:email_exit_status', $status);
1426 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 return FALSE;
1428 }
1429
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 return TRUE;
1431 }
Barry Mienydd671972010-10-04 16:33:58 +02001432
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 // --------------------------------------------------------------------
1434
1435 /**
1436 * Send using SMTP
1437 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 * @return bool
1439 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001440 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001441 {
1442 if ($this->smtp_host == '')
1443 {
patworkb0707982011-04-08 15:10:05 +02001444 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 return FALSE;
1446 }
1447
Diogo Osório593f7982012-03-02 18:04:17 +00001448 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001449 {
1450 return FALSE;
1451 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001452
1453 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1454
Pascal Kriete14287f32011-02-14 13:39:34 -05001455 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 {
1457 $this->_send_command('to', $val);
1458 }
1459
1460 if (count($this->_cc_array) > 0)
1461 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001462 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 {
1464 if ($val != "")
1465 {
1466 $this->_send_command('to', $val);
1467 }
1468 }
1469 }
1470
1471 if (count($this->_bcc_array) > 0)
1472 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001473 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 {
1475 if ($val != "")
1476 {
1477 $this->_send_command('to', $val);
1478 }
1479 }
1480 }
1481
1482 $this->_send_command('data');
1483
1484 // perform dot transformation on any lines that begin with a dot
1485 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1486
1487 $this->_send_data('.');
1488
1489 $reply = $this->_get_smtp_data();
1490
1491 $this->_set_error_message($reply);
1492
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001493 if (strncmp($reply, '250', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 {
patworkb0707982011-04-08 15:10:05 +02001495 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 return FALSE;
1497 }
1498
1499 $this->_send_command('quit');
1500 return TRUE;
1501 }
Barry Mienydd671972010-10-04 16:33:58 +02001502
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 // --------------------------------------------------------------------
1504
1505 /**
1506 * SMTP Connect
1507 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 * @param string
1509 * @return string
1510 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001511 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001513 $ssl = ($this->smtp_crypto == 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001514
Radu Potopbbf04b02011-09-28 13:57:51 +03001515 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 $this->smtp_port,
1517 $errno,
1518 $errstr,
1519 $this->smtp_timeout);
1520
Pascal Kriete14287f32011-02-14 13:39:34 -05001521 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 {
patworkb0707982011-04-08 15:10:05 +02001523 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 return FALSE;
1525 }
1526
1527 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001528
1529 if ($this->smtp_crypto == 'tls')
1530 {
1531 $this->_send_command('hello');
1532 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001533
Radu Potop4c589ae2011-09-29 10:19:55 +03001534 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001535
Sean Fishere862ddd2011-10-26 22:45:00 -03001536 if ($crypto !== TRUE)
1537 {
1538 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1539 return FALSE;
1540 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001541 }
1542
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 return $this->_send_command('hello');
1544 }
Barry Mienydd671972010-10-04 16:33:58 +02001545
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 // --------------------------------------------------------------------
1547
1548 /**
1549 * Send SMTP command
1550 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 * @param string
1552 * @param string
1553 * @return string
1554 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001555 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 {
1557 switch ($cmd)
1558 {
1559 case 'hello' :
1560
1561 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1562 $this->_send_data('EHLO '.$this->_get_hostname());
1563 else
1564 $this->_send_data('HELO '.$this->_get_hostname());
1565
1566 $resp = 250;
1567 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001568 case 'starttls' :
1569
1570 $this->_send_data('STARTTLS');
1571
1572 $resp = 220;
1573 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 case 'from' :
1575
1576 $this->_send_data('MAIL FROM:<'.$data.'>');
1577
1578 $resp = 250;
1579 break;
1580 case 'to' :
leandronfb4552942012-03-21 23:54:26 -03001581
leandronf7686c122012-03-22 08:07:31 -03001582 if ($this->dsn)
1583 {
leandronfb4552942012-03-21 23:54:26 -03001584 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001585 }
leandronfb4552942012-03-21 23:54:26 -03001586 else
leandronf7686c122012-03-22 08:07:31 -03001587 {
leandronfb4552942012-03-21 23:54:26 -03001588 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001589 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 $resp = 250;
1591 break;
1592 case 'data' :
1593
1594 $this->_send_data('DATA');
1595
1596 $resp = 354;
1597 break;
1598 case 'quit' :
1599
1600 $this->_send_data('QUIT');
1601
1602 $resp = 221;
1603 break;
1604 }
1605
1606 $reply = $this->_get_smtp_data();
1607
1608 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1609
1610 if (substr($reply, 0, 3) != $resp)
1611 {
patworkb0707982011-04-08 15:10:05 +02001612 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 return FALSE;
1614 }
1615
1616 if ($cmd == 'quit')
1617 {
1618 fclose($this->_smtp_connect);
1619 }
1620
1621 return TRUE;
1622 }
Barry Mienydd671972010-10-04 16:33:58 +02001623
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 // --------------------------------------------------------------------
1625
1626 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001627 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 * @return bool
1630 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001631 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
1633 if ( ! $this->_smtp_auth)
1634 {
1635 return TRUE;
1636 }
1637
Andrey Andreev76f15c92012-03-01 13:05:07 +02001638 if ($this->smtp_user == '' && $this->smtp_pass == '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
patworkb0707982011-04-08 15:10:05 +02001640 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 return FALSE;
1642 }
1643
1644 $this->_send_data('AUTH LOGIN');
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_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 return FALSE;
1652 }
1653
1654 $this->_send_data(base64_encode($this->smtp_user));
1655
1656 $reply = $this->_get_smtp_data();
1657
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001658 if (strncmp($reply, '334', 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_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 return FALSE;
1662 }
1663
1664 $this->_send_data(base64_encode($this->smtp_pass));
1665
1666 $reply = $this->_get_smtp_data();
1667
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001668 if (strncmp($reply, '235', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 {
patworkb0707982011-04-08 15:10:05 +02001670 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 return FALSE;
1672 }
1673
1674 return TRUE;
1675 }
Barry Mienydd671972010-10-04 16:33:58 +02001676
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 // --------------------------------------------------------------------
1678
1679 /**
1680 * Send SMTP data
1681 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 * @return bool
1683 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001684 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 {
1686 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1687 {
patworkb0707982011-04-08 15:10:05 +02001688 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 return FALSE;
1690 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001691
1692 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 }
Barry Mienydd671972010-10-04 16:33:58 +02001694
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 // --------------------------------------------------------------------
1696
1697 /**
1698 * Get SMTP data
1699 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 * @return string
1701 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001702 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 {
1704 $data = "";
1705
1706 while ($str = fgets($this->_smtp_connect, 512))
1707 {
1708 $data .= $str;
1709
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001710 if ($str[3] == " ")
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 {
1712 break;
1713 }
1714 }
1715
1716 return $data;
1717 }
Barry Mienydd671972010-10-04 16:33:58 +02001718
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 // --------------------------------------------------------------------
1720
1721 /**
1722 * Get Hostname
1723 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 * @return string
1725 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001726 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001728 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 }
Barry Mienydd671972010-10-04 16:33:58 +02001730
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 // --------------------------------------------------------------------
1732
1733 /**
1734 * Get IP
1735 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 * @return string
1737 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001738 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
1740 if ($this->_IP !== FALSE)
1741 {
1742 return $this->_IP;
1743 }
1744
Andrey Andreev76f15c92012-03-01 13:05:07 +02001745 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1746 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001747 if ($cip) $this->_IP = $cip;
1748 elseif ($rip) $this->_IP = $rip;
1749 else
1750 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001751 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001752 if ($fip)
1753 {
1754 $this->_IP = $fip;
1755 }
1756 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001757
Robin Sowell76b369e2010-03-19 11:15:28 -04001758 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
1760 $x = explode(',', $this->_IP);
1761 $this->_IP = end($x);
1762 }
1763
Andrey Andreev59c5b532012-03-01 14:09:51 +02001764 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 +00001765 {
1766 $this->_IP = '0.0.0.0';
1767 }
1768
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 return $this->_IP;
1770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 // --------------------------------------------------------------------
1773
1774 /**
1775 * Get Debug Message
1776 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 * @return string
1778 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001779 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 {
1781 $msg = '';
1782
1783 if (count($this->_debug_msg) > 0)
1784 {
1785 foreach ($this->_debug_msg as $val)
1786 {
1787 $msg .= $val;
1788 }
1789 }
1790
Andrey Andreev59c5b532012-03-01 14:09:51 +02001791 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Set Message
1798 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001800 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001802 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 {
1804 $CI =& get_instance();
1805 $CI->lang->load('email');
1806
Andrey Andreev76f15c92012-03-01 13:05:07 +02001807 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
1809 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1810 }
1811 else
1812 {
1813 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1814 }
1815 }
Barry Mienydd671972010-10-04 16:33:58 +02001816
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 // --------------------------------------------------------------------
1818
1819 /**
1820 * Mime Types
1821 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 * @param string
1823 * @return string
1824 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001825 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001827 $mimes = array(
1828 'hqx' => 'application/mac-binhex40',
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 'cpt' => 'application/mac-compactpro',
1830 'doc' => 'application/msword',
1831 'bin' => 'application/macbinary',
1832 'dms' => 'application/octet-stream',
1833 'lha' => 'application/octet-stream',
1834 'lzh' => 'application/octet-stream',
1835 'exe' => 'application/octet-stream',
1836 'class' => 'application/octet-stream',
1837 'psd' => 'application/octet-stream',
1838 'so' => 'application/octet-stream',
1839 'sea' => 'application/octet-stream',
1840 'dll' => 'application/octet-stream',
1841 'oda' => 'application/oda',
1842 'pdf' => 'application/pdf',
1843 'ai' => 'application/postscript',
1844 'eps' => 'application/postscript',
1845 'ps' => 'application/postscript',
1846 'smi' => 'application/smil',
1847 'smil' => 'application/smil',
1848 'mif' => 'application/vnd.mif',
1849 'xls' => 'application/vnd.ms-excel',
1850 'ppt' => 'application/vnd.ms-powerpoint',
1851 'wbxml' => 'application/vnd.wap.wbxml',
1852 'wmlc' => 'application/vnd.wap.wmlc',
1853 'dcr' => 'application/x-director',
1854 'dir' => 'application/x-director',
1855 'dxr' => 'application/x-director',
1856 'dvi' => 'application/x-dvi',
1857 'gtar' => 'application/x-gtar',
1858 'php' => 'application/x-httpd-php',
1859 'php4' => 'application/x-httpd-php',
1860 'php3' => 'application/x-httpd-php',
1861 'phtml' => 'application/x-httpd-php',
1862 'phps' => 'application/x-httpd-php-source',
1863 'js' => 'application/x-javascript',
1864 'swf' => 'application/x-shockwave-flash',
1865 'sit' => 'application/x-stuffit',
1866 'tar' => 'application/x-tar',
1867 'tgz' => 'application/x-tar',
1868 'xhtml' => 'application/xhtml+xml',
1869 'xht' => 'application/xhtml+xml',
1870 'zip' => 'application/zip',
1871 'mid' => 'audio/midi',
1872 'midi' => 'audio/midi',
1873 'mpga' => 'audio/mpeg',
1874 'mp2' => 'audio/mpeg',
1875 'mp3' => 'audio/mpeg',
1876 'aif' => 'audio/x-aiff',
1877 'aiff' => 'audio/x-aiff',
1878 'aifc' => 'audio/x-aiff',
1879 'ram' => 'audio/x-pn-realaudio',
1880 'rm' => 'audio/x-pn-realaudio',
1881 'rpm' => 'audio/x-pn-realaudio-plugin',
1882 'ra' => 'audio/x-realaudio',
1883 'rv' => 'video/vnd.rn-realvideo',
1884 'wav' => 'audio/x-wav',
1885 'bmp' => 'image/bmp',
1886 'gif' => 'image/gif',
1887 'jpeg' => 'image/jpeg',
1888 'jpg' => 'image/jpeg',
1889 'jpe' => 'image/jpeg',
1890 'png' => 'image/png',
1891 'tiff' => 'image/tiff',
1892 'tif' => 'image/tiff',
1893 'css' => 'text/css',
1894 'html' => 'text/html',
1895 'htm' => 'text/html',
1896 'shtml' => 'text/html',
1897 'txt' => 'text/plain',
1898 'text' => 'text/plain',
1899 'log' => 'text/plain',
1900 'rtx' => 'text/richtext',
1901 'rtf' => 'text/rtf',
1902 'xml' => 'text/xml',
1903 'xsl' => 'text/xml',
1904 'mpeg' => 'video/mpeg',
1905 'mpg' => 'video/mpeg',
1906 'mpe' => 'video/mpeg',
1907 'qt' => 'video/quicktime',
1908 'mov' => 'video/quicktime',
1909 'avi' => 'video/x-msvideo',
1910 'movie' => 'video/x-sgi-movie',
1911 'doc' => 'application/msword',
1912 'word' => 'application/msword',
1913 'xl' => 'application/excel',
1914 'eml' => 'message/rfc822'
1915 );
1916
Andrey Andreev59c5b532012-03-01 14:09:51 +02001917 return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 }
1919
1920}
Derek Allard2067d1a2008-11-13 22:59:24 +00001921
1922/* End of file Email.php */
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001923/* Location: ./system/libraries/Email.php */