blob: 1b457aee411d59834e3a089fd83223eb4fbe5d54 [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 */
Melounek58dfc082012-06-29 08:43:47 +0200191 public function from($from, $name = '', $return_path = '')
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));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300201 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200202 {
203 $this->validate_email($this->_str_to_array($return_path));
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100208 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 // only use Q encoding if there are characters that would require it
211 if ( ! preg_match('/[\200-\377]/', $name))
212 {
213 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000214 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216 else
217 {
218 $name = $this->_prep_q_encoding($name, TRUE);
219 }
220 }
221
Mickey Wubfc1cad2012-05-31 22:28:40 -0700222 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200223
Andrey Andreevccd01c72012-10-05 17:12:55 +0300224 if( ! $return_path)
Melounek58dfc082012-06-29 08:43:47 +0200225 {
226 $return_path = $from;
227 }
228 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500229
Greg Akera769deb2010-11-10 15:47:29 -0600230 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Set Reply-to
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string
239 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200240 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000242 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200244 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200246 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249 if ($this->validate)
250 {
251 $this->validate_email($this->_str_to_array($replyto));
252 }
253
Alex Bilbied261b1e2012-06-02 11:12:16 +0100254 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 $name = $replyto;
257 }
258
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300259 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $name = '"'.$name.'"';
262 }
263
Mickey Wubfc1cad2012-05-31 22:28:40 -0700264 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600266
267 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Set Recipients
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200276 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000278 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $to = $this->_str_to_array($to);
281 $to = $this->clean_email($to);
282
283 if ($this->validate)
284 {
285 $this->validate_email($to);
286 }
287
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200288 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700290 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 switch ($this->_get_protocol())
294 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200295 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000296 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200298 case 'sendmail':
299 case 'mail':
300 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 break;
302 }
Eric Barnes6113f542010-12-29 13:36:12 -0500303
Greg Akera769deb2010-11-10 15:47:29 -0600304 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // --------------------------------------------------------------------
308
309 /**
310 * Set CC
311 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200313 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000315 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Andrey Andreev56454792012-05-17 14:32:19 +0300317 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
319 if ($this->validate)
320 {
321 $this->validate_email($cc);
322 }
323
Mickey Wubfc1cad2012-05-31 22:28:40 -0700324 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200326 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $this->_cc_array = $cc;
329 }
Eric Barnes6113f542010-12-29 13:36:12 -0500330
Greg Akera769deb2010-11-10 15:47:29 -0600331 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
337 * Set BCC
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @param string
340 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200341 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000343 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100345 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 $this->bcc_batch_mode = TRUE;
348 $this->bcc_batch_size = $limit;
349 }
350
Andrey Andreev56454792012-05-17 14:32:19 +0300351 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 if ($this->validate)
354 {
355 $this->validate_email($bcc);
356 }
357
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200358 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $this->_bcc_array = $bcc;
361 }
362 else
363 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700364 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Eric Barnes6113f542010-12-29 13:36:12 -0500366
Greg Akera769deb2010-11-10 15:47:29 -0600367 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
371
372 /**
373 * Set Email Subject
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200376 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000378 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700381 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600382 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
386
387 /**
388 * Set Body
389 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200391 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000393 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200395 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200396
Andrey Andreevaf728622011-10-20 10:11:59 +0300397 /* strip slashes only if magic quotes is ON
398 if we do it with magic quotes OFF, it strips real, user-inputted chars.
399
400 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
401 it will probably not exist in future versions at all.
402 */
403 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200404 {
diegorivera33c32802011-10-19 02:56:15 -0200405 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200406 }
diegorivera33c32802011-10-19 02:56:15 -0200407
Greg Akera769deb2010-11-10 15:47:29 -0600408 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * Assign file attachments
415 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200417 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200419 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
trit151fc682011-11-23 07:30:06 -0500421 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500422 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200423 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600424 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Add a Header Item
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string
433 * @param string
434 * @return void
435 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700436 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 $this->_headers[$header] = $value;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * Convert a String to an Array
445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param string
447 * @return array
448 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600449 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 if ( ! is_array($email))
452 {
Andrey Andreev56454792012-05-17 14:32:19 +0300453 return (strpos($email, ',') !== FALSE)
454 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
455 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Andrey Andreev56454792012-05-17 14:32:19 +0300457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return $email;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * Set Multipart Value
465 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200467 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000469 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400471 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600472 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Set Mailtype
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200481 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000483 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev56454792012-05-17 14:32:19 +0300485 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600486 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Set Wordwrap
493 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400494 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200495 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000497 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400499 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600500 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // --------------------------------------------------------------------
504
505 /**
506 * Set Protocol
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200509 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000511 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200513 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600514 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * Set Priority
521 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200522 * @param int
523 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000525 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200527 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600528 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // --------------------------------------------------------------------
532
533 /**
534 * Set Newline Character
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200537 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000539 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200541 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600542 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // --------------------------------------------------------------------
546
547 /**
548 * Set CRLF
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200551 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000553 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200555 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600556 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
562 * Set Message Boundary
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @return void
565 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600566 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200568 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
569 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Get the Message ID
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return string
578 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600579 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200581 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300582 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
586
587 /**
588 * Get Mail Protocol
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200591 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600593 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200596 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000597
Alex Bilbied261b1e2012-06-02 11:12:16 +0100598 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 return $this->protocol;
601 }
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * Get Mail Encoding
608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param bool
610 * @return string
611 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600612 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200614 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 foreach ($this->_base_charsets as $charset)
617 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300618 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 $this->_encoding = '7bit';
621 }
622 }
623
Alex Bilbied261b1e2012-06-02 11:12:16 +0100624 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 return $this->_encoding;
627 }
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Get content type (text/html/attachment)
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @return string
636 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600637 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreev56454792012-05-17 14:32:19 +0300639 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100641 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200643 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 return 'plain-attach';
646 }
647 else
648 {
649 return 'plain';
650 }
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Set RFC 822 Date
657 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 * @return string
659 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600660 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200662 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300663 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200665 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000666
Andrey Andreev59c5b532012-03-01 14:09:51 +0200667 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 // --------------------------------------------------------------------
671
672 /**
673 * Mime message
674 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @return string
676 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600677 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200679 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 +0000680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // --------------------------------------------------------------------
683
684 /**
685 * Validate Email Address
686 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * @param string
688 * @return bool
689 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000690 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 if ( ! is_array($email))
693 {
patworkb0707982011-04-08 15:10:05 +0200694 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 return FALSE;
696 }
697
698 foreach ($email as $val)
699 {
700 if ( ! $this->valid_email($val))
701 {
patworkb0707982011-04-08 15:10:05 +0200702 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 return FALSE;
704 }
705 }
706
707 return TRUE;
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // --------------------------------------------------------------------
711
712 /**
713 * Email Validation
714 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 * @param string
716 * @return bool
717 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400718 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300720 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 // --------------------------------------------------------------------
724
725 /**
726 * Clean Extended Email Address: Joe Smith <joe@smith.com>
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string
729 * @return string
730 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000731 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 if ( ! is_array($email))
734 {
Andrey Andreev56454792012-05-17 14:32:19 +0300735 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 $clean_email = array();
739
740 foreach ($email as $addy)
741 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200742 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744
745 return $clean_email;
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // --------------------------------------------------------------------
749
750 /**
751 * Build alternative plain text message
752 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000753 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * in plain-text headers of HTML-formatted emails.
755 * If the user hasn't specified his own alternative message
756 * it creates one by stripping the HTML
757 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 * @return string
759 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600760 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100762 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 {
764 return $this->word_wrap($this->alt_message, '76');
765 }
766
Andrey Andreev56454792012-05-17 14:32:19 +0300767 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200768 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000769
770 for ($i = 20; $i >= 3; $i--)
771 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200772 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200775 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 // --------------------------------------------------------------------
779
780 /**
781 * Word Wrap
782 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200784 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @return string
786 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000787 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
789 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100790 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100792 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
794
795 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300796 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000797
798 // Standardize newlines
799 if (strpos($str, "\r") !== FALSE)
800 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200801 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
803
804 // If the current word is surrounded by {unwrap} tags we'll
805 // strip the entire chunk and replace it with a marker.
806 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300807 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200809 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200811 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300812 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
814 }
815
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000816 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300818 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 $str = wordwrap($str, $charlim, "\n", FALSE);
820
821 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300822 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 foreach (explode("\n", $str) as $line)
824 {
825 // Is the line within the allowed character count?
826 // If so we'll join it to the output and continue
827 if (strlen($line) <= $charlim)
828 {
829 $output .= $line.$this->newline;
830 continue;
831 }
832
833 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200834 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
836 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300837 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 break;
840 }
841
842 // Trim the word down
843 $temp .= substr($line, 0, $charlim-1);
844 $line = substr($line, $charlim-1);
845 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200846 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000847
848 // If $temp contains data it means we had to split up an over-length
849 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100850 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200852 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
854
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200855 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
857
858 // Put our markers back
859 if (count($unwrap) > 0)
860 {
861 foreach ($unwrap as $key => $val)
862 {
Andrey Andreev56454792012-05-17 14:32:19 +0300863 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
865 }
866
867 return $output;
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 // --------------------------------------------------------------------
871
872 /**
873 * Build final headers
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @return string
876 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600877 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700879 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
880 $this->set_header('X-Mailer', $this->useragent);
881 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
882 $this->set_header('Message-ID', $this->_get_message_id());
883 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
889 * Write Headers as a string
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @return void
892 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600893 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200895 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 $this->_subject = $this->_headers['Subject'];
898 unset($this->_headers['Subject']);
899 }
900
901 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300902 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000903
Pascal Kriete14287f32011-02-14 13:39:34 -0500904 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
906 $val = trim($val);
907
Alex Bilbied261b1e2012-06-02 11:12:16 +0100908 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Andrey Andreev56454792012-05-17 14:32:19 +0300910 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
912 }
913
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200914 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Derek Jones1d890882009-02-10 20:32:14 +0000916 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
921
922 /**
923 * Build Final Body and attachments
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @return void
926 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600927 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200929 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
931 $this->_body = $this->word_wrap($this->_body);
932 }
933
934 $this->_set_boundaries();
935 $this->_write_headers();
936
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200937 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500938 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000939
940 switch ($this->_get_content_type())
941 {
942 case 'plain' :
943
Andrey Andreev56454792012-05-17 14:32:19 +0300944 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
945 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000946
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200947 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $this->_header_str .= $hdr;
950 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Brandon Jones485d7412010-11-09 16:38:17 -0500952 else
953 {
954 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
955 }
Eric Barnes6113f542010-12-29 13:36:12 -0500956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 return;
958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 case 'html' :
960
961 if ($this->send_multipart === FALSE)
962 {
Andrey Andreev56454792012-05-17 14:32:19 +0300963 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
964 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
966 else
967 {
Andrey Andreev56454792012-05-17 14:32:19 +0300968 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969
Andrey Andreev56454792012-05-17 14:32:19 +0300970 $body .= $this->_get_mime_message().$this->newline.$this->newline
971 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000972
Andrey Andreev56454792012-05-17 14:32:19 +0300973 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
974 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
975 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500976
Andrey Andreev56454792012-05-17 14:32:19 +0300977 .'Content-Type: text/html; charset='.$this->charset.$this->newline
978 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Eric Barnes6113f542010-12-29 13:36:12 -0500980
Andrey Andreev56454792012-05-17 14:32:19 +0300981 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500982
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200983 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500986 }
987 else
988 {
Andrey Andreev56454792012-05-17 14:32:19 +0300989 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 }
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
993 if ($this->send_multipart !== FALSE)
994 {
Andrey Andreev56454792012-05-17 14:32:19 +0300995 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 return;
999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 case 'plain-attach' :
1001
Andrey Andreev56454792012-05-17 14:32:19 +03001002 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001004 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001007 }
1008
Andrey Andreev56454792012-05-17 14:32:19 +03001009 $body .= $this->_get_mime_message().$this->newline.$this->newline
1010 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001011
Andrey Andreev56454792012-05-17 14:32:19 +03001012 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1013 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
Andrey Andreev56454792012-05-17 14:32:19 +03001015 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
1017 break;
1018 case 'html-attach' :
1019
Andrey Andreev56454792012-05-17 14:32:19 +03001020 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001021
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001022 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
1026
Andrey Andreev56454792012-05-17 14:32:19 +03001027 $body .= $this->_get_mime_message().$this->newline.$this->newline
1028 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001029
Andrey Andreev56454792012-05-17 14:32:19 +03001030 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1031 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev56454792012-05-17 14:32:19 +03001033 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1034 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1035 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001036
Andrey Andreev56454792012-05-17 14:32:19 +03001037 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1038 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001039
Andrey Andreev56454792012-05-17 14:32:19 +03001040 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1041 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 break;
1044 }
1045
1046 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001047 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
tritbc4ac992011-11-23 07:19:41 -05001049 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001050 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001052 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Alex Bilbied261b1e2012-06-02 11:12:16 +01001054 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001056 if ( ! file_exists($filename))
1057 {
1058 $this->_set_error_message('lang:email_attachment_missing', $filename);
1059 return FALSE;
1060 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001061
Matteo Mattei5688d582012-03-13 19:29:29 +01001062 $file = filesize($filename) +1;
1063
1064 if ( ! $fp = fopen($filename, FOPEN_READ))
1065 {
1066 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1067 return FALSE;
1068 }
1069
Matteo Matteic3b36f42012-03-26 10:27:17 +02001070 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001071 $file_content = fread($fp, $file);
1072 fclose($fp);
1073 }
1074 else
1075 {
1076 $file_content =& $this->_attach_content[$i];
1077 }
Andrey Andreev56454792012-05-17 14:32:19 +03001078
1079 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1080 .'Content-type: '.$ctype.'; '
1081 .'name="'.$basename.'"'.$this->newline
1082 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1083 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001084
Matteo Mattei5688d582012-03-13 19:29:29 +01001085 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
1087
Andrey Andreev56454792012-05-17 14:32:19 +03001088 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1089 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 return;
1091 }
Barry Mienydd671972010-10-04 16:33:58 +02001092
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 // --------------------------------------------------------------------
1094
1095 /**
1096 * Prep Quoted Printable
1097 *
1098 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1099 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1100 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @return string
1103 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001104 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
Andrey Andreev683b34d2012-10-09 15:00:00 +03001106 // RFC 2045 specifies CRLF as "\r\n".
1107 // However, many developers choose to override that and violate
1108 // the RFC rules due to (apparently) a bug in MS Exchange,
1109 // which only works with "\n".
1110 if ($this->crlf === "\r\n" && is_php('5.3'))
1111 {
1112 return quoted_printable_encode($str);
1113 }
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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001402 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001404 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 }
1406 else
1407 {
1408 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1409 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001410 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 +00001411 }
1412 }
Barry Mienydd671972010-10-04 16:33:58 +02001413
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 // --------------------------------------------------------------------
1415
1416 /**
1417 * Send using Sendmail
1418 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 * @return bool
1420 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001421 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 {
Melounek58dfc082012-06-29 08:43:47 +02001423 $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 +00001424
Derek Jones4cefaa42009-04-29 19:13:56 +00001425 if ($fp === FALSE OR $fp === NULL)
1426 {
1427 // server probably has popen disabled, so nothing we can do to get a verbose error.
1428 return FALSE;
1429 }
Derek Jones71141ce2010-03-02 16:41:20 -06001430
Derek Jonesc630bcf2008-11-17 21:09:45 +00001431 fputs($fp, $this->_header_str);
1432 fputs($fp, $this->_finalbody);
1433
Barry Mienydd671972010-10-04 16:33:58 +02001434 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001435
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001436 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 {
patworkb0707982011-04-08 15:10:05 +02001438 $this->_set_error_message('lang:email_exit_status', $status);
1439 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 return FALSE;
1441 }
1442
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 return TRUE;
1444 }
Barry Mienydd671972010-10-04 16:33:58 +02001445
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 // --------------------------------------------------------------------
1447
1448 /**
1449 * Send using SMTP
1450 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 * @return bool
1452 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001453 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001455 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 {
patworkb0707982011-04-08 15:10:05 +02001457 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 return FALSE;
1459 }
1460
Diogo Osório593f7982012-03-02 18:04:17 +00001461 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001462 {
1463 return FALSE;
1464 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001465
1466 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1467
Pascal Kriete14287f32011-02-14 13:39:34 -05001468 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
1470 $this->_send_command('to', $val);
1471 }
1472
1473 if (count($this->_cc_array) > 0)
1474 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001475 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001477 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 {
1479 $this->_send_command('to', $val);
1480 }
1481 }
1482 }
1483
1484 if (count($this->_bcc_array) > 0)
1485 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001486 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001488 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 {
1490 $this->_send_command('to', $val);
1491 }
1492 }
1493 }
1494
1495 $this->_send_command('data');
1496
1497 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001498 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001499
1500 $this->_send_data('.');
1501
1502 $reply = $this->_get_smtp_data();
1503
1504 $this->_set_error_message($reply);
1505
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001506 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 {
patworkb0707982011-04-08 15:10:05 +02001508 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 return FALSE;
1510 }
1511
1512 $this->_send_command('quit');
1513 return TRUE;
1514 }
Barry Mienydd671972010-10-04 16:33:58 +02001515
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 // --------------------------------------------------------------------
1517
1518 /**
1519 * SMTP Connect
1520 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 * @param string
1522 * @return string
1523 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001524 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001526 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001527
Radu Potopbbf04b02011-09-28 13:57:51 +03001528 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001529 $this->smtp_port,
1530 $errno,
1531 $errstr,
1532 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001533
Pascal Kriete14287f32011-02-14 13:39:34 -05001534 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 {
Andrey Andreev56454792012-05-17 14:32:19 +03001536 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 return FALSE;
1538 }
1539
1540 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001541
Alex Bilbied261b1e2012-06-02 11:12:16 +01001542 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001543 {
1544 $this->_send_command('hello');
1545 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001546
Radu Potop4c589ae2011-09-29 10:19:55 +03001547 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001548
Sean Fishere862ddd2011-10-26 22:45:00 -03001549 if ($crypto !== TRUE)
1550 {
1551 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1552 return FALSE;
1553 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001554 }
1555
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 return $this->_send_command('hello');
1557 }
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Send SMTP command
1563 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 * @param string
1565 * @param string
1566 * @return string
1567 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001568 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 {
1570 switch ($cmd)
1571 {
1572 case 'hello' :
1573
Andrey Andreev56454792012-05-17 14:32:19 +03001574 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1575 {
1576 $this->_send_data('EHLO '.$this->_get_hostname());
1577 }
1578 else
1579 {
1580 $this->_send_data('HELO '.$this->_get_hostname());
1581 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001582
1583 $resp = 250;
1584 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001585 case 'starttls' :
1586
1587 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001588 $resp = 220;
1589 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 case 'from' :
1591
1592 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 $resp = 250;
1594 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001595 case 'to' :
1596
leandronf7686c122012-03-22 08:07:31 -03001597 if ($this->dsn)
1598 {
leandronfb4552942012-03-21 23:54:26 -03001599 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001600 }
leandronfb4552942012-03-21 23:54:26 -03001601 else
leandronf7686c122012-03-22 08:07:31 -03001602 {
leandronfb4552942012-03-21 23:54:26 -03001603 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001604 }
Andrey Andreev56454792012-05-17 14:32:19 +03001605
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 $resp = 250;
1607 break;
1608 case 'data' :
1609
1610 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 $resp = 354;
1612 break;
1613 case 'quit' :
1614
1615 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 $resp = 221;
1617 break;
1618 }
1619
1620 $reply = $this->_get_smtp_data();
1621
Andrey Andreev56454792012-05-17 14:32:19 +03001622 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001623
Andrey Andreevba261762012-06-11 14:11:35 +03001624 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 {
patworkb0707982011-04-08 15:10:05 +02001626 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 return FALSE;
1628 }
1629
Alex Bilbied261b1e2012-06-02 11:12:16 +01001630 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 {
1632 fclose($this->_smtp_connect);
1633 }
1634
1635 return TRUE;
1636 }
Barry Mienydd671972010-10-04 16:33:58 +02001637
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 // --------------------------------------------------------------------
1639
1640 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001641 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 * @return bool
1644 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001645 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001646 {
1647 if ( ! $this->_smtp_auth)
1648 {
1649 return TRUE;
1650 }
1651
Alex Bilbied261b1e2012-06-02 11:12:16 +01001652 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 {
patworkb0707982011-04-08 15:10:05 +02001654 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 return FALSE;
1656 }
1657
1658 $this->_send_data('AUTH LOGIN');
1659
1660 $reply = $this->_get_smtp_data();
1661
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001662 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 {
patworkb0707982011-04-08 15:10:05 +02001664 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 return FALSE;
1666 }
1667
1668 $this->_send_data(base64_encode($this->smtp_user));
1669
1670 $reply = $this->_get_smtp_data();
1671
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001672 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 {
patworkb0707982011-04-08 15:10:05 +02001674 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 return FALSE;
1676 }
1677
1678 $this->_send_data(base64_encode($this->smtp_pass));
1679
1680 $reply = $this->_get_smtp_data();
1681
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001682 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 {
patworkb0707982011-04-08 15:10:05 +02001684 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 return FALSE;
1686 }
1687
1688 return TRUE;
1689 }
Barry Mienydd671972010-10-04 16:33:58 +02001690
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 // --------------------------------------------------------------------
1692
1693 /**
1694 * Send SMTP data
1695 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 * @return bool
1697 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001698 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 {
1700 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1701 {
patworkb0707982011-04-08 15:10:05 +02001702 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 return FALSE;
1704 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001705
1706 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 }
Barry Mienydd671972010-10-04 16:33:58 +02001708
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 // --------------------------------------------------------------------
1710
1711 /**
1712 * Get SMTP data
1713 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 * @return string
1715 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001716 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 {
Andrey Andreev56454792012-05-17 14:32:19 +03001718 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001719
1720 while ($str = fgets($this->_smtp_connect, 512))
1721 {
1722 $data .= $str;
1723
Alex Bilbied261b1e2012-06-02 11:12:16 +01001724 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
1726 break;
1727 }
1728 }
1729
1730 return $data;
1731 }
Barry Mienydd671972010-10-04 16:33:58 +02001732
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 // --------------------------------------------------------------------
1734
1735 /**
1736 * Get Hostname
1737 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 * @return string
1739 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001740 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001742 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 }
Barry Mienydd671972010-10-04 16:33:58 +02001744
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 // --------------------------------------------------------------------
1746
1747 /**
1748 * Get IP
1749 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 * @return string
1751 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001752 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 {
1754 if ($this->_IP !== FALSE)
1755 {
1756 return $this->_IP;
1757 }
1758
Andrey Andreev76f15c92012-03-01 13:05:07 +02001759 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1760 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001761 if ($cip) $this->_IP = $cip;
1762 elseif ($rip) $this->_IP = $rip;
1763 else
1764 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001765 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001766 if ($fip)
1767 {
1768 $this->_IP = $fip;
1769 }
1770 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001771
Robin Sowell76b369e2010-03-19 11:15:28 -04001772 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 {
1774 $x = explode(',', $this->_IP);
1775 $this->_IP = end($x);
1776 }
1777
Andrey Andreev59c5b532012-03-01 14:09:51 +02001778 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 +00001779 {
1780 $this->_IP = '0.0.0.0';
1781 }
1782
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 return $this->_IP;
1784 }
Barry Mienydd671972010-10-04 16:33:58 +02001785
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 // --------------------------------------------------------------------
1787
1788 /**
1789 * Get Debug Message
1790 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 * @return string
1792 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001793 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 {
1795 $msg = '';
1796
1797 if (count($this->_debug_msg) > 0)
1798 {
1799 foreach ($this->_debug_msg as $val)
1800 {
1801 $msg .= $val;
1802 }
1803 }
1804
Andrey Andreev59c5b532012-03-01 14:09:51 +02001805 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 }
Barry Mienydd671972010-10-04 16:33:58 +02001807
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 // --------------------------------------------------------------------
1809
1810 /**
1811 * Set Message
1812 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001814 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001816 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 {
1818 $CI =& get_instance();
1819 $CI->lang->load('email');
1820
Andrey Andreev76f15c92012-03-01 13:05:07 +02001821 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
Andrey Andreev56454792012-05-17 14:32:19 +03001823 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 }
1825 else
1826 {
Andrey Andreev56454792012-05-17 14:32:19 +03001827 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 }
1829 }
Barry Mienydd671972010-10-04 16:33:58 +02001830
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 // --------------------------------------------------------------------
1832
1833 /**
1834 * Mime Types
1835 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 * @param string
1837 * @return string
1838 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001839 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001841 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001842
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001843 $ext = strtolower($ext);
1844
1845 if ( ! is_array($mimes))
1846 {
1847 $mimes =& get_mimes();
1848 }
1849
1850 if (isset($mimes[$ext]))
1851 {
1852 return is_array($mimes[$ext])
1853 ? current($mimes[$ext])
1854 : $mimes[$ext];
1855 }
1856
1857 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 }
1859
1860}
Derek Allard2067d1a2008-11-13 22:59:24 +00001861
1862/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001863/* Location: ./system/libraries/Email.php */