blob: 45866797b133170aa8b2e9ce53aff5ef25a3a39a [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
169 $this->_set_header('User-Agent', $this->useragent);
170 $this->_set_header('Date', $this->_set_date());
171
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 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000191 public function from($from, $name = '')
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));
201 }
202
203 // prepare the display name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100204 if ($name !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 // only use Q encoding if there are characters that would require it
207 if ( ! preg_match('/[\200-\377]/', $name))
208 {
209 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000210 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212 else
213 {
214 $name = $this->_prep_q_encoding($name, TRUE);
215 }
216 }
217
218 $this->_set_header('From', $name.' <'.$from.'>');
219 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500220
Greg Akera769deb2010-11-10 15:47:29 -0600221 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Set Reply-to
228 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @param string
230 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200231 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000233 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200235 if (preg_match('/\<(.*)\>/', $replyto, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200237 $replyto = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 if ($this->validate)
241 {
242 $this->validate_email($this->_str_to_array($replyto));
243 }
244
Alex Bilbied261b1e2012-06-02 11:12:16 +0100245 if ($name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 $name = $replyto;
248 }
249
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200250 if (strncmp($name, '"', 1) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 $name = '"'.$name.'"';
253 }
254
255 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
256 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600257
258 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * Set Recipients
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200267 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000269 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 $to = $this->_str_to_array($to);
272 $to = $this->clean_email($to);
273
274 if ($this->validate)
275 {
276 $this->validate_email($to);
277 }
278
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200279 if ($this->_get_protocol() !== 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200281 $this->_set_header('To', implode(', ', $to));
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 switch ($this->_get_protocol())
285 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200286 case 'smtp':
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000287 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 break;
Andrey Andreev59c5b532012-03-01 14:09:51 +0200289 case 'sendmail':
290 case 'mail':
291 $this->_recipients = implode(', ', $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 break;
293 }
Eric Barnes6113f542010-12-29 13:36:12 -0500294
Greg Akera769deb2010-11-10 15:47:29 -0600295 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Set CC
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200304 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000306 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreev56454792012-05-17 14:32:19 +0300308 $cc = $this->clean_email($this->_str_to_array($cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310 if ($this->validate)
311 {
312 $this->validate_email($cc);
313 }
314
Andrey Andreev59c5b532012-03-01 14:09:51 +0200315 $this->_set_header('Cc', implode(', ', $cc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200317 if ($this->_get_protocol() === 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->_cc_array = $cc;
320 }
Eric Barnes6113f542010-12-29 13:36:12 -0500321
Greg Akera769deb2010-11-10 15:47:29 -0600322 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Set BCC
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string
331 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200332 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000334 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100336 if ($limit !== '' && is_numeric($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->bcc_batch_mode = TRUE;
339 $this->bcc_batch_size = $limit;
340 }
341
Andrey Andreev56454792012-05-17 14:32:19 +0300342 $bcc = $this->clean_email($this->_str_to_array($bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
344 if ($this->validate)
345 {
346 $this->validate_email($bcc);
347 }
348
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200349 if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $this->_bcc_array = $bcc;
352 }
353 else
354 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200355 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Eric Barnes6113f542010-12-29 13:36:12 -0500357
Greg Akera769deb2010-11-10 15:47:29 -0600358 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Set Email Subject
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200367 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000369 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 $subject = $this->_prep_q_encoding($subject);
372 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600373 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Set Body
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200382 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000384 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200386 $this->_body = rtrim(str_replace("\r", '', $body));
diegorivera33c32802011-10-19 02:56:15 -0200387
Andrey Andreevaf728622011-10-20 10:11:59 +0300388 /* strip slashes only if magic quotes is ON
389 if we do it with magic quotes OFF, it strips real, user-inputted chars.
390
391 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
392 it will probably not exist in future versions at all.
393 */
394 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200395 {
diegorivera33c32802011-10-19 02:56:15 -0200396 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200397 }
diegorivera33c32802011-10-19 02:56:15 -0200398
Greg Akera769deb2010-11-10 15:47:29 -0600399 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // --------------------------------------------------------------------
403
404 /**
405 * Assign file attachments
406 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200408 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 */
Matteo Matteic3b36f42012-03-26 10:27:17 +0200410 public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
trit151fc682011-11-23 07:30:06 -0500412 $this->_attach_name[] = array($filename, $newname);
tritd5269982011-11-23 17:22:44 -0500413 $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
Matteo Matteic3b36f42012-03-26 10:27:17 +0200414 $this->_attach_type[] = $mime;
Greg Akera769deb2010-11-10 15:47:29 -0600415 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Add a Header Item
422 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @param string
424 * @param string
425 * @return void
426 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600427 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 $this->_headers[$header] = $value;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
435 * Convert a String to an Array
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string
438 * @return array
439 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600440 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 if ( ! is_array($email))
443 {
Andrey Andreev56454792012-05-17 14:32:19 +0300444 return (strpos($email, ',') !== FALSE)
445 ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
446 : (array) trim($email);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Andrey Andreev56454792012-05-17 14:32:19 +0300448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 return $email;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Set Multipart Value
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200458 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000460 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400462 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600463 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * Set Mailtype
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200472 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000474 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreev56454792012-05-17 14:32:19 +0300476 $this->mailtype = ($type === 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600477 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Set Wordwrap
484 *
Dan Horrigan628e6602011-08-21 09:08:31 -0400485 * @param bool
Andrey Andreev081c9462012-03-01 12:58:11 +0200486 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000488 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400490 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600491 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // --------------------------------------------------------------------
495
496 /**
497 * Set Protocol
498 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200500 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000502 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200504 $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
Greg Akera769deb2010-11-10 15:47:29 -0600505 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // --------------------------------------------------------------------
509
510 /**
511 * Set Priority
512 *
Andrey Andreev081c9462012-03-01 12:58:11 +0200513 * @param int
514 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000516 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200518 $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
Greg Akera769deb2010-11-10 15:47:29 -0600519 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Set Newline Character
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200528 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000530 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200532 $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
Greg Akera769deb2010-11-10 15:47:29 -0600533 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
537
538 /**
539 * Set CRLF
540 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200542 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000544 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200546 $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
Greg Akera769deb2010-11-10 15:47:29 -0600547 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
553 * Set Message Boundary
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @return void
556 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600557 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200559 $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative
560 $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // --------------------------------------------------------------------
564
565 /**
566 * Get the Message ID
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @return string
569 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600570 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200572 $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
Andrey Andreev56454792012-05-17 14:32:19 +0300573 return '<'.uniqid('').strstr($from, '@').'>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
577
578 /**
579 * Get Mail Protocol
580 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @param bool
Andrey Andreev59c5b532012-03-01 14:09:51 +0200582 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600584 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $this->protocol = strtolower($this->protocol);
Andrey Andreev59c5b532012-03-01 14:09:51 +0200587 in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
Derek Allard2067d1a2008-11-13 22:59:24 +0000588
Alex Bilbied261b1e2012-06-02 11:12:16 +0100589 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 return $this->protocol;
592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
596
597 /**
598 * Get Mail Encoding
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param bool
601 * @return string
602 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600603 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200605 in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
Derek Allard2067d1a2008-11-13 22:59:24 +0000606
607 foreach ($this->_base_charsets as $charset)
608 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200609 if (strncmp($charset, $this->charset, strlen($charset)) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 $this->_encoding = '7bit';
612 }
613 }
614
Alex Bilbied261b1e2012-06-02 11:12:16 +0100615 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 return $this->_encoding;
618 }
619 }
620
621 // --------------------------------------------------------------------
622
623 /**
624 * Get content type (text/html/attachment)
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @return string
627 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600628 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
Andrey Andreev56454792012-05-17 14:32:19 +0300630 if ($this->mailtype === 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100632 return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200634 elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return 'plain-attach';
637 }
638 else
639 {
640 return 'plain';
641 }
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // --------------------------------------------------------------------
645
646 /**
647 * Set RFC 822 Date
648 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * @return string
650 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600651 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200653 $timezone = date('Z');
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200654 $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+';
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 $timezone = abs($timezone);
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200656 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657
Andrey Andreev59c5b532012-03-01 14:09:51 +0200658 return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Mime message
665 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return string
667 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600668 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200670 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 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // --------------------------------------------------------------------
674
675 /**
676 * Validate Email Address
677 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @param string
679 * @return bool
680 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000681 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 if ( ! is_array($email))
684 {
patworkb0707982011-04-08 15:10:05 +0200685 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return FALSE;
687 }
688
689 foreach ($email as $val)
690 {
691 if ( ! $this->valid_email($val))
692 {
patworkb0707982011-04-08 15:10:05 +0200693 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return FALSE;
695 }
696 }
697
698 return TRUE;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Email Validation
705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param string
707 * @return bool
708 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000709 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200711 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $address);
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * Clean Extended Email Address: Joe Smith <joe@smith.com>
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @param string
720 * @return string
721 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000722 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
724 if ( ! is_array($email))
725 {
Andrey Andreev56454792012-05-17 14:32:19 +0300726 return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
728
729 $clean_email = array();
730
731 foreach ($email as $addy)
732 {
Andrey Andreev59c5b532012-03-01 14:09:51 +0200733 $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
735
736 return $clean_email;
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 // --------------------------------------------------------------------
740
741 /**
742 * Build alternative plain text message
743 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000744 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * in plain-text headers of HTML-formatted emails.
746 * If the user hasn't specified his own alternative message
747 * it creates one by stripping the HTML
748 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * @return string
750 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600751 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100753 if ($this->alt_message !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 return $this->word_wrap($this->alt_message, '76');
756 }
757
Andrey Andreev56454792012-05-17 14:32:19 +0300758 $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200759 $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000760
761 for ($i = 20; $i >= 3; $i--)
762 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200763 $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200766 return $this->word_wrap($body, 76);
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Word Wrap
773 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +0200775 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @return string
777 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000778 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 // Se the character limit
Alex Bilbied261b1e2012-06-02 11:12:16 +0100781 if ($charlim === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100783 $charlim = ($this->wrapchars === '') ? 76 : $this->wrapchars;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785
786 // Reduce multiple spaces
Andrey Andreev56454792012-05-17 14:32:19 +0300787 $str = preg_replace('| +|', ' ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 // Standardize newlines
790 if (strpos($str, "\r") !== FALSE)
791 {
Andrey Andreevb71e06b2011-12-22 19:22:50 +0200792 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
794
795 // If the current word is surrounded by {unwrap} tags we'll
796 // strip the entire chunk and replace it with a marker.
797 $unwrap = array();
Andrey Andreev56454792012-05-17 14:32:19 +0300798 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200800 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200802 $unwrap[] = $matches[1][$i];
Andrey Andreev56454792012-05-17 14:32:19 +0300803 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805 }
806
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000807 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev56454792012-05-17 14:32:19 +0300809 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $str = wordwrap($str, $charlim, "\n", FALSE);
811
812 // Split the string into individual lines of text and cycle through them
Andrey Andreev56454792012-05-17 14:32:19 +0300813 $output = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 foreach (explode("\n", $str) as $line)
815 {
816 // Is the line within the allowed character count?
817 // If so we'll join it to the output and continue
818 if (strlen($line) <= $charlim)
819 {
820 $output .= $line.$this->newline;
821 continue;
822 }
823
824 $temp = '';
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200825 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 // If the over-length word is a URL we won't wrap it
Andrey Andreev56454792012-05-17 14:32:19 +0300828 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
830 break;
831 }
832
833 // Trim the word down
834 $temp .= substr($line, 0, $charlim-1);
835 $line = substr($line, $charlim-1);
836 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200837 while (strlen($line) > $charlim);
Derek Allard2067d1a2008-11-13 22:59:24 +0000838
839 // If $temp contains data it means we had to split up an over-length
840 // word into smaller chunks so we'll add it back to our current line
Alex Bilbied261b1e2012-06-02 11:12:16 +0100841 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200843 $output .= $temp.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
845
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200846 $output .= $line.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
848
849 // Put our markers back
850 if (count($unwrap) > 0)
851 {
852 foreach ($unwrap as $key => $val)
853 {
Andrey Andreev56454792012-05-17 14:32:19 +0300854 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856 }
857
858 return $output;
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
864 * Build final headers
865 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 * @return string
867 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600868 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
870 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
871 $this->_set_header('X-Mailer', $this->useragent);
872 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
873 $this->_set_header('Message-ID', $this->_get_message_id());
874 $this->_set_header('Mime-Version', '1.0');
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Write Headers as a string
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @return void
883 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600884 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200886 if ($this->protocol === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 $this->_subject = $this->_headers['Subject'];
889 unset($this->_headers['Subject']);
890 }
891
892 reset($this->_headers);
Andrey Andreev56454792012-05-17 14:32:19 +0300893 $this->_header_str = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000894
Pascal Kriete14287f32011-02-14 13:39:34 -0500895 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 $val = trim($val);
898
Alex Bilbied261b1e2012-06-02 11:12:16 +0100899 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreev56454792012-05-17 14:32:19 +0300901 $this->_header_str .= $key.': '.$val.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903 }
904
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200905 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
Derek Jones1d890882009-02-10 20:32:14 +0000907 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Build Final Body and attachments
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @return void
917 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600918 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Andrey Andreev76f15c92012-03-01 13:05:07 +0200920 if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
922 $this->_body = $this->word_wrap($this->_body);
923 }
924
925 $this->_set_boundaries();
926 $this->_write_headers();
927
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200928 $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500929 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000930
931 switch ($this->_get_content_type())
932 {
933 case 'plain' :
934
Andrey Andreev56454792012-05-17 14:32:19 +0300935 $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
936 .'Content-Transfer-Encoding: '.$this->_get_encoding();
Derek Allard2067d1a2008-11-13 22:59:24 +0000937
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200938 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $this->_header_str .= $hdr;
941 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Brandon Jones485d7412010-11-09 16:38:17 -0500943 else
944 {
945 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
946 }
Eric Barnes6113f542010-12-29 13:36:12 -0500947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 return;
949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 case 'html' :
951
952 if ($this->send_multipart === FALSE)
953 {
Andrey Andreev56454792012-05-17 14:32:19 +0300954 $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
955 .'Content-Transfer-Encoding: quoted-printable';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957 else
958 {
Andrey Andreev56454792012-05-17 14:32:19 +0300959 $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Andrey Andreev56454792012-05-17 14:32:19 +0300961 $body .= $this->_get_mime_message().$this->newline.$this->newline
962 .'--'.$this->_alt_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +0000963
Andrey Andreev56454792012-05-17 14:32:19 +0300964 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
965 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
966 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -0500967
Andrey Andreev56454792012-05-17 14:32:19 +0300968 .'Content-Type: text/html; charset='.$this->charset.$this->newline
969 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Eric Barnes6113f542010-12-29 13:36:12 -0500971
Andrey Andreev56454792012-05-17 14:32:19 +0300972 $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500973
974
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200975 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -0500978 }
979 else
980 {
Andrey Andreev56454792012-05-17 14:32:19 +0300981 $this->_finalbody = $hdr.$this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984
985 if ($this->send_multipart !== FALSE)
986 {
Andrey Andreev56454792012-05-17 14:32:19 +0300987 $this->_finalbody .= '--'.$this->_alt_boundary.'--';
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 return;
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 case 'plain-attach' :
993
Andrey Andreev56454792012-05-17 14:32:19 +0300994 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000995
Andrey Andreev1bd3d882011-12-22 15:38:20 +0200996 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
998 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -0500999 }
1000
Andrey Andreev56454792012-05-17 14:32:19 +03001001 $body .= $this->_get_mime_message().$this->newline.$this->newline
1002 .'--'.$this->_atc_boundary.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Andrey Andreev56454792012-05-17 14:32:19 +03001004 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1005 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
Derek Allard2067d1a2008-11-13 22:59:24 +00001006
Andrey Andreev56454792012-05-17 14:32:19 +03001007 .$this->_body.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001008
1009 break;
1010 case 'html-attach' :
1011
Andrey Andreev56454792012-05-17 14:32:19 +03001012 $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'.$this->newline.$this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001013
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001014 if ($this->_get_protocol() === 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
1018
Andrey Andreev56454792012-05-17 14:32:19 +03001019 $body .= $this->_get_mime_message().$this->newline.$this->newline
1020 .'--'.$this->_atc_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001021
Andrey Andreev56454792012-05-17 14:32:19 +03001022 .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
1023 .'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001024
Andrey Andreev56454792012-05-17 14:32:19 +03001025 .'Content-Type: text/plain; charset='.$this->charset.$this->newline
1026 .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1027 .$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001028
Andrey Andreev56454792012-05-17 14:32:19 +03001029 .'Content-Type: text/html; charset='.$this->charset.$this->newline
1030 .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
Brandon Jones485d7412010-11-09 16:38:17 -05001031
Andrey Andreev56454792012-05-17 14:32:19 +03001032 .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1033 .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
1035 break;
1036 }
1037
1038 $attachment = array();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001039 for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
tritbc4ac992011-11-23 07:19:41 -05001041 $filename = $this->_attach_name[$i][0];
Andrey Andreev56454792012-05-17 14:32:19 +03001042 $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $ctype = $this->_attach_type[$i];
Matteo Mattei5688d582012-03-13 19:29:29 +01001044 $file_content = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001045
Alex Bilbied261b1e2012-06-02 11:12:16 +01001046 if ($this->_attach_type[$i] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Matteo Mattei5688d582012-03-13 19:29:29 +01001048 if ( ! file_exists($filename))
1049 {
1050 $this->_set_error_message('lang:email_attachment_missing', $filename);
1051 return FALSE;
1052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Matteo Mattei5688d582012-03-13 19:29:29 +01001054 $file = filesize($filename) +1;
1055
1056 if ( ! $fp = fopen($filename, FOPEN_READ))
1057 {
1058 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1059 return FALSE;
1060 }
1061
Matteo Matteic3b36f42012-03-26 10:27:17 +02001062 $ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Matteo Mattei5688d582012-03-13 19:29:29 +01001063 $file_content = fread($fp, $file);
1064 fclose($fp);
1065 }
1066 else
1067 {
1068 $file_content =& $this->_attach_content[$i];
1069 }
Andrey Andreev56454792012-05-17 14:32:19 +03001070
1071 $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
1072 .'Content-type: '.$ctype.'; '
1073 .'name="'.$basename.'"'.$this->newline
1074 .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
1075 .'Content-Transfer-Encoding: base64'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076
Matteo Mattei5688d582012-03-13 19:29:29 +01001077 $attachment[$z++] = chunk_split(base64_encode($file_content));
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 }
1079
Andrey Andreev56454792012-05-17 14:32:19 +03001080 $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
1081 $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr.$body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 return;
1083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Prep Quoted Printable
1089 *
1090 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1091 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1092 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001094 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @return string
1096 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001097 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 // Set the character limit
1100 // Don't allow over 76, as that will make servers and MUAs barf
1101 // all over quoted-printable data
Alex Bilbied261b1e2012-06-02 11:12:16 +01001102 if ($charlim === '' OR $charlim > 76)
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001104 $charlim = 76;
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 }
1106
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001107 // Reduce multiple spaces & remove nulls
Andrey Andreev56454792012-05-17 14:32:19 +03001108 $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001109
1110 // Standardize newlines
1111 if (strpos($str, "\r") !== FALSE)
1112 {
1113 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1114 }
1115
1116 // We are intentionally wrapping so mail servers will encode characters
1117 // properly and MUAs will behave, so {unwrap} must go!
1118 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 $escape = '=';
1121 $output = '';
1122
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001123 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 $length = strlen($line);
1126 $temp = '';
1127
1128 // Loop through each character in the line to add soft-wrap
1129 // characters at the end of a line " =\r\n" and add the newly
1130 // processed line(s) to the output (see comment on $crlf class property)
1131 for ($i = 0; $i < $length; $i++)
1132 {
1133 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001134 $char = $line[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $ascii = ord($char);
1136
1137 // Convert spaces and tabs but only if it's the end of the line
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001138 if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9))
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001140 $char = $escape.sprintf('%02s', dechex($ascii));
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001142 elseif ($ascii === 61) // encode = signs
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001144 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
1146
1147 // If we're at the character limit, add the line to the output,
1148 // reset our temp variable, and keep on chuggin'
1149 if ((strlen($temp) + strlen($char)) >= $charlim)
1150 {
1151 $output .= $temp.$escape.$this->crlf;
1152 $temp = '';
1153 }
1154
1155 // Add the character to our temporary line
1156 $temp .= $char;
1157 }
1158
1159 // Add our completed line to the output
1160 $output .= $temp.$this->crlf;
1161 }
1162
1163 // get rid of extra CRLF tacked onto the end
Andrey Andreev59c5b532012-03-01 14:09:51 +02001164 return substr($output, 0, strlen($this->crlf) * -1);
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
1166
1167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 /**
1170 * Prep Q Encoding
1171 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001172 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 * but not identical to quoted-printable, so it has its own method
1174 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001175 * @param string
1176 * @param bool set to TRUE for processing From: headers
1177 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001179 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 {
1181 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1182
1183 // Line length must not exceed 76 characters, so we adjust for
1184 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1185 $limit = 75 - 7 - strlen($this->charset);
1186
1187 // these special characters must be converted too
1188 $convert = array('_', '=', '?');
1189
1190 if ($from === TRUE)
1191 {
1192 $convert[] = ',';
1193 $convert[] = ';';
1194 }
1195
1196 $output = '';
1197 $temp = '';
1198
1199 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1200 {
1201 // Grab the next character
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001202 $char = $str[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 $ascii = ord($char);
1204
1205 // convert ALL non-printable ASCII characters and our specials
1206 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1207 {
1208 $char = '='.dechex($ascii);
1209 }
1210
1211 // handle regular spaces a bit more compactly than =20
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001212 if ($ascii === 32)
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
1214 $char = '_';
1215 }
1216
1217 // If we're at the character limit, add the line to the output,
1218 // reset our temp variable, and keep on chuggin'
1219 if ((strlen($temp) + strlen($char)) >= $limit)
1220 {
1221 $output .= $temp.$this->crlf;
1222 $temp = '';
1223 }
1224
1225 // Add the character to our temporary line
1226 $temp .= $char;
1227 }
1228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 // wrap each line with the shebang, charset, and transfer encoding
1230 // the preceding space on successive lines is required for header "folding"
Andrey Andreev56454792012-05-17 14:32:19 +03001231 return trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $output.$temp));
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 }
1233
1234 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 /**
1237 * Send Email
1238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 * @return bool
1240 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001241 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001243 if ($this->_replyto_flag === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 $this->reply_to($this->_headers['From']);
1246 }
1247
Andrey Andreev76f15c92012-03-01 13:05:07 +02001248 if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1249 && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1250 && ! isset($this->_headers['Cc']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 {
patworkb0707982011-04-08 15:10:05 +02001252 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 return FALSE;
1254 }
1255
1256 $this->_build_headers();
1257
Andrey Andreev76f15c92012-03-01 13:05:07 +02001258 if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001260 return $this->batch_bcc_send();
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 }
1262
1263 $this->_build_message();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001264 return $this->_spool_email();
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 // --------------------------------------------------------------------
1268
1269 /**
Andrey Andreev081c9462012-03-01 12:58:11 +02001270 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 *
Andrey Andreev081c9462012-03-01 12:58:11 +02001272 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001274 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001276 $float = $this->bcc_batch_size - 1;
1277 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 $chunk = array();
1279
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001280 for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
1282 if (isset($this->_bcc_array[$i]))
1283 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001284 $set .= ', '.$this->_bcc_array[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 }
1286
Alex Bilbied261b1e2012-06-02 11:12:16 +01001287 if ($i === $float)
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 {
1289 $chunk[] = substr($set, 1);
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001290 $float += $this->bcc_batch_size;
Andrey Andreev59c5b532012-03-01 14:09:51 +02001291 $set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 }
1293
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001294 if ($i === $c-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
1296 $chunk[] = substr($set, 1);
1297 }
1298 }
1299
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001300 for ($i = 0, $c = count($chunk); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 {
1302 unset($this->_headers['Bcc']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001303
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001304 $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
Derek Allard2067d1a2008-11-13 22:59:24 +00001305
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001306 if ($this->protocol !== 'smtp')
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001308 $this->_set_header('Bcc', implode(', ', $bcc));
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 }
1310 else
1311 {
1312 $this->_bcc_array = $bcc;
1313 }
1314
1315 $this->_build_message();
1316 $this->_spool_email();
1317 }
1318 }
Barry Mienydd671972010-10-04 16:33:58 +02001319
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 // --------------------------------------------------------------------
1321
1322 /**
1323 * Unwrap special elements
1324 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 * @return void
1326 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001327 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
Andrey Andreev56454792012-05-17 14:32:19 +03001329 $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 // --------------------------------------------------------------------
1333
1334 /**
1335 * Strip line-breaks via callback
1336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 * @return string
1338 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001339 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 {
1341 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1342 {
1343 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1344 }
1345
1346 return $matches[1];
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 // --------------------------------------------------------------------
1350
1351 /**
1352 * Spool mail to the mail server
1353 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 * @return bool
1355 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001356 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 {
1358 $this->_unwrap_specials();
1359
Andrey Andreev56454792012-05-17 14:32:19 +03001360 $method = '_send_with_'.$this->_get_protocol();
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001361 if ( ! $this->$method())
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 {
Andrey Andreev56454792012-05-17 14:32:19 +03001363 $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001364 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 }
1366
patworkb0707982011-04-08 15:10:05 +02001367 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 return TRUE;
1369 }
Barry Mienydd671972010-10-04 16:33:58 +02001370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 // --------------------------------------------------------------------
1372
1373 /**
1374 * Send using mail()
1375 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 * @return bool
1377 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001378 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001380 if ($this->_safe_mode === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001382 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 }
1384 else
1385 {
1386 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1387 // we've encountered servers that seem to require it to be in place.
Andrey Andreev56454792012-05-17 14:32:19 +03001388 return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['From']));
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 }
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 // --------------------------------------------------------------------
1393
1394 /**
1395 * Send using Sendmail
1396 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * @return bool
1398 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001399 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
Andrey Andreev56454792012-05-17 14:32:19 +03001401 $fp = @popen($this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t', 'w');
Derek Allard2067d1a2008-11-13 22:59:24 +00001402
Derek Jones4cefaa42009-04-29 19:13:56 +00001403 if ($fp === FALSE OR $fp === NULL)
1404 {
1405 // server probably has popen disabled, so nothing we can do to get a verbose error.
1406 return FALSE;
1407 }
Derek Jones71141ce2010-03-02 16:41:20 -06001408
Derek Jonesc630bcf2008-11-17 21:09:45 +00001409 fputs($fp, $this->_header_str);
1410 fputs($fp, $this->_finalbody);
1411
Barry Mienydd671972010-10-04 16:33:58 +02001412 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001413
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001414 if ($status !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 {
patworkb0707982011-04-08 15:10:05 +02001416 $this->_set_error_message('lang:email_exit_status', $status);
1417 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 return FALSE;
1419 }
1420
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 return TRUE;
1422 }
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Send using SMTP
1428 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 * @return bool
1430 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001431 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001433 if ($this->smtp_host === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 {
patworkb0707982011-04-08 15:10:05 +02001435 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 return FALSE;
1437 }
1438
Diogo Osório593f7982012-03-02 18:04:17 +00001439 if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
Diogo Osório7fcc7bd2012-03-02 16:54:52 +00001440 {
1441 return FALSE;
1442 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001443
1444 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1445
Pascal Kriete14287f32011-02-14 13:39:34 -05001446 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 {
1448 $this->_send_command('to', $val);
1449 }
1450
1451 if (count($this->_cc_array) > 0)
1452 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001453 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001455 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 {
1457 $this->_send_command('to', $val);
1458 }
1459 }
1460 }
1461
1462 if (count($this->_bcc_array) > 0)
1463 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001464 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001466 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 {
1468 $this->_send_command('to', $val);
1469 }
1470 }
1471 }
1472
1473 $this->_send_command('data');
1474
1475 // perform dot transformation on any lines that begin with a dot
Andrey Andreev56454792012-05-17 14:32:19 +03001476 $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard2067d1a2008-11-13 22:59:24 +00001477
1478 $this->_send_data('.');
1479
1480 $reply = $this->_get_smtp_data();
1481
1482 $this->_set_error_message($reply);
1483
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001484 if (strncmp($reply, '250', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 {
patworkb0707982011-04-08 15:10:05 +02001486 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 return FALSE;
1488 }
1489
1490 $this->_send_command('quit');
1491 return TRUE;
1492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 // --------------------------------------------------------------------
1495
1496 /**
1497 * SMTP Connect
1498 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 * @param string
1500 * @return string
1501 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001502 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001504 $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001505
Radu Potopbbf04b02011-09-28 13:57:51 +03001506 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Andrey Andreev56454792012-05-17 14:32:19 +03001507 $this->smtp_port,
1508 $errno,
1509 $errstr,
1510 $this->smtp_timeout);
Derek Allard2067d1a2008-11-13 22:59:24 +00001511
Pascal Kriete14287f32011-02-14 13:39:34 -05001512 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Andrey Andreev56454792012-05-17 14:32:19 +03001514 $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 return FALSE;
1516 }
1517
1518 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001519
Alex Bilbied261b1e2012-06-02 11:12:16 +01001520 if ($this->smtp_crypto === 'tls')
Radu Potopbbf04b02011-09-28 13:57:51 +03001521 {
1522 $this->_send_command('hello');
1523 $this->_send_command('starttls');
Phil Sturgeonc00a5a02011-11-22 15:25:32 +00001524
Radu Potop4c589ae2011-09-29 10:19:55 +03001525 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
Radu Potop4c589ae2011-09-29 10:19:55 +03001526
Sean Fishere862ddd2011-10-26 22:45:00 -03001527 if ($crypto !== TRUE)
1528 {
1529 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1530 return FALSE;
1531 }
Radu Potopbbf04b02011-09-28 13:57:51 +03001532 }
1533
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 return $this->_send_command('hello');
1535 }
Barry Mienydd671972010-10-04 16:33:58 +02001536
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 // --------------------------------------------------------------------
1538
1539 /**
1540 * Send SMTP command
1541 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 * @param string
1543 * @param string
1544 * @return string
1545 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001546 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001547 {
1548 switch ($cmd)
1549 {
1550 case 'hello' :
1551
Andrey Andreev56454792012-05-17 14:32:19 +03001552 if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
1553 {
1554 $this->_send_data('EHLO '.$this->_get_hostname());
1555 }
1556 else
1557 {
1558 $this->_send_data('HELO '.$this->_get_hostname());
1559 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001560
1561 $resp = 250;
1562 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001563 case 'starttls' :
1564
1565 $this->_send_data('STARTTLS');
Radu Potopbbf04b02011-09-28 13:57:51 +03001566 $resp = 220;
1567 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 case 'from' :
1569
1570 $this->_send_data('MAIL FROM:<'.$data.'>');
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 $resp = 250;
1572 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001573 case 'to' :
1574
leandronf7686c122012-03-22 08:07:31 -03001575 if ($this->dsn)
1576 {
leandronfb4552942012-03-21 23:54:26 -03001577 $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
leandronf7686c122012-03-22 08:07:31 -03001578 }
leandronfb4552942012-03-21 23:54:26 -03001579 else
leandronf7686c122012-03-22 08:07:31 -03001580 {
leandronfb4552942012-03-21 23:54:26 -03001581 $this->_send_data('RCPT TO:<'.$data.'>');
leandronf7686c122012-03-22 08:07:31 -03001582 }
Andrey Andreev56454792012-05-17 14:32:19 +03001583
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 $resp = 250;
1585 break;
1586 case 'data' :
1587
1588 $this->_send_data('DATA');
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 $resp = 354;
1590 break;
1591 case 'quit' :
1592
1593 $this->_send_data('QUIT');
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 $resp = 221;
1595 break;
1596 }
1597
1598 $reply = $this->_get_smtp_data();
1599
Andrey Andreev56454792012-05-17 14:32:19 +03001600 $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001601
Alex Bilbied261b1e2012-06-02 11:12:16 +01001602 if (substr($reply, 0, 3) !== $resp)
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
patworkb0707982011-04-08 15:10:05 +02001604 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 return FALSE;
1606 }
1607
Alex Bilbied261b1e2012-06-02 11:12:16 +01001608 if ($cmd === 'quit')
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 {
1610 fclose($this->_smtp_connect);
1611 }
1612
1613 return TRUE;
1614 }
Barry Mienydd671972010-10-04 16:33:58 +02001615
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 // --------------------------------------------------------------------
1617
1618 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001619 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 * @return bool
1622 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001623 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 {
1625 if ( ! $this->_smtp_auth)
1626 {
1627 return TRUE;
1628 }
1629
Alex Bilbied261b1e2012-06-02 11:12:16 +01001630 if ($this->smtp_user === '' && $this->smtp_pass === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 {
patworkb0707982011-04-08 15:10:05 +02001632 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 return FALSE;
1634 }
1635
1636 $this->_send_data('AUTH LOGIN');
1637
1638 $reply = $this->_get_smtp_data();
1639
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001640 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 {
patworkb0707982011-04-08 15:10:05 +02001642 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 return FALSE;
1644 }
1645
1646 $this->_send_data(base64_encode($this->smtp_user));
1647
1648 $reply = $this->_get_smtp_data();
1649
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001650 if (strncmp($reply, '334', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
patworkb0707982011-04-08 15:10:05 +02001652 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 return FALSE;
1654 }
1655
1656 $this->_send_data(base64_encode($this->smtp_pass));
1657
1658 $reply = $this->_get_smtp_data();
1659
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001660 if (strncmp($reply, '235', 3) !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
patworkb0707982011-04-08 15:10:05 +02001662 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 return FALSE;
1664 }
1665
1666 return TRUE;
1667 }
Barry Mienydd671972010-10-04 16:33:58 +02001668
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 // --------------------------------------------------------------------
1670
1671 /**
1672 * Send SMTP data
1673 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 * @return bool
1675 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001676 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 {
1678 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1679 {
patworkb0707982011-04-08 15:10:05 +02001680 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 return FALSE;
1682 }
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001683
1684 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 }
Barry Mienydd671972010-10-04 16:33:58 +02001686
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 // --------------------------------------------------------------------
1688
1689 /**
1690 * Get SMTP data
1691 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 * @return string
1693 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001694 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 {
Andrey Andreev56454792012-05-17 14:32:19 +03001696 $data = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001697
1698 while ($str = fgets($this->_smtp_connect, 512))
1699 {
1700 $data .= $str;
1701
Alex Bilbied261b1e2012-06-02 11:12:16 +01001702 if ($str[3] === ' ')
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 {
1704 break;
1705 }
1706 }
1707
1708 return $data;
1709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 // --------------------------------------------------------------------
1712
1713 /**
1714 * Get Hostname
1715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 * @return string
1717 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001718 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001720 return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 }
Barry Mienydd671972010-10-04 16:33:58 +02001722
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 // --------------------------------------------------------------------
1724
1725 /**
1726 * Get IP
1727 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 * @return string
1729 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001730 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
1732 if ($this->_IP !== FALSE)
1733 {
1734 return $this->_IP;
1735 }
1736
Andrey Andreev76f15c92012-03-01 13:05:07 +02001737 $cip = ( ! empty($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1738 $rip = ( ! empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001739 if ($cip) $this->_IP = $cip;
1740 elseif ($rip) $this->_IP = $rip;
1741 else
1742 {
Andrey Andreev76f15c92012-03-01 13:05:07 +02001743 $fip = ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
Andrey Andreev1bd3d882011-12-22 15:38:20 +02001744 if ($fip)
1745 {
1746 $this->_IP = $fip;
1747 }
1748 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001749
Robin Sowell76b369e2010-03-19 11:15:28 -04001750 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
1752 $x = explode(',', $this->_IP);
1753 $this->_IP = end($x);
1754 }
1755
Andrey Andreev59c5b532012-03-01 14:09:51 +02001756 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 +00001757 {
1758 $this->_IP = '0.0.0.0';
1759 }
1760
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 return $this->_IP;
1762 }
Barry Mienydd671972010-10-04 16:33:58 +02001763
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 // --------------------------------------------------------------------
1765
1766 /**
1767 * Get Debug Message
1768 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 * @return string
1770 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001771 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 {
1773 $msg = '';
1774
1775 if (count($this->_debug_msg) > 0)
1776 {
1777 foreach ($this->_debug_msg as $val)
1778 {
1779 $msg .= $val;
1780 }
1781 }
1782
Andrey Andreev59c5b532012-03-01 14:09:51 +02001783 return $msg.'<pre>'.$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 }
Barry Mienydd671972010-10-04 16:33:58 +02001785
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 // --------------------------------------------------------------------
1787
1788 /**
1789 * Set Message
1790 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 * @param string
Andrey Andreev081c9462012-03-01 12:58:11 +02001792 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001794 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 {
1796 $CI =& get_instance();
1797 $CI->lang->load('email');
1798
Andrey Andreev76f15c92012-03-01 13:05:07 +02001799 if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 {
Andrey Andreev56454792012-05-17 14:32:19 +03001801 $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 }
1803 else
1804 {
Andrey Andreev56454792012-05-17 14:32:19 +03001805 $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 }
1807 }
Barry Mienydd671972010-10-04 16:33:58 +02001808
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 // --------------------------------------------------------------------
1810
1811 /**
1812 * Mime Types
1813 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 * @param string
1815 * @return string
1816 */
Andrey Andreev59c5b532012-03-01 14:09:51 +02001817 protected function _mime_types($ext = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
Andrey Andreev59c5b532012-03-01 14:09:51 +02001819 $mimes = array(
1820 'hqx' => 'application/mac-binhex40',
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 'cpt' => 'application/mac-compactpro',
1822 'doc' => 'application/msword',
1823 'bin' => 'application/macbinary',
1824 'dms' => 'application/octet-stream',
1825 'lha' => 'application/octet-stream',
1826 'lzh' => 'application/octet-stream',
1827 'exe' => 'application/octet-stream',
1828 'class' => 'application/octet-stream',
1829 'psd' => 'application/octet-stream',
1830 'so' => 'application/octet-stream',
1831 'sea' => 'application/octet-stream',
1832 'dll' => 'application/octet-stream',
1833 'oda' => 'application/oda',
1834 'pdf' => 'application/pdf',
1835 'ai' => 'application/postscript',
1836 'eps' => 'application/postscript',
1837 'ps' => 'application/postscript',
1838 'smi' => 'application/smil',
1839 'smil' => 'application/smil',
1840 'mif' => 'application/vnd.mif',
1841 'xls' => 'application/vnd.ms-excel',
1842 'ppt' => 'application/vnd.ms-powerpoint',
1843 'wbxml' => 'application/vnd.wap.wbxml',
1844 'wmlc' => 'application/vnd.wap.wmlc',
1845 'dcr' => 'application/x-director',
1846 'dir' => 'application/x-director',
1847 'dxr' => 'application/x-director',
1848 'dvi' => 'application/x-dvi',
1849 'gtar' => 'application/x-gtar',
1850 'php' => 'application/x-httpd-php',
1851 'php4' => 'application/x-httpd-php',
1852 'php3' => 'application/x-httpd-php',
1853 'phtml' => 'application/x-httpd-php',
1854 'phps' => 'application/x-httpd-php-source',
1855 'js' => 'application/x-javascript',
1856 'swf' => 'application/x-shockwave-flash',
1857 'sit' => 'application/x-stuffit',
1858 'tar' => 'application/x-tar',
1859 'tgz' => 'application/x-tar',
1860 'xhtml' => 'application/xhtml+xml',
1861 'xht' => 'application/xhtml+xml',
1862 'zip' => 'application/zip',
1863 'mid' => 'audio/midi',
1864 'midi' => 'audio/midi',
1865 'mpga' => 'audio/mpeg',
1866 'mp2' => 'audio/mpeg',
1867 'mp3' => 'audio/mpeg',
1868 'aif' => 'audio/x-aiff',
1869 'aiff' => 'audio/x-aiff',
1870 'aifc' => 'audio/x-aiff',
1871 'ram' => 'audio/x-pn-realaudio',
1872 'rm' => 'audio/x-pn-realaudio',
1873 'rpm' => 'audio/x-pn-realaudio-plugin',
1874 'ra' => 'audio/x-realaudio',
1875 'rv' => 'video/vnd.rn-realvideo',
1876 'wav' => 'audio/x-wav',
1877 'bmp' => 'image/bmp',
1878 'gif' => 'image/gif',
1879 'jpeg' => 'image/jpeg',
1880 'jpg' => 'image/jpeg',
1881 'jpe' => 'image/jpeg',
1882 'png' => 'image/png',
1883 'tiff' => 'image/tiff',
1884 'tif' => 'image/tiff',
1885 'css' => 'text/css',
Kevin Kingd3c94472012-04-12 13:46:20 -03001886 'ics' => 'text/calendar',
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 'html' => 'text/html',
1888 'htm' => 'text/html',
1889 'shtml' => 'text/html',
1890 'txt' => 'text/plain',
1891 'text' => 'text/plain',
1892 'log' => 'text/plain',
1893 'rtx' => 'text/richtext',
1894 'rtf' => 'text/rtf',
1895 'xml' => 'text/xml',
1896 'xsl' => 'text/xml',
1897 'mpeg' => 'video/mpeg',
1898 'mpg' => 'video/mpeg',
1899 'mpe' => 'video/mpeg',
1900 'qt' => 'video/quicktime',
1901 'mov' => 'video/quicktime',
1902 'avi' => 'video/x-msvideo',
1903 'movie' => 'video/x-sgi-movie',
1904 'doc' => 'application/msword',
1905 'word' => 'application/msword',
1906 'xl' => 'application/excel',
1907 'eml' => 'message/rfc822'
1908 );
1909
Andrey Andreev59c5b532012-03-01 14:09:51 +02001910 return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 }
1912
1913}
Derek Allard2067d1a2008-11-13 22:59:24 +00001914
1915/* End of file Email.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001916/* Location: ./system/libraries/Email.php */