blob: 8239862a958ade5676a023d37f595ba37b77bb57 [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
Andrey Andreev56454792012-05-17 14:32:19 +030096 *
97 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000099 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 if (count($config) > 0)
102 {
103 $this->initialize($config);
104 }
105 else
106 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100107 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200108 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110
Andrey Andreev59c5b532012-03-01 14:09:51 +0200111 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Initialize preferences
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @param array
120 * @return void
121 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000122 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 foreach ($config as $key => $val)
125 {
126 if (isset($this->$key))
127 {
128 $method = 'set_'.$key;
129
130 if (method_exists($this, $method))
131 {
132 $this->$method($val);
133 }
134 else
135 {
136 $this->$key = $val;
137 }
138 }
139 }
Eric Barnes6113f542010-12-29 13:36:12 -0500140 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
Alex Bilbied261b1e2012-06-02 11:12:16 +0100142 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200143 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000144
145 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
149
150 /**
151 * Initialize the Email Data
152 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800153 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200154 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000156 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200158 $this->_subject = '';
159 $this->_body = '';
160 $this->_finalbody = '';
161 $this->_header_str = '';
162 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500164 $this->_cc_array = array();
165 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $this->_headers = array();
167 $this->_debug_msg = array();
168
Mickey Wubfc1cad2012-05-31 22:28:40 -0700169 $this->set_header('User-Agent', $this->useragent);
170 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000171
172 if ($clear_attachments !== FALSE)
173 {
174 $this->_attach_name = array();
175 $this->_attach_type = array();
176 $this->_attach_disp = array();
177 }
Eric Barnes6113f542010-12-29 13:36:12 -0500178
Greg Akera769deb2010-11-10 15:47:29 -0600179 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // --------------------------------------------------------------------
183
184 /**
185 * Set FROM
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string
188 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200189 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000191 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200193 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200195 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 if ($this->validate)
199 {
200 $this->validate_email($this->_str_to_array($from));
201 }
202
203 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100204 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 // only use Q encoding if there are characters that would require it
207 if ( ! preg_match('/[\200-\377]/', $name))
208 {
209 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000210 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212 else
213 {
214 $name = $this->_prep_q_encoding($name, TRUE);
215 }
216 }
217
Mickey Wubfc1cad2012-05-31 22:28:40 -0700218 $this->set_header('From', $name.' <'.$from.'>');
219 $this->set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500220
Greg Akera769deb2010-11-10 15:47:29 -0600221 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Set Reply-to
228 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @param string
230 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200231 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000233 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200235 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200237 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 if ($this->validate)
241 {
242 $this->validate_email($this->_str_to_array($replyto));
243 }
244
Alex Bilbied261b1e2012-06-02 11:12:16 +0100245 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 $name = $replyto;
248 }
249
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300250 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 $name = '"'.$name.'"';
253 }
254
Mickey Wubfc1cad2012-05-31 22:28:40 -0700255 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600257
258 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * Set Recipients
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200267 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000269 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 $to = $this->_str_to_array($to);
272 $to = $this->clean_email($to);
273
274 if ($this->validate)
275 {
276 $this->validate_email($to);
277 }
278
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200279 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700281 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 switch ($this->_get_protocol())
285 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200286 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000287 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200289 case 'sendmail':
290 case 'mail':
291 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 break;
293 }
Eric Barnes6113f542010-12-29 13:36:12 -0500294
Greg Akera769deb2010-11-10 15:47:29 -0600295 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Set CC
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200304 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000306 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreev56454792012-05-17 14:32:19 +0300308 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310 if ($this->validate)
311 {
312 $this->validate_email($cc);
313 }
314
Mickey Wubfc1cad2012-05-31 22:28:40 -0700315 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200317 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->_cc_array = $cc;
320 }
Eric Barnes6113f542010-12-29 13:36:12 -0500321
Greg Akera769deb2010-11-10 15:47:29 -0600322 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Set BCC
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string
331 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200332 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000334 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100336 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->bcc_batch_mode = TRUE;
339 $this->bcc_batch_size = $limit;
340 }
341
Andrey Andreev56454792012-05-17 14:32:19 +0300342 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
344 if ($this->validate)
345 {
346 $this->validate_email($bcc);
347 }
348
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200349 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $this->_bcc_array = $bcc;
352 }
353 else
354 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700355 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Eric Barnes6113f542010-12-29 13:36:12 -0500357
Greg Akera769deb2010-11-10 15:47:29 -0600358 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Set Email Subject
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200367 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000369 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700372 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600373 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Set Body
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200382 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000384 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200386 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200387
Andrey Andreevaf728622011-10-20 10:11:59 +0300388 /* strip slashes only if magic quotes is ON
389 if we do it with magic quotes OFF, it strips real, user-inputted chars.
390
391 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
392 it will probably not exist in future versions at all.
393 */
394 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200395 {
diegorivera33c32802011-10-19 02:56:15 -0200396 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200397 }
diegorivera33c32802011-10-19 02:56:15 -0200398
Greg Akera769deb2010-11-10 15:47:29 -0600399 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // --------------------------------------------------------------------
403
404 /**
405 * Assign file attachments
406 *
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 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700427 public 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 {
Andrey Andreev56454792012-05-17 14:32:19 +0300444 return (strpos($email, ',') !== FALSE)
445 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
446 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Andrey Andreev56454792012-05-17 14:32:19 +0300448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 return $email;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Set Multipart Value
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200458 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000460 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400462 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600463 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * Set Mailtype
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200472 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000474 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreev56454792012-05-17 14:32:19 +0300476 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600477 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Set Wordwrap
484 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400485 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200486 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000488 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400490 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600491 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // --------------------------------------------------------------------
495
496 /**
497 * Set Protocol
498 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200500 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000502 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200504 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600505 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // --------------------------------------------------------------------
509
510 /**
511 * Set Priority
512 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200513 * @param int
514 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000516 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200518 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600519 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Set Newline Character
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200528 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000530 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200532 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600533 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
537
538 /**
539 * Set CRLF
540 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200542 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000544 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200546 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600547 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
553 * Set Message Boundary
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @return void
556 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600557 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200559 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
560 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // --------------------------------------------------------------------
564
565 /**
566 * Get the Message ID
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @return string
569 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600570 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200572 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300573 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
577
578 /**
579 * Get Mail Protocol
580 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200582 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600584 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200587 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000588
Alex Bilbied261b1e2012-06-02 11:12:16 +0100589 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 return $this->protocol;
592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
596
597 /**
598 * Get Mail Encoding
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param bool
601 * @return string
602 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600603 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200605 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000606
607 foreach ($this->_base_charsets as $charset)
608 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300609 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 $this->_encoding = '7bit';
612 }
613 }
614
Alex Bilbied261b1e2012-06-02 11:12:16 +0100615 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 return $this->_encoding;
618 }
619 }
620
621 // --------------------------------------------------------------------
622
623 /**
624 * Get content type (text/html/attachment)
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @return string
627 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600628 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
Andrey Andreev56454792012-05-17 14:32:19 +0300630 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100632 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200634 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return 'plain-attach';
637 }
638 else
639 {
640 return 'plain';
641 }
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // --------------------------------------------------------------------
645
646 /**
647 * Set RFC 822 Date
648 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * @return string
650 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600651 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200653 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300654 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200656 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657
Andrey Andreev59c5b532012-03-01 14:09:51 +0200658 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Mime message
665 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return string
667 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600668 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200670 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 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // --------------------------------------------------------------------
674
675 /**
676 * Validate Email Address
677 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @param string
679 * @return bool
680 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000681 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 if ( ! is_array($email))
684 {
patworkb0707982011-04-08 15:10:05 +0200685 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return FALSE;
687 }
688
689 foreach ($email as $val)
690 {
691 if ( ! $this->valid_email($val))
692 {
patworkb0707982011-04-08 15:10:05 +0200693 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return FALSE;
695 }
696 }
697
698 return TRUE;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Email Validation
705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param string
707 * @return bool
708 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400709 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300711 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * Clean Extended Email Address: Joe Smith <joe@smith.com>
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @param string
720 * @return string
721 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000722 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
724 if ( ! is_array($email))
725 {
Andrey Andreev56454792012-05-17 14:32:19 +0300726 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
728
729 $clean_email = array();
730
731 foreach ($email as $addy)
732 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200733 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
735
736 return $clean_email;
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 // --------------------------------------------------------------------
740
741 /**
742 * Build alternative plain text message
743 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000744 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * in plain-text headers of HTML-formatted emails.
746 * If the user hasn't specified his own alternative message
747 * it creates one by stripping the HTML
748 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * @return string
750 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600751 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100753 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 return $this->word_wrap($this->alt_message, '76');
756 }
757
Andrey Andreev56454792012-05-17 14:32:19 +0300758 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200759 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000760
761 for ($i = 20; $i >= 3; $i--)
762 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200763 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200766 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Word Wrap
773 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200775 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @return string
777 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000778 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100781 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100783 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785
786 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300787 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 // Standardize newlines
790 if (strpos($str, "\r") !== FALSE)
791 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200792 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
794
795 // If the current word is surrounded by {unwrap} tags we'll
796 // strip the entire chunk and replace it with a marker.
797 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300798 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200800 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200802 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300803 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805 }
806
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000807 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300809 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $str = wordwrap($str, $charlim, "\n", FALSE);
811
812 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300813 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 foreach (explode("\n", $str) as $line)
815 {
816 // Is the line within the allowed character count?
817 // If so we'll join it to the output and continue
818 if (strlen($line) <= $charlim)
819 {
820 $output .= $line.$this->newline;
821 continue;
822 }
823
824 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200825 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300828 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
830 break;
831 }
832
833 // Trim the word down
834 $temp .= substr($line, 0, $charlim-1);
835 $line = substr($line, $charlim-1);
836 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200837 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000838
839 // If $temp contains data it means we had to split up an over-length
840 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100841 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200843 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
845
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200846 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
848
849 // Put our markers back
850 if (count($unwrap) > 0)
851 {
852 foreach ($unwrap as $key => $val)
853 {
Andrey Andreev56454792012-05-17 14:32:19 +0300854 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856 }
857
858 return $output;
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
864 * Build final headers
865 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 * @return string
867 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600868 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700870 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
871 $this->set_header('X-Mailer', $this->useragent);
872 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
873 $this->set_header('Message-ID', $this->_get_message_id());
874 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Write Headers as a string
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @return void
883 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600884 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200886 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 $this->_subject = $this->_headers['Subject'];
889 unset($this->_headers['Subject']);
890 }
891
892 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300893 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000894
Pascal Kriete14287f32011-02-14 13:39:34 -0500895 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 $val = trim($val);
898
Alex Bilbied261b1e2012-06-02 11:12:16 +0100899 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreev56454792012-05-17 14:32:19 +0300901 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903 }
904
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200905 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
Derek Jones1d890882009-02-10 20:32:14 +0000907 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Build Final Body and attachments
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @return void
917 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600918 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200920 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
922 $this->_body = $this->word_wrap($this->_body);
923 }
924
925 $this->_set_boundaries();
926 $this->_write_headers();
927
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200928 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500929 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000930
931 switch ($this->_get_content_type())
932 {
933 case 'plain' :
934
Andrey Andreev56454792012-05-17 14:32:19 +0300935 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
936 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000937
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200938 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $this->_header_str .= $hdr;
941 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Brandon Jones485d7412010-11-09 16:38:17 -0500943 else
944 {
945 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
946 }
Eric Barnes6113f542010-12-29 13:36:12 -0500947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 return;
949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 case 'html' :
951
952 if ($this->send_multipart === FALSE)
953 {
Andrey Andreev56454792012-05-17 14:32:19 +0300954 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
955 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957 else
958 {
Andrey Andreev56454792012-05-17 14:32:19 +0300959 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Andrey Andreev56454792012-05-17 14:32:19 +0300961 $body .= $this->_get_mime_message().$this->newline.$this->newline
962 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000963
Andrey Andreev56454792012-05-17 14:32:19 +0300964 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
965 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
966 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500967
Andrey Andreev56454792012-05-17 14:32:19 +0300968 .'Content-Type: text/html; charset='.$this->charset.$this->newline
969 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Eric Barnes6113f542010-12-29 13:36:12 -0500971
Andrey Andreev56454792012-05-17 14:32:19 +0300972 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500973
974
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200975 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500978 }
979 else
980 {
Andrey Andreev56454792012-05-17 14:32:19 +0300981 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984
985 if ($this->send_multipart !== FALSE)
986 {
Andrey Andreev56454792012-05-17 14:32:19 +0300987 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 return;
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 case 'plain-attach' :
993
Andrey Andreev56454792012-05-17 14:32:19 +0300994 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000995
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200996 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
998 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -0500999 }
1000
Andrey Andreev56454792012-05-17 14:32:19 +03001001 $body .= $this->_get_mime_message().$this->newline.$this->newline
1002 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Andrey Andreev56454792012-05-17 14:32:19 +03001004 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1005 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001006
Andrey Andreev56454792012-05-17 14:32:19 +03001007 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001008
1009 break;
1010 case 'html-attach' :
1011
Andrey Andreev56454792012-05-17 14:32:19 +03001012 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001013
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001014 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
1018
Andrey Andreev56454792012-05-17 14:32:19 +03001019 $body .= $this->_get_mime_message().$this->newline.$this->newline
1020 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001021
Andrey Andreev56454792012-05-17 14:32:19 +03001022 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1023 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001024
Andrey Andreev56454792012-05-17 14:32:19 +03001025 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1026 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1027 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001028
Andrey Andreev56454792012-05-17 14:32:19 +03001029 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1030 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001031
Andrey Andreev56454792012-05-17 14:32:19 +03001032 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1033 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
1035 break;
1036 }
1037
1038 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001039 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
tritbc4ac992011-11-23 07:19:41 -05001041 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001042 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001044 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001045
Alex Bilbied261b1e2012-06-02 11:12:16 +01001046 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001048 if ( ! file_exists($filename))
1049 {
1050 $this->_set_error_message('lang:email_attachment_missing', $filename);
1051 return FALSE;
1052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Matteo Mattei5688d582012-03-13 19:29:29 +01001054 $file = filesize($filename) +1;
1055
1056 if ( ! $fp = fopen($filename, FOPEN_READ))
1057 {
1058 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1059 return FALSE;
1060 }
1061
Matteo Matteic3b36f42012-03-26 10:27:17 +02001062 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001063 $file_content = fread($fp, $file);
1064 fclose($fp);
1065 }
1066 else
1067 {
1068 $file_content =& $this->_attach_content[$i];
1069 }
Andrey Andreev56454792012-05-17 14:32:19 +03001070
1071 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1072 .'Content-type: '.$ctype.'; '
1073 .'name="'.$basename.'"'.$this->newline
1074 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1075 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076
Matteo Mattei5688d582012-03-13 19:29:29 +01001077 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 }
1079
Andrey Andreev56454792012-05-17 14:32:19 +03001080 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1081 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 return;
1083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Prep Quoted Printable
1089 *
1090 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1091 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1092 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001094 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @return string
1096 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001097 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 // Set the character limit
1100 // Don't allow over 76, as that will make servers and MUAs barf
1101 // all over quoted-printable data
Alex Bilbied261b1e2012-06-02 11:12:16 +01001102 if ($charlim === '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001104 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 }
1106
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001107 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001108 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001109
1110 // Standardize newlines
1111 if (strpos($str, "\r") !== FALSE)
1112 {
1113 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1114 }
1115
1116 // We are intentionally wrapping so mail servers will encode characters
1117 // properly and MUAs will behave, so {unwrap} must go!
1118 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 $escape = '=';
1121 $output = '';
1122
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001123 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 $length = strlen($line);
1126 $temp = '';
1127
1128 // Loop through each character in the line to add soft-wrap
1129 // characters at the end of a line " =\r\n" and add the newly
1130 // processed line(s) to the output (see comment on $crlf class property)
1131 for ($i = 0; $i < $length; $i++)
1132 {
1133 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001134 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $ascii = ord($char);
1136
1137 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001138 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001140 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001142 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001144 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
1146
1147 // If we're at the character limit, add the line to the output,
1148 // reset our temp variable, and keep on chuggin'
1149 if ((strlen($temp) + strlen($char)) >= $charlim)
1150 {
1151 $output .= $temp.$escape.$this->crlf;
1152 $temp = '';
1153 }
1154
1155 // Add the character to our temporary line
1156 $temp .= $char;
1157 }
1158
1159 // Add our completed line to the output
1160 $output .= $temp.$this->crlf;
1161 }
1162
1163 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001164 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
1166
1167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 /**
1170 * Prep Q Encoding
1171 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001172 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 * but not identical to quoted-printable, so it has its own method
1174 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001175 * @param string
1176 * @param bool set to TRUE for processing From: headers
1177 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001179 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 {
1181 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1182
1183 // Line length must not exceed 76 characters, so we adjust for
1184 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1185 $limit = 75 - 7 - strlen($this->charset);
1186
1187 // these special characters must be converted too
1188 $convert = array('_', '=', '?');
1189
1190 if ($from === TRUE)
1191 {
1192 $convert[] = ',';
1193 $convert[] = ';';
1194 }
1195
1196 $output = '';
1197 $temp = '';
1198
1199 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1200 {
1201 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001202 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 $ascii = ord($char);
1204
1205 // convert ALL non-printable ASCII characters and our specials
1206 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1207 {
1208 $char = '='.dechex($ascii);
1209 }
1210
1211 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001212 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
1214 $char = '_';
1215 }
1216
1217 // If we're at the character limit, add the line to the output,
1218 // reset our temp variable, and keep on chuggin'
1219 if ((strlen($temp) + strlen($char)) >= $limit)
1220 {
1221 $output .= $temp.$this->crlf;
1222 $temp = '';
1223 }
1224
1225 // Add the character to our temporary line
1226 $temp .= $char;
1227 }
1228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 // wrap each line with the shebang, charset, and transfer encoding
1230 // the preceding space on successive lines is required for header "folding"
Andrey Andreev56454792012-05-17 14:32:19 +03001231 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 }
1233
1234 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 /**
1237 * Send Email
1238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 * @return bool
1240 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001241 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001243 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 $this->reply_to($this->_headers['From']);
1246 }
1247
Andrey Andreev76f15c92012-03-01 13:05:07 +02001248 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1249 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1250 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
patworkb0707982011-04-08 15:10:05 +02001252 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 return FALSE;
1254 }
1255
1256 $this->_build_headers();
1257
Andrey Andreev76f15c92012-03-01 13:05:07 +02001258 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001260 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001261
1262 if ($auto_clear)
1263 {
1264 $this->clear();
1265 }
1266
Alex Bilbieb901e732012-07-30 09:44:57 +01001267 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 }
1269
1270 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001271 $result = $this->_spool_email();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001272
1273 if ($auto_clear)
1274 {
1275 $this->clear();
1276 }
1277
Alex Bilbieb901e732012-07-30 09:44:57 +01001278 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 // --------------------------------------------------------------------
1282
1283 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001284 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001286 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001288 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001290 $float = $this->bcc_batch_size - 1;
1291 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 $chunk = array();
1293
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001294 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
1296 if (isset($this->_bcc_array[$i]))
1297 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001298 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 }
1300
Alex Bilbied261b1e2012-06-02 11:12:16 +01001301 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 {
1303 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001304 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001305 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
1307
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001308 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
1310 $chunk[] = substr($set, 1);
1311 }
1312 }
1313
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001314 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
1316 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001317
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001318 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001319
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001320 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001322 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 }
1324 else
1325 {
1326 $this->_bcc_array = $bcc;
1327 }
1328
1329 $this->_build_message();
1330 $this->_spool_email();
1331 }
1332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 // --------------------------------------------------------------------
1335
1336 /**
1337 * Unwrap special elements
1338 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 * @return void
1340 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001341 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 {
Andrey Andreev56454792012-05-17 14:32:19 +03001343 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 }
Barry Mienydd671972010-10-04 16:33:58 +02001345
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 // --------------------------------------------------------------------
1347
1348 /**
1349 * Strip line-breaks via callback
1350 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 * @return string
1352 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001353 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
1355 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1356 {
1357 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1358 }
1359
1360 return $matches[1];
1361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 // --------------------------------------------------------------------
1364
1365 /**
1366 * Spool mail to the mail server
1367 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * @return bool
1369 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001370 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
1372 $this->_unwrap_specials();
1373
Andrey Andreev56454792012-05-17 14:32:19 +03001374 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001375 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
Andrey Andreev56454792012-05-17 14:32:19 +03001377 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001378 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 }
1380
patworkb0707982011-04-08 15:10:05 +02001381 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 return TRUE;
1383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 // --------------------------------------------------------------------
1386
1387 /**
1388 * Send using mail()
1389 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 * @return bool
1391 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001392 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001394 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001396 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 }
1398 else
1399 {
1400 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1401 // we've encountered servers that seem to require it to be in place.
Andrey Andreev56454792012-05-17 14:32:19 +03001402 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 +00001403 }
1404 }
Barry Mienydd671972010-10-04 16:33:58 +02001405
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 // --------------------------------------------------------------------
1407
1408 /**
1409 * Send using Sendmail
1410 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 * @return bool
1412 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001413 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 {
Andrey Andreev56454792012-05-17 14:32:19 +03001415 $fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t', 'w');
Derek Allard2067d1a2008-11-13 22:59:24 +00001416
Derek Jones4cefaa42009-04-29 19:13:56 +00001417 if ($fp === FALSE OR $fp === NULL)
1418 {
1419 // server probably has popen disabled, so nothing we can do to get a verbose error.
1420 return FALSE;
1421 }
Derek Jones71141ce2010-03-02 16:41:20 -06001422
Derek Jonesc630bcf2008-11-17 21:09:45 +00001423 fputs($fp, $this->_header_str);
1424 fputs($fp, $this->_finalbody);
1425
Barry Mienydd671972010-10-04 16:33:58 +02001426 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001427
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001428 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 {
patworkb0707982011-04-08 15:10:05 +02001430 $this->_set_error_message('lang:email_exit_status', $status);
1431 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 return FALSE;
1433 }
1434
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 return TRUE;
1436 }
Barry Mienydd671972010-10-04 16:33:58 +02001437
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 // --------------------------------------------------------------------
1439
1440 /**
1441 * Send using SMTP
1442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 * @return bool
1444 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001445 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001447 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 {
patworkb0707982011-04-08 15:10:05 +02001449 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 return FALSE;
1451 }
1452
Diogo Osório593f7982012-03-02 18:04:17 +00001453 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001454 {
1455 return FALSE;
1456 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001457
1458 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1459
Pascal Kriete14287f32011-02-14 13:39:34 -05001460 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 {
1462 $this->_send_command('to', $val);
1463 }
1464
1465 if (count($this->_cc_array) > 0)
1466 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001467 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001469 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 $this->_send_command('to', $val);
1472 }
1473 }
1474 }
1475
1476 if (count($this->_bcc_array) > 0)
1477 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001478 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001480 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 {
1482 $this->_send_command('to', $val);
1483 }
1484 }
1485 }
1486
1487 $this->_send_command('data');
1488
1489 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001490 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001491
1492 $this->_send_data('.');
1493
1494 $reply = $this->_get_smtp_data();
1495
1496 $this->_set_error_message($reply);
1497
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001498 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 {
patworkb0707982011-04-08 15:10:05 +02001500 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 return FALSE;
1502 }
1503
1504 $this->_send_command('quit');
1505 return TRUE;
1506 }
Barry Mienydd671972010-10-04 16:33:58 +02001507
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 // --------------------------------------------------------------------
1509
1510 /**
1511 * SMTP Connect
1512 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 * @param string
1514 * @return string
1515 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001516 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001518 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001519
Radu Potopbbf04b02011-09-28 13:57:51 +03001520 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001521 $this->smtp_port,
1522 $errno,
1523 $errstr,
1524 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001525
Pascal Kriete14287f32011-02-14 13:39:34 -05001526 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 {
Andrey Andreev56454792012-05-17 14:32:19 +03001528 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 return FALSE;
1530 }
1531
1532 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001533
Alex Bilbied261b1e2012-06-02 11:12:16 +01001534 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001535 {
1536 $this->_send_command('hello');
1537 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001538
Radu Potop4c589ae2011-09-29 10:19:55 +03001539 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001540
Sean Fishere862ddd2011-10-26 22:45:00 -03001541 if ($crypto !== TRUE)
1542 {
1543 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1544 return FALSE;
1545 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001546 }
1547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 return $this->_send_command('hello');
1549 }
Barry Mienydd671972010-10-04 16:33:58 +02001550
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 // --------------------------------------------------------------------
1552
1553 /**
1554 * Send SMTP command
1555 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 * @param string
1557 * @param string
1558 * @return string
1559 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001560 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 {
1562 switch ($cmd)
1563 {
1564 case 'hello' :
1565
Andrey Andreev56454792012-05-17 14:32:19 +03001566 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1567 {
1568 $this->_send_data('EHLO '.$this->_get_hostname());
1569 }
1570 else
1571 {
1572 $this->_send_data('HELO '.$this->_get_hostname());
1573 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001574
1575 $resp = 250;
1576 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001577 case 'starttls' :
1578
1579 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001580 $resp = 220;
1581 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 case 'from' :
1583
1584 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 $resp = 250;
1586 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001587 case 'to' :
1588
leandronf7686c122012-03-22 08:07:31 -03001589 if ($this->dsn)
1590 {
leandronfb4552942012-03-21 23:54:26 -03001591 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001592 }
leandronfb4552942012-03-21 23:54:26 -03001593 else
leandronf7686c122012-03-22 08:07:31 -03001594 {
leandronfb4552942012-03-21 23:54:26 -03001595 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001596 }
Andrey Andreev56454792012-05-17 14:32:19 +03001597
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 $resp = 250;
1599 break;
1600 case 'data' :
1601
1602 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 $resp = 354;
1604 break;
1605 case 'quit' :
1606
1607 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 $resp = 221;
1609 break;
1610 }
1611
1612 $reply = $this->_get_smtp_data();
1613
Andrey Andreev56454792012-05-17 14:32:19 +03001614 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001615
Andrey Andreevba261762012-06-11 14:11:35 +03001616 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 {
patworkb0707982011-04-08 15:10:05 +02001618 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 return FALSE;
1620 }
1621
Alex Bilbied261b1e2012-06-02 11:12:16 +01001622 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 {
1624 fclose($this->_smtp_connect);
1625 }
1626
1627 return TRUE;
1628 }
Barry Mienydd671972010-10-04 16:33:58 +02001629
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 // --------------------------------------------------------------------
1631
1632 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001633 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 * @return bool
1636 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001637 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 {
1639 if ( ! $this->_smtp_auth)
1640 {
1641 return TRUE;
1642 }
1643
Alex Bilbied261b1e2012-06-02 11:12:16 +01001644 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
patworkb0707982011-04-08 15:10:05 +02001646 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 return FALSE;
1648 }
1649
1650 $this->_send_data('AUTH LOGIN');
1651
1652 $reply = $this->_get_smtp_data();
1653
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001654 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 {
patworkb0707982011-04-08 15:10:05 +02001656 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 return FALSE;
1658 }
1659
1660 $this->_send_data(base64_encode($this->smtp_user));
1661
1662 $reply = $this->_get_smtp_data();
1663
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001664 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
patworkb0707982011-04-08 15:10:05 +02001666 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 return FALSE;
1668 }
1669
1670 $this->_send_data(base64_encode($this->smtp_pass));
1671
1672 $reply = $this->_get_smtp_data();
1673
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001674 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 {
patworkb0707982011-04-08 15:10:05 +02001676 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 return FALSE;
1678 }
1679
1680 return TRUE;
1681 }
Barry Mienydd671972010-10-04 16:33:58 +02001682
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 // --------------------------------------------------------------------
1684
1685 /**
1686 * Send SMTP data
1687 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 * @return bool
1689 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001690 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
1692 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1693 {
patworkb0707982011-04-08 15:10:05 +02001694 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 return FALSE;
1696 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001697
1698 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 }
Barry Mienydd671972010-10-04 16:33:58 +02001700
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 // --------------------------------------------------------------------
1702
1703 /**
1704 * Get SMTP data
1705 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 * @return string
1707 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001708 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 {
Andrey Andreev56454792012-05-17 14:32:19 +03001710 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001711
1712 while ($str = fgets($this->_smtp_connect, 512))
1713 {
1714 $data .= $str;
1715
Alex Bilbied261b1e2012-06-02 11:12:16 +01001716 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 {
1718 break;
1719 }
1720 }
1721
1722 return $data;
1723 }
Barry Mienydd671972010-10-04 16:33:58 +02001724
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 // --------------------------------------------------------------------
1726
1727 /**
1728 * Get Hostname
1729 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 * @return string
1731 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001732 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001734 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 }
Barry Mienydd671972010-10-04 16:33:58 +02001736
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 // --------------------------------------------------------------------
1738
1739 /**
1740 * Get IP
1741 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 * @return string
1743 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001744 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 {
1746 if ($this->_IP !== FALSE)
1747 {
1748 return $this->_IP;
1749 }
1750
Andrey Andreev76f15c92012-03-01 13:05:07 +02001751 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1752 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001753 if ($cip) $this->_IP = $cip;
1754 elseif ($rip) $this->_IP = $rip;
1755 else
1756 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001757 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001758 if ($fip)
1759 {
1760 $this->_IP = $fip;
1761 }
1762 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001763
Robin Sowell76b369e2010-03-19 11:15:28 -04001764 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
1766 $x = explode(',', $this->_IP);
1767 $this->_IP = end($x);
1768 }
1769
Andrey Andreev59c5b532012-03-01 14:09:51 +02001770 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 +00001771 {
1772 $this->_IP = '0.0.0.0';
1773 }
1774
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 return $this->_IP;
1776 }
Barry Mienydd671972010-10-04 16:33:58 +02001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // --------------------------------------------------------------------
1779
1780 /**
1781 * Get Debug Message
1782 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 * @return string
1784 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001785 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 {
1787 $msg = '';
1788
1789 if (count($this->_debug_msg) > 0)
1790 {
1791 foreach ($this->_debug_msg as $val)
1792 {
1793 $msg .= $val;
1794 }
1795 }
1796
Andrey Andreev59c5b532012-03-01 14:09:51 +02001797 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 }
Barry Mienydd671972010-10-04 16:33:58 +02001799
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 // --------------------------------------------------------------------
1801
1802 /**
1803 * Set Message
1804 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001806 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001808 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
1810 $CI =& get_instance();
1811 $CI->lang->load('email');
1812
Andrey Andreev76f15c92012-03-01 13:05:07 +02001813 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 {
Andrey Andreev56454792012-05-17 14:32:19 +03001815 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 }
1817 else
1818 {
Andrey Andreev56454792012-05-17 14:32:19 +03001819 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 }
1821 }
Barry Mienydd671972010-10-04 16:33:58 +02001822
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 // --------------------------------------------------------------------
1824
1825 /**
1826 * Mime Types
1827 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 * @param string
1829 * @return string
1830 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001831 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001833 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001834
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001835 $ext = strtolower($ext);
1836
1837 if ( ! is_array($mimes))
1838 {
1839 $mimes =& get_mimes();
1840 }
1841
1842 if (isset($mimes[$ext]))
1843 {
1844 return is_array($mimes[$ext])
1845 ? current($mimes[$ext])
1846 : $mimes[$ext];
1847 }
1848
1849 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 }
1851
1852}
Derek Allard2067d1a2008-11-13 22:59:24 +00001853
1854/* End of file Email.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001855/* Location: ./system/libraries/Email.php */