blob: 698cb76798161b3289f971fbc23ea7efaba95fc5 [file] [log] [blame]
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Email Class
30 *
31 * Permits email to be sent using Mail, Sendmail, or SMTP.
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/email.html
38 */
trita15dd4f2011-11-23 07:40:05 -050039class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev59c5b532012-03-01 14:09:51 +020041 public $useragent = 'CodeIgniter';
42 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
43 public $protocol = 'mail'; // mail/sendmail/smtp
44 public $smtp_host = ''; // SMTP Server. Example: mail.earthlink.net
45 public $smtp_user = ''; // SMTP Username
46 public $smtp_pass = ''; // SMTP Password
47 public $smtp_port = 25; // SMTP Port
48 public $smtp_timeout = 5; // SMTP Timeout in seconds
49 public $smtp_crypto = ''; // SMTP Encryption. Can be null, tls or ssl.
50 public $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
51 public $wrapchars = 76; // Number of characters to wrap at.
52 public $mailtype = 'text'; // text/html Defines email formatting
53 public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
54 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
55 public $alt_message = ''; // Alternative message for HTML emails
56 public $validate = FALSE; // TRUE/FALSE. Enables email validation
57 public $priority = 3; // Default priority (1 - 5)
58 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
59 public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
61 // distasteful, is the only thing that seems to work for all environments.
leandronf7686c122012-03-22 08:07:31 -030062 public $dsn = FALSE; // Delivery Status Notification
Andrey Andreev1bd3d882011-12-22 15:38:20 +020063 public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Andrey Andreev59c5b532012-03-01 14:09:51 +020064 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020065 public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Andrey Andreev50814092012-03-01 12:36:12 +020066
67 protected $_safe_mode = FALSE;
68 protected $_subject = '';
69 protected $_body = '';
70 protected $_finalbody = '';
71 protected $_alt_boundary = '';
72 protected $_atc_boundary = '';
73 protected $_header_str = '';
74 protected $_smtp_connect = '';
75 protected $_encoding = '8bit';
76 protected $_IP = FALSE;
77 protected $_smtp_auth = FALSE;
78 protected $_replyto_flag = FALSE;
79 protected $_debug_msg = array();
80 protected $_recipients = array();
81 protected $_cc_array = array();
82 protected $_bcc_array = array();
83 protected $_headers = array();
84 protected $_attach_name = array();
85 protected $_attach_type = array();
86 protected $_attach_disp = array();
87 protected $_protocols = array('mail', 'sendmail', 'smtp');
88 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
89 protected $_bit_depths = array('7bit', '8bit');
90 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Constructor - Sets Email Preferences
94 *
95 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +030096 *
97 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000099 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreev9f44c212012-10-10 16:07:17 +0300101 $this->charset = strtoupper(config_item('charset'));
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 if (count($config) > 0)
104 {
105 $this->initialize($config);
106 }
107 else
108 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100109 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200110 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
Andrey Andreev59c5b532012-03-01 14:09:51 +0200113 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Initialize preferences
120 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * @param array
122 * @return void
123 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000124 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 foreach ($config as $key => $val)
127 {
128 if (isset($this->$key))
129 {
130 $method = 'set_'.$key;
131
132 if (method_exists($this, $method))
133 {
134 $this->$method($val);
135 }
136 else
137 {
138 $this->$key = $val;
139 }
140 }
141 }
Eric Barnes6113f542010-12-29 13:36:12 -0500142 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
Alex Bilbied261b1e2012-06-02 11:12:16 +0100144 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200145 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000146
147 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // --------------------------------------------------------------------
151
152 /**
153 * Initialize the Email Data
154 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800155 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200156 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000158 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200160 $this->_subject = '';
161 $this->_body = '';
162 $this->_finalbody = '';
163 $this->_header_str = '';
164 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500166 $this->_cc_array = array();
167 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $this->_headers = array();
169 $this->_debug_msg = array();
170
Mickey Wubfc1cad2012-05-31 22:28:40 -0700171 $this->set_header('User-Agent', $this->useragent);
172 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
174 if ($clear_attachments !== FALSE)
175 {
176 $this->_attach_name = array();
177 $this->_attach_type = array();
178 $this->_attach_disp = array();
179 }
Eric Barnes6113f542010-12-29 13:36:12 -0500180
Greg Akera769deb2010-11-10 15:47:29 -0600181 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Set FROM
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string
190 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200191 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Melounek58dfc082012-06-29 08:43:47 +0200193 public function from($from, $name = '', $return_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200195 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200197 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
199
200 if ($this->validate)
201 {
202 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300203 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200204 {
205 $this->validate_email($this->_str_to_array($return_path));
206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100210 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 // only use Q encoding if there are characters that would require it
213 if ( ! preg_match('/[\200-\377]/', $name))
214 {
215 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000216 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218 else
219 {
220 $name = $this->_prep_q_encoding($name, TRUE);
221 }
222 }
223
Mickey Wubfc1cad2012-05-31 22:28:40 -0700224 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200225
Andrey Andreevccd01c72012-10-05 17:12:55 +0300226 if( ! $return_path)
Melounek58dfc082012-06-29 08:43:47 +0200227 {
228 $return_path = $from;
229 }
230 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500231
Greg Akera769deb2010-11-10 15:47:29 -0600232 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Set Reply-to
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string
241 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200242 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000244 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200246 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200248 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250
251 if ($this->validate)
252 {
253 $this->validate_email($this->_str_to_array($replyto));
254 }
255
Alex Bilbied261b1e2012-06-02 11:12:16 +0100256 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $name = $replyto;
259 }
260
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300261 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $name = '"'.$name.'"';
264 }
265
Mickey Wubfc1cad2012-05-31 22:28:40 -0700266 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600268
269 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Set Recipients
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200278 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000280 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $to = $this->_str_to_array($to);
283 $to = $this->clean_email($to);
284
285 if ($this->validate)
286 {
287 $this->validate_email($to);
288 }
289
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200290 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700292 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 switch ($this->_get_protocol())
296 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200297 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000298 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200300 case 'sendmail':
301 case 'mail':
302 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 break;
304 }
Eric Barnes6113f542010-12-29 13:36:12 -0500305
Greg Akera769deb2010-11-10 15:47:29 -0600306 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // --------------------------------------------------------------------
310
311 /**
312 * Set CC
313 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200315 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000317 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Andrey Andreev56454792012-05-17 14:32:19 +0300319 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321 if ($this->validate)
322 {
323 $this->validate_email($cc);
324 }
325
Mickey Wubfc1cad2012-05-31 22:28:40 -0700326 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200328 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $this->_cc_array = $cc;
331 }
Eric Barnes6113f542010-12-29 13:36:12 -0500332
Greg Akera769deb2010-11-10 15:47:29 -0600333 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Set BCC
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param string
342 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200343 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000345 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100347 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $this->bcc_batch_mode = TRUE;
350 $this->bcc_batch_size = $limit;
351 }
352
Andrey Andreev56454792012-05-17 14:32:19 +0300353 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
355 if ($this->validate)
356 {
357 $this->validate_email($bcc);
358 }
359
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200360 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $this->_bcc_array = $bcc;
363 }
364 else
365 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700366 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Eric Barnes6113f542010-12-29 13:36:12 -0500368
Greg Akera769deb2010-11-10 15:47:29 -0600369 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // --------------------------------------------------------------------
373
374 /**
375 * Set Email Subject
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200378 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000380 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700383 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600384 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // --------------------------------------------------------------------
388
389 /**
390 * Set Body
391 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200393 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000395 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200397 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200398
Andrey Andreevaf728622011-10-20 10:11:59 +0300399 /* strip slashes only if magic quotes is ON
400 if we do it with magic quotes OFF, it strips real, user-inputted chars.
401
402 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
403 it will probably not exist in future versions at all.
404 */
405 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200406 {
diegorivera33c32802011-10-19 02:56:15 -0200407 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200408 }
diegorivera33c32802011-10-19 02:56:15 -0200409
Greg Akera769deb2010-11-10 15:47:29 -0600410 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * Assign file attachments
417 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200419 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200421 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
trit151fc682011-11-23 07:30:06 -0500423 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500424 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200425 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600426 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Add a Header Item
433 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param string
435 * @param string
436 * @return void
437 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700438 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
440 $this->_headers[$header] = $value;
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // --------------------------------------------------------------------
444
445 /**
446 * Convert a String to an Array
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string
449 * @return array
450 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600451 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 if ( ! is_array($email))
454 {
Andrey Andreev56454792012-05-17 14:32:19 +0300455 return (strpos($email, ',') !== FALSE)
456 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
457 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Andrey Andreev56454792012-05-17 14:32:19 +0300459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 return $email;
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
464
465 /**
466 * Set Multipart Value
467 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200469 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000471 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400473 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600474 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
478
479 /**
480 * Set Mailtype
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200483 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000485 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev56454792012-05-17 14:32:19 +0300487 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600488 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Set Wordwrap
495 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400496 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200497 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000499 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400501 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600502 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * Set Protocol
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200511 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000513 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200515 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600516 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Set Priority
523 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200524 * @param int
525 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000527 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200529 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600530 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // --------------------------------------------------------------------
534
535 /**
536 * Set Newline Character
537 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200539 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000541 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200543 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600544 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 // --------------------------------------------------------------------
548
549 /**
550 * Set CRLF
551 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200553 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000555 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200557 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600558 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // --------------------------------------------------------------------
562
563 /**
564 * Set Message Boundary
565 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @return void
567 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600568 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200570 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
571 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // --------------------------------------------------------------------
575
576 /**
577 * Get the Message ID
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @return string
580 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600581 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200583 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300584 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // --------------------------------------------------------------------
588
589 /**
590 * Get Mail Protocol
591 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200593 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600595 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200598 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Alex Bilbied261b1e2012-06-02 11:12:16 +0100600 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 return $this->protocol;
603 }
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // --------------------------------------------------------------------
607
608 /**
609 * Get Mail Encoding
610 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 * @param bool
612 * @return string
613 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600614 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200616 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000617
618 foreach ($this->_base_charsets as $charset)
619 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300620 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 $this->_encoding = '7bit';
623 }
624 }
625
Alex Bilbied261b1e2012-06-02 11:12:16 +0100626 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 return $this->_encoding;
629 }
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * Get content type (text/html/attachment)
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @return string
638 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600639 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Andrey Andreev56454792012-05-17 14:32:19 +0300641 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200645 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 return 'plain-attach';
648 }
649 else
650 {
651 return 'plain';
652 }
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // --------------------------------------------------------------------
656
657 /**
658 * Set RFC 822 Date
659 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @return string
661 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600662 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200664 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300665 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200667 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000668
Andrey Andreev59c5b532012-03-01 14:09:51 +0200669 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // --------------------------------------------------------------------
673
674 /**
675 * Mime message
676 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 * @return string
678 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600679 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200681 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 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 // --------------------------------------------------------------------
685
686 /**
687 * Validate Email Address
688 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 * @param string
690 * @return bool
691 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000692 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 if ( ! is_array($email))
695 {
patworkb0707982011-04-08 15:10:05 +0200696 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 return FALSE;
698 }
699
700 foreach ($email as $val)
701 {
702 if ( ! $this->valid_email($val))
703 {
patworkb0707982011-04-08 15:10:05 +0200704 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 return FALSE;
706 }
707 }
708
709 return TRUE;
710 }
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 // --------------------------------------------------------------------
713
714 /**
715 * Email Validation
716 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 * @param string
718 * @return bool
719 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400720 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300722 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 // --------------------------------------------------------------------
726
727 /**
728 * Clean Extended Email Address: Joe Smith <joe@smith.com>
729 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * @param string
731 * @return string
732 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000733 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
735 if ( ! is_array($email))
736 {
Andrey Andreev56454792012-05-17 14:32:19 +0300737 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
739
740 $clean_email = array();
741
742 foreach ($email as $addy)
743 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200744 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 }
746
747 return $clean_email;
748 }
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 // --------------------------------------------------------------------
751
752 /**
753 * Build alternative plain text message
754 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000755 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 * in plain-text headers of HTML-formatted emails.
757 * If the user hasn't specified his own alternative message
758 * it creates one by stripping the HTML
759 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 * @return string
761 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600762 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100764 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 {
766 return $this->word_wrap($this->alt_message, '76');
767 }
768
Andrey Andreev56454792012-05-17 14:32:19 +0300769 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200770 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000771
772 for ($i = 20; $i >= 3; $i--)
773 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200774 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
776
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200777 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 // --------------------------------------------------------------------
781
782 /**
783 * Word Wrap
784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200786 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 * @return string
788 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000789 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100792 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100794 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
796
797 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300798 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000799
800 // Standardize newlines
801 if (strpos($str, "\r") !== FALSE)
802 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200803 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805
806 // If the current word is surrounded by {unwrap} tags we'll
807 // strip the entire chunk and replace it with a marker.
808 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300809 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200811 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200813 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300814 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
816 }
817
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000818 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300820 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 $str = wordwrap($str, $charlim, "\n", FALSE);
822
823 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300824 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 foreach (explode("\n", $str) as $line)
826 {
827 // Is the line within the allowed character count?
828 // If so we'll join it to the output and continue
829 if (strlen($line) <= $charlim)
830 {
831 $output .= $line.$this->newline;
832 continue;
833 }
834
835 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200836 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
838 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300839 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
841 break;
842 }
843
844 // Trim the word down
845 $temp .= substr($line, 0, $charlim-1);
846 $line = substr($line, $charlim-1);
847 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200848 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000849
850 // If $temp contains data it means we had to split up an over-length
851 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100852 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200854 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200857 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 }
859
860 // Put our markers back
861 if (count($unwrap) > 0)
862 {
863 foreach ($unwrap as $key => $val)
864 {
Andrey Andreev56454792012-05-17 14:32:19 +0300865 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 }
867 }
868
869 return $output;
870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 // --------------------------------------------------------------------
873
874 /**
875 * Build final headers
876 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 * @return string
878 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600879 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700881 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
882 $this->set_header('X-Mailer', $this->useragent);
883 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
884 $this->set_header('Message-ID', $this->_get_message_id());
885 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 }
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 // --------------------------------------------------------------------
889
890 /**
891 * Write Headers as a string
892 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * @return void
894 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600895 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200897 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $this->_subject = $this->_headers['Subject'];
900 unset($this->_headers['Subject']);
901 }
902
903 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300904 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000905
Pascal Kriete14287f32011-02-14 13:39:34 -0500906 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
908 $val = trim($val);
909
Alex Bilbied261b1e2012-06-02 11:12:16 +0100910 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Andrey Andreev56454792012-05-17 14:32:19 +0300912 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914 }
915
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200916 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
Derek Jones1d890882009-02-10 20:32:14 +0000918 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 // --------------------------------------------------------------------
923
924 /**
925 * Build Final Body and attachments
926 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 * @return void
928 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600929 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200931 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
933 $this->_body = $this->word_wrap($this->_body);
934 }
935
936 $this->_set_boundaries();
937 $this->_write_headers();
938
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200939 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500940 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000941
942 switch ($this->_get_content_type())
943 {
944 case 'plain' :
945
Andrey Andreev56454792012-05-17 14:32:19 +0300946 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
947 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200949 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
951 $this->_header_str .= $hdr;
952 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 }
Brandon Jones485d7412010-11-09 16:38:17 -0500954 else
955 {
956 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
957 }
Eric Barnes6113f542010-12-29 13:36:12 -0500958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return;
960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 case 'html' :
962
963 if ($this->send_multipart === FALSE)
964 {
Andrey Andreev56454792012-05-17 14:32:19 +0300965 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
966 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
968 else
969 {
Andrey Andreev56454792012-05-17 14:32:19 +0300970 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
Andrey Andreev56454792012-05-17 14:32:19 +0300972 $body .= $this->_get_mime_message().$this->newline.$this->newline
973 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000974
Andrey Andreev56454792012-05-17 14:32:19 +0300975 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
976 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
977 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500978
Andrey Andreev56454792012-05-17 14:32:19 +0300979 .'Content-Type: text/html; charset='.$this->charset.$this->newline
980 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 }
Eric Barnes6113f542010-12-29 13:36:12 -0500982
Andrey Andreev56454792012-05-17 14:32:19 +0300983 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500984
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200985 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
987 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500988 }
989 else
990 {
Andrey Andreev56454792012-05-17 14:32:19 +0300991 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994
995 if ($this->send_multipart !== FALSE)
996 {
Andrey Andreev56454792012-05-17 14:32:19 +0300997 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 return;
1001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 case 'plain-attach' :
1003
Andrey Andreev56454792012-05-17 14:32:19 +03001004 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001005
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001006 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
1008 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001009 }
1010
Andrey Andreev56454792012-05-17 14:32:19 +03001011 $body .= $this->_get_mime_message().$this->newline.$this->newline
1012 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001013
Andrey Andreev56454792012-05-17 14:32:19 +03001014 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1015 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
Andrey Andreev56454792012-05-17 14:32:19 +03001017 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001018
1019 break;
1020 case 'html-attach' :
1021
Andrey Andreev56454792012-05-17 14:32:19 +03001022 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001023
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001024 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
1026 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
1028
Andrey Andreev56454792012-05-17 14:32:19 +03001029 $body .= $this->_get_mime_message().$this->newline.$this->newline
1030 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001031
Andrey Andreev56454792012-05-17 14:32:19 +03001032 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1033 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001034
Andrey Andreev56454792012-05-17 14:32:19 +03001035 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1036 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1037 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001038
Andrey Andreev56454792012-05-17 14:32:19 +03001039 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1040 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001041
Andrey Andreev56454792012-05-17 14:32:19 +03001042 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1043 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001044
1045 break;
1046 }
1047
1048 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001049 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
tritbc4ac992011-11-23 07:19:41 -05001051 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001052 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001054 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001055
Alex Bilbied261b1e2012-06-02 11:12:16 +01001056 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001058 if ( ! file_exists($filename))
1059 {
1060 $this->_set_error_message('lang:email_attachment_missing', $filename);
1061 return FALSE;
1062 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001063
Matteo Mattei5688d582012-03-13 19:29:29 +01001064 $file = filesize($filename) +1;
1065
1066 if ( ! $fp = fopen($filename, FOPEN_READ))
1067 {
1068 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1069 return FALSE;
1070 }
1071
Matteo Matteic3b36f42012-03-26 10:27:17 +02001072 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001073 $file_content = fread($fp, $file);
1074 fclose($fp);
1075 }
1076 else
1077 {
1078 $file_content =& $this->_attach_content[$i];
1079 }
Andrey Andreev56454792012-05-17 14:32:19 +03001080
1081 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1082 .'Content-type: '.$ctype.'; '
1083 .'name="'.$basename.'"'.$this->newline
1084 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1085 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001086
Matteo Mattei5688d582012-03-13 19:29:29 +01001087 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089
Andrey Andreev56454792012-05-17 14:32:19 +03001090 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1091 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 return;
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 // --------------------------------------------------------------------
1096
1097 /**
1098 * Prep Quoted Printable
1099 *
1100 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1101 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1102 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @return string
1105 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001106 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 {
Andrey Andreev683b34d2012-10-09 15:00:00 +03001108 // RFC 2045 specifies CRLF as "\r\n".
1109 // However, many developers choose to override that and violate
1110 // the RFC rules due to (apparently) a bug in MS Exchange,
1111 // which only works with "\n".
1112 if ($this->crlf === "\r\n" && is_php('5.3'))
1113 {
1114 return quoted_printable_encode($str);
1115 }
1116
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001117 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001118 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001119
1120 // Standardize newlines
1121 if (strpos($str, "\r") !== FALSE)
1122 {
1123 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1124 }
1125
1126 // We are intentionally wrapping so mail servers will encode characters
1127 // properly and MUAs will behave, so {unwrap} must go!
1128 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 $escape = '=';
1131 $output = '';
1132
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001133 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
1135 $length = strlen($line);
1136 $temp = '';
1137
1138 // Loop through each character in the line to add soft-wrap
1139 // characters at the end of a line " =\r\n" and add the newly
1140 // processed line(s) to the output (see comment on $crlf class property)
1141 for ($i = 0; $i < $length; $i++)
1142 {
1143 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001144 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 $ascii = ord($char);
1146
1147 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001148 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001150 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001152 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001154 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 }
1156
1157 // If we're at the character limit, add the line to the output,
1158 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001159 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
1161 $output .= $temp.$escape.$this->crlf;
1162 $temp = '';
1163 }
1164
1165 // Add the character to our temporary line
1166 $temp .= $char;
1167 }
1168
1169 // Add our completed line to the output
1170 $output .= $temp.$this->crlf;
1171 }
1172
1173 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001174 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
1176
1177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 /**
1180 * Prep Q Encoding
1181 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001182 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 * but not identical to quoted-printable, so it has its own method
1184 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001185 * @param string
1186 * @param bool set to TRUE for processing From: headers
1187 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001189 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
1191 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1192
1193 // Line length must not exceed 76 characters, so we adjust for
1194 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1195 $limit = 75 - 7 - strlen($this->charset);
1196
1197 // these special characters must be converted too
1198 $convert = array('_', '=', '?');
1199
1200 if ($from === TRUE)
1201 {
1202 $convert[] = ',';
1203 $convert[] = ';';
1204 }
1205
1206 $output = '';
1207 $temp = '';
1208
1209 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1210 {
1211 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001212 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 $ascii = ord($char);
1214
1215 // convert ALL non-printable ASCII characters and our specials
1216 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1217 {
1218 $char = '='.dechex($ascii);
1219 }
1220
1221 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001222 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
1224 $char = '_';
1225 }
1226
1227 // If we're at the character limit, add the line to the output,
1228 // reset our temp variable, and keep on chuggin'
1229 if ((strlen($temp) + strlen($char)) >= $limit)
1230 {
1231 $output .= $temp.$this->crlf;
1232 $temp = '';
1233 }
1234
1235 // Add the character to our temporary line
1236 $temp .= $char;
1237 }
1238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // wrap each line with the shebang, charset, and transfer encoding
1240 // the preceding space on successive lines is required for header "folding"
Bruno Barão51f72cd2012-10-08 16:31:46 +01001241 return trim(preg_replace('/^(.*?)(\r*)$/m', ' =?'.$this->charset.'?Q?$1?=$2', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
1243
1244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 /**
1247 * Send Email
1248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @return bool
1250 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001251 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001253 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
1255 $this->reply_to($this->_headers['From']);
1256 }
1257
Andrey Andreev76f15c92012-03-01 13:05:07 +02001258 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1259 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1260 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
patworkb0707982011-04-08 15:10:05 +02001262 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 return FALSE;
1264 }
1265
1266 $this->_build_headers();
1267
Andrey Andreev76f15c92012-03-01 13:05:07 +02001268 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001270 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001271
Alex Bilbiea87aab32012-07-30 09:50:37 +01001272 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001273 {
1274 $this->clear();
1275 }
1276
Alex Bilbieb901e732012-07-30 09:44:57 +01001277 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
1279
1280 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001281 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001282
Alex Bilbiea87aab32012-07-30 09:50:37 +01001283 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001284 {
1285 $this->clear();
1286 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001287
Alex Bilbieb901e732012-07-30 09:44:57 +01001288 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 // --------------------------------------------------------------------
1292
1293 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001294 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001296 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001298 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001300 $float = $this->bcc_batch_size - 1;
1301 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 $chunk = array();
1303
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001304 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
1306 if (isset($this->_bcc_array[$i]))
1307 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001308 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 }
1310
Alex Bilbied261b1e2012-06-02 11:12:16 +01001311 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 {
1313 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001314 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001315 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 }
1317
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001318 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 {
1320 $chunk[] = substr($set, 1);
1321 }
1322 }
1323
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001324 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
1326 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001327
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001328 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001329
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001330 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001332 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 }
1334 else
1335 {
1336 $this->_bcc_array = $bcc;
1337 }
1338
1339 $this->_build_message();
1340 $this->_spool_email();
1341 }
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 // --------------------------------------------------------------------
1345
1346 /**
1347 * Unwrap special elements
1348 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 * @return void
1350 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001351 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 {
Andrey Andreev56454792012-05-17 14:32:19 +03001353 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 // --------------------------------------------------------------------
1357
1358 /**
1359 * Strip line-breaks via callback
1360 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 * @return string
1362 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001363 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 {
1365 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1366 {
1367 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1368 }
1369
1370 return $matches[1];
1371 }
Barry Mienydd671972010-10-04 16:33:58 +02001372
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 // --------------------------------------------------------------------
1374
1375 /**
1376 * Spool mail to the mail server
1377 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @return bool
1379 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001380 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
1382 $this->_unwrap_specials();
1383
Andrey Andreev56454792012-05-17 14:32:19 +03001384 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001385 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 {
Andrey Andreev56454792012-05-17 14:32:19 +03001387 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001388 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 }
1390
patworkb0707982011-04-08 15:10:05 +02001391 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 return TRUE;
1393 }
Barry Mienydd671972010-10-04 16:33:58 +02001394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 // --------------------------------------------------------------------
1396
1397 /**
1398 * Send using mail()
1399 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 * @return bool
1401 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001402 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001404 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001406 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 }
1408 else
1409 {
1410 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1411 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001412 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 +00001413 }
1414 }
Barry Mienydd671972010-10-04 16:33:58 +02001415
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 // --------------------------------------------------------------------
1417
1418 /**
1419 * Send using Sendmail
1420 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 * @return bool
1422 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001423 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 {
Melounek58dfc082012-06-29 08:43:47 +02001425 $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 +00001426
Derek Jones4cefaa42009-04-29 19:13:56 +00001427 if ($fp === FALSE OR $fp === NULL)
1428 {
1429 // server probably has popen disabled, so nothing we can do to get a verbose error.
1430 return FALSE;
1431 }
Derek Jones71141ce2010-03-02 16:41:20 -06001432
Derek Jonesc630bcf2008-11-17 21:09:45 +00001433 fputs($fp, $this->_header_str);
1434 fputs($fp, $this->_finalbody);
1435
Barry Mienydd671972010-10-04 16:33:58 +02001436 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001437
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001438 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 {
patworkb0707982011-04-08 15:10:05 +02001440 $this->_set_error_message('lang:email_exit_status', $status);
1441 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 return FALSE;
1443 }
1444
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 return TRUE;
1446 }
Barry Mienydd671972010-10-04 16:33:58 +02001447
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 // --------------------------------------------------------------------
1449
1450 /**
1451 * Send using SMTP
1452 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 * @return bool
1454 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001455 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001457 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 {
patworkb0707982011-04-08 15:10:05 +02001459 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 return FALSE;
1461 }
1462
Diogo Osório593f7982012-03-02 18:04:17 +00001463 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001464 {
1465 return FALSE;
1466 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001467
1468 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1469
Pascal Kriete14287f32011-02-14 13:39:34 -05001470 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 {
1472 $this->_send_command('to', $val);
1473 }
1474
1475 if (count($this->_cc_array) > 0)
1476 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001477 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001479 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
1481 $this->_send_command('to', $val);
1482 }
1483 }
1484 }
1485
1486 if (count($this->_bcc_array) > 0)
1487 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001488 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001490 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 {
1492 $this->_send_command('to', $val);
1493 }
1494 }
1495 }
1496
1497 $this->_send_command('data');
1498
1499 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001500 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001501
1502 $this->_send_data('.');
1503
1504 $reply = $this->_get_smtp_data();
1505
1506 $this->_set_error_message($reply);
1507
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001508 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 {
patworkb0707982011-04-08 15:10:05 +02001510 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 return FALSE;
1512 }
1513
1514 $this->_send_command('quit');
1515 return TRUE;
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 // --------------------------------------------------------------------
1519
1520 /**
1521 * SMTP Connect
1522 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 * @param string
1524 * @return string
1525 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001526 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001528 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001529
Radu Potopbbf04b02011-09-28 13:57:51 +03001530 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001531 $this->smtp_port,
1532 $errno,
1533 $errstr,
1534 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001535
Pascal Kriete14287f32011-02-14 13:39:34 -05001536 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Andrey Andreev56454792012-05-17 14:32:19 +03001538 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 return FALSE;
1540 }
1541
1542 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001543
Alex Bilbied261b1e2012-06-02 11:12:16 +01001544 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001545 {
1546 $this->_send_command('hello');
1547 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001548
Radu Potop4c589ae2011-09-29 10:19:55 +03001549 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001550
Sean Fishere862ddd2011-10-26 22:45:00 -03001551 if ($crypto !== TRUE)
1552 {
1553 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1554 return FALSE;
1555 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001556 }
1557
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 return $this->_send_command('hello');
1559 }
Barry Mienydd671972010-10-04 16:33:58 +02001560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 // --------------------------------------------------------------------
1562
1563 /**
1564 * Send SMTP command
1565 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 * @param string
1567 * @param string
1568 * @return string
1569 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001570 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
1572 switch ($cmd)
1573 {
1574 case 'hello' :
1575
Andrey Andreev56454792012-05-17 14:32:19 +03001576 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1577 {
1578 $this->_send_data('EHLO '.$this->_get_hostname());
1579 }
1580 else
1581 {
1582 $this->_send_data('HELO '.$this->_get_hostname());
1583 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001584
1585 $resp = 250;
1586 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001587 case 'starttls' :
1588
1589 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001590 $resp = 220;
1591 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 case 'from' :
1593
1594 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 $resp = 250;
1596 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001597 case 'to' :
1598
leandronf7686c122012-03-22 08:07:31 -03001599 if ($this->dsn)
1600 {
leandronfb4552942012-03-21 23:54:26 -03001601 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001602 }
leandronfb4552942012-03-21 23:54:26 -03001603 else
leandronf7686c122012-03-22 08:07:31 -03001604 {
leandronfb4552942012-03-21 23:54:26 -03001605 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001606 }
Andrey Andreev56454792012-05-17 14:32:19 +03001607
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 $resp = 250;
1609 break;
1610 case 'data' :
1611
1612 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 $resp = 354;
1614 break;
1615 case 'quit' :
1616
1617 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 $resp = 221;
1619 break;
1620 }
1621
1622 $reply = $this->_get_smtp_data();
1623
Andrey Andreev56454792012-05-17 14:32:19 +03001624 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001625
Andrey Andreevba261762012-06-11 14:11:35 +03001626 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 {
patworkb0707982011-04-08 15:10:05 +02001628 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 return FALSE;
1630 }
1631
Alex Bilbied261b1e2012-06-02 11:12:16 +01001632 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 {
1634 fclose($this->_smtp_connect);
1635 }
1636
1637 return TRUE;
1638 }
Barry Mienydd671972010-10-04 16:33:58 +02001639
Derek Allard2067d1a2008-11-13 22:59:24 +00001640 // --------------------------------------------------------------------
1641
1642 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001643 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 * @return bool
1646 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001647 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
1649 if ( ! $this->_smtp_auth)
1650 {
1651 return TRUE;
1652 }
1653
Alex Bilbied261b1e2012-06-02 11:12:16 +01001654 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 {
patworkb0707982011-04-08 15:10:05 +02001656 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 return FALSE;
1658 }
1659
1660 $this->_send_data('AUTH LOGIN');
1661
1662 $reply = $this->_get_smtp_data();
1663
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001664 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
patworkb0707982011-04-08 15:10:05 +02001666 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 return FALSE;
1668 }
1669
1670 $this->_send_data(base64_encode($this->smtp_user));
1671
1672 $reply = $this->_get_smtp_data();
1673
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001674 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 {
patworkb0707982011-04-08 15:10:05 +02001676 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 return FALSE;
1678 }
1679
1680 $this->_send_data(base64_encode($this->smtp_pass));
1681
1682 $reply = $this->_get_smtp_data();
1683
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001684 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 {
patworkb0707982011-04-08 15:10:05 +02001686 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 return FALSE;
1688 }
1689
1690 return TRUE;
1691 }
Barry Mienydd671972010-10-04 16:33:58 +02001692
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 // --------------------------------------------------------------------
1694
1695 /**
1696 * Send SMTP data
1697 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 * @return bool
1699 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001700 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 {
1702 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1703 {
patworkb0707982011-04-08 15:10:05 +02001704 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 return FALSE;
1706 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001707
1708 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 // --------------------------------------------------------------------
1712
1713 /**
1714 * Get SMTP data
1715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 * @return string
1717 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001718 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
Andrey Andreev56454792012-05-17 14:32:19 +03001720 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001721
1722 while ($str = fgets($this->_smtp_connect, 512))
1723 {
1724 $data .= $str;
1725
Alex Bilbied261b1e2012-06-02 11:12:16 +01001726 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
1728 break;
1729 }
1730 }
1731
1732 return $data;
1733 }
Barry Mienydd671972010-10-04 16:33:58 +02001734
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 // --------------------------------------------------------------------
1736
1737 /**
1738 * Get Hostname
1739 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 * @return string
1741 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001742 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001744 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 }
Barry Mienydd671972010-10-04 16:33:58 +02001746
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 // --------------------------------------------------------------------
1748
1749 /**
1750 * Get IP
1751 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 * @return string
1753 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001754 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 {
1756 if ($this->_IP !== FALSE)
1757 {
1758 return $this->_IP;
1759 }
1760
Andrey Andreev76f15c92012-03-01 13:05:07 +02001761 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1762 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001763 if ($cip) $this->_IP = $cip;
1764 elseif ($rip) $this->_IP = $rip;
1765 else
1766 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001767 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001768 if ($fip)
1769 {
1770 $this->_IP = $fip;
1771 }
1772 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001773
Robin Sowell76b369e2010-03-19 11:15:28 -04001774 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 {
1776 $x = explode(',', $this->_IP);
1777 $this->_IP = end($x);
1778 }
1779
Andrey Andreev59c5b532012-03-01 14:09:51 +02001780 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 +00001781 {
1782 $this->_IP = '0.0.0.0';
1783 }
1784
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 return $this->_IP;
1786 }
Barry Mienydd671972010-10-04 16:33:58 +02001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 // --------------------------------------------------------------------
1789
1790 /**
1791 * Get Debug Message
1792 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 * @return string
1794 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001795 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 {
1797 $msg = '';
1798
1799 if (count($this->_debug_msg) > 0)
1800 {
1801 foreach ($this->_debug_msg as $val)
1802 {
1803 $msg .= $val;
1804 }
1805 }
1806
Andrey Andreev59c5b532012-03-01 14:09:51 +02001807 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 }
Barry Mienydd671972010-10-04 16:33:58 +02001809
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 // --------------------------------------------------------------------
1811
1812 /**
1813 * Set Message
1814 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001816 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001818 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 {
1820 $CI =& get_instance();
1821 $CI->lang->load('email');
1822
Andrey Andreev76f15c92012-03-01 13:05:07 +02001823 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
Andrey Andreev56454792012-05-17 14:32:19 +03001825 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 }
1827 else
1828 {
Andrey Andreev56454792012-05-17 14:32:19 +03001829 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 }
1831 }
Barry Mienydd671972010-10-04 16:33:58 +02001832
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 // --------------------------------------------------------------------
1834
1835 /**
1836 * Mime Types
1837 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 * @param string
1839 * @return string
1840 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001841 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001843 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001844
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001845 $ext = strtolower($ext);
1846
1847 if ( ! is_array($mimes))
1848 {
1849 $mimes =& get_mimes();
1850 }
1851
1852 if (isset($mimes[$ext]))
1853 {
1854 return is_array($mimes[$ext])
1855 ? current($mimes[$ext])
1856 : $mimes[$ext];
1857 }
1858
1859 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 }
1861
1862}
Derek Allard2067d1a2008-11-13 22:59:24 +00001863
1864/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001865/* Location: ./system/libraries/Email.php */