blob: 7280466a5363eef63d684e34c583fefcf8c0047f [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.
vkeranov7e4356b2012-10-27 18:41:00 +030050 public $wordwrap = TRUE; // TRUE/FALSE - Turns word-wrap on/off
Andrey Andreev59c5b532012-03-01 14:09:51 +020051 public $wrapchars = 76; // Number of characters to wrap at.
vkeranov7e4356b2012-10-27 18:41:00 +030052 public $mailtype = 'text'; // text/html - Defines email formatting
Andrey Andreev59c5b532012-03-01 14:09:51 +020053 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
vkeranov7e4356b2012-10-27 18:41:00 +030056 public $validate = FALSE; // TRUE/FALSE - Enables email validation
Andrey Andreev59c5b532012-03-01 14:09:51 +020057 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)
vkeranov7e4356b2012-10-27 18:41:00 +030059 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
vkeranov7e4356b2012-10-27 18:41:00 +030063 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();
Andrey Andreevb8f9a152012-10-27 01:36:51 +030084 protected $_attachments = array();
Andrey Andreev50814092012-03-01 12:36:12 +020085 protected $_protocols = array('mail', 'sendmail', 'smtp');
86 protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
87 protected $_bit_depths = array('7bit', '8bit');
88 protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
Derek Allard2067d1a2008-11-13 22:59:24 +000089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 /**
91 * Constructor - Sets Email Preferences
92 *
93 * The constructor can be passed an array of config values
Andrey Andreev56454792012-05-17 14:32:19 +030094 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030095 * @param array $config = array()
Andrey Andreev56454792012-05-17 14:32:19 +030096 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000097 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000098 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300100 $this->charset = config_item('charset');
Andrey Andreev9f44c212012-10-10 16:07:17 +0300101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 if (count($config) > 0)
103 {
104 $this->initialize($config);
105 }
106 else
107 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100108 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200109 $this->_safe_mode = (bool) @ini_get('safe_mode');
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111
Andrey Andreev925dd902012-10-19 11:06:31 +0300112 $this->charset = strtoupper($this->charset);
113
Andrey Andreev59c5b532012-03-01 14:09:51 +0200114 log_message('debug', 'Email Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Initialize preferences
121 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * @param array
123 * @return void
124 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000125 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 foreach ($config as $key => $val)
128 {
129 if (isset($this->$key))
130 {
131 $method = 'set_'.$key;
132
133 if (method_exists($this, $method))
134 {
135 $this->$method($val);
136 }
137 else
138 {
139 $this->$key = $val;
140 }
141 }
142 }
Eric Barnes6113f542010-12-29 13:36:12 -0500143 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000144
Alex Bilbied261b1e2012-06-02 11:12:16 +0100145 $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
Andrey Andreev59c5b532012-03-01 14:09:51 +0200146 $this->_safe_mode = (bool) @ini_get('safe_mode');
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000147
148 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // --------------------------------------------------------------------
152
153 /**
154 * Initialize the Email Data
155 *
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800156 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200157 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000159 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200161 $this->_subject = '';
162 $this->_body = '';
163 $this->_finalbody = '';
164 $this->_header_str = '';
165 $this->_replyto_flag = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500167 $this->_cc_array = array();
168 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 $this->_headers = array();
170 $this->_debug_msg = array();
171
Mickey Wubfc1cad2012-05-31 22:28:40 -0700172 $this->set_header('User-Agent', $this->useragent);
173 $this->set_header('Date', $this->_set_date());
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
175 if ($clear_attachments !== FALSE)
176 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300177 $this->_attachments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Eric Barnes6113f542010-12-29 13:36:12 -0500179
Greg Akera769deb2010-11-10 15:47:29 -0600180 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Set FROM
187 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300188 * @param string $from
189 * @param string $name
190 * @param string $return_path = NULL Return-Path
Andrey Andreev081c9462012-03-01 12:58:11 +0200191 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreev925dd902012-10-19 11:06:31 +0300193 public function from($from, $name = '', $return_path = NULL)
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 {
Andrey Andreev925dd902012-10-19 11:06:31 +0300220 $name = $this->_prep_q_encoding($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222 }
223
Mickey Wubfc1cad2012-05-31 22:28:40 -0700224 $this->set_header('From', $name.' <'.$from.'>');
Melounek58dfc082012-06-29 08:43:47 +0200225
Andrey Andreev925dd902012-10-19 11:06:31 +0300226 isset($return_path) OR $return_path = $from;
Melounek58dfc082012-06-29 08:43:47 +0200227 $this->set_header('Return-Path', '<'.$return_path.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500228
Greg Akera769deb2010-11-10 15:47:29 -0600229 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * Set Reply-to
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string
238 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200239 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000241 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200243 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200245 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 if ($this->validate)
249 {
250 $this->validate_email($this->_str_to_array($replyto));
251 }
252
Alex Bilbied261b1e2012-06-02 11:12:16 +0100253 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 $name = $replyto;
256 }
257
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300258 if (strpos($name, '"') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 $name = '"'.$name.'"';
261 }
262
Mickey Wubfc1cad2012-05-31 22:28:40 -0700263 $this->set_header('Reply-To', $name.' <'.$replyto.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600265
266 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
270
271 /**
272 * Set Recipients
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200275 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000277 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 $to = $this->_str_to_array($to);
280 $to = $this->clean_email($to);
281
282 if ($this->validate)
283 {
284 $this->validate_email($to);
285 }
286
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200287 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700289 $this->set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291
Andrey Andreev8a7078b2012-10-17 10:52:49 +0300292 $this->_recipients = $to;
Eric Barnes6113f542010-12-29 13:36:12 -0500293
Greg Akera769deb2010-11-10 15:47:29 -0600294 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // --------------------------------------------------------------------
298
299 /**
300 * Set CC
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200303 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000305 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreev56454792012-05-17 14:32:19 +0300307 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000308
309 if ($this->validate)
310 {
311 $this->validate_email($cc);
312 }
313
Mickey Wubfc1cad2012-05-31 22:28:40 -0700314 $this->set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000315
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200316 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $this->_cc_array = $cc;
319 }
Eric Barnes6113f542010-12-29 13:36:12 -0500320
Greg Akera769deb2010-11-10 15:47:29 -0600321 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
325
326 /**
327 * Set BCC
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @param string
330 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200331 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000333 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100335 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 $this->bcc_batch_mode = TRUE;
338 $this->bcc_batch_size = $limit;
339 }
340
Andrey Andreev56454792012-05-17 14:32:19 +0300341 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
343 if ($this->validate)
344 {
345 $this->validate_email($bcc);
346 }
347
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200348 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $this->_bcc_array = $bcc;
351 }
352 else
353 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700354 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Eric Barnes6113f542010-12-29 13:36:12 -0500356
Greg Akera769deb2010-11-10 15:47:29 -0600357 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
361
362 /**
363 * Set Email Subject
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200366 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000368 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $subject = $this->_prep_q_encoding($subject);
Mickey Wubfc1cad2012-05-31 22:28:40 -0700371 $this->set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600372 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // --------------------------------------------------------------------
376
377 /**
378 * Set Body
379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200381 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000383 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200385 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200386
Andrey Andreevaf728622011-10-20 10:11:59 +0300387 /* strip slashes only if magic quotes is ON
388 if we do it with magic quotes OFF, it strips real, user-inputted chars.
389
390 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
391 it will probably not exist in future versions at all.
392 */
393 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200394 {
diegorivera33c32802011-10-19 02:56:15 -0200395 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200396 }
diegorivera33c32802011-10-19 02:56:15 -0200397
Greg Akera769deb2010-11-10 15:47:29 -0600398 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 // --------------------------------------------------------------------
402
403 /**
404 * Assign file attachments
405 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300406 * @param string $filename
407 * @param string $disposition = 'attachment'
408 * @param string $newname = NULL
409 * @param string $mime = ''
Andrey Andreev081c9462012-03-01 12:58:11 +0200410 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200412 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300414 $this->_attachments[] = array(
415 'name' => array($filename, $newname),
416 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
417 'type' => $mime
418 );
419
Greg Akera769deb2010-11-10 15:47:29 -0600420 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Add a Header Item
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string
429 * @param string
430 * @return void
431 */
Mickey Wubfc1cad2012-05-31 22:28:40 -0700432 public function set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $this->_headers[$header] = $value;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
438
439 /**
440 * Convert a String to an Array
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param string
443 * @return array
444 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600445 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 if ( ! is_array($email))
448 {
Andrey Andreev56454792012-05-17 14:32:19 +0300449 return (strpos($email, ',') !== FALSE)
450 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
451 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
Andrey Andreev56454792012-05-17 14:32:19 +0300453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 return $email;
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // --------------------------------------------------------------------
458
459 /**
460 * Set Multipart Value
461 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200463 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000465 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400467 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600468 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Set Mailtype
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200477 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000479 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev56454792012-05-17 14:32:19 +0300481 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600482 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
486
487 /**
488 * Set Wordwrap
489 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400490 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200491 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000493 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400495 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600496 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // --------------------------------------------------------------------
500
501 /**
502 * Set Protocol
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200505 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000507 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200509 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600510 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * Set Priority
517 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200518 * @param int
519 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000521 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200523 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600524 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // --------------------------------------------------------------------
528
529 /**
530 * Set Newline Character
531 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200533 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000535 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200537 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600538 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // --------------------------------------------------------------------
542
543 /**
544 * Set CRLF
545 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200547 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000549 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200551 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600552 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * Set Message Boundary
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @return void
561 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600562 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200564 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
565 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // --------------------------------------------------------------------
569
570 /**
571 * Get the Message ID
572 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * @return string
574 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600575 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200577 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300578 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
582
583 /**
584 * Get Mail Protocol
585 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200587 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600589 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200592 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000593
Alex Bilbied261b1e2012-06-02 11:12:16 +0100594 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 return $this->protocol;
597 }
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // --------------------------------------------------------------------
601
602 /**
603 * Get Mail Encoding
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param bool
606 * @return string
607 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600608 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200610 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000611
612 foreach ($this->_base_charsets as $charset)
613 {
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300614 if (strpos($charset, $this->charset) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 $this->_encoding = '7bit';
617 }
618 }
619
Alex Bilbied261b1e2012-06-02 11:12:16 +0100620 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 return $this->_encoding;
623 }
624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Get content type (text/html/attachment)
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @return string
632 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600633 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev56454792012-05-17 14:32:19 +0300635 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300637 return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
Andrey Andreevb8f9a152012-10-27 01:36:51 +0300639 elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 return 'plain-attach';
642 }
643 else
644 {
645 return 'plain';
646 }
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // --------------------------------------------------------------------
650
651 /**
652 * Set RFC 822 Date
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @return string
655 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600656 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200658 $timezone = date('Z');
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300659 $operator = ($timezone[0] === '-') ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200661 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000662
Andrey Andreev59c5b532012-03-01 14:09:51 +0200663 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // --------------------------------------------------------------------
667
668 /**
669 * Mime message
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return string
672 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600673 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200675 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 +0000676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // --------------------------------------------------------------------
679
680 /**
681 * Validate Email Address
682 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 * @param string
684 * @return bool
685 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000686 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 if ( ! is_array($email))
689 {
patworkb0707982011-04-08 15:10:05 +0200690 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 return FALSE;
692 }
693
694 foreach ($email as $val)
695 {
696 if ( ! $this->valid_email($val))
697 {
patworkb0707982011-04-08 15:10:05 +0200698 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return FALSE;
700 }
701 }
702
703 return TRUE;
704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // --------------------------------------------------------------------
707
708 /**
709 * Email Validation
710 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * @param string
712 * @return bool
713 */
Timothy Warrend37f0e92012-06-27 08:21:59 -0400714 public function valid_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
Andrey Andreev580388b2012-06-27 15:43:46 +0300716 return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 // --------------------------------------------------------------------
720
721 /**
722 * Clean Extended Email Address: Joe Smith <joe@smith.com>
723 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 * @param string
725 * @return string
726 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000727 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
729 if ( ! is_array($email))
730 {
Andrey Andreev56454792012-05-17 14:32:19 +0300731 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 }
733
734 $clean_email = array();
735
736 foreach ($email as $addy)
737 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200738 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 }
740
741 return $clean_email;
742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 // --------------------------------------------------------------------
745
746 /**
747 * Build alternative plain text message
748 *
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300749 * Provides the raw message for use in plain-text headers of
750 * HTML-formatted emails.
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 * If the user hasn't specified his own alternative message
752 * it creates one by stripping the HTML
753 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * @return string
755 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600756 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300758 if ( ! empty($this->alt_message))
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300760 return ($this->wordwrap)
761 ? $this->word_wrap($this->alt_message, 76)
762 : $this->alt_message;
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
764
Andrey Andreev56454792012-05-17 14:32:19 +0300765 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200766 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000767
768 for ($i = 20; $i >= 3; $i--)
769 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200770 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
GDmac9bea4be2012-10-30 06:14:19 +0100773 // Reduce multiple spaces
774 $str = preg_replace('| +|', ' ', $str);
775
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300776 return ($this->wordwrap)
777 ? $this->word_wrap($body, 76)
778 : $body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
782
783 /**
784 * Word Wrap
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string
Andrey Andreev8df1ae22012-10-19 11:20:54 +0300787 * @param int line-length limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 * @return string
789 */
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300790 public function word_wrap($str, $charlim = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300792 // Set the character limit, if not already present
793 if (empty($charlim))
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +0300795 $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 }
797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 // 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
GDmac9bea4be2012-10-30 06:14:19 +0100804 // Reduce multiple spaces at end of line
805 $str = preg_replace('| +\n|', "\n", $str);
806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // If the current word is surrounded by {unwrap} tags we'll
808 // strip the entire chunk and replace it with a marker.
809 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300810 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200812 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200814 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300815 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 }
817 }
818
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000819 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300821 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 $str = wordwrap($str, $charlim, "\n", FALSE);
823
824 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300825 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 foreach (explode("\n", $str) as $line)
827 {
828 // Is the line within the allowed character count?
829 // If so we'll join it to the output and continue
830 if (strlen($line) <= $charlim)
831 {
832 $output .= $line.$this->newline;
833 continue;
834 }
835
836 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200837 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300840 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
842 break;
843 }
844
845 // Trim the word down
846 $temp .= substr($line, 0, $charlim-1);
847 $line = substr($line, $charlim-1);
848 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200849 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000850
851 // If $temp contains data it means we had to split up an over-length
852 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100853 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200855 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
857
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200858 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
860
861 // Put our markers back
862 if (count($unwrap) > 0)
863 {
864 foreach ($unwrap as $key => $val)
865 {
Andrey Andreev56454792012-05-17 14:32:19 +0300866 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
868 }
869
870 return $output;
871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
874
875 /**
876 * Build final headers
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @return string
879 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600880 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Mickey Wubfc1cad2012-05-31 22:28:40 -0700882 $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
883 $this->set_header('X-Mailer', $this->useragent);
884 $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]);
885 $this->set_header('Message-ID', $this->_get_message_id());
886 $this->set_header('Mime-Version', '1.0');
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * Write Headers as a string
893 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 * @return void
895 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600896 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200898 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
900 $this->_subject = $this->_headers['Subject'];
901 unset($this->_headers['Subject']);
902 }
903
904 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300905 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000906
Pascal Kriete14287f32011-02-14 13:39:34 -0500907 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
909 $val = trim($val);
910
Alex Bilbied261b1e2012-06-02 11:12:16 +0100911 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Andrey Andreev56454792012-05-17 14:32:19 +0300913 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
915 }
916
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200917 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Derek Jones1d890882009-02-10 20:32:14 +0000919 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 // --------------------------------------------------------------------
924
925 /**
926 * Build Final Body and attachments
927 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @return void
929 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600930 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200932 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 $this->_body = $this->word_wrap($this->_body);
935 }
936
937 $this->_set_boundaries();
938 $this->_write_headers();
939
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200940 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500941 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000942
943 switch ($this->_get_content_type())
944 {
945 case 'plain' :
946
Andrey Andreev56454792012-05-17 14:32:19 +0300947 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
948 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000949
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200950 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 $this->_header_str .= $hdr;
953 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
Brandon Jones485d7412010-11-09 16:38:17 -0500955 else
956 {
957 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
958 }
Eric Barnes6113f542010-12-29 13:36:12 -0500959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return;
961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 case 'html' :
963
964 if ($this->send_multipart === FALSE)
965 {
Andrey Andreev56454792012-05-17 14:32:19 +0300966 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
967 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
969 else
970 {
Andrey Andreev56454792012-05-17 14:32:19 +0300971 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000972
Andrey Andreev56454792012-05-17 14:32:19 +0300973 $body .= $this->_get_mime_message().$this->newline.$this->newline
974 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000975
Andrey Andreev56454792012-05-17 14:32:19 +0300976 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
977 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
978 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500979
Andrey Andreev56454792012-05-17 14:32:19 +0300980 .'Content-Type: text/html; charset='.$this->charset.$this->newline
981 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Eric Barnes6113f542010-12-29 13:36:12 -0500983
Andrey Andreev56454792012-05-17 14:32:19 +0300984 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500985
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200986 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500989 }
990 else
991 {
Andrey Andreev56454792012-05-17 14:32:19 +0300992 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 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 Andreevb8f9a152012-10-27 01:36:51 +03001049 for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001051 $filename = $this->_attachments[$i]['name'][0];
1052 $basename = is_null($this->_attachments[$i]['name'][1])
1053 ? basename($filename) : $this->_attachments[$i]['name'][1];
1054 $ctype = $this->_attachments[$i]['type'];
Matteo Mattei5688d582012-03-13 19:29:29 +01001055 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001056
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001057 if ($ctype === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001059 if ( ! file_exists($filename))
1060 {
1061 $this->_set_error_message('lang:email_attachment_missing', $filename);
1062 return FALSE;
1063 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001064
Matteo Mattei5688d582012-03-13 19:29:29 +01001065 $file = filesize($filename) +1;
1066
1067 if ( ! $fp = fopen($filename, FOPEN_READ))
1068 {
1069 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1070 return FALSE;
1071 }
1072
Matteo Matteic3b36f42012-03-26 10:27:17 +02001073 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001074 $file_content = fread($fp, $file);
1075 fclose($fp);
1076 }
1077 else
1078 {
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001079 $file_content =& $this->_attachments[$i]['name'][0];
Matteo Mattei5688d582012-03-13 19:29:29 +01001080 }
Andrey Andreev56454792012-05-17 14:32:19 +03001081
1082 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1083 .'Content-type: '.$ctype.'; '
1084 .'name="'.$basename.'"'.$this->newline
Andrey Andreevb8f9a152012-10-27 01:36:51 +03001085 .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
Andrey Andreev56454792012-05-17 14:32:19 +03001086 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001087
Matteo Mattei5688d582012-03-13 19:29:29 +01001088 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
1090
Andrey Andreev56454792012-05-17 14:32:19 +03001091 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1092 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 return;
1094 }
Barry Mienydd671972010-10-04 16:33:58 +02001095
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 // --------------------------------------------------------------------
1097
1098 /**
1099 * Prep Quoted Printable
1100 *
1101 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1102 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1103 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 * @return string
1106 */
Andrey Andreev683b34d2012-10-09 15:00:00 +03001107 protected function _prep_quoted_printable($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 {
Andrey Andreev00ea2a92012-10-18 14:59:29 +03001109 // We are intentionally wrapping so mail servers will encode characters
1110 // properly and MUAs will behave, so {unwrap} must go!
1111 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1112
Andrey Andreev683b34d2012-10-09 15:00:00 +03001113 // RFC 2045 specifies CRLF as "\r\n".
1114 // However, many developers choose to override that and violate
1115 // the RFC rules due to (apparently) a bug in MS Exchange,
1116 // which only works with "\n".
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001117 if ($this->crlf === "\r\n")
Andrey Andreev683b34d2012-10-09 15:00:00 +03001118 {
Andrey Andreev26f0cf92012-10-11 13:52:39 +03001119 if (is_php('5.3'))
1120 {
1121 return quoted_printable_encode($str);
1122 }
1123 elseif (function_exists('imap_8bit'))
1124 {
1125 return imap_8bit($str);
1126 }
Andrey Andreev683b34d2012-10-09 15:00:00 +03001127 }
1128
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001129 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001130 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001131
1132 // Standardize newlines
1133 if (strpos($str, "\r") !== FALSE)
1134 {
1135 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1136 }
1137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 $escape = '=';
1139 $output = '';
1140
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001141 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
1143 $length = strlen($line);
1144 $temp = '';
1145
1146 // Loop through each character in the line to add soft-wrap
1147 // characters at the end of a line " =\r\n" and add the newly
1148 // processed line(s) to the output (see comment on $crlf class property)
1149 for ($i = 0; $i < $length; $i++)
1150 {
1151 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001152 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 $ascii = ord($char);
1154
1155 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001156 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001158 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001160 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001162 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
1164
1165 // If we're at the character limit, add the line to the output,
1166 // reset our temp variable, and keep on chuggin'
Andrey Andreeve8bc5f42012-10-10 11:13:59 +03001167 if ((strlen($temp) + strlen($char)) >= 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
1169 $output .= $temp.$escape.$this->crlf;
1170 $temp = '';
1171 }
1172
1173 // Add the character to our temporary line
1174 $temp .= $char;
1175 }
1176
1177 // Add our completed line to the output
1178 $output .= $temp.$this->crlf;
1179 }
1180
1181 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001182 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 }
1184
1185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 /**
1188 * Prep Q Encoding
1189 *
Andrey Andreev925dd902012-10-19 11:06:31 +03001190 * Performs "Q Encoding" on a string for use in email headers.
1191 * It's related but not identical to quoted-printable, so it has its
1192 * own method.
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001194 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001195 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 */
Andrey Andreev925dd902012-10-19 11:06:31 +03001197 protected function _prep_q_encoding($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001199 $str = str_replace(array("\r", "\n"), '', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001200
Andrey Andreev925dd902012-10-19 11:06:31 +03001201 if ($this->charset === 'UTF-8')
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001203 if (MB_ENABLED === TRUE)
1204 {
1205 return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf);
1206 }
1207 elseif (extension_loaded('iconv'))
1208 {
1209 $output = @iconv_mime_encode('', $str,
1210 array(
1211 'scheme' => 'Q',
1212 'line-length' => 76,
1213 'input-charset' => $this->charset,
1214 'output-charset' => $this->charset,
1215 'line-break-chars' => $this->crlf
1216 )
1217 );
1218
1219 // There are reports that iconv_mime_encode() might fail and return FALSE
1220 if ($output !== FALSE)
1221 {
1222 // iconv_mime_encode() will always put a header field name.
1223 // We've passed it an empty one, but it still prepends our
1224 // encoded string with ': ', so we need to strip it.
1225 return substr($output, 2);
1226 }
1227
1228 $chars = iconv_strlen($str, 'UTF-8');
1229 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
1231
Andrey Andreev925dd902012-10-19 11:06:31 +03001232 // We might already have this set for UTF-8
1233 isset($chars) OR $chars = strlen($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001234
Andrey Andreev925dd902012-10-19 11:06:31 +03001235 $output = '=?'.$this->charset.'?Q?';
1236 for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001238 $chr = ($this->charset === 'UTF-8' && $iconv === TRUE)
1239 ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1240 : '='.strtoupper(bin2hex($str[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001241
Andrey Andreev925dd902012-10-19 11:06:31 +03001242 // RFC 2045 sets a limit of 76 characters per line.
1243 // We'll append ?= to the end of each line though.
1244 if ($length + ($l = strlen($chr)) > 74)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001246 $output .= '?='.$this->crlf // EOL
1247 .' =?'.$this->charset.'?Q?'.$chr; // New line
1248 $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 }
Andrey Andreev925dd902012-10-19 11:06:31 +03001250 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
Andrey Andreev925dd902012-10-19 11:06:31 +03001252 $output .= $chr;
1253 $length += $l;
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 }
1256
Andrey Andreev925dd902012-10-19 11:06:31 +03001257 // End the header
1258 return $output.'?=';
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 }
1260
1261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 /**
1264 * Send Email
1265 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001266 * @param bool $auto_clear = TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 * @return bool
1268 */
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001269 public function send($auto_clear = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001271 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
1273 $this->reply_to($this->_headers['From']);
1274 }
1275
Andrey Andreev76f15c92012-03-01 13:05:07 +02001276 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1277 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1278 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
patworkb0707982011-04-08 15:10:05 +02001280 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 return FALSE;
1282 }
1283
1284 $this->_build_headers();
1285
Andrey Andreev76f15c92012-03-01 13:05:07 +02001286 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 {
Alex Bilbieb901e732012-07-30 09:44:57 +01001288 $result = $this->batch_bcc_send();
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001289
Alex Bilbiea87aab32012-07-30 09:50:37 +01001290 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001291 {
1292 $this->clear();
1293 }
1294
Alex Bilbieb901e732012-07-30 09:44:57 +01001295 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 }
1297
1298 $this->_build_message();
Alex Bilbieb901e732012-07-30 09:44:57 +01001299 $result = $this->_spool_email();
Andrey Andreevbdb99992012-07-30 17:38:05 +03001300
Alex Bilbiea87aab32012-07-30 09:50:37 +01001301 if ($result && $auto_clear)
Alex Bilbied7bc8d02012-07-30 09:46:20 +01001302 {
1303 $this->clear();
1304 }
Alex Bilbiea87aab32012-07-30 09:50:37 +01001305
Alex Bilbieb901e732012-07-30 09:44:57 +01001306 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // --------------------------------------------------------------------
1310
1311 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001312 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001314 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001316 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001318 $float = $this->bcc_batch_size - 1;
1319 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 $chunk = array();
1321
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001322 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
1324 if (isset($this->_bcc_array[$i]))
1325 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001326 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 }
1328
Alex Bilbied261b1e2012-06-02 11:12:16 +01001329 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 {
1331 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001332 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001333 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 }
1335
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001336 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $chunk[] = substr($set, 1);
1339 }
1340 }
1341
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001342 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 {
1344 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001345
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001346 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001347
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001348 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
Mickey Wubfc1cad2012-05-31 22:28:40 -07001350 $this->set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 }
1352 else
1353 {
1354 $this->_bcc_array = $bcc;
1355 }
1356
1357 $this->_build_message();
1358 $this->_spool_email();
1359 }
1360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
1363
1364 /**
1365 * Unwrap special elements
1366 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 * @return void
1368 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001369 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 {
Andrey Andreev56454792012-05-17 14:32:19 +03001371 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 }
Barry Mienydd671972010-10-04 16:33:58 +02001373
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 // --------------------------------------------------------------------
1375
1376 /**
1377 * Strip line-breaks via callback
1378 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001379 * @param string $matches
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 * @return string
1381 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001382 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 {
1384 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1385 {
1386 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1387 }
1388
1389 return $matches[1];
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 // --------------------------------------------------------------------
1393
1394 /**
1395 * Spool mail to the mail server
1396 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * @return bool
1398 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001399 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
1401 $this->_unwrap_specials();
1402
Andrey Andreev56454792012-05-17 14:32:19 +03001403 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001404 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
Andrey Andreev56454792012-05-17 14:32:19 +03001406 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001407 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 }
1409
patworkb0707982011-04-08 15:10:05 +02001410 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 return TRUE;
1412 }
Barry Mienydd671972010-10-04 16:33:58 +02001413
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 // --------------------------------------------------------------------
1415
1416 /**
1417 * Send using mail()
1418 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 * @return bool
1420 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001421 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 {
Andrey Andreev8a7078b2012-10-17 10:52:49 +03001423 if (is_array($this->_recipients))
1424 {
1425 $this->_recipients = implode(', ', $this->_recipients);
1426 }
1427
Alex Bilbied261b1e2012-06-02 11:12:16 +01001428 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001430 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 }
1432 else
1433 {
1434 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1435 // we've encountered servers that seem to require it to be in place.
Melounek58dfc082012-06-29 08:43:47 +02001436 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 +00001437 }
1438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 // --------------------------------------------------------------------
1441
1442 /**
1443 * Send using Sendmail
1444 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 * @return bool
1446 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001447 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 {
Melounek58dfc082012-06-29 08:43:47 +02001449 $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 +00001450
Derek Jones4cefaa42009-04-29 19:13:56 +00001451 if ($fp === FALSE OR $fp === NULL)
1452 {
1453 // server probably has popen disabled, so nothing we can do to get a verbose error.
1454 return FALSE;
1455 }
Derek Jones71141ce2010-03-02 16:41:20 -06001456
Derek Jonesc630bcf2008-11-17 21:09:45 +00001457 fputs($fp, $this->_header_str);
1458 fputs($fp, $this->_finalbody);
1459
Barry Mienydd671972010-10-04 16:33:58 +02001460 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001461
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001462 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 {
patworkb0707982011-04-08 15:10:05 +02001464 $this->_set_error_message('lang:email_exit_status', $status);
1465 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 return FALSE;
1467 }
1468
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 return TRUE;
1470 }
Barry Mienydd671972010-10-04 16:33:58 +02001471
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 // --------------------------------------------------------------------
1473
1474 /**
1475 * Send using SMTP
1476 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 * @return bool
1478 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001479 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001481 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
patworkb0707982011-04-08 15:10:05 +02001483 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 return FALSE;
1485 }
1486
Diogo Osório593f7982012-03-02 18:04:17 +00001487 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001488 {
1489 return FALSE;
1490 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001491
1492 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1493
Pascal Kriete14287f32011-02-14 13:39:34 -05001494 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 {
1496 $this->_send_command('to', $val);
1497 }
1498
1499 if (count($this->_cc_array) > 0)
1500 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001501 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001503 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 {
1505 $this->_send_command('to', $val);
1506 }
1507 }
1508 }
1509
1510 if (count($this->_bcc_array) > 0)
1511 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001512 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001514 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
1516 $this->_send_command('to', $val);
1517 }
1518 }
1519 }
1520
1521 $this->_send_command('data');
1522
1523 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001524 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001525
1526 $this->_send_data('.');
1527
1528 $reply = $this->_get_smtp_data();
1529
1530 $this->_set_error_message($reply);
1531
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001532 if (strpos($reply, '250') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 {
patworkb0707982011-04-08 15:10:05 +02001534 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 return FALSE;
1536 }
1537
1538 $this->_send_command('quit');
1539 return TRUE;
1540 }
Barry Mienydd671972010-10-04 16:33:58 +02001541
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 // --------------------------------------------------------------------
1543
1544 /**
1545 * SMTP Connect
1546 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001547 * @return string
1548 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001549 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001551 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001552
Radu Potopbbf04b02011-09-28 13:57:51 +03001553 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001554 $this->smtp_port,
1555 $errno,
1556 $errstr,
1557 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001558
Pascal Kriete14287f32011-02-14 13:39:34 -05001559 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 {
Andrey Andreev56454792012-05-17 14:32:19 +03001561 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 return FALSE;
1563 }
1564
1565 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001566
Alex Bilbied261b1e2012-06-02 11:12:16 +01001567 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001568 {
1569 $this->_send_command('hello');
1570 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001571
Radu Potop4c589ae2011-09-29 10:19:55 +03001572 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001573
Sean Fishere862ddd2011-10-26 22:45:00 -03001574 if ($crypto !== TRUE)
1575 {
1576 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1577 return FALSE;
1578 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001579 }
1580
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 return $this->_send_command('hello');
1582 }
Barry Mienydd671972010-10-04 16:33:58 +02001583
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 // --------------------------------------------------------------------
1585
1586 /**
1587 * Send SMTP command
1588 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 * @param string
1590 * @param string
1591 * @return string
1592 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001593 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
1595 switch ($cmd)
1596 {
1597 case 'hello' :
1598
Andrey Andreev56454792012-05-17 14:32:19 +03001599 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1600 {
1601 $this->_send_data('EHLO '.$this->_get_hostname());
1602 }
1603 else
1604 {
1605 $this->_send_data('HELO '.$this->_get_hostname());
1606 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001607
1608 $resp = 250;
1609 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001610 case 'starttls' :
1611
1612 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001613 $resp = 220;
1614 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 case 'from' :
1616
1617 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 $resp = 250;
1619 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001620 case 'to' :
1621
leandronf7686c122012-03-22 08:07:31 -03001622 if ($this->dsn)
1623 {
leandronfb4552942012-03-21 23:54:26 -03001624 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001625 }
leandronfb4552942012-03-21 23:54:26 -03001626 else
leandronf7686c122012-03-22 08:07:31 -03001627 {
leandronfb4552942012-03-21 23:54:26 -03001628 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001629 }
Andrey Andreev56454792012-05-17 14:32:19 +03001630
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 $resp = 250;
1632 break;
1633 case 'data' :
1634
1635 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 $resp = 354;
1637 break;
1638 case 'quit' :
1639
1640 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 $resp = 221;
1642 break;
1643 }
1644
1645 $reply = $this->_get_smtp_data();
1646
Andrey Andreev56454792012-05-17 14:32:19 +03001647 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001648
Andrey Andreevba261762012-06-11 14:11:35 +03001649 if ( (int) substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
patworkb0707982011-04-08 15:10:05 +02001651 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 return FALSE;
1653 }
1654
Alex Bilbied261b1e2012-06-02 11:12:16 +01001655 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 {
1657 fclose($this->_smtp_connect);
1658 }
1659
1660 return TRUE;
1661 }
Barry Mienydd671972010-10-04 16:33:58 +02001662
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 // --------------------------------------------------------------------
1664
1665 /**
vkeranov7e4356b2012-10-27 18:41:00 +03001666 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 * @return bool
1669 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001670 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
1672 if ( ! $this->_smtp_auth)
1673 {
1674 return TRUE;
1675 }
1676
Alex Bilbied261b1e2012-06-02 11:12:16 +01001677 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
patworkb0707982011-04-08 15:10:05 +02001679 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 return FALSE;
1681 }
1682
1683 $this->_send_data('AUTH LOGIN');
1684
1685 $reply = $this->_get_smtp_data();
1686
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001687 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 {
patworkb0707982011-04-08 15:10:05 +02001689 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 return FALSE;
1691 }
1692
1693 $this->_send_data(base64_encode($this->smtp_user));
1694
1695 $reply = $this->_get_smtp_data();
1696
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001697 if (strpos($reply, '334') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 {
patworkb0707982011-04-08 15:10:05 +02001699 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 return FALSE;
1701 }
1702
1703 $this->_send_data(base64_encode($this->smtp_pass));
1704
1705 $reply = $this->_get_smtp_data();
1706
Andrey Andreev3dad2e72012-06-14 15:10:56 +03001707 if (strpos($reply, '235') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 {
patworkb0707982011-04-08 15:10:05 +02001709 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 return FALSE;
1711 }
1712
1713 return TRUE;
1714 }
Barry Mienydd671972010-10-04 16:33:58 +02001715
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 // --------------------------------------------------------------------
1717
1718 /**
1719 * Send SMTP data
1720 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001721 * @param string $data
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 * @return bool
1723 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001724 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001726 if ( ! fwrite($this->_smtp_connect, $data.$this->newline))
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
patworkb0707982011-04-08 15:10:05 +02001728 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 return FALSE;
1730 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001731
1732 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 }
Barry Mienydd671972010-10-04 16:33:58 +02001734
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 // --------------------------------------------------------------------
1736
1737 /**
1738 * Get SMTP data
1739 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 * @return string
1741 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001742 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 {
Andrey Andreev56454792012-05-17 14:32:19 +03001744 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001745
1746 while ($str = fgets($this->_smtp_connect, 512))
1747 {
1748 $data .= $str;
1749
Alex Bilbied261b1e2012-06-02 11:12:16 +01001750 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
1752 break;
1753 }
1754 }
1755
1756 return $data;
1757 }
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 // --------------------------------------------------------------------
1760
1761 /**
1762 * Get Hostname
1763 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 * @return string
1765 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001766 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001768 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 }
Barry Mienydd671972010-10-04 16:33:58 +02001770
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 // --------------------------------------------------------------------
1772
1773 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 * Get Debug Message
1775 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 * @return string
1777 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001778 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 {
1780 $msg = '';
1781
1782 if (count($this->_debug_msg) > 0)
1783 {
1784 foreach ($this->_debug_msg as $val)
1785 {
1786 $msg .= $val;
1787 }
1788 }
1789
Andrey Andreev59c5b532012-03-01 14:09:51 +02001790 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 }
Barry Mienydd671972010-10-04 16:33:58 +02001792
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 // --------------------------------------------------------------------
1794
1795 /**
1796 * Set Message
1797 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001798 * @param string $msg
1799 * @param string $val = ''
Andrey Andreev081c9462012-03-01 12:58:11 +02001800 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001802 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 {
1804 $CI =& get_instance();
1805 $CI->lang->load('email');
1806
Andrey Andreev76f15c92012-03-01 13:05:07 +02001807 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
Andrey Andreev56454792012-05-17 14:32:19 +03001809 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 }
1811 else
1812 {
Andrey Andreev56454792012-05-17 14:32:19 +03001813 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 }
1815 }
Barry Mienydd671972010-10-04 16:33:58 +02001816
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 // --------------------------------------------------------------------
1818
1819 /**
1820 * Mime Types
1821 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 * @param string
1823 * @return string
1824 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001825 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001827 static $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001828
Andrey Andreev6ef498b2012-06-05 22:01:58 +03001829 $ext = strtolower($ext);
1830
1831 if ( ! is_array($mimes))
1832 {
1833 $mimes =& get_mimes();
1834 }
1835
1836 if (isset($mimes[$ext]))
1837 {
1838 return is_array($mimes[$ext])
1839 ? current($mimes[$ext])
1840 : $mimes[$ext];
1841 }
1842
1843 return 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 }
1845
1846}
Derek Allard2067d1a2008-11-13 22:59:24 +00001847
1848/* End of file Email.php */
Andrey Andreev50988592012-10-08 20:46:04 +03001849/* Location: ./system/libraries/Email.php */