blob: 5b17edf9bc89b6c25ed6c65dcd6a2b502807f598 [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 {
Andrey Andreev9f44c212012-10-10 16:07:17 +0300101 $this->charset = strtoupper(config_item('charset'));
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 if (count($config) > 0)
104 {
105 $this->initialize($config);
106 }
107 else
108 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100109 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200110 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
Andrey Andreev59c5b532012-03-01 14:09:51 +0200113 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Initialize preferences
120 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * @param array
122 * @return void
123 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000124 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 foreach ($config as $key => $val)
127 {
128 if (isset($this->$key))
129 {
130 $method = 'set_'.$key;
131
132 if (method_exists($this, $method))
133 {
134 $this->$method($val);
135 }
136 else
137 {
138 $this->$key = $val;
139 }
140 }
141 }
Eric Barnes6113f542010-12-29 13:36:12 -0500142 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
Alex Bilbied261b1e2012-06-02 11:12:16 +0100144 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200145 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000146
147 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // --------------------------------------------------------------------
151
152 /**
153 * Initialize the Email Data
154 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800155 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200156 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000158 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200160 $this->_subject = '';
161 $this->_body = '';
162 $this->_finalbody = '';
163 $this->_header_str = '';
164 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500166 $this->_cc_array = array();
167 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $this->_headers = array();
169 $this->_debug_msg = array();
170
Mickey Wubfc1cad2012-05-31 22:28:40 -0700171 $this->set_header('User-Agent', $this->useragent);
172 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
174 if ($clear_attachments !== FALSE)
175 {
176 $this->_attach_name = array();
177 $this->_attach_type = array();
178 $this->_attach_disp = array();
179 }
Eric Barnes6113f542010-12-29 13:36:12 -0500180
Greg Akera769deb2010-11-10 15:47:29 -0600181 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Set FROM
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string
190 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200191 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Melounek58dfc082012-06-29 08:43:47 +0200193 public function from($from, $name = '', $return_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200195 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200197 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
199
200 if ($this->validate)
201 {
202 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300203 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200204 {
205 $this->validate_email($this->_str_to_array($return_path));
206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100210 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 // only use Q encoding if there are characters that would require it
213 if ( ! preg_match('/[\200-\377]/', $name))
214 {
215 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000216 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218 else
219 {
220 $name = $this->_prep_q_encoding($name, TRUE);
221 }
222 }
223
Mickey Wubfc1cad2012-05-31 22:28:40 -0700224 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200225
Andrey Andreevccd01c72012-10-05 17:12:55 +0300226 if( ! $return_path)
Melounek58dfc082012-06-29 08:43:47 +0200227 {
228 $return_path = $from;
229 }
230 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500231
Greg Akera769deb2010-11-10 15:47:29 -0600232 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Set Reply-to
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string
241 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200242 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000244 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200246 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200248 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250
251 if ($this->validate)
252 {
253 $this->validate_email($this->_str_to_array($replyto));
254 }
255
Alex Bilbied261b1e2012-06-02 11:12:16 +0100256 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $name = $replyto;
259 }
260
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300261 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $name = '"'.$name.'"';
264 }
265
Mickey Wubfc1cad2012-05-31 22:28:40 -0700266 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600268
269 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Set Recipients
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200278 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000280 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $to = $this->_str_to_array($to);
283 $to = $this->clean_email($to);
284
285 if ($this->validate)
286 {
287 $this->validate_email($to);
288 }
289
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200290 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700292 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300295 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500296
Greg Akera769deb2010-11-10 15:47:29 -0600297 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Set CC
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200306 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000308 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreev56454792012-05-17 14:32:19 +0300310 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
312 if ($this->validate)
313 {
314 $this->validate_email($cc);
315 }
316
Mickey Wubfc1cad2012-05-31 22:28:40 -0700317 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200319 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->_cc_array = $cc;
322 }
Eric Barnes6113f542010-12-29 13:36:12 -0500323
Greg Akera769deb2010-11-10 15:47:29 -0600324 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Set BCC
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string
333 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200334 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000336 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100338 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 $this->bcc_batch_mode = TRUE;
341 $this->bcc_batch_size = $limit;
342 }
343
Andrey Andreev56454792012-05-17 14:32:19 +0300344 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
346 if ($this->validate)
347 {
348 $this->validate_email($bcc);
349 }
350
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200351 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 $this->_bcc_array = $bcc;
354 }
355 else
356 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700357 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Eric Barnes6113f542010-12-29 13:36:12 -0500359
Greg Akera769deb2010-11-10 15:47:29 -0600360 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
364
365 /**
366 * Set Email Subject
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200369 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000371 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700374 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600375 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // --------------------------------------------------------------------
379
380 /**
381 * Set Body
382 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200384 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000386 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200388 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200389
Andrey Andreevaf728622011-10-20 10:11:59 +0300390 /* strip slashes only if magic quotes is ON
391 if we do it with magic quotes OFF, it strips real, user-inputted chars.
392
393 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
394 it will probably not exist in future versions at all.
395 */
396 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200397 {
diegorivera33c32802011-10-19 02:56:15 -0200398 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200399 }
diegorivera33c32802011-10-19 02:56:15 -0200400
Greg Akera769deb2010-11-10 15:47:29 -0600401 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // --------------------------------------------------------------------
405
406 /**
407 * Assign file attachments
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200410 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200412 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
trit151fc682011-11-23 07:30:06 -0500414 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500415 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200416 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600417 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Add a Header Item
424 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @param string
426 * @param string
427 * @return void
428 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700429 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 $this->_headers[$header] = $value;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
435
436 /**
437 * Convert a String to an Array
438 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @param string
440 * @return array
441 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600442 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 if ( ! is_array($email))
445 {
Andrey Andreev56454792012-05-17 14:32:19 +0300446 return (strpos($email, ',') !== FALSE)
447 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
448 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Andrey Andreev56454792012-05-17 14:32:19 +0300450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 return $email;
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
455
456 /**
457 * Set Multipart Value
458 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200460 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000462 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400464 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600465 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
471 * Set Mailtype
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200474 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000476 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreev56454792012-05-17 14:32:19 +0300478 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600479 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Set Wordwrap
486 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400487 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200488 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000490 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400492 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600493 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
499 * Set Protocol
500 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200502 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000504 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200506 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600507 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 // --------------------------------------------------------------------
511
512 /**
513 * Set Priority
514 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200515 * @param int
516 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000518 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200520 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600521 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // --------------------------------------------------------------------
525
526 /**
527 * Set Newline Character
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200530 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000532 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200534 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600535 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // --------------------------------------------------------------------
539
540 /**
541 * Set CRLF
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200544 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000546 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200548 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600549 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // --------------------------------------------------------------------
553
554 /**
555 * Set Message Boundary
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @return void
558 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600559 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200561 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
562 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // --------------------------------------------------------------------
566
567 /**
568 * Get the Message ID
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @return string
571 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600572 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200574 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300575 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // --------------------------------------------------------------------
579
580 /**
581 * Get Mail Protocol
582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200584 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600586 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200589 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000590
Alex Bilbied261b1e2012-06-02 11:12:16 +0100591 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 return $this->protocol;
594 }
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Get Mail Encoding
601 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @param bool
603 * @return string
604 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600605 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200607 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
609 foreach ($this->_base_charsets as $charset)
610 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300611 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 $this->_encoding = '7bit';
614 }
615 }
616
Alex Bilbied261b1e2012-06-02 11:12:16 +0100617 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 return $this->_encoding;
620 }
621 }
622
623 // --------------------------------------------------------------------
624
625 /**
626 * Get content type (text/html/attachment)
627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return string
629 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600630 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreev56454792012-05-17 14:32:19 +0300632 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100634 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200636 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 return 'plain-attach';
639 }
640 else
641 {
642 return 'plain';
643 }
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // --------------------------------------------------------------------
647
648 /**
649 * Set RFC 822 Date
650 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * @return string
652 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600653 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200655 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300656 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200658 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659
Andrey Andreev59c5b532012-03-01 14:09:51 +0200660 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // --------------------------------------------------------------------
664
665 /**
666 * Mime message
667 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @return string
669 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600670 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200672 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 +0000673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
676
677 /**
678 * Validate Email Address
679 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 * @param string
681 * @return bool
682 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000683 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 if ( ! is_array($email))
686 {
patworkb0707982011-04-08 15:10:05 +0200687 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 return FALSE;
689 }
690
691 foreach ($email as $val)
692 {
693 if ( ! $this->valid_email($val))
694 {
patworkb0707982011-04-08 15:10:05 +0200695 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 return FALSE;
697 }
698 }
699
700 return TRUE;
701 }
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 // --------------------------------------------------------------------
704
705 /**
706 * Email Validation
707 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 * @param string
709 * @return bool
710 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400711 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300713 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 // --------------------------------------------------------------------
717
718 /**
719 * Clean Extended Email Address: Joe Smith <joe@smith.com>
720 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 * @param string
722 * @return string
723 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000724 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 if ( ! is_array($email))
727 {
Andrey Andreev56454792012-05-17 14:32:19 +0300728 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730
731 $clean_email = array();
732
733 foreach ($email as $addy)
734 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200735 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 return $clean_email;
739 }
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 // --------------------------------------------------------------------
742
743 /**
744 * Build alternative plain text message
745 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000746 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 * in plain-text headers of HTML-formatted emails.
748 * If the user hasn't specified his own alternative message
749 * it creates one by stripping the HTML
750 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 * @return string
752 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600753 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100755 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 {
757 return $this->word_wrap($this->alt_message, '76');
758 }
759
Andrey Andreev56454792012-05-17 14:32:19 +0300760 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200761 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000762
763 for ($i = 20; $i >= 3; $i--)
764 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200765 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
767
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200768 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 // --------------------------------------------------------------------
772
773 /**
774 * Word Wrap
775 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200777 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 * @return string
779 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000780 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100783 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100785 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787
788 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300789 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000790
791 // Standardize newlines
792 if (strpos($str, "\r") !== FALSE)
793 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200794 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
796
797 // If the current word is surrounded by {unwrap} tags we'll
798 // strip the entire chunk and replace it with a marker.
799 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300800 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200802 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200804 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300805 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
807 }
808
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000809 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300811 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 $str = wordwrap($str, $charlim, "\n", FALSE);
813
814 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300815 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 foreach (explode("\n", $str) as $line)
817 {
818 // Is the line within the allowed character count?
819 // If so we'll join it to the output and continue
820 if (strlen($line) <= $charlim)
821 {
822 $output .= $line.$this->newline;
823 continue;
824 }
825
826 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200827 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 {
829 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300830 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
832 break;
833 }
834
835 // Trim the word down
836 $temp .= substr($line, 0, $charlim-1);
837 $line = substr($line, $charlim-1);
838 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200839 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000840
841 // If $temp contains data it means we had to split up an over-length
842 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100843 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200845 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 }
847
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200848 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
850
851 // Put our markers back
852 if (count($unwrap) > 0)
853 {
854 foreach ($unwrap as $key => $val)
855 {
Andrey Andreev56454792012-05-17 14:32:19 +0300856 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
858 }
859
860 return $output;
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
864
865 /**
866 * Build final headers
867 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * @return string
869 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600870 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700872 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
873 $this->set_header('X-Mailer', $this->useragent);
874 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
875 $this->set_header('Message-ID', $this->_get_message_id());
876 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
880
881 /**
882 * Write Headers as a string
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @return void
885 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600886 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200888 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 $this->_subject = $this->_headers['Subject'];
891 unset($this->_headers['Subject']);
892 }
893
894 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300895 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000896
Pascal Kriete14287f32011-02-14 13:39:34 -0500897 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $val = trim($val);
900
Alex Bilbied261b1e2012-06-02 11:12:16 +0100901 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreev56454792012-05-17 14:32:19 +0300903 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905 }
906
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200907 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
Derek Jones1d890882009-02-10 20:32:14 +0000909 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 // --------------------------------------------------------------------
914
915 /**
916 * Build Final Body and attachments
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @return void
919 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600920 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200922 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 $this->_body = $this->word_wrap($this->_body);
925 }
926
927 $this->_set_boundaries();
928 $this->_write_headers();
929
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200930 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500931 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932
933 switch ($this->_get_content_type())
934 {
935 case 'plain' :
936
Andrey Andreev56454792012-05-17 14:32:19 +0300937 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
938 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000939
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200940 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
942 $this->_header_str .= $hdr;
943 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 }
Brandon Jones485d7412010-11-09 16:38:17 -0500945 else
946 {
947 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
948 }
Eric Barnes6113f542010-12-29 13:36:12 -0500949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 return;
951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 case 'html' :
953
954 if ($this->send_multipart === FALSE)
955 {
Andrey Andreev56454792012-05-17 14:32:19 +0300956 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
957 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
959 else
960 {
Andrey Andreev56454792012-05-17 14:32:19 +0300961 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000962
Andrey Andreev56454792012-05-17 14:32:19 +0300963 $body .= $this->_get_mime_message().$this->newline.$this->newline
964 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Andrey Andreev56454792012-05-17 14:32:19 +0300966 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
967 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
968 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500969
Andrey Andreev56454792012-05-17 14:32:19 +0300970 .'Content-Type: text/html; charset='.$this->charset.$this->newline
971 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Eric Barnes6113f542010-12-29 13:36:12 -0500973
Andrey Andreev56454792012-05-17 14:32:19 +0300974 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500975
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200976 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
978 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500979 }
980 else
981 {
Andrey Andreev56454792012-05-17 14:32:19 +0300982 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985
986 if ($this->send_multipart !== FALSE)
987 {
Andrey Andreev56454792012-05-17 14:32:19 +0300988 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 return;
992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 case 'plain-attach' :
994
Andrey Andreev56454792012-05-17 14:32:19 +0300995 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000996
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200997 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
999 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001000 }
1001
Andrey Andreev56454792012-05-17 14:32:19 +03001002 $body .= $this->_get_mime_message().$this->newline.$this->newline
1003 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
Andrey Andreev56454792012-05-17 14:32:19 +03001005 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1006 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001007
Andrey Andreev56454792012-05-17 14:32:19 +03001008 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001009
1010 break;
1011 case 'html-attach' :
1012
Andrey Andreev56454792012-05-17 14:32:19 +03001013 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001014
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001015 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
1017 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 }
1019
Andrey Andreev56454792012-05-17 14:32:19 +03001020 $body .= $this->_get_mime_message().$this->newline.$this->newline
1021 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001022
Andrey Andreev56454792012-05-17 14:32:19 +03001023 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1024 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001025
Andrey Andreev56454792012-05-17 14:32:19 +03001026 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1027 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1028 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001029
Andrey Andreev56454792012-05-17 14:32:19 +03001030 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1031 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev56454792012-05-17 14:32:19 +03001033 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1034 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001035
1036 break;
1037 }
1038
1039 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001040 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
tritbc4ac992011-11-23 07:19:41 -05001042 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001043 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001045 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001046
Alex Bilbied261b1e2012-06-02 11:12:16 +01001047 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001049 if ( ! file_exists($filename))
1050 {
1051 $this->_set_error_message('lang:email_attachment_missing', $filename);
1052 return FALSE;
1053 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
Matteo Mattei5688d582012-03-13 19:29:29 +01001055 $file = filesize($filename) +1;
1056
1057 if ( ! $fp = fopen($filename, FOPEN_READ))
1058 {
1059 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1060 return FALSE;
1061 }
1062
Matteo Matteic3b36f42012-03-26 10:27:17 +02001063 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001064 $file_content = fread($fp, $file);
1065 fclose($fp);
1066 }
1067 else
1068 {
1069 $file_content =& $this->_attach_content[$i];
1070 }
Andrey Andreev56454792012-05-17 14:32:19 +03001071
1072 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1073 .'Content-type: '.$ctype.'; '
1074 .'name="'.$basename.'"'.$this->newline
1075 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1076 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001077
Matteo Mattei5688d582012-03-13 19:29:29 +01001078 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080
Andrey Andreev56454792012-05-17 14:32:19 +03001081 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1082 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 return;
1084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 // --------------------------------------------------------------------
1087
1088 /**
1089 * Prep Quoted Printable
1090 *
1091 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1092 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @return string
1096 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001097 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreev683b34d2012-10-09 15:00:00 +03001099 // RFC 2045 specifies CRLF as "\r\n".
1100 // However, many developers choose to override that and violate
1101 // the RFC rules due to (apparently) a bug in MS Exchange,
1102 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001103 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001104 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001105 if (is_php('5.3'))
1106 {
1107 return quoted_printable_encode($str);
1108 }
1109 elseif (function_exists('imap_8bit'))
1110 {
1111 return imap_8bit($str);
1112 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001113 }
1114
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001115 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001116 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001117
1118 // Standardize newlines
1119 if (strpos($str, "\r") !== FALSE)
1120 {
1121 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1122 }
1123
1124 // We are intentionally wrapping so mail servers will encode characters
1125 // properly and MUAs will behave, so {unwrap} must go!
1126 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 $escape = '=';
1129 $output = '';
1130
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001131 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
1133 $length = strlen($line);
1134 $temp = '';
1135
1136 // Loop through each character in the line to add soft-wrap
1137 // characters at the end of a line " =\r\n" and add the newly
1138 // processed line(s) to the output (see comment on $crlf class property)
1139 for ($i = 0; $i < $length; $i++)
1140 {
1141 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001142 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 $ascii = ord($char);
1144
1145 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001146 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001148 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001150 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001152 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
1154
1155 // If we're at the character limit, add the line to the output,
1156 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001157 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
1159 $output .= $temp.$escape.$this->crlf;
1160 $temp = '';
1161 }
1162
1163 // Add the character to our temporary line
1164 $temp .= $char;
1165 }
1166
1167 // Add our completed line to the output
1168 $output .= $temp.$this->crlf;
1169 }
1170
1171 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001172 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
1175 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 /**
1178 * Prep Q Encoding
1179 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001180 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 * but not identical to quoted-printable, so it has its own method
1182 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001183 * @param string
1184 * @param bool set to TRUE for processing From: headers
1185 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001187 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
1189 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1190
1191 // Line length must not exceed 76 characters, so we adjust for
1192 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1193 $limit = 75 - 7 - strlen($this->charset);
1194
1195 // these special characters must be converted too
1196 $convert = array('_', '=', '?');
1197
1198 if ($from === TRUE)
1199 {
1200 $convert[] = ',';
1201 $convert[] = ';';
1202 }
1203
1204 $output = '';
1205 $temp = '';
1206
1207 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1208 {
1209 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001210 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 $ascii = ord($char);
1212
1213 // convert ALL non-printable ASCII characters and our specials
1214 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1215 {
1216 $char = '='.dechex($ascii);
1217 }
1218
1219 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001220 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
1222 $char = '_';
1223 }
1224
1225 // If we're at the character limit, add the line to the output,
1226 // reset our temp variable, and keep on chuggin'
1227 if ((strlen($temp) + strlen($char)) >= $limit)
1228 {
1229 $output .= $temp.$this->crlf;
1230 $temp = '';
1231 }
1232
1233 // Add the character to our temporary line
1234 $temp .= $char;
1235 }
1236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // wrap each line with the shebang, charset, and transfer encoding
1238 // the preceding space on successive lines is required for header "folding"
Bruno Barão51f72cd2012-10-08 16:31:46 +01001239 return trim(preg_replace('/^(.*?)(\r*)$/m', ' =?'.$this->charset.'?Q?$1?=$2', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 }
1241
1242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 /**
1245 * Send Email
1246 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 * @return bool
1248 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001249 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001251 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
1253 $this->reply_to($this->_headers['From']);
1254 }
1255
Andrey Andreev76f15c92012-03-01 13:05:07 +02001256 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1257 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1258 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
patworkb0707982011-04-08 15:10:05 +02001260 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 return FALSE;
1262 }
1263
1264 $this->_build_headers();
1265
Andrey Andreev76f15c92012-03-01 13:05:07 +02001266 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001268 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001269
Alex Bilbiea87aab32012-07-30 09:50:37 +01001270 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001271 {
1272 $this->clear();
1273 }
1274
Alex Bilbieb901e732012-07-30 09:44:57 +01001275 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 }
1277
1278 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001279 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001280
Alex Bilbiea87aab32012-07-30 09:50:37 +01001281 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001282 {
1283 $this->clear();
1284 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001285
Alex Bilbieb901e732012-07-30 09:44:57 +01001286 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 // --------------------------------------------------------------------
1290
1291 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001292 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001294 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001296 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001298 $float = $this->bcc_batch_size - 1;
1299 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 $chunk = array();
1301
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001302 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 {
1304 if (isset($this->_bcc_array[$i]))
1305 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001306 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
1308
Alex Bilbied261b1e2012-06-02 11:12:16 +01001309 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 {
1311 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001312 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001313 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 }
1315
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001316 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
1318 $chunk[] = substr($set, 1);
1319 }
1320 }
1321
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001322 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
1324 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001325
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001326 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001327
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001328 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001330 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 }
1332 else
1333 {
1334 $this->_bcc_array = $bcc;
1335 }
1336
1337 $this->_build_message();
1338 $this->_spool_email();
1339 }
1340 }
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // --------------------------------------------------------------------
1343
1344 /**
1345 * Unwrap special elements
1346 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 * @return void
1348 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001349 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
Andrey Andreev56454792012-05-17 14:32:19 +03001351 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 }
Barry Mienydd671972010-10-04 16:33:58 +02001353
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 // --------------------------------------------------------------------
1355
1356 /**
1357 * Strip line-breaks via callback
1358 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 * @return string
1360 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001361 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 {
1363 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1364 {
1365 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1366 }
1367
1368 return $matches[1];
1369 }
Barry Mienydd671972010-10-04 16:33:58 +02001370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 // --------------------------------------------------------------------
1372
1373 /**
1374 * Spool mail to the mail server
1375 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 * @return bool
1377 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001378 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 {
1380 $this->_unwrap_specials();
1381
Andrey Andreev56454792012-05-17 14:32:19 +03001382 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001383 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
Andrey Andreev56454792012-05-17 14:32:19 +03001385 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001386 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 }
1388
patworkb0707982011-04-08 15:10:05 +02001389 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 return TRUE;
1391 }
Barry Mienydd671972010-10-04 16:33:58 +02001392
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 // --------------------------------------------------------------------
1394
1395 /**
1396 * Send using mail()
1397 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 * @return bool
1399 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001400 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001402 if (is_array($this->_recipients))
1403 {
1404 $this->_recipients = implode(', ', $this->_recipients);
1405 }
1406
Alex Bilbied261b1e2012-06-02 11:12:16 +01001407 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001409 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 }
1411 else
1412 {
1413 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1414 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001415 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 }
1417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Send using Sendmail
1423 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 * @return bool
1425 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001426 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
Melounek58dfc082012-06-29 08:43:47 +02001428 $fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'.' -r '.$this->clean_email($this->_headers['Return-Path']), 'w');
Derek Allard2067d1a2008-11-13 22:59:24 +00001429
Derek Jones4cefaa42009-04-29 19:13:56 +00001430 if ($fp === FALSE OR $fp === NULL)
1431 {
1432 // server probably has popen disabled, so nothing we can do to get a verbose error.
1433 return FALSE;
1434 }
Derek Jones71141ce2010-03-02 16:41:20 -06001435
Derek Jonesc630bcf2008-11-17 21:09:45 +00001436 fputs($fp, $this->_header_str);
1437 fputs($fp, $this->_finalbody);
1438
Barry Mienydd671972010-10-04 16:33:58 +02001439 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001440
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001441 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
patworkb0707982011-04-08 15:10:05 +02001443 $this->_set_error_message('lang:email_exit_status', $status);
1444 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 return FALSE;
1446 }
1447
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 return TRUE;
1449 }
Barry Mienydd671972010-10-04 16:33:58 +02001450
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 // --------------------------------------------------------------------
1452
1453 /**
1454 * Send using SMTP
1455 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 * @return bool
1457 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001458 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001460 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 {
patworkb0707982011-04-08 15:10:05 +02001462 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 return FALSE;
1464 }
1465
Diogo Osório593f7982012-03-02 18:04:17 +00001466 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001467 {
1468 return FALSE;
1469 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001470
1471 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1472
Pascal Kriete14287f32011-02-14 13:39:34 -05001473 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 {
1475 $this->_send_command('to', $val);
1476 }
1477
1478 if (count($this->_cc_array) > 0)
1479 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001480 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001482 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 {
1484 $this->_send_command('to', $val);
1485 }
1486 }
1487 }
1488
1489 if (count($this->_bcc_array) > 0)
1490 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001491 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001493 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 {
1495 $this->_send_command('to', $val);
1496 }
1497 }
1498 }
1499
1500 $this->_send_command('data');
1501
1502 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001503 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001504
1505 $this->_send_data('.');
1506
1507 $reply = $this->_get_smtp_data();
1508
1509 $this->_set_error_message($reply);
1510
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001511 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
patworkb0707982011-04-08 15:10:05 +02001513 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 return FALSE;
1515 }
1516
1517 $this->_send_command('quit');
1518 return TRUE;
1519 }
Barry Mienydd671972010-10-04 16:33:58 +02001520
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 // --------------------------------------------------------------------
1522
1523 /**
1524 * SMTP Connect
1525 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 * @param string
1527 * @return string
1528 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001529 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001531 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001532
Radu Potopbbf04b02011-09-28 13:57:51 +03001533 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001534 $this->smtp_port,
1535 $errno,
1536 $errstr,
1537 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001538
Pascal Kriete14287f32011-02-14 13:39:34 -05001539 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 {
Andrey Andreev56454792012-05-17 14:32:19 +03001541 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 return FALSE;
1543 }
1544
1545 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001546
Alex Bilbied261b1e2012-06-02 11:12:16 +01001547 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001548 {
1549 $this->_send_command('hello');
1550 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001551
Radu Potop4c589ae2011-09-29 10:19:55 +03001552 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001553
Sean Fishere862ddd2011-10-26 22:45:00 -03001554 if ($crypto !== TRUE)
1555 {
1556 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1557 return FALSE;
1558 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001559 }
1560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 return $this->_send_command('hello');
1562 }
Barry Mienydd671972010-10-04 16:33:58 +02001563
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 // --------------------------------------------------------------------
1565
1566 /**
1567 * Send SMTP command
1568 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 * @param string
1570 * @param string
1571 * @return string
1572 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001573 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
1575 switch ($cmd)
1576 {
1577 case 'hello' :
1578
Andrey Andreev56454792012-05-17 14:32:19 +03001579 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1580 {
1581 $this->_send_data('EHLO '.$this->_get_hostname());
1582 }
1583 else
1584 {
1585 $this->_send_data('HELO '.$this->_get_hostname());
1586 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001587
1588 $resp = 250;
1589 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001590 case 'starttls' :
1591
1592 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001593 $resp = 220;
1594 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 case 'from' :
1596
1597 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 $resp = 250;
1599 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001600 case 'to' :
1601
leandronf7686c122012-03-22 08:07:31 -03001602 if ($this->dsn)
1603 {
leandronfb4552942012-03-21 23:54:26 -03001604 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001605 }
leandronfb4552942012-03-21 23:54:26 -03001606 else
leandronf7686c122012-03-22 08:07:31 -03001607 {
leandronfb4552942012-03-21 23:54:26 -03001608 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001609 }
Andrey Andreev56454792012-05-17 14:32:19 +03001610
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 $resp = 250;
1612 break;
1613 case 'data' :
1614
1615 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 $resp = 354;
1617 break;
1618 case 'quit' :
1619
1620 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 $resp = 221;
1622 break;
1623 }
1624
1625 $reply = $this->_get_smtp_data();
1626
Andrey Andreev56454792012-05-17 14:32:19 +03001627 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001628
Andrey Andreevba261762012-06-11 14:11:35 +03001629 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
patworkb0707982011-04-08 15:10:05 +02001631 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 return FALSE;
1633 }
1634
Alex Bilbied261b1e2012-06-02 11:12:16 +01001635 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 {
1637 fclose($this->_smtp_connect);
1638 }
1639
1640 return TRUE;
1641 }
Barry Mienydd671972010-10-04 16:33:58 +02001642
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 // --------------------------------------------------------------------
1644
1645 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001646 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 * @return bool
1649 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001650 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
1652 if ( ! $this->_smtp_auth)
1653 {
1654 return TRUE;
1655 }
1656
Alex Bilbied261b1e2012-06-02 11:12:16 +01001657 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 {
patworkb0707982011-04-08 15:10:05 +02001659 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 return FALSE;
1661 }
1662
1663 $this->_send_data('AUTH LOGIN');
1664
1665 $reply = $this->_get_smtp_data();
1666
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001667 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 {
patworkb0707982011-04-08 15:10:05 +02001669 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 return FALSE;
1671 }
1672
1673 $this->_send_data(base64_encode($this->smtp_user));
1674
1675 $reply = $this->_get_smtp_data();
1676
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001677 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
patworkb0707982011-04-08 15:10:05 +02001679 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 return FALSE;
1681 }
1682
1683 $this->_send_data(base64_encode($this->smtp_pass));
1684
1685 $reply = $this->_get_smtp_data();
1686
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001687 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 {
patworkb0707982011-04-08 15:10:05 +02001689 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 return FALSE;
1691 }
1692
1693 return TRUE;
1694 }
Barry Mienydd671972010-10-04 16:33:58 +02001695
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 // --------------------------------------------------------------------
1697
1698 /**
1699 * Send SMTP data
1700 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 * @return bool
1702 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001703 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 {
1705 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1706 {
patworkb0707982011-04-08 15:10:05 +02001707 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 return FALSE;
1709 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001710
1711 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 }
Barry Mienydd671972010-10-04 16:33:58 +02001713
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // --------------------------------------------------------------------
1715
1716 /**
1717 * Get SMTP data
1718 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 * @return string
1720 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001721 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 {
Andrey Andreev56454792012-05-17 14:32:19 +03001723 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001724
1725 while ($str = fgets($this->_smtp_connect, 512))
1726 {
1727 $data .= $str;
1728
Alex Bilbied261b1e2012-06-02 11:12:16 +01001729 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 {
1731 break;
1732 }
1733 }
1734
1735 return $data;
1736 }
Barry Mienydd671972010-10-04 16:33:58 +02001737
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 // --------------------------------------------------------------------
1739
1740 /**
1741 * Get Hostname
1742 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 * @return string
1744 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001745 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001747 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 }
Barry Mienydd671972010-10-04 16:33:58 +02001749
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 // --------------------------------------------------------------------
1751
1752 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 * Get Debug Message
1754 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 * @return string
1756 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001757 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
1759 $msg = '';
1760
1761 if (count($this->_debug_msg) > 0)
1762 {
1763 foreach ($this->_debug_msg as $val)
1764 {
1765 $msg .= $val;
1766 }
1767 }
1768
Andrey Andreev59c5b532012-03-01 14:09:51 +02001769 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 // --------------------------------------------------------------------
1773
1774 /**
1775 * Set Message
1776 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001778 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001780 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
1782 $CI =& get_instance();
1783 $CI->lang->load('email');
1784
Andrey Andreev76f15c92012-03-01 13:05:07 +02001785 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 {
Andrey Andreev56454792012-05-17 14:32:19 +03001787 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 }
1789 else
1790 {
Andrey Andreev56454792012-05-17 14:32:19 +03001791 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 }
1793 }
Barry Mienydd671972010-10-04 16:33:58 +02001794
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 // --------------------------------------------------------------------
1796
1797 /**
1798 * Mime Types
1799 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 * @param string
1801 * @return string
1802 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001803 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001805 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001806
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001807 $ext = strtolower($ext);
1808
1809 if ( ! is_array($mimes))
1810 {
1811 $mimes =& get_mimes();
1812 }
1813
1814 if (isset($mimes[$ext]))
1815 {
1816 return is_array($mimes[$ext])
1817 ? current($mimes[$ext])
1818 : $mimes[$ext];
1819 }
1820
1821 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 }
1823
1824}
Derek Allard2067d1a2008-11-13 22:59:24 +00001825
1826/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001827/* Location: ./system/libraries/Email.php */