blob: 4776df4982e7932e7099845abfed5e33dc578da8 [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
983
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200984 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500987 }
988 else
989 {
Andrey Andreev56454792012-05-17 14:32:19 +0300990 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
994 if ($this->send_multipart !== FALSE)
995 {
Andrey Andreev56454792012-05-17 14:32:19 +0300996 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return;
1000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 case 'plain-attach' :
1002
Andrey Andreev56454792012-05-17 14:32:19 +03001003 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001005 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
1007 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001008 }
1009
Andrey Andreev56454792012-05-17 14:32:19 +03001010 $body .= $this->_get_mime_message().$this->newline.$this->newline
1011 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
Andrey Andreev56454792012-05-17 14:32:19 +03001013 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1014 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001015
Andrey Andreev56454792012-05-17 14:32:19 +03001016 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017
1018 break;
1019 case 'html-attach' :
1020
Andrey Andreev56454792012-05-17 14:32:19 +03001021 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001022
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001023 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027
Andrey Andreev56454792012-05-17 14:32:19 +03001028 $body .= $this->_get_mime_message().$this->newline.$this->newline
1029 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001030
Andrey Andreev56454792012-05-17 14:32:19 +03001031 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1032 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001033
Andrey Andreev56454792012-05-17 14:32:19 +03001034 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1035 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1036 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001037
Andrey Andreev56454792012-05-17 14:32:19 +03001038 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1039 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001040
Andrey Andreev56454792012-05-17 14:32:19 +03001041 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1042 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043
1044 break;
1045 }
1046
1047 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001048 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 {
tritbc4ac992011-11-23 07:19:41 -05001050 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001051 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001053 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
Alex Bilbied261b1e2012-06-02 11:12:16 +01001055 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001057 if ( ! file_exists($filename))
1058 {
1059 $this->_set_error_message('lang:email_attachment_missing', $filename);
1060 return FALSE;
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062
Matteo Mattei5688d582012-03-13 19:29:29 +01001063 $file = filesize($filename) +1;
1064
1065 if ( ! $fp = fopen($filename, FOPEN_READ))
1066 {
1067 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1068 return FALSE;
1069 }
1070
Matteo Matteic3b36f42012-03-26 10:27:17 +02001071 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001072 $file_content = fread($fp, $file);
1073 fclose($fp);
1074 }
1075 else
1076 {
1077 $file_content =& $this->_attach_content[$i];
1078 }
Andrey Andreev56454792012-05-17 14:32:19 +03001079
1080 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1081 .'Content-type: '.$ctype.'; '
1082 .'name="'.$basename.'"'.$this->newline
1083 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1084 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001085
Matteo Mattei5688d582012-03-13 19:29:29 +01001086 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
1088
Andrey Andreev56454792012-05-17 14:32:19 +03001089 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1090 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 return;
1092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Prep Quoted Printable
1098 *
1099 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1100 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1101 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001103 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @return string
1105 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001106 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 {
1108 // Set the character limit
1109 // Don't allow over 76, as that will make servers and MUAs barf
1110 // all over quoted-printable data
Alex Bilbied261b1e2012-06-02 11:12:16 +01001111 if ($charlim === '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001113 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 }
1115
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001116 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001117 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001118
1119 // Standardize newlines
1120 if (strpos($str, "\r") !== FALSE)
1121 {
1122 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1123 }
1124
1125 // We are intentionally wrapping so mail servers will encode characters
1126 // properly and MUAs will behave, so {unwrap} must go!
1127 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 $escape = '=';
1130 $output = '';
1131
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001132 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 {
1134 $length = strlen($line);
1135 $temp = '';
1136
1137 // Loop through each character in the line to add soft-wrap
1138 // characters at the end of a line " =\r\n" and add the newly
1139 // processed line(s) to the output (see comment on $crlf class property)
1140 for ($i = 0; $i < $length; $i++)
1141 {
1142 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001143 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 $ascii = ord($char);
1145
1146 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001147 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001149 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001151 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001153 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 }
1155
1156 // If we're at the character limit, add the line to the output,
1157 // reset our temp variable, and keep on chuggin'
1158 if ((strlen($temp) + strlen($char)) >= $charlim)
1159 {
1160 $output .= $temp.$escape.$this->crlf;
1161 $temp = '';
1162 }
1163
1164 // Add the character to our temporary line
1165 $temp .= $char;
1166 }
1167
1168 // Add our completed line to the output
1169 $output .= $temp.$this->crlf;
1170 }
1171
1172 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001173 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
1175
1176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 /**
1179 * Prep Q Encoding
1180 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001181 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 * but not identical to quoted-printable, so it has its own method
1183 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001184 * @param string
1185 * @param bool set to TRUE for processing From: headers
1186 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001188 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
1190 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1191
1192 // Line length must not exceed 76 characters, so we adjust for
1193 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1194 $limit = 75 - 7 - strlen($this->charset);
1195
1196 // these special characters must be converted too
1197 $convert = array('_', '=', '?');
1198
1199 if ($from === TRUE)
1200 {
1201 $convert[] = ',';
1202 $convert[] = ';';
1203 }
1204
1205 $output = '';
1206 $temp = '';
1207
1208 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1209 {
1210 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001211 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 $ascii = ord($char);
1213
1214 // convert ALL non-printable ASCII characters and our specials
1215 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1216 {
1217 $char = '='.dechex($ascii);
1218 }
1219
1220 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001221 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 {
1223 $char = '_';
1224 }
1225
1226 // If we're at the character limit, add the line to the output,
1227 // reset our temp variable, and keep on chuggin'
1228 if ((strlen($temp) + strlen($char)) >= $limit)
1229 {
1230 $output .= $temp.$this->crlf;
1231 $temp = '';
1232 }
1233
1234 // Add the character to our temporary line
1235 $temp .= $char;
1236 }
1237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // wrap each line with the shebang, charset, and transfer encoding
1239 // the preceding space on successive lines is required for header "folding"
Andrey Andreev56454792012-05-17 14:32:19 +03001240 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 }
1242
1243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 /**
1246 * Send Email
1247 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 * @return bool
1249 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001250 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001252 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
1254 $this->reply_to($this->_headers['From']);
1255 }
1256
Andrey Andreev76f15c92012-03-01 13:05:07 +02001257 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1258 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1259 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
patworkb0707982011-04-08 15:10:05 +02001261 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 return FALSE;
1263 }
1264
1265 $this->_build_headers();
1266
Andrey Andreev76f15c92012-03-01 13:05:07 +02001267 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001269 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001270
Alex Bilbiea87aab32012-07-30 09:50:37 +01001271 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001272 {
1273 $this->clear();
1274 }
1275
Alex Bilbieb901e732012-07-30 09:44:57 +01001276 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
1278
1279 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001280 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001281
Alex Bilbiea87aab32012-07-30 09:50:37 +01001282 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001283 {
1284 $this->clear();
1285 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001286
Alex Bilbieb901e732012-07-30 09:44:57 +01001287 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 // --------------------------------------------------------------------
1291
1292 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001293 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001295 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001297 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001299 $float = $this->bcc_batch_size - 1;
1300 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 $chunk = array();
1302
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001303 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 {
1305 if (isset($this->_bcc_array[$i]))
1306 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001307 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 }
1309
Alex Bilbied261b1e2012-06-02 11:12:16 +01001310 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
1312 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001313 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001314 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 }
1316
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001317 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
1319 $chunk[] = substr($set, 1);
1320 }
1321 }
1322
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001323 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001326
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001327 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001328
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001329 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001331 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 }
1333 else
1334 {
1335 $this->_bcc_array = $bcc;
1336 }
1337
1338 $this->_build_message();
1339 $this->_spool_email();
1340 }
1341 }
Barry Mienydd671972010-10-04 16:33:58 +02001342
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 // --------------------------------------------------------------------
1344
1345 /**
1346 * Unwrap special elements
1347 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 * @return void
1349 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001350 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 {
Andrey Andreev56454792012-05-17 14:32:19 +03001352 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 // --------------------------------------------------------------------
1356
1357 /**
1358 * Strip line-breaks via callback
1359 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 * @return string
1361 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001362 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 {
1364 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1365 {
1366 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1367 }
1368
1369 return $matches[1];
1370 }
Barry Mienydd671972010-10-04 16:33:58 +02001371
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 // --------------------------------------------------------------------
1373
1374 /**
1375 * Spool mail to the mail server
1376 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 * @return bool
1378 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001379 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 {
1381 $this->_unwrap_specials();
1382
Andrey Andreev56454792012-05-17 14:32:19 +03001383 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001384 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 {
Andrey Andreev56454792012-05-17 14:32:19 +03001386 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001387 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 }
1389
patworkb0707982011-04-08 15:10:05 +02001390 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 return TRUE;
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 // --------------------------------------------------------------------
1395
1396 /**
1397 * Send using mail()
1398 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 * @return bool
1400 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001401 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001403 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001405 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 }
1407 else
1408 {
1409 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1410 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001411 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 +00001412 }
1413 }
Barry Mienydd671972010-10-04 16:33:58 +02001414
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 // --------------------------------------------------------------------
1416
1417 /**
1418 * Send using Sendmail
1419 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 * @return bool
1421 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001422 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 {
Melounek58dfc082012-06-29 08:43:47 +02001424 $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 +00001425
Derek Jones4cefaa42009-04-29 19:13:56 +00001426 if ($fp === FALSE OR $fp === NULL)
1427 {
1428 // server probably has popen disabled, so nothing we can do to get a verbose error.
1429 return FALSE;
1430 }
Derek Jones71141ce2010-03-02 16:41:20 -06001431
Derek Jonesc630bcf2008-11-17 21:09:45 +00001432 fputs($fp, $this->_header_str);
1433 fputs($fp, $this->_finalbody);
1434
Barry Mienydd671972010-10-04 16:33:58 +02001435 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001436
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001437 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 {
patworkb0707982011-04-08 15:10:05 +02001439 $this->_set_error_message('lang:email_exit_status', $status);
1440 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001441 return FALSE;
1442 }
1443
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 return TRUE;
1445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 // --------------------------------------------------------------------
1448
1449 /**
1450 * Send using SMTP
1451 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 * @return bool
1453 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001454 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001456 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 {
patworkb0707982011-04-08 15:10:05 +02001458 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 return FALSE;
1460 }
1461
Diogo Osório593f7982012-03-02 18:04:17 +00001462 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001463 {
1464 return FALSE;
1465 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001466
1467 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1468
Pascal Kriete14287f32011-02-14 13:39:34 -05001469 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 $this->_send_command('to', $val);
1472 }
1473
1474 if (count($this->_cc_array) > 0)
1475 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001476 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001478 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 {
1480 $this->_send_command('to', $val);
1481 }
1482 }
1483 }
1484
1485 if (count($this->_bcc_array) > 0)
1486 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001487 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001489 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 {
1491 $this->_send_command('to', $val);
1492 }
1493 }
1494 }
1495
1496 $this->_send_command('data');
1497
1498 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001499 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001500
1501 $this->_send_data('.');
1502
1503 $reply = $this->_get_smtp_data();
1504
1505 $this->_set_error_message($reply);
1506
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001507 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
patworkb0707982011-04-08 15:10:05 +02001509 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 return FALSE;
1511 }
1512
1513 $this->_send_command('quit');
1514 return TRUE;
1515 }
Barry Mienydd671972010-10-04 16:33:58 +02001516
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 // --------------------------------------------------------------------
1518
1519 /**
1520 * SMTP Connect
1521 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 * @param string
1523 * @return string
1524 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001525 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001527 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001528
Radu Potopbbf04b02011-09-28 13:57:51 +03001529 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001530 $this->smtp_port,
1531 $errno,
1532 $errstr,
1533 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001534
Pascal Kriete14287f32011-02-14 13:39:34 -05001535 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 {
Andrey Andreev56454792012-05-17 14:32:19 +03001537 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 return FALSE;
1539 }
1540
1541 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001542
Alex Bilbied261b1e2012-06-02 11:12:16 +01001543 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001544 {
1545 $this->_send_command('hello');
1546 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001547
Radu Potop4c589ae2011-09-29 10:19:55 +03001548 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001549
Sean Fishere862ddd2011-10-26 22:45:00 -03001550 if ($crypto !== TRUE)
1551 {
1552 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1553 return FALSE;
1554 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001555 }
1556
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 return $this->_send_command('hello');
1558 }
Barry Mienydd671972010-10-04 16:33:58 +02001559
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 // --------------------------------------------------------------------
1561
1562 /**
1563 * Send SMTP command
1564 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 * @param string
1566 * @param string
1567 * @return string
1568 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001569 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 {
1571 switch ($cmd)
1572 {
1573 case 'hello' :
1574
Andrey Andreev56454792012-05-17 14:32:19 +03001575 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1576 {
1577 $this->_send_data('EHLO '.$this->_get_hostname());
1578 }
1579 else
1580 {
1581 $this->_send_data('HELO '.$this->_get_hostname());
1582 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001583
1584 $resp = 250;
1585 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001586 case 'starttls' :
1587
1588 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001589 $resp = 220;
1590 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 case 'from' :
1592
1593 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 $resp = 250;
1595 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001596 case 'to' :
1597
leandronf7686c122012-03-22 08:07:31 -03001598 if ($this->dsn)
1599 {
leandronfb4552942012-03-21 23:54:26 -03001600 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001601 }
leandronfb4552942012-03-21 23:54:26 -03001602 else
leandronf7686c122012-03-22 08:07:31 -03001603 {
leandronfb4552942012-03-21 23:54:26 -03001604 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001605 }
Andrey Andreev56454792012-05-17 14:32:19 +03001606
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 $resp = 250;
1608 break;
1609 case 'data' :
1610
1611 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 $resp = 354;
1613 break;
1614 case 'quit' :
1615
1616 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 $resp = 221;
1618 break;
1619 }
1620
1621 $reply = $this->_get_smtp_data();
1622
Andrey Andreev56454792012-05-17 14:32:19 +03001623 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001624
Andrey Andreevba261762012-06-11 14:11:35 +03001625 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 {
patworkb0707982011-04-08 15:10:05 +02001627 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 return FALSE;
1629 }
1630
Alex Bilbied261b1e2012-06-02 11:12:16 +01001631 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
1633 fclose($this->_smtp_connect);
1634 }
1635
1636 return TRUE;
1637 }
Barry Mienydd671972010-10-04 16:33:58 +02001638
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 // --------------------------------------------------------------------
1640
1641 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001642 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 * @return bool
1645 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001646 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 {
1648 if ( ! $this->_smtp_auth)
1649 {
1650 return TRUE;
1651 }
1652
Alex Bilbied261b1e2012-06-02 11:12:16 +01001653 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 {
patworkb0707982011-04-08 15:10:05 +02001655 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 return FALSE;
1657 }
1658
1659 $this->_send_data('AUTH LOGIN');
1660
1661 $reply = $this->_get_smtp_data();
1662
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001663 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 {
patworkb0707982011-04-08 15:10:05 +02001665 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 return FALSE;
1667 }
1668
1669 $this->_send_data(base64_encode($this->smtp_user));
1670
1671 $reply = $this->_get_smtp_data();
1672
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001673 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 {
patworkb0707982011-04-08 15:10:05 +02001675 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 return FALSE;
1677 }
1678
1679 $this->_send_data(base64_encode($this->smtp_pass));
1680
1681 $reply = $this->_get_smtp_data();
1682
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001683 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 {
patworkb0707982011-04-08 15:10:05 +02001685 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 return FALSE;
1687 }
1688
1689 return TRUE;
1690 }
Barry Mienydd671972010-10-04 16:33:58 +02001691
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 // --------------------------------------------------------------------
1693
1694 /**
1695 * Send SMTP data
1696 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 * @return bool
1698 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001699 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 {
1701 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1702 {
patworkb0707982011-04-08 15:10:05 +02001703 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 return FALSE;
1705 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001706
1707 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 }
Barry Mienydd671972010-10-04 16:33:58 +02001709
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 // --------------------------------------------------------------------
1711
1712 /**
1713 * Get SMTP data
1714 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 * @return string
1716 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001717 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
Andrey Andreev56454792012-05-17 14:32:19 +03001719 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001720
1721 while ($str = fgets($this->_smtp_connect, 512))
1722 {
1723 $data .= $str;
1724
Alex Bilbied261b1e2012-06-02 11:12:16 +01001725 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 {
1727 break;
1728 }
1729 }
1730
1731 return $data;
1732 }
Barry Mienydd671972010-10-04 16:33:58 +02001733
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 // --------------------------------------------------------------------
1735
1736 /**
1737 * Get Hostname
1738 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 * @return string
1740 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001741 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001743 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 }
Barry Mienydd671972010-10-04 16:33:58 +02001745
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 // --------------------------------------------------------------------
1747
1748 /**
1749 * Get IP
1750 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 * @return string
1752 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001753 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
1755 if ($this->_IP !== FALSE)
1756 {
1757 return $this->_IP;
1758 }
1759
Andrey Andreev76f15c92012-03-01 13:05:07 +02001760 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1761 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001762 if ($cip) $this->_IP = $cip;
1763 elseif ($rip) $this->_IP = $rip;
1764 else
1765 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001766 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001767 if ($fip)
1768 {
1769 $this->_IP = $fip;
1770 }
1771 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001772
Robin Sowell76b369e2010-03-19 11:15:28 -04001773 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 {
1775 $x = explode(',', $this->_IP);
1776 $this->_IP = end($x);
1777 }
1778
Andrey Andreev59c5b532012-03-01 14:09:51 +02001779 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 +00001780 {
1781 $this->_IP = '0.0.0.0';
1782 }
1783
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 return $this->_IP;
1785 }
Barry Mienydd671972010-10-04 16:33:58 +02001786
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 // --------------------------------------------------------------------
1788
1789 /**
1790 * Get Debug Message
1791 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 * @return string
1793 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001794 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 {
1796 $msg = '';
1797
1798 if (count($this->_debug_msg) > 0)
1799 {
1800 foreach ($this->_debug_msg as $val)
1801 {
1802 $msg .= $val;
1803 }
1804 }
1805
Andrey Andreev59c5b532012-03-01 14:09:51 +02001806 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 }
Barry Mienydd671972010-10-04 16:33:58 +02001808
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 // --------------------------------------------------------------------
1810
1811 /**
1812 * Set Message
1813 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001815 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001817 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
1819 $CI =& get_instance();
1820 $CI->lang->load('email');
1821
Andrey Andreev76f15c92012-03-01 13:05:07 +02001822 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 {
Andrey Andreev56454792012-05-17 14:32:19 +03001824 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 }
1826 else
1827 {
Andrey Andreev56454792012-05-17 14:32:19 +03001828 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 }
1830 }
Barry Mienydd671972010-10-04 16:33:58 +02001831
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 // --------------------------------------------------------------------
1833
1834 /**
1835 * Mime Types
1836 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 * @param string
1838 * @return string
1839 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001840 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001842 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001843
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001844 $ext = strtolower($ext);
1845
1846 if ( ! is_array($mimes))
1847 {
1848 $mimes =& get_mimes();
1849 }
1850
1851 if (isset($mimes[$ext]))
1852 {
1853 return is_array($mimes[$ext])
1854 ? current($mimes[$ext])
1855 : $mimes[$ext];
1856 }
1857
1858 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 }
1860
1861}
Derek Allard2067d1a2008-11-13 22:59:24 +00001862
1863/* End of file Email.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001864/* Location: ./system/libraries/Email.php */