blob: 36ecc347de910ab6f6784b6c219ce829de9252e7 [file] [log] [blame]
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1bd3d882011-12-22 15:38:20 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1bd3d882011-12-22 15:38:20 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Email Class
30 *
31 * Permits email to be sent using Mail, Sendmail, or SMTP.
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/email.html
38 */
trita15dd4f2011-11-23 07:40:05 -050039class CI_Email {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev59c5b532012-03-01 14:09:51 +020041 public $useragent = 'CodeIgniter';
42 public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
43 public $protocol = 'mail'; // mail/sendmail/smtp
44 public $smtp_host = ''; // SMTP Server. Example: mail.earthlink.net
45 public $smtp_user = ''; // SMTP Username
46 public $smtp_pass = ''; // SMTP Password
47 public $smtp_port = 25; // SMTP Port
48 public $smtp_timeout = 5; // SMTP Timeout in seconds
49 public $smtp_crypto = ''; // SMTP Encryption. Can be null, tls or ssl.
50 public $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
51 public $wrapchars = 76; // Number of characters to wrap at.
52 public $mailtype = 'text'; // text/html Defines email formatting
53 public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
54 public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
55 public $alt_message = ''; // Alternative message for HTML emails
56 public $validate = FALSE; // TRUE/FALSE. Enables email validation
57 public $priority = 3; // Default priority (1 - 5)
58 public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
59 public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
61 // distasteful, is the only thing that seems to work for all environments.
leandronf7686c122012-03-22 08:07:31 -030062 public $dsn = FALSE; // Delivery Status Notification
Andrey Andreev1bd3d882011-12-22 15:38:20 +020063 public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Andrey Andreev59c5b532012-03-01 14:09:51 +020064 public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
Andrey Andreev1bd3d882011-12-22 15:38:20 +020065 public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Andrey Andreev50814092012-03-01 12:36:12 +020066
67 protected $_safe_mode = FALSE;
68 protected $_subject = '';
69 protected $_body = '';
70 protected $_finalbody = '';
71 protected $_alt_boundary = '';
72 protected $_atc_boundary = '';
73 protected $_header_str = '';
74 protected $_smtp_connect = '';
75 protected $_encoding = '8bit';
76 protected $_IP = FALSE;
77 protected $_smtp_auth = FALSE;
78 protected $_replyto_flag = FALSE;
79 protected $_debug_msg = array();
80 protected $_recipients = array();
81 protected $_cc_array = array();
82 protected $_bcc_array = array();
83 protected $_headers = array();
84 protected $_attach_name = array();
85 protected $_attach_type = array();
86 protected $_attach_disp = array();
87 protected $_protocols = array('mail', 'sendmail', 'smtp');
88 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
89 protected $_bit_depths = array('7bit', '8bit');
90 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Constructor - Sets Email Preferences
94 *
95 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +030096 *
97 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000099 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 if (count($config) > 0)
102 {
103 $this->initialize($config);
104 }
105 else
106 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100107 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200108 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110
Andrey Andreev59c5b532012-03-01 14:09:51 +0200111 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Initialize preferences
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @param array
120 * @return void
121 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000122 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 foreach ($config as $key => $val)
125 {
126 if (isset($this->$key))
127 {
128 $method = 'set_'.$key;
129
130 if (method_exists($this, $method))
131 {
132 $this->$method($val);
133 }
134 else
135 {
136 $this->$key = $val;
137 }
138 }
139 }
Eric Barnes6113f542010-12-29 13:36:12 -0500140 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
Alex Bilbied261b1e2012-06-02 11:12:16 +0100142 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200143 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000144
145 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
149
150 /**
151 * Initialize the Email Data
152 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800153 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200154 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000156 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200158 $this->_subject = '';
159 $this->_body = '';
160 $this->_finalbody = '';
161 $this->_header_str = '';
162 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500164 $this->_cc_array = array();
165 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $this->_headers = array();
167 $this->_debug_msg = array();
168
Mickey Wubfc1cad2012-05-31 22:28:40 -0700169 $this->set_header('User-Agent', $this->useragent);
170 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000171
172 if ($clear_attachments !== FALSE)
173 {
174 $this->_attach_name = array();
175 $this->_attach_type = array();
176 $this->_attach_disp = array();
177 }
Eric Barnes6113f542010-12-29 13:36:12 -0500178
Greg Akera769deb2010-11-10 15:47:29 -0600179 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // --------------------------------------------------------------------
183
184 /**
185 * Set FROM
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string
188 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200189 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 */
Melounek58dfc082012-06-29 08:43:47 +0200191 public function from($from, $name = '', $return_path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200193 if (preg_match('/\<(.*)\>/', $from, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200195 $from = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 if ($this->validate)
199 {
200 $this->validate_email($this->_str_to_array($from));
Andrey Andreevccd01c72012-10-05 17:12:55 +0300201 if ($return_path)
Melounek58dfc082012-06-29 08:43:47 +0200202 {
203 $this->validate_email($this->_str_to_array($return_path));
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100208 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 // only use Q encoding if there are characters that would require it
211 if ( ! preg_match('/[\200-\377]/', $name))
212 {
213 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000214 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216 else
217 {
218 $name = $this->_prep_q_encoding($name, TRUE);
219 }
220 }
221
Mickey Wubfc1cad2012-05-31 22:28:40 -0700222 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200223
Andrey Andreevccd01c72012-10-05 17:12:55 +0300224 if( ! $return_path)
Melounek58dfc082012-06-29 08:43:47 +0200225 {
226 $return_path = $from;
227 }
228 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500229
Greg Akera769deb2010-11-10 15:47:29 -0600230 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Set Reply-to
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string
239 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200240 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000242 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200244 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200246 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249 if ($this->validate)
250 {
251 $this->validate_email($this->_str_to_array($replyto));
252 }
253
Alex Bilbied261b1e2012-06-02 11:12:16 +0100254 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 $name = $replyto;
257 }
258
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300259 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $name = '"'.$name.'"';
262 }
263
Mickey Wubfc1cad2012-05-31 22:28:40 -0700264 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600266
267 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Set Recipients
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200276 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000278 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $to = $this->_str_to_array($to);
281 $to = $this->clean_email($to);
282
283 if ($this->validate)
284 {
285 $this->validate_email($to);
286 }
287
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200288 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700290 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 switch ($this->_get_protocol())
294 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200295 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000296 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200298 case 'sendmail':
299 case 'mail':
300 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 break;
302 }
Eric Barnes6113f542010-12-29 13:36:12 -0500303
Greg Akera769deb2010-11-10 15:47:29 -0600304 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // --------------------------------------------------------------------
308
309 /**
310 * Set CC
311 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200313 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000315 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Andrey Andreev56454792012-05-17 14:32:19 +0300317 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
319 if ($this->validate)
320 {
321 $this->validate_email($cc);
322 }
323
Mickey Wubfc1cad2012-05-31 22:28:40 -0700324 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200326 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $this->_cc_array = $cc;
329 }
Eric Barnes6113f542010-12-29 13:36:12 -0500330
Greg Akera769deb2010-11-10 15:47:29 -0600331 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
337 * Set BCC
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @param string
340 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200341 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000343 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100345 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 $this->bcc_batch_mode = TRUE;
348 $this->bcc_batch_size = $limit;
349 }
350
Andrey Andreev56454792012-05-17 14:32:19 +0300351 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 if ($this->validate)
354 {
355 $this->validate_email($bcc);
356 }
357
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200358 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $this->_bcc_array = $bcc;
361 }
362 else
363 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700364 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Eric Barnes6113f542010-12-29 13:36:12 -0500366
Greg Akera769deb2010-11-10 15:47:29 -0600367 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
371
372 /**
373 * Set Email Subject
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200376 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000378 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700381 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600382 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
386
387 /**
388 * Set Body
389 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200391 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000393 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200395 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200396
Andrey Andreevaf728622011-10-20 10:11:59 +0300397 /* strip slashes only if magic quotes is ON
398 if we do it with magic quotes OFF, it strips real, user-inputted chars.
399
400 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
401 it will probably not exist in future versions at all.
402 */
403 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200404 {
diegorivera33c32802011-10-19 02:56:15 -0200405 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200406 }
diegorivera33c32802011-10-19 02:56:15 -0200407
Greg Akera769deb2010-11-10 15:47:29 -0600408 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * Assign file attachments
415 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200417 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200419 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
trit151fc682011-11-23 07:30:06 -0500421 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500422 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200423 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600424 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Add a Header Item
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string
433 * @param string
434 * @return void
435 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700436 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 $this->_headers[$header] = $value;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * Convert a String to an Array
445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param string
447 * @return array
448 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600449 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 if ( ! is_array($email))
452 {
Andrey Andreev56454792012-05-17 14:32:19 +0300453 return (strpos($email, ',') !== FALSE)
454 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
455 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Andrey Andreev56454792012-05-17 14:32:19 +0300457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return $email;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * Set Multipart Value
465 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200467 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000469 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400471 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600472 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Set Mailtype
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200481 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000483 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev56454792012-05-17 14:32:19 +0300485 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600486 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Set Wordwrap
493 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400494 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200495 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000497 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400499 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600500 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // --------------------------------------------------------------------
504
505 /**
506 * Set Protocol
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200509 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000511 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200513 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600514 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * Set Priority
521 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200522 * @param int
523 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000525 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200527 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600528 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // --------------------------------------------------------------------
532
533 /**
534 * Set Newline Character
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200537 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000539 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200541 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600542 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // --------------------------------------------------------------------
546
547 /**
548 * Set CRLF
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200551 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000553 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200555 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600556 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
562 * Set Message Boundary
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @return void
565 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600566 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200568 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
569 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Get the Message ID
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return string
578 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600579 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200581 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300582 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
586
587 /**
588 * Get Mail Protocol
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200591 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600593 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200596 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000597
Alex Bilbied261b1e2012-06-02 11:12:16 +0100598 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 return $this->protocol;
601 }
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * Get Mail Encoding
608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param bool
610 * @return string
611 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600612 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200614 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 foreach ($this->_base_charsets as $charset)
617 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300618 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 $this->_encoding = '7bit';
621 }
622 }
623
Alex Bilbied261b1e2012-06-02 11:12:16 +0100624 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 return $this->_encoding;
627 }
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Get content type (text/html/attachment)
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @return string
636 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600637 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreev56454792012-05-17 14:32:19 +0300639 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100641 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200643 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 return 'plain-attach';
646 }
647 else
648 {
649 return 'plain';
650 }
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Set RFC 822 Date
657 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 * @return string
659 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600660 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200662 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300663 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200665 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000666
Andrey Andreev59c5b532012-03-01 14:09:51 +0200667 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 // --------------------------------------------------------------------
671
672 /**
673 * Mime message
674 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @return string
676 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600677 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200679 return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.';
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // --------------------------------------------------------------------
683
684 /**
685 * Validate Email Address
686 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * @param string
688 * @return bool
689 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000690 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 if ( ! is_array($email))
693 {
patworkb0707982011-04-08 15:10:05 +0200694 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 return FALSE;
696 }
697
698 foreach ($email as $val)
699 {
700 if ( ! $this->valid_email($val))
701 {
patworkb0707982011-04-08 15:10:05 +0200702 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 return FALSE;
704 }
705 }
706
707 return TRUE;
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // --------------------------------------------------------------------
711
712 /**
713 * Email Validation
714 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 * @param string
716 * @return bool
717 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400718 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300720 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 // --------------------------------------------------------------------
724
725 /**
726 * Clean Extended Email Address: Joe Smith <joe@smith.com>
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string
729 * @return string
730 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000731 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 if ( ! is_array($email))
734 {
Andrey Andreev56454792012-05-17 14:32:19 +0300735 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 $clean_email = array();
739
740 foreach ($email as $addy)
741 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200742 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744
745 return $clean_email;
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // --------------------------------------------------------------------
749
750 /**
751 * Build alternative plain text message
752 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000753 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * in plain-text headers of HTML-formatted emails.
755 * If the user hasn't specified his own alternative message
756 * it creates one by stripping the HTML
757 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 * @return string
759 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600760 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100762 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 {
764 return $this->word_wrap($this->alt_message, '76');
765 }
766
Andrey Andreev56454792012-05-17 14:32:19 +0300767 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200768 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000769
770 for ($i = 20; $i >= 3; $i--)
771 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200772 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200775 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 // --------------------------------------------------------------------
779
780 /**
781 * Word Wrap
782 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200784 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @return string
786 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000787 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
789 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100790 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100792 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
794
795 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300796 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000797
798 // Standardize newlines
799 if (strpos($str, "\r") !== FALSE)
800 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200801 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
803
804 // If the current word is surrounded by {unwrap} tags we'll
805 // strip the entire chunk and replace it with a marker.
806 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300807 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200809 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200811 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300812 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
814 }
815
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000816 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300818 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 $str = wordwrap($str, $charlim, "\n", FALSE);
820
821 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300822 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 foreach (explode("\n", $str) as $line)
824 {
825 // Is the line within the allowed character count?
826 // If so we'll join it to the output and continue
827 if (strlen($line) <= $charlim)
828 {
829 $output .= $line.$this->newline;
830 continue;
831 }
832
833 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200834 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
836 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300837 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 break;
840 }
841
842 // Trim the word down
843 $temp .= substr($line, 0, $charlim-1);
844 $line = substr($line, $charlim-1);
845 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200846 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000847
848 // If $temp contains data it means we had to split up an over-length
849 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100850 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200852 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
854
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200855 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
857
858 // Put our markers back
859 if (count($unwrap) > 0)
860 {
861 foreach ($unwrap as $key => $val)
862 {
Andrey Andreev56454792012-05-17 14:32:19 +0300863 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
865 }
866
867 return $output;
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 // --------------------------------------------------------------------
871
872 /**
873 * Build final headers
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @return string
876 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600877 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700879 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
880 $this->set_header('X-Mailer', $this->useragent);
881 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
882 $this->set_header('Message-ID', $this->_get_message_id());
883 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
889 * Write Headers as a string
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @return void
892 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600893 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200895 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 $this->_subject = $this->_headers['Subject'];
898 unset($this->_headers['Subject']);
899 }
900
901 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300902 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000903
Pascal Kriete14287f32011-02-14 13:39:34 -0500904 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
906 $val = trim($val);
907
Alex Bilbied261b1e2012-06-02 11:12:16 +0100908 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Andrey Andreev56454792012-05-17 14:32:19 +0300910 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
912 }
913
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200914 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Derek Jones1d890882009-02-10 20:32:14 +0000916 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
921
922 /**
923 * Build Final Body and attachments
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @return void
926 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600927 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200929 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
931 $this->_body = $this->word_wrap($this->_body);
932 }
933
934 $this->_set_boundaries();
935 $this->_write_headers();
936
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200937 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500938 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000939
940 switch ($this->_get_content_type())
941 {
942 case 'plain' :
943
Andrey Andreev56454792012-05-17 14:32:19 +0300944 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
945 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000946
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200947 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $this->_header_str .= $hdr;
950 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Brandon Jones485d7412010-11-09 16:38:17 -0500952 else
953 {
954 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
955 }
Eric Barnes6113f542010-12-29 13:36:12 -0500956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 return;
958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 case 'html' :
960
961 if ($this->send_multipart === FALSE)
962 {
Andrey Andreev56454792012-05-17 14:32:19 +0300963 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
964 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
966 else
967 {
Andrey Andreev56454792012-05-17 14:32:19 +0300968 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969
Andrey Andreev56454792012-05-17 14:32:19 +0300970 $body .= $this->_get_mime_message().$this->newline.$this->newline
971 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000972
Andrey Andreev56454792012-05-17 14:32:19 +0300973 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
974 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
975 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500976
Andrey Andreev56454792012-05-17 14:32:19 +0300977 .'Content-Type: text/html; charset='.$this->charset.$this->newline
978 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Eric Barnes6113f542010-12-29 13:36:12 -0500980
Andrey Andreev56454792012-05-17 14:32:19 +0300981 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500982
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200983 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500986 }
987 else
988 {
Andrey Andreev56454792012-05-17 14:32:19 +0300989 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 }
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
993 if ($this->send_multipart !== FALSE)
994 {
Andrey Andreev56454792012-05-17 14:32:19 +0300995 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 return;
999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 case 'plain-attach' :
1001
Andrey Andreev56454792012-05-17 14:32:19 +03001002 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001004 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001007 }
1008
Andrey Andreev56454792012-05-17 14:32:19 +03001009 $body .= $this->_get_mime_message().$this->newline.$this->newline
1010 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001011
Andrey Andreev56454792012-05-17 14:32:19 +03001012 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1013 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
Andrey Andreev56454792012-05-17 14:32:19 +03001015 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
1017 break;
1018 case 'html-attach' :
1019
Andrey Andreev56454792012-05-17 14:32:19 +03001020 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001021
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001022 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
1026
Andrey Andreev56454792012-05-17 14:32:19 +03001027 $body .= $this->_get_mime_message().$this->newline.$this->newline
1028 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001029
Andrey Andreev56454792012-05-17 14:32:19 +03001030 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1031 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001032
Andrey Andreev56454792012-05-17 14:32:19 +03001033 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1034 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1035 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001036
Andrey Andreev56454792012-05-17 14:32:19 +03001037 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1038 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001039
Andrey Andreev56454792012-05-17 14:32:19 +03001040 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1041 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 break;
1044 }
1045
1046 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001047 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
tritbc4ac992011-11-23 07:19:41 -05001049 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001050 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001052 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Alex Bilbied261b1e2012-06-02 11:12:16 +01001054 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001056 if ( ! file_exists($filename))
1057 {
1058 $this->_set_error_message('lang:email_attachment_missing', $filename);
1059 return FALSE;
1060 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001061
Matteo Mattei5688d582012-03-13 19:29:29 +01001062 $file = filesize($filename) +1;
1063
1064 if ( ! $fp = fopen($filename, FOPEN_READ))
1065 {
1066 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1067 return FALSE;
1068 }
1069
Matteo Matteic3b36f42012-03-26 10:27:17 +02001070 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001071 $file_content = fread($fp, $file);
1072 fclose($fp);
1073 }
1074 else
1075 {
1076 $file_content =& $this->_attach_content[$i];
1077 }
Andrey Andreev56454792012-05-17 14:32:19 +03001078
1079 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1080 .'Content-type: '.$ctype.'; '
1081 .'name="'.$basename.'"'.$this->newline
1082 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1083 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001084
Matteo Mattei5688d582012-03-13 19:29:29 +01001085 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
1087
Andrey Andreev56454792012-05-17 14:32:19 +03001088 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1089 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 return;
1091 }
Barry Mienydd671972010-10-04 16:33:58 +02001092
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 // --------------------------------------------------------------------
1094
1095 /**
1096 * Prep Quoted Printable
1097 *
1098 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1099 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1100 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @return string
1103 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001104 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
Andrey Andreev683b34d2012-10-09 15:00:00 +03001106 // RFC 2045 specifies CRLF as "\r\n".
1107 // However, many developers choose to override that and violate
1108 // the RFC rules due to (apparently) a bug in MS Exchange,
1109 // which only works with "\n".
1110 if ($this->crlf === "\r\n" && is_php('5.3'))
1111 {
1112 return quoted_printable_encode($str);
1113 }
1114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // Set the character limit
1116 // Don't allow over 76, as that will make servers and MUAs barf
1117 // all over quoted-printable data
Alex Bilbied261b1e2012-06-02 11:12:16 +01001118 if ($charlim === '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001120 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 }
1122
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001123 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001124 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001125
1126 // Standardize newlines
1127 if (strpos($str, "\r") !== FALSE)
1128 {
1129 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1130 }
1131
1132 // We are intentionally wrapping so mail servers will encode characters
1133 // properly and MUAs will behave, so {unwrap} must go!
1134 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1135
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $escape = '=';
1137 $output = '';
1138
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001139 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 $length = strlen($line);
1142 $temp = '';
1143
1144 // Loop through each character in the line to add soft-wrap
1145 // characters at the end of a line " =\r\n" and add the newly
1146 // processed line(s) to the output (see comment on $crlf class property)
1147 for ($i = 0; $i < $length; $i++)
1148 {
1149 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001150 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 $ascii = ord($char);
1152
1153 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001154 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001156 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001158 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001160 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 }
1162
1163 // If we're at the character limit, add the line to the output,
1164 // reset our temp variable, and keep on chuggin'
1165 if ((strlen($temp) + strlen($char)) >= $charlim)
1166 {
1167 $output .= $temp.$escape.$this->crlf;
1168 $temp = '';
1169 }
1170
1171 // Add the character to our temporary line
1172 $temp .= $char;
1173 }
1174
1175 // Add our completed line to the output
1176 $output .= $temp.$this->crlf;
1177 }
1178
1179 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001180 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 }
1182
1183 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 /**
1186 * Prep Q Encoding
1187 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001188 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 * but not identical to quoted-printable, so it has its own method
1190 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001191 * @param string
1192 * @param bool set to TRUE for processing From: headers
1193 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001195 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1198
1199 // Line length must not exceed 76 characters, so we adjust for
1200 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1201 $limit = 75 - 7 - strlen($this->charset);
1202
1203 // these special characters must be converted too
1204 $convert = array('_', '=', '?');
1205
1206 if ($from === TRUE)
1207 {
1208 $convert[] = ',';
1209 $convert[] = ';';
1210 }
1211
1212 $output = '';
1213 $temp = '';
1214
1215 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1216 {
1217 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001218 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 $ascii = ord($char);
1220
1221 // convert ALL non-printable ASCII characters and our specials
1222 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1223 {
1224 $char = '='.dechex($ascii);
1225 }
1226
1227 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001228 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
1230 $char = '_';
1231 }
1232
1233 // If we're at the character limit, add the line to the output,
1234 // reset our temp variable, and keep on chuggin'
1235 if ((strlen($temp) + strlen($char)) >= $limit)
1236 {
1237 $output .= $temp.$this->crlf;
1238 $temp = '';
1239 }
1240
1241 // Add the character to our temporary line
1242 $temp .= $char;
1243 }
1244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // wrap each line with the shebang, charset, and transfer encoding
1246 // the preceding space on successive lines is required for header "folding"
Bruno Barão51f72cd2012-10-08 16:31:46 +01001247 return trim(preg_replace('/^(.*?)(\r*)$/m', ' =?'.$this->charset.'?Q?$1?=$2', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 }
1249
1250 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 /**
1253 * Send Email
1254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @return bool
1256 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001257 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001259 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 $this->reply_to($this->_headers['From']);
1262 }
1263
Andrey Andreev76f15c92012-03-01 13:05:07 +02001264 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1265 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1266 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
patworkb0707982011-04-08 15:10:05 +02001268 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 return FALSE;
1270 }
1271
1272 $this->_build_headers();
1273
Andrey Andreev76f15c92012-03-01 13:05:07 +02001274 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001276 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001277
Alex Bilbiea87aab32012-07-30 09:50:37 +01001278 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001279 {
1280 $this->clear();
1281 }
1282
Alex Bilbieb901e732012-07-30 09:44:57 +01001283 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
1285
1286 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001287 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001288
Alex Bilbiea87aab32012-07-30 09:50:37 +01001289 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001290 {
1291 $this->clear();
1292 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001293
Alex Bilbieb901e732012-07-30 09:44:57 +01001294 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // --------------------------------------------------------------------
1298
1299 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001300 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001302 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001304 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001306 $float = $this->bcc_batch_size - 1;
1307 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 $chunk = array();
1309
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001310 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
1312 if (isset($this->_bcc_array[$i]))
1313 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001314 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 }
1316
Alex Bilbied261b1e2012-06-02 11:12:16 +01001317 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
1319 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001320 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001321 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 }
1323
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001324 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
1326 $chunk[] = substr($set, 1);
1327 }
1328 }
1329
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001330 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
1332 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001333
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001334 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001335
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001336 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001338 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 }
1340 else
1341 {
1342 $this->_bcc_array = $bcc;
1343 }
1344
1345 $this->_build_message();
1346 $this->_spool_email();
1347 }
1348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 // --------------------------------------------------------------------
1351
1352 /**
1353 * Unwrap special elements
1354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * @return void
1356 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001357 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 {
Andrey Andreev56454792012-05-17 14:32:19 +03001359 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
1363
1364 /**
1365 * Strip line-breaks via callback
1366 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 * @return string
1368 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001369 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 {
1371 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1372 {
1373 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1374 }
1375
1376 return $matches[1];
1377 }
Barry Mienydd671972010-10-04 16:33:58 +02001378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 // --------------------------------------------------------------------
1380
1381 /**
1382 * Spool mail to the mail server
1383 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 * @return bool
1385 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001386 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 {
1388 $this->_unwrap_specials();
1389
Andrey Andreev56454792012-05-17 14:32:19 +03001390 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001391 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 {
Andrey Andreev56454792012-05-17 14:32:19 +03001393 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001394 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 }
1396
patworkb0707982011-04-08 15:10:05 +02001397 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 return TRUE;
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 // --------------------------------------------------------------------
1402
1403 /**
1404 * Send using mail()
1405 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 * @return bool
1407 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001408 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001410 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001412 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414 else
1415 {
1416 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1417 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001418 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 +00001419 }
1420 }
Barry Mienydd671972010-10-04 16:33:58 +02001421
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 // --------------------------------------------------------------------
1423
1424 /**
1425 * Send using Sendmail
1426 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * @return bool
1428 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001429 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
Melounek58dfc082012-06-29 08:43:47 +02001431 $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 +00001432
Derek Jones4cefaa42009-04-29 19:13:56 +00001433 if ($fp === FALSE OR $fp === NULL)
1434 {
1435 // server probably has popen disabled, so nothing we can do to get a verbose error.
1436 return FALSE;
1437 }
Derek Jones71141ce2010-03-02 16:41:20 -06001438
Derek Jonesc630bcf2008-11-17 21:09:45 +00001439 fputs($fp, $this->_header_str);
1440 fputs($fp, $this->_finalbody);
1441
Barry Mienydd671972010-10-04 16:33:58 +02001442 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001443
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001444 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 {
patworkb0707982011-04-08 15:10:05 +02001446 $this->_set_error_message('lang:email_exit_status', $status);
1447 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 return FALSE;
1449 }
1450
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 return TRUE;
1452 }
Barry Mienydd671972010-10-04 16:33:58 +02001453
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 // --------------------------------------------------------------------
1455
1456 /**
1457 * Send using SMTP
1458 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 * @return bool
1460 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001461 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001463 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
patworkb0707982011-04-08 15:10:05 +02001465 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 return FALSE;
1467 }
1468
Diogo Osório593f7982012-03-02 18:04:17 +00001469 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001470 {
1471 return FALSE;
1472 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001473
1474 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1475
Pascal Kriete14287f32011-02-14 13:39:34 -05001476 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 {
1478 $this->_send_command('to', $val);
1479 }
1480
1481 if (count($this->_cc_array) > 0)
1482 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001483 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001485 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 {
1487 $this->_send_command('to', $val);
1488 }
1489 }
1490 }
1491
1492 if (count($this->_bcc_array) > 0)
1493 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001494 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001496 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 {
1498 $this->_send_command('to', $val);
1499 }
1500 }
1501 }
1502
1503 $this->_send_command('data');
1504
1505 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001506 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001507
1508 $this->_send_data('.');
1509
1510 $reply = $this->_get_smtp_data();
1511
1512 $this->_set_error_message($reply);
1513
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001514 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
patworkb0707982011-04-08 15:10:05 +02001516 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 return FALSE;
1518 }
1519
1520 $this->_send_command('quit');
1521 return TRUE;
1522 }
Barry Mienydd671972010-10-04 16:33:58 +02001523
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 // --------------------------------------------------------------------
1525
1526 /**
1527 * SMTP Connect
1528 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 * @param string
1530 * @return string
1531 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001532 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001534 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001535
Radu Potopbbf04b02011-09-28 13:57:51 +03001536 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001537 $this->smtp_port,
1538 $errno,
1539 $errstr,
1540 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001541
Pascal Kriete14287f32011-02-14 13:39:34 -05001542 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 {
Andrey Andreev56454792012-05-17 14:32:19 +03001544 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 return FALSE;
1546 }
1547
1548 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001549
Alex Bilbied261b1e2012-06-02 11:12:16 +01001550 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001551 {
1552 $this->_send_command('hello');
1553 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001554
Radu Potop4c589ae2011-09-29 10:19:55 +03001555 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001556
Sean Fishere862ddd2011-10-26 22:45:00 -03001557 if ($crypto !== TRUE)
1558 {
1559 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1560 return FALSE;
1561 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001562 }
1563
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 return $this->_send_command('hello');
1565 }
Barry Mienydd671972010-10-04 16:33:58 +02001566
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 // --------------------------------------------------------------------
1568
1569 /**
1570 * Send SMTP command
1571 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 * @param string
1573 * @param string
1574 * @return string
1575 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001576 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 {
1578 switch ($cmd)
1579 {
1580 case 'hello' :
1581
Andrey Andreev56454792012-05-17 14:32:19 +03001582 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1583 {
1584 $this->_send_data('EHLO '.$this->_get_hostname());
1585 }
1586 else
1587 {
1588 $this->_send_data('HELO '.$this->_get_hostname());
1589 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001590
1591 $resp = 250;
1592 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001593 case 'starttls' :
1594
1595 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001596 $resp = 220;
1597 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 case 'from' :
1599
1600 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 $resp = 250;
1602 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001603 case 'to' :
1604
leandronf7686c122012-03-22 08:07:31 -03001605 if ($this->dsn)
1606 {
leandronfb4552942012-03-21 23:54:26 -03001607 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001608 }
leandronfb4552942012-03-21 23:54:26 -03001609 else
leandronf7686c122012-03-22 08:07:31 -03001610 {
leandronfb4552942012-03-21 23:54:26 -03001611 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001612 }
Andrey Andreev56454792012-05-17 14:32:19 +03001613
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 $resp = 250;
1615 break;
1616 case 'data' :
1617
1618 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 $resp = 354;
1620 break;
1621 case 'quit' :
1622
1623 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 $resp = 221;
1625 break;
1626 }
1627
1628 $reply = $this->_get_smtp_data();
1629
Andrey Andreev56454792012-05-17 14:32:19 +03001630 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001631
Andrey Andreevba261762012-06-11 14:11:35 +03001632 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 {
patworkb0707982011-04-08 15:10:05 +02001634 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 return FALSE;
1636 }
1637
Alex Bilbied261b1e2012-06-02 11:12:16 +01001638 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
1640 fclose($this->_smtp_connect);
1641 }
1642
1643 return TRUE;
1644 }
Barry Mienydd671972010-10-04 16:33:58 +02001645
Derek Allard2067d1a2008-11-13 22:59:24 +00001646 // --------------------------------------------------------------------
1647
1648 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001649 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 * @return bool
1652 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001653 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 {
1655 if ( ! $this->_smtp_auth)
1656 {
1657 return TRUE;
1658 }
1659
Alex Bilbied261b1e2012-06-02 11:12:16 +01001660 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
patworkb0707982011-04-08 15:10:05 +02001662 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 return FALSE;
1664 }
1665
1666 $this->_send_data('AUTH LOGIN');
1667
1668 $reply = $this->_get_smtp_data();
1669
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001670 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
patworkb0707982011-04-08 15:10:05 +02001672 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 return FALSE;
1674 }
1675
1676 $this->_send_data(base64_encode($this->smtp_user));
1677
1678 $reply = $this->_get_smtp_data();
1679
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001680 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 {
patworkb0707982011-04-08 15:10:05 +02001682 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 return FALSE;
1684 }
1685
1686 $this->_send_data(base64_encode($this->smtp_pass));
1687
1688 $reply = $this->_get_smtp_data();
1689
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001690 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
patworkb0707982011-04-08 15:10:05 +02001692 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 return FALSE;
1694 }
1695
1696 return TRUE;
1697 }
Barry Mienydd671972010-10-04 16:33:58 +02001698
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 // --------------------------------------------------------------------
1700
1701 /**
1702 * Send SMTP data
1703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 * @return bool
1705 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001706 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
1708 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1709 {
patworkb0707982011-04-08 15:10:05 +02001710 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 return FALSE;
1712 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001713
1714 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 }
Barry Mienydd671972010-10-04 16:33:58 +02001716
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 // --------------------------------------------------------------------
1718
1719 /**
1720 * Get SMTP data
1721 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 * @return string
1723 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001724 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Andrey Andreev56454792012-05-17 14:32:19 +03001726 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001727
1728 while ($str = fgets($this->_smtp_connect, 512))
1729 {
1730 $data .= $str;
1731
Alex Bilbied261b1e2012-06-02 11:12:16 +01001732 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
1734 break;
1735 }
1736 }
1737
1738 return $data;
1739 }
Barry Mienydd671972010-10-04 16:33:58 +02001740
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 // --------------------------------------------------------------------
1742
1743 /**
1744 * Get Hostname
1745 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 * @return string
1747 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001748 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001750 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 }
Barry Mienydd671972010-10-04 16:33:58 +02001752
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 // --------------------------------------------------------------------
1754
1755 /**
1756 * Get IP
1757 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 * @return string
1759 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001760 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 {
1762 if ($this->_IP !== FALSE)
1763 {
1764 return $this->_IP;
1765 }
1766
Andrey Andreev76f15c92012-03-01 13:05:07 +02001767 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1768 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001769 if ($cip) $this->_IP = $cip;
1770 elseif ($rip) $this->_IP = $rip;
1771 else
1772 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001773 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001774 if ($fip)
1775 {
1776 $this->_IP = $fip;
1777 }
1778 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001779
Robin Sowell76b369e2010-03-19 11:15:28 -04001780 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
1782 $x = explode(',', $this->_IP);
1783 $this->_IP = end($x);
1784 }
1785
Andrey Andreev59c5b532012-03-01 14:09:51 +02001786 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 +00001787 {
1788 $this->_IP = '0.0.0.0';
1789 }
1790
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 return $this->_IP;
1792 }
Barry Mienydd671972010-10-04 16:33:58 +02001793
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 // --------------------------------------------------------------------
1795
1796 /**
1797 * Get Debug Message
1798 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @return string
1800 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001801 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 {
1803 $msg = '';
1804
1805 if (count($this->_debug_msg) > 0)
1806 {
1807 foreach ($this->_debug_msg as $val)
1808 {
1809 $msg .= $val;
1810 }
1811 }
1812
Andrey Andreev59c5b532012-03-01 14:09:51 +02001813 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 }
Barry Mienydd671972010-10-04 16:33:58 +02001815
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 // --------------------------------------------------------------------
1817
1818 /**
1819 * Set Message
1820 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001822 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001824 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
1826 $CI =& get_instance();
1827 $CI->lang->load('email');
1828
Andrey Andreev76f15c92012-03-01 13:05:07 +02001829 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 {
Andrey Andreev56454792012-05-17 14:32:19 +03001831 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 }
1833 else
1834 {
Andrey Andreev56454792012-05-17 14:32:19 +03001835 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 }
1837 }
Barry Mienydd671972010-10-04 16:33:58 +02001838
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 // --------------------------------------------------------------------
1840
1841 /**
1842 * Mime Types
1843 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 * @param string
1845 * @return string
1846 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001847 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001849 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001850
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001851 $ext = strtolower($ext);
1852
1853 if ( ! is_array($mimes))
1854 {
1855 $mimes =& get_mimes();
1856 }
1857
1858 if (isset($mimes[$ext]))
1859 {
1860 return is_array($mimes[$ext])
1861 ? current($mimes[$ext])
1862 : $mimes[$ext];
1863 }
1864
1865 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 }
1867
1868}
Derek Allard2067d1a2008-11-13 22:59:24 +00001869
1870/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001871/* Location: ./system/libraries/Email.php */