Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Email Class |
| 32 | * |
| 33 | * Permits email to be sent using Mail, Sendmail, or SMTP. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/email.html |
| 40 | */ |
trit | a15dd4f | 2011-11-23 07:40:05 -0500 | [diff] [blame] | 41 | class CI_Email { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 43 | public $useragent = "CodeIgniter"; |
| 44 | public $mailpath = "/usr/sbin/sendmail"; // Sendmail path |
| 45 | public $protocol = "mail"; // mail/sendmail/smtp |
| 46 | public $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net |
| 47 | public $smtp_user = ""; // SMTP Username |
| 48 | public $smtp_pass = ""; // SMTP Password |
| 49 | public $smtp_port = "25"; // SMTP Port |
| 50 | public $smtp_timeout = 5; // SMTP Timeout in seconds |
| 51 | public $smtp_crypto = ""; // SMTP Encryption. Can be null, tls or ssl. |
| 52 | public $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off |
| 53 | public $wrapchars = "76"; // Number of characters to wrap at. |
| 54 | public $mailtype = "text"; // text/html Defines email formatting |
| 55 | public $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii |
| 56 | public $multipart = "mixed"; // "mixed" (in the body) or "related" (separate) |
| 57 | public $alt_message = ''; // Alternative message for HTML emails |
| 58 | public $validate = FALSE; // TRUE/FALSE. Enables email validation |
| 59 | public $priority = "3"; // Default priority (1 - 5) |
| 60 | public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822) |
| 61 | public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | // even on the receiving end think they need to muck with CRLFs, so using "\n", while |
| 63 | // distasteful, is the only thing that seems to work for all environments. |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 64 | public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo. |
| 65 | public $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature |
| 66 | public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame^] | 67 | |
| 68 | protected $_safe_mode = FALSE; |
| 69 | protected $_subject = ''; |
| 70 | protected $_body = ''; |
| 71 | protected $_finalbody = ''; |
| 72 | protected $_alt_boundary = ''; |
| 73 | protected $_atc_boundary = ''; |
| 74 | protected $_header_str = ''; |
| 75 | protected $_smtp_connect = ''; |
| 76 | protected $_encoding = '8bit'; |
| 77 | protected $_IP = FALSE; |
| 78 | protected $_smtp_auth = FALSE; |
| 79 | protected $_replyto_flag = FALSE; |
| 80 | protected $_debug_msg = array(); |
| 81 | protected $_recipients = array(); |
| 82 | protected $_cc_array = array(); |
| 83 | protected $_bcc_array = array(); |
| 84 | protected $_headers = array(); |
| 85 | protected $_attach_name = array(); |
| 86 | protected $_attach_type = array(); |
| 87 | protected $_attach_disp = array(); |
| 88 | protected $_protocols = array('mail', 'sendmail', 'smtp'); |
| 89 | protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix) |
| 90 | protected $_bit_depths = array('7bit', '8bit'); |
| 91 | protected $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | /** |
| 95 | * Constructor - Sets Email Preferences |
| 96 | * |
| 97 | * The constructor can be passed an array of config values |
| 98 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 99 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
| 101 | if (count($config) > 0) |
| 102 | { |
| 103 | $this->initialize($config); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 108 | $this->_safe_mode = (bool) @ini_get("safe_mode"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | log_message('debug', "Email Class Initialized"); |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | /** |
| 117 | * Initialize preferences |
| 118 | * |
| 119 | * @access public |
| 120 | * @param array |
| 121 | * @return void |
| 122 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 123 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | foreach ($config as $key => $val) |
| 126 | { |
| 127 | if (isset($this->$key)) |
| 128 | { |
| 129 | $method = 'set_'.$key; |
| 130 | |
| 131 | if (method_exists($this, $method)) |
| 132 | { |
| 133 | $this->$method($val); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | $this->$key = $val; |
| 138 | } |
| 139 | } |
| 140 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 141 | $this->clear(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | |
| 143 | $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 144 | $this->_safe_mode = (bool) @ini_get("safe_mode"); |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 145 | |
| 146 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
| 152 | * Initialize the Email Data |
| 153 | * |
| 154 | * @access public |
Bo-Yi Wu | 83320eb | 2011-09-15 13:28:02 +0800 | [diff] [blame] | 155 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | * @return void |
| 157 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 158 | public function clear($clear_attachments = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | { |
| 160 | $this->_subject = ""; |
| 161 | $this->_body = ""; |
| 162 | $this->_finalbody = ""; |
| 163 | $this->_header_str = ""; |
| 164 | $this->_replyto_flag = FALSE; |
| 165 | $this->_recipients = array(); |
Derek Jones | d160635 | 2010-09-01 11:16:07 -0500 | [diff] [blame] | 166 | $this->_cc_array = array(); |
| 167 | $this->_bcc_array = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | $this->_headers = array(); |
| 169 | $this->_debug_msg = array(); |
| 170 | |
| 171 | $this->_set_header('User-Agent', $this->useragent); |
| 172 | $this->_set_header('Date', $this->_set_date()); |
| 173 | |
| 174 | if ($clear_attachments !== FALSE) |
| 175 | { |
| 176 | $this->_attach_name = array(); |
| 177 | $this->_attach_type = array(); |
| 178 | $this->_attach_disp = array(); |
| 179 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 180 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 181 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Set FROM |
| 188 | * |
| 189 | * @access public |
| 190 | * @param string |
| 191 | * @param string |
| 192 | * @return void |
| 193 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 194 | public function from($from, $name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | { |
| 196 | if (preg_match( '/\<(.*)\>/', $from, $match)) |
| 197 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 198 | $from = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if ($this->validate) |
| 202 | { |
| 203 | $this->validate_email($this->_str_to_array($from)); |
| 204 | } |
| 205 | |
| 206 | // prepare the display name |
| 207 | if ($name != '') |
| 208 | { |
| 209 | // only use Q encoding if there are characters that would require it |
| 210 | if ( ! preg_match('/[\200-\377]/', $name)) |
| 211 | { |
| 212 | // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes |
Derek Jones | c630bcf | 2008-11-17 21:09:45 +0000 | [diff] [blame] | 213 | $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
| 217 | $name = $this->_prep_q_encoding($name, TRUE); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $this->_set_header('From', $name.' <'.$from.'>'); |
| 222 | $this->_set_header('Return-Path', '<'.$from.'>'); |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 223 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 224 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Set Reply-to |
| 231 | * |
| 232 | * @access public |
| 233 | * @param string |
| 234 | * @param string |
| 235 | * @return void |
| 236 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 237 | public function reply_to($replyto, $name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | { |
| 239 | if (preg_match( '/\<(.*)\>/', $replyto, $match)) |
| 240 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 241 | $replyto = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if ($this->validate) |
| 245 | { |
| 246 | $this->validate_email($this->_str_to_array($replyto)); |
| 247 | } |
| 248 | |
| 249 | if ($name == '') |
| 250 | { |
| 251 | $name = $replyto; |
| 252 | } |
| 253 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 254 | if (strncmp($name, '"', 1) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
| 256 | $name = '"'.$name.'"'; |
| 257 | } |
| 258 | |
| 259 | $this->_set_header('Reply-To', $name.' <'.$replyto.'>'); |
| 260 | $this->_replyto_flag = TRUE; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 261 | |
| 262 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Set Recipients |
| 269 | * |
| 270 | * @access public |
| 271 | * @param string |
| 272 | * @return void |
| 273 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 274 | public function to($to) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
| 276 | $to = $this->_str_to_array($to); |
| 277 | $to = $this->clean_email($to); |
| 278 | |
| 279 | if ($this->validate) |
| 280 | { |
| 281 | $this->validate_email($to); |
| 282 | } |
| 283 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 284 | if ($this->_get_protocol() !== 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | $this->_set_header('To', implode(", ", $to)); |
| 287 | } |
| 288 | |
| 289 | switch ($this->_get_protocol()) |
| 290 | { |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 291 | case 'smtp' : |
| 292 | $this->_recipients = $to; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | break; |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 294 | case 'sendmail' : |
| 295 | case 'mail' : |
| 296 | $this->_recipients = implode(", ", $to); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 299 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 300 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Set CC |
| 307 | * |
| 308 | * @access public |
| 309 | * @param string |
| 310 | * @return void |
| 311 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 312 | public function cc($cc) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | { |
| 314 | $cc = $this->_str_to_array($cc); |
| 315 | $cc = $this->clean_email($cc); |
| 316 | |
| 317 | if ($this->validate) |
| 318 | { |
| 319 | $this->validate_email($cc); |
| 320 | } |
| 321 | |
| 322 | $this->_set_header('Cc', implode(", ", $cc)); |
| 323 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 324 | if ($this->_get_protocol() === 'smtp') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | { |
| 326 | $this->_cc_array = $cc; |
| 327 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 328 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 329 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * Set BCC |
| 336 | * |
| 337 | * @access public |
| 338 | * @param string |
| 339 | * @param string |
| 340 | * @return void |
| 341 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 342 | public function bcc($bcc, $limit = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | { |
| 344 | if ($limit != '' && is_numeric($limit)) |
| 345 | { |
| 346 | $this->bcc_batch_mode = TRUE; |
| 347 | $this->bcc_batch_size = $limit; |
| 348 | } |
| 349 | |
| 350 | $bcc = $this->_str_to_array($bcc); |
| 351 | $bcc = $this->clean_email($bcc); |
| 352 | |
| 353 | if ($this->validate) |
| 354 | { |
| 355 | $this->validate_email($bcc); |
| 356 | } |
| 357 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 358 | if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | { |
| 360 | $this->_bcc_array = $bcc; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | $this->_set_header('Bcc', implode(", ", $bcc)); |
| 365 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 366 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 367 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | // -------------------------------------------------------------------- |
| 371 | |
| 372 | /** |
| 373 | * Set Email Subject |
| 374 | * |
| 375 | * @access public |
| 376 | * @param string |
| 377 | * @return void |
| 378 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 379 | public function subject($subject) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | { |
| 381 | $subject = $this->_prep_q_encoding($subject); |
| 382 | $this->_set_header('Subject', $subject); |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 383 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | // -------------------------------------------------------------------- |
| 387 | |
| 388 | /** |
| 389 | * Set Body |
| 390 | * |
| 391 | * @access public |
| 392 | * @param string |
| 393 | * @return void |
| 394 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 395 | public function message($body) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 397 | $this->_body = rtrim(str_replace("\r", "", $body)); |
| 398 | |
Andrey Andreev | af72862 | 2011-10-20 10:11:59 +0300 | [diff] [blame] | 399 | /* strip slashes only if magic quotes is ON |
| 400 | if we do it with magic quotes OFF, it strips real, user-inputted chars. |
| 401 | |
| 402 | NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 403 | it will probably not exist in future versions at all. |
| 404 | */ |
| 405 | if ( ! is_php('5.4') && get_magic_quotes_gpc()) |
diegorivera | 9fca615 | 2011-10-19 11:18:45 -0200 | [diff] [blame] | 406 | { |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 407 | $this->_body = stripslashes($this->_body); |
diegorivera | 9fca615 | 2011-10-19 11:18:45 -0200 | [diff] [blame] | 408 | } |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 409 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 410 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
| 416 | * Assign file attachments |
| 417 | * |
| 418 | * @access public |
| 419 | * @param string |
| 420 | * @return void |
| 421 | */ |
trit | 10eb14d | 2011-11-23 15:48:21 -0500 | [diff] [blame] | 422 | public function attach($filename, $disposition = '', $newname = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
trit | 151fc68 | 2011-11-23 07:30:06 -0500 | [diff] [blame] | 424 | $this->_attach_name[] = array($filename, $newname); |
Phil Sturgeon | 0aaf42b | 2011-08-10 08:06:37 -0600 | [diff] [blame] | 425 | $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION)); |
trit | d526998 | 2011-11-23 17:22:44 -0500 | [diff] [blame] | 426 | $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 427 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // -------------------------------------------------------------------- |
| 431 | |
| 432 | /** |
| 433 | * Add a Header Item |
| 434 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 435 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | * @param string |
| 437 | * @param string |
| 438 | * @return void |
| 439 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 440 | protected function _set_header($header, $value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
| 442 | $this->_headers[$header] = $value; |
| 443 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | // -------------------------------------------------------------------- |
| 446 | |
| 447 | /** |
| 448 | * Convert a String to an Array |
| 449 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 450 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | * @param string |
| 452 | * @return array |
| 453 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 454 | protected function _str_to_array($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | { |
| 456 | if ( ! is_array($email)) |
| 457 | { |
| 458 | if (strpos($email, ',') !== FALSE) |
| 459 | { |
| 460 | $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | $email = trim($email); |
| 465 | settype($email, "array"); |
| 466 | } |
| 467 | } |
| 468 | return $email; |
| 469 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | // -------------------------------------------------------------------- |
| 472 | |
| 473 | /** |
| 474 | * Set Multipart Value |
| 475 | * |
| 476 | * @access public |
| 477 | * @param string |
| 478 | * @return void |
| 479 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 480 | public function set_alt_message($str = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | { |
Dan Horrigan | d0ddeaf | 2011-08-21 09:07:27 -0400 | [diff] [blame] | 482 | $this->alt_message = (string) $str; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 483 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | // -------------------------------------------------------------------- |
| 487 | |
| 488 | /** |
| 489 | * Set Mailtype |
| 490 | * |
| 491 | * @access public |
| 492 | * @param string |
| 493 | * @return void |
| 494 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 495 | public function set_mailtype($type = 'text') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | { |
| 497 | $this->mailtype = ($type == 'html') ? 'html' : 'text'; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 498 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | // -------------------------------------------------------------------- |
| 502 | |
| 503 | /** |
| 504 | * Set Wordwrap |
| 505 | * |
| 506 | * @access public |
Dan Horrigan | 628e660 | 2011-08-21 09:08:31 -0400 | [diff] [blame] | 507 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | * @return void |
| 509 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 510 | public function set_wordwrap($wordwrap = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | { |
Dan Horrigan | 628e660 | 2011-08-21 09:08:31 -0400 | [diff] [blame] | 512 | $this->wordwrap = (bool) $wordwrap; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 513 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | // -------------------------------------------------------------------- |
| 517 | |
| 518 | /** |
| 519 | * Set Protocol |
| 520 | * |
| 521 | * @access public |
| 522 | * @param string |
| 523 | * @return void |
| 524 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 525 | public function set_protocol($protocol = 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | { |
| 527 | $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol); |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 528 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 530 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | // -------------------------------------------------------------------- |
| 532 | |
| 533 | /** |
| 534 | * Set Priority |
| 535 | * |
| 536 | * @access public |
| 537 | * @param integer |
| 538 | * @return void |
| 539 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 540 | public function set_priority($n = 3) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 542 | if ( ! is_numeric($n) OR $n < 1 OR $n > 5) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | { |
| 544 | $this->priority = 3; |
| 545 | return; |
| 546 | } |
| 547 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 548 | $this->priority = (int) $n; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 549 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | // -------------------------------------------------------------------- |
| 553 | |
| 554 | /** |
| 555 | * Set Newline Character |
| 556 | * |
| 557 | * @access public |
| 558 | * @param string |
| 559 | * @return void |
| 560 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 561 | public function set_newline($newline = "\n") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 563 | $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n"; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 564 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 566 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | // -------------------------------------------------------------------- |
| 568 | |
| 569 | /** |
| 570 | * Set CRLF |
| 571 | * |
| 572 | * @access public |
| 573 | * @param string |
| 574 | * @return void |
| 575 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 576 | public function set_crlf($crlf = "\n") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 578 | $this->crlf = ($crlf !== "\n" AND $crlf !== "\r\n" AND $crlf !== "\r") ? "\n" : $crlf; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 579 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | // -------------------------------------------------------------------- |
| 583 | |
| 584 | /** |
| 585 | * Set Message Boundary |
| 586 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 587 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | * @return void |
| 589 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 590 | protected function _set_boundaries() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
| 592 | $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative |
| 593 | $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary |
| 594 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 595 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | // -------------------------------------------------------------------- |
| 597 | |
| 598 | /** |
| 599 | * Get the Message ID |
| 600 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 601 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | * @return string |
| 603 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 604 | protected function _get_message_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 606 | $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 608 | return "<".uniqid('').strstr($from, '@').">"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 609 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 610 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | // -------------------------------------------------------------------- |
| 612 | |
| 613 | /** |
| 614 | * Get Mail Protocol |
| 615 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 616 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | * @param bool |
| 618 | * @return string |
| 619 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 620 | protected function _get_protocol($return = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | { |
| 622 | $this->protocol = strtolower($this->protocol); |
| 623 | $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol; |
| 624 | |
| 625 | if ($return == TRUE) |
| 626 | { |
| 627 | return $this->protocol; |
| 628 | } |
| 629 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 630 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | // -------------------------------------------------------------------- |
| 632 | |
| 633 | /** |
| 634 | * Get Mail Encoding |
| 635 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 636 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | * @param bool |
| 638 | * @return string |
| 639 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 640 | protected function _get_encoding($return = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | { |
| 642 | $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding; |
| 643 | |
| 644 | foreach ($this->_base_charsets as $charset) |
| 645 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 646 | if (strncmp($charset, $this->charset, strlen($charset)) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | { |
| 648 | $this->_encoding = '7bit'; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if ($return == TRUE) |
| 653 | { |
| 654 | return $this->_encoding; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // -------------------------------------------------------------------- |
| 659 | |
| 660 | /** |
| 661 | * Get content type (text/html/attachment) |
| 662 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 663 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 664 | * @return string |
| 665 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 666 | protected function _get_content_type() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 668 | if ($this->mailtype === 'html' && count($this->_attach_name) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | { |
| 670 | return 'html'; |
| 671 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 672 | elseif ($this->mailtype === 'html' && count($this->_attach_name) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | { |
| 674 | return 'html-attach'; |
| 675 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 676 | elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | { |
| 678 | return 'plain-attach'; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | return 'plain'; |
| 683 | } |
| 684 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 685 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | // -------------------------------------------------------------------- |
| 687 | |
| 688 | /** |
| 689 | * Set RFC 822 Date |
| 690 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 691 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | * @return string |
| 693 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 694 | protected function _set_date() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 695 | { |
| 696 | $timezone = date("Z"); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 697 | $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | $timezone = abs($timezone); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 699 | $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | |
| 701 | return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone); |
| 702 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | // -------------------------------------------------------------------- |
| 705 | |
| 706 | /** |
| 707 | * Mime message |
| 708 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 709 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 710 | * @return string |
| 711 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 712 | protected function _get_mime_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 713 | { |
| 714 | return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format."; |
| 715 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 716 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 717 | // -------------------------------------------------------------------- |
| 718 | |
| 719 | /** |
| 720 | * Validate Email Address |
| 721 | * |
| 722 | * @access public |
| 723 | * @param string |
| 724 | * @return bool |
| 725 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 726 | public function validate_email($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 727 | { |
| 728 | if ( ! is_array($email)) |
| 729 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 730 | $this->_set_error_message('lang:email_must_be_array'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | return FALSE; |
| 732 | } |
| 733 | |
| 734 | foreach ($email as $val) |
| 735 | { |
| 736 | if ( ! $this->valid_email($val)) |
| 737 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 738 | $this->_set_error_message('lang:email_invalid_address', $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | return FALSE; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return TRUE; |
| 744 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 745 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | // -------------------------------------------------------------------- |
| 747 | |
| 748 | /** |
| 749 | * Email Validation |
| 750 | * |
| 751 | * @access public |
| 752 | * @param string |
| 753 | * @return bool |
| 754 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 755 | public function valid_email($address) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 756 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 757 | return (bool) preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 758 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 759 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 760 | // -------------------------------------------------------------------- |
| 761 | |
| 762 | /** |
| 763 | * Clean Extended Email Address: Joe Smith <joe@smith.com> |
| 764 | * |
| 765 | * @access public |
| 766 | * @param string |
| 767 | * @return string |
| 768 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 769 | public function clean_email($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 770 | { |
| 771 | if ( ! is_array($email)) |
| 772 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 773 | return (preg_match('/\<(.*)\>/', $email, $match)) ? $match[1] : $email; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | $clean_email = array(); |
| 777 | |
| 778 | foreach ($email as $addy) |
| 779 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 780 | $clean_email[] = (preg_match( '/\<(.*)\>/', $addy, $match)) ? $match[1] : $addy; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | return $clean_email; |
| 784 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 785 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 786 | // -------------------------------------------------------------------- |
| 787 | |
| 788 | /** |
| 789 | * Build alternative plain text message |
| 790 | * |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 791 | * This public function provides the raw message for use |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 792 | * in plain-text headers of HTML-formatted emails. |
| 793 | * If the user hasn't specified his own alternative message |
| 794 | * it creates one by stripping the HTML |
| 795 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 796 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 797 | * @return string |
| 798 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 799 | protected function _get_alt_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 800 | { |
| 801 | if ($this->alt_message != "") |
| 802 | { |
| 803 | return $this->word_wrap($this->alt_message, '76'); |
| 804 | } |
| 805 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 806 | $body = (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match)) ? $match[1] : $this->_body; |
| 807 | $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 808 | |
| 809 | for ($i = 20; $i >= 3; $i--) |
| 810 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 811 | $body = str_replace(str_repeat("\n", $i), "\n\n", $body); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 814 | return $this->word_wrap($body, 76); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 815 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 817 | // -------------------------------------------------------------------- |
| 818 | |
| 819 | /** |
| 820 | * Word Wrap |
| 821 | * |
| 822 | * @access public |
| 823 | * @param string |
| 824 | * @param integer |
| 825 | * @return string |
| 826 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 827 | public function word_wrap($str, $charlim = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 828 | { |
| 829 | // Se the character limit |
| 830 | if ($charlim == '') |
| 831 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 832 | $charlim = ($this->wrapchars == "") ? 76 : $this->wrapchars; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Reduce multiple spaces |
| 836 | $str = preg_replace("| +|", " ", $str); |
| 837 | |
| 838 | // Standardize newlines |
| 839 | if (strpos($str, "\r") !== FALSE) |
| 840 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 841 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | // If the current word is surrounded by {unwrap} tags we'll |
| 845 | // strip the entire chunk and replace it with a marker. |
| 846 | $unwrap = array(); |
| 847 | if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) |
| 848 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 849 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 850 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 851 | $unwrap[] = $matches[1][$i]; |
| 852 | $str = str_replace($matches[1][$i], "{{unwrapped".$i."}}", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 856 | // Use PHP's native public function to do the initial wordwrap. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 857 | // We set the cut flag to FALSE so that any individual words that are |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 858 | // too long get left alone. In the next step we'll deal with them. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | $str = wordwrap($str, $charlim, "\n", FALSE); |
| 860 | |
| 861 | // Split the string into individual lines of text and cycle through them |
| 862 | $output = ""; |
| 863 | foreach (explode("\n", $str) as $line) |
| 864 | { |
| 865 | // Is the line within the allowed character count? |
| 866 | // If so we'll join it to the output and continue |
| 867 | if (strlen($line) <= $charlim) |
| 868 | { |
| 869 | $output .= $line.$this->newline; |
| 870 | continue; |
| 871 | } |
| 872 | |
| 873 | $temp = ''; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 874 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | { |
| 876 | // If the over-length word is a URL we won't wrap it |
| 877 | if (preg_match("!\[url.+\]|://|wwww.!", $line)) |
| 878 | { |
| 879 | break; |
| 880 | } |
| 881 | |
| 882 | // Trim the word down |
| 883 | $temp .= substr($line, 0, $charlim-1); |
| 884 | $line = substr($line, $charlim-1); |
| 885 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 886 | while (strlen($line) > $charlim); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 887 | |
| 888 | // If $temp contains data it means we had to split up an over-length |
| 889 | // word into smaller chunks so we'll add it back to our current line |
| 890 | if ($temp != '') |
| 891 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 892 | $output .= $temp.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 895 | $output .= $line.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | // Put our markers back |
| 899 | if (count($unwrap) > 0) |
| 900 | { |
| 901 | foreach ($unwrap as $key => $val) |
| 902 | { |
| 903 | $output = str_replace("{{unwrapped".$key."}}", $val, $output); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | return $output; |
| 908 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 909 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | // -------------------------------------------------------------------- |
| 911 | |
| 912 | /** |
| 913 | * Build final headers |
| 914 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 915 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | * @param string |
| 917 | * @return string |
| 918 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 919 | protected function _build_headers() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | { |
| 921 | $this->_set_header('X-Sender', $this->clean_email($this->_headers['From'])); |
| 922 | $this->_set_header('X-Mailer', $this->useragent); |
| 923 | $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]); |
| 924 | $this->_set_header('Message-ID', $this->_get_message_id()); |
| 925 | $this->_set_header('Mime-Version', '1.0'); |
| 926 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 927 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | // -------------------------------------------------------------------- |
| 929 | |
| 930 | /** |
| 931 | * Write Headers as a string |
| 932 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 933 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 934 | * @return void |
| 935 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 936 | protected function _write_headers() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 937 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 938 | if ($this->protocol === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 939 | { |
| 940 | $this->_subject = $this->_headers['Subject']; |
| 941 | unset($this->_headers['Subject']); |
| 942 | } |
| 943 | |
| 944 | reset($this->_headers); |
| 945 | $this->_header_str = ""; |
| 946 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 947 | foreach ($this->_headers as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | { |
| 949 | $val = trim($val); |
| 950 | |
| 951 | if ($val != "") |
| 952 | { |
| 953 | $this->_header_str .= $key.": ".$val.$this->newline; |
| 954 | } |
| 955 | } |
| 956 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 957 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 958 | { |
Derek Jones | 1d89088 | 2009-02-10 20:32:14 +0000 | [diff] [blame] | 959 | $this->_header_str = rtrim($this->_header_str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 962 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 963 | // -------------------------------------------------------------------- |
| 964 | |
| 965 | /** |
| 966 | * Build Final Body and attachments |
| 967 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 968 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 969 | * @return void |
| 970 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 971 | protected function _build_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 972 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 973 | if ($this->wordwrap === TRUE AND $this->mailtype !== 'html') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 974 | { |
| 975 | $this->_body = $this->word_wrap($this->_body); |
| 976 | } |
| 977 | |
| 978 | $this->_set_boundaries(); |
| 979 | $this->_write_headers(); |
| 980 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 981 | $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : ''; |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 982 | $body = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 983 | |
| 984 | switch ($this->_get_content_type()) |
| 985 | { |
| 986 | case 'plain' : |
| 987 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 988 | $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline |
| 989 | . "Content-Transfer-Encoding: " . $this->_get_encoding(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 991 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 992 | { |
| 993 | $this->_header_str .= $hdr; |
| 994 | $this->_finalbody = $this->_body; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 995 | } |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 996 | else |
| 997 | { |
| 998 | $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body; |
| 999 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1000 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1001 | return; |
| 1002 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1003 | case 'html' : |
| 1004 | |
| 1005 | if ($this->send_multipart === FALSE) |
| 1006 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1007 | $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline |
| 1008 | . "Content-Transfer-Encoding: quoted-printable"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1009 | } |
| 1010 | else |
| 1011 | { |
Derek Jones | a45e761 | 2009-02-10 18:33:01 +0000 | [diff] [blame] | 1012 | $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1013 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1014 | $body .= $this->_get_mime_message() . $this->newline . $this->newline |
| 1015 | . "--" . $this->_alt_boundary . $this->newline |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1016 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1017 | . "Content-Type: text/plain; charset=" . $this->charset . $this->newline |
| 1018 | . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline |
| 1019 | . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1020 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1021 | . "Content-Type: text/html; charset=" . $this->charset . $this->newline |
| 1022 | . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1023 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1024 | |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1025 | $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1026 | |
| 1027 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1028 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1029 | { |
| 1030 | $this->_header_str .= $hdr; |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | $this->_finalbody = $hdr . $this->_finalbody; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1037 | |
| 1038 | if ($this->send_multipart !== FALSE) |
| 1039 | { |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1040 | $this->_finalbody .= "--" . $this->_alt_boundary . "--"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1043 | return; |
| 1044 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1045 | case 'plain-attach' : |
| 1046 | |
Derek Jones | a45e761 | 2009-02-10 18:33:01 +0000 | [diff] [blame] | 1047 | $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1048 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1049 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1050 | { |
| 1051 | $this->_header_str .= $hdr; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1052 | } |
| 1053 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1054 | $body .= $this->_get_mime_message() . $this->newline . $this->newline |
| 1055 | . "--" . $this->_atc_boundary . $this->newline |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1057 | . "Content-Type: text/plain; charset=" . $this->charset . $this->newline |
| 1058 | . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1059 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1060 | . $this->_body . $this->newline . $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | |
| 1062 | break; |
| 1063 | case 'html-attach' : |
| 1064 | |
Derek Jones | a45e761 | 2009-02-10 18:33:01 +0000 | [diff] [blame] | 1065 | $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1066 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1067 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1068 | { |
| 1069 | $this->_header_str .= $hdr; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1072 | $body .= $this->_get_mime_message() . $this->newline . $this->newline |
| 1073 | . "--" . $this->_atc_boundary . $this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1074 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1075 | . "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline |
| 1076 | . "--" . $this->_alt_boundary . $this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1077 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1078 | . "Content-Type: text/plain; charset=" . $this->charset . $this->newline |
| 1079 | . "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline |
| 1080 | . $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1081 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1082 | . "Content-Type: text/html; charset=" . $this->charset . $this->newline |
| 1083 | . "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1084 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1085 | . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline |
| 1086 | . "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1087 | |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | $attachment = array(); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1092 | for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1093 | { |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1094 | $filename = $this->_attach_name[$i][0]; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1095 | $basename = (is_null($this->_attach_name[$i][1])) ? basename($filename) : $this->_attach_name[$i][1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1096 | $ctype = $this->_attach_type[$i]; |
| 1097 | |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1098 | if ( ! file_exists($filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1099 | { |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1100 | $this->_set_error_message('lang:email_attachment_missing', $filename); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1101 | return FALSE; |
| 1102 | } |
| 1103 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1104 | $attachment[$z++] = "--".$this->_atc_boundary.$this->newline |
| 1105 | . "Content-type: ".$ctype."; " |
| 1106 | . "name=\"".$basename."\"".$this->newline |
| 1107 | . "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline |
| 1108 | . "Content-Transfer-Encoding: base64".$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1109 | |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1110 | $file = filesize($filename) +1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1111 | |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1112 | if ( ! $fp = fopen($filename, FOPEN_READ)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1113 | { |
trit | bc4ac99 | 2011-11-23 07:19:41 -0500 | [diff] [blame] | 1114 | $this->_set_error_message('lang:email_attachment_unreadable', $filename); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1115 | return FALSE; |
| 1116 | } |
| 1117 | |
| 1118 | $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file))); |
| 1119 | fclose($fp); |
| 1120 | } |
| 1121 | |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1122 | $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--"; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1123 | $this->_finalbody = ($this->_get_protocol() === 'mail') ? $body : $hdr . $body; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | // -------------------------------------------------------------------- |
| 1128 | |
| 1129 | /** |
| 1130 | * Prep Quoted Printable |
| 1131 | * |
| 1132 | * Prepares string for Quoted-Printable Content-Transfer-Encoding |
| 1133 | * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt |
| 1134 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1135 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1136 | * @param string |
| 1137 | * @param integer |
| 1138 | * @return string |
| 1139 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1140 | protected function _prep_quoted_printable($str, $charlim = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1141 | { |
| 1142 | // Set the character limit |
| 1143 | // Don't allow over 76, as that will make servers and MUAs barf |
| 1144 | // all over quoted-printable data |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1145 | if ($charlim == '' OR $charlim > 76) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1146 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1147 | $charlim = 76; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1150 | // Reduce multiple spaces & remove nulls |
| 1151 | $str = preg_replace(array("| +|", '/\x00+/'), array(' ', ''), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1152 | |
| 1153 | // Standardize newlines |
| 1154 | if (strpos($str, "\r") !== FALSE) |
| 1155 | { |
| 1156 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 1157 | } |
| 1158 | |
| 1159 | // We are intentionally wrapping so mail servers will encode characters |
| 1160 | // properly and MUAs will behave, so {unwrap} must go! |
| 1161 | $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str); |
| 1162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1163 | $escape = '='; |
| 1164 | $output = ''; |
| 1165 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1166 | foreach (explode("\n", $str) as $line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1167 | { |
| 1168 | $length = strlen($line); |
| 1169 | $temp = ''; |
| 1170 | |
| 1171 | // Loop through each character in the line to add soft-wrap |
| 1172 | // characters at the end of a line " =\r\n" and add the newly |
| 1173 | // processed line(s) to the output (see comment on $crlf class property) |
| 1174 | for ($i = 0; $i < $length; $i++) |
| 1175 | { |
| 1176 | // Grab the next character |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1177 | $char = $line[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1178 | $ascii = ord($char); |
| 1179 | |
| 1180 | // Convert spaces and tabs but only if it's the end of the line |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1181 | if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1182 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1183 | $char = $escape.sprintf('%02s', dechex($ascii)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1184 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1185 | elseif ($ascii === 61) // encode = signs |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1186 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1187 | $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | // If we're at the character limit, add the line to the output, |
| 1191 | // reset our temp variable, and keep on chuggin' |
| 1192 | if ((strlen($temp) + strlen($char)) >= $charlim) |
| 1193 | { |
| 1194 | $output .= $temp.$escape.$this->crlf; |
| 1195 | $temp = ''; |
| 1196 | } |
| 1197 | |
| 1198 | // Add the character to our temporary line |
| 1199 | $temp .= $char; |
| 1200 | } |
| 1201 | |
| 1202 | // Add our completed line to the output |
| 1203 | $output .= $temp.$this->crlf; |
| 1204 | } |
| 1205 | |
| 1206 | // get rid of extra CRLF tacked onto the end |
| 1207 | $output = substr($output, 0, strlen($this->crlf) * -1); |
| 1208 | |
| 1209 | return $output; |
| 1210 | } |
| 1211 | |
| 1212 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1214 | /** |
| 1215 | * Prep Q Encoding |
| 1216 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1217 | * Performs "Q Encoding" on a string for use in email headers. It's related |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1218 | * but not identical to quoted-printable, so it has its own method |
| 1219 | * |
| 1220 | * @access public |
| 1221 | * @param str |
| 1222 | * @param bool // set to TRUE for processing From: headers |
| 1223 | * @return str |
| 1224 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1225 | protected function _prep_q_encoding($str, $from = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1226 | { |
| 1227 | $str = str_replace(array("\r", "\n"), array('', ''), $str); |
| 1228 | |
| 1229 | // Line length must not exceed 76 characters, so we adjust for |
| 1230 | // a space, 7 extra characters =??Q??=, and the charset that we will add to each line |
| 1231 | $limit = 75 - 7 - strlen($this->charset); |
| 1232 | |
| 1233 | // these special characters must be converted too |
| 1234 | $convert = array('_', '=', '?'); |
| 1235 | |
| 1236 | if ($from === TRUE) |
| 1237 | { |
| 1238 | $convert[] = ','; |
| 1239 | $convert[] = ';'; |
| 1240 | } |
| 1241 | |
| 1242 | $output = ''; |
| 1243 | $temp = ''; |
| 1244 | |
| 1245 | for ($i = 0, $length = strlen($str); $i < $length; $i++) |
| 1246 | { |
| 1247 | // Grab the next character |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1248 | $char = $str[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1249 | $ascii = ord($char); |
| 1250 | |
| 1251 | // convert ALL non-printable ASCII characters and our specials |
| 1252 | if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert)) |
| 1253 | { |
| 1254 | $char = '='.dechex($ascii); |
| 1255 | } |
| 1256 | |
| 1257 | // handle regular spaces a bit more compactly than =20 |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1258 | if ($ascii === 32) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1259 | { |
| 1260 | $char = '_'; |
| 1261 | } |
| 1262 | |
| 1263 | // If we're at the character limit, add the line to the output, |
| 1264 | // reset our temp variable, and keep on chuggin' |
| 1265 | if ((strlen($temp) + strlen($char)) >= $limit) |
| 1266 | { |
| 1267 | $output .= $temp.$this->crlf; |
| 1268 | $temp = ''; |
| 1269 | } |
| 1270 | |
| 1271 | // Add the character to our temporary line |
| 1272 | $temp .= $char; |
| 1273 | } |
| 1274 | |
| 1275 | $str = $output.$temp; |
| 1276 | |
| 1277 | // wrap each line with the shebang, charset, and transfer encoding |
| 1278 | // the preceding space on successive lines is required for header "folding" |
| 1279 | $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str)); |
| 1280 | |
| 1281 | return $str; |
| 1282 | } |
| 1283 | |
| 1284 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1285 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1286 | /** |
| 1287 | * Send Email |
| 1288 | * |
| 1289 | * @access public |
| 1290 | * @return bool |
| 1291 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1292 | public function send() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1293 | { |
| 1294 | if ($this->_replyto_flag == FALSE) |
| 1295 | { |
| 1296 | $this->reply_to($this->_headers['From']); |
| 1297 | } |
| 1298 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1299 | if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1300 | ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND |
| 1301 | ( ! isset($this->_headers['Cc']))) |
| 1302 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1303 | $this->_set_error_message('lang:email_no_recipients'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1304 | return FALSE; |
| 1305 | } |
| 1306 | |
| 1307 | $this->_build_headers(); |
| 1308 | |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 1309 | if ($this->bcc_batch_mode AND count($this->_bcc_array) > $this->bcc_batch_size) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1310 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1311 | return $this->batch_bcc_send(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | $this->_build_message(); |
| 1315 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1316 | return $this->_spool_email(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1317 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1319 | // -------------------------------------------------------------------- |
| 1320 | |
| 1321 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1322 | * Batch Bcc Send. Sends groups of BCCs in batches |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | * |
| 1324 | * @access public |
| 1325 | * @return bool |
| 1326 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1327 | public function batch_bcc_send() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1328 | { |
| 1329 | $float = $this->bcc_batch_size -1; |
| 1330 | |
| 1331 | $set = ""; |
| 1332 | |
| 1333 | $chunk = array(); |
| 1334 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1335 | for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1336 | { |
| 1337 | if (isset($this->_bcc_array[$i])) |
| 1338 | { |
| 1339 | $set .= ", ".$this->_bcc_array[$i]; |
| 1340 | } |
| 1341 | |
| 1342 | if ($i == $float) |
| 1343 | { |
| 1344 | $chunk[] = substr($set, 1); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1345 | $float += $this->bcc_batch_size; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1346 | $set = ""; |
| 1347 | } |
| 1348 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1349 | if ($i === $c-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1350 | { |
| 1351 | $chunk[] = substr($set, 1); |
| 1352 | } |
| 1353 | } |
| 1354 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1355 | for ($i = 0, $c = count($chunk); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1356 | { |
| 1357 | unset($this->_headers['Bcc']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1358 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1359 | $bcc = $this->clean_email($this->_str_to_array($chunk[$i])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1360 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1361 | if ($this->protocol !== 'smtp') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1362 | { |
| 1363 | $this->_set_header('Bcc', implode(", ", $bcc)); |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | $this->_bcc_array = $bcc; |
| 1368 | } |
| 1369 | |
| 1370 | $this->_build_message(); |
| 1371 | $this->_spool_email(); |
| 1372 | } |
| 1373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1374 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1375 | // -------------------------------------------------------------------- |
| 1376 | |
| 1377 | /** |
| 1378 | * Unwrap special elements |
| 1379 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1380 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1381 | * @return void |
| 1382 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1383 | protected function _unwrap_specials() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1384 | { |
| 1385 | $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody); |
| 1386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1388 | // -------------------------------------------------------------------- |
| 1389 | |
| 1390 | /** |
| 1391 | * Strip line-breaks via callback |
| 1392 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1393 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1394 | * @return string |
| 1395 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1396 | protected function _remove_nl_callback($matches) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1397 | { |
| 1398 | if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE) |
| 1399 | { |
| 1400 | $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]); |
| 1401 | } |
| 1402 | |
| 1403 | return $matches[1]; |
| 1404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1406 | // -------------------------------------------------------------------- |
| 1407 | |
| 1408 | /** |
| 1409 | * Spool mail to the mail server |
| 1410 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1411 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1412 | * @return bool |
| 1413 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1414 | protected function _spool_email() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1415 | { |
| 1416 | $this->_unwrap_specials(); |
| 1417 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1418 | $method = '_send_with_' . $this->_get_protocol(); |
| 1419 | if ( ! $this->$method()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1420 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1421 | $this->_set_error_message('lang:email_send_failure_' . ($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol())); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1424 | $this->_set_error_message('lang:email_sent', $this->_get_protocol()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1425 | return TRUE; |
| 1426 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1428 | // -------------------------------------------------------------------- |
| 1429 | |
| 1430 | /** |
| 1431 | * Send using mail() |
| 1432 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1433 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1434 | * @return bool |
| 1435 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1436 | protected function _send_with_mail() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1437 | { |
| 1438 | if ($this->_safe_mode == TRUE) |
| 1439 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1440 | return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1441 | } |
| 1442 | else |
| 1443 | { |
| 1444 | // most documentation of sendmail using the "-f" flag lacks a space after it, however |
| 1445 | // we've encountered servers that seem to require it to be in place. |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1446 | return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1447 | } |
| 1448 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1449 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1450 | // -------------------------------------------------------------------- |
| 1451 | |
| 1452 | /** |
| 1453 | * Send using Sendmail |
| 1454 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1455 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1456 | * @return bool |
| 1457 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1458 | protected function _send_with_sendmail() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1459 | { |
| 1460 | $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w'); |
| 1461 | |
Derek Jones | 4cefaa4 | 2009-04-29 19:13:56 +0000 | [diff] [blame] | 1462 | if ($fp === FALSE OR $fp === NULL) |
| 1463 | { |
| 1464 | // server probably has popen disabled, so nothing we can do to get a verbose error. |
| 1465 | return FALSE; |
| 1466 | } |
Derek Jones | 71141ce | 2010-03-02 16:41:20 -0600 | [diff] [blame] | 1467 | |
Derek Jones | c630bcf | 2008-11-17 21:09:45 +0000 | [diff] [blame] | 1468 | fputs($fp, $this->_header_str); |
| 1469 | fputs($fp, $this->_finalbody); |
| 1470 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1471 | $status = pclose($fp); |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1472 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1473 | if ($status !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1474 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1475 | $this->_set_error_message('lang:email_exit_status', $status); |
| 1476 | $this->_set_error_message('lang:email_no_socket'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1477 | return FALSE; |
| 1478 | } |
| 1479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1480 | return TRUE; |
| 1481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1483 | // -------------------------------------------------------------------- |
| 1484 | |
| 1485 | /** |
| 1486 | * Send using SMTP |
| 1487 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1488 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1489 | * @return bool |
| 1490 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1491 | protected function _send_with_smtp() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1492 | { |
| 1493 | if ($this->smtp_host == '') |
| 1494 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1495 | $this->_set_error_message('lang:email_no_hostname'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1496 | return FALSE; |
| 1497 | } |
| 1498 | |
| 1499 | $this->_smtp_connect(); |
| 1500 | $this->_smtp_authenticate(); |
| 1501 | |
| 1502 | $this->_send_command('from', $this->clean_email($this->_headers['From'])); |
| 1503 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1504 | foreach ($this->_recipients as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1505 | { |
| 1506 | $this->_send_command('to', $val); |
| 1507 | } |
| 1508 | |
| 1509 | if (count($this->_cc_array) > 0) |
| 1510 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1511 | foreach ($this->_cc_array as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1512 | { |
| 1513 | if ($val != "") |
| 1514 | { |
| 1515 | $this->_send_command('to', $val); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | if (count($this->_bcc_array) > 0) |
| 1521 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1522 | foreach ($this->_bcc_array as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1523 | { |
| 1524 | if ($val != "") |
| 1525 | { |
| 1526 | $this->_send_command('to', $val); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | $this->_send_command('data'); |
| 1532 | |
| 1533 | // perform dot transformation on any lines that begin with a dot |
| 1534 | $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody)); |
| 1535 | |
| 1536 | $this->_send_data('.'); |
| 1537 | |
| 1538 | $reply = $this->_get_smtp_data(); |
| 1539 | |
| 1540 | $this->_set_error_message($reply); |
| 1541 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1542 | if (strncmp($reply, '250', 3) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1543 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1544 | $this->_set_error_message('lang:email_smtp_error', $reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1545 | return FALSE; |
| 1546 | } |
| 1547 | |
| 1548 | $this->_send_command('quit'); |
| 1549 | return TRUE; |
| 1550 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1552 | // -------------------------------------------------------------------- |
| 1553 | |
| 1554 | /** |
| 1555 | * SMTP Connect |
| 1556 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1557 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1558 | * @param string |
| 1559 | * @return string |
| 1560 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1561 | protected function _smtp_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1562 | { |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 1563 | $ssl = ($this->smtp_crypto == 'ssl') ? 'ssl://' : NULL; |
Radu Potop | 4c589ae | 2011-09-29 10:19:55 +0300 | [diff] [blame] | 1564 | |
Radu Potop | bbf04b0 | 2011-09-28 13:57:51 +0300 | [diff] [blame] | 1565 | $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1566 | $this->smtp_port, |
| 1567 | $errno, |
| 1568 | $errstr, |
| 1569 | $this->smtp_timeout); |
| 1570 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1571 | if ( ! is_resource($this->_smtp_connect)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1572 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1573 | $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1574 | return FALSE; |
| 1575 | } |
| 1576 | |
| 1577 | $this->_set_error_message($this->_get_smtp_data()); |
Radu Potop | bbf04b0 | 2011-09-28 13:57:51 +0300 | [diff] [blame] | 1578 | |
| 1579 | if ($this->smtp_crypto == 'tls') |
| 1580 | { |
| 1581 | $this->_send_command('hello'); |
| 1582 | $this->_send_command('starttls'); |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 1583 | |
Radu Potop | 4c589ae | 2011-09-29 10:19:55 +0300 | [diff] [blame] | 1584 | $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); |
Radu Potop | 4c589ae | 2011-09-29 10:19:55 +0300 | [diff] [blame] | 1585 | |
Sean Fisher | e862ddd | 2011-10-26 22:45:00 -0300 | [diff] [blame] | 1586 | if ($crypto !== TRUE) |
| 1587 | { |
| 1588 | $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); |
| 1589 | return FALSE; |
| 1590 | } |
Radu Potop | bbf04b0 | 2011-09-28 13:57:51 +0300 | [diff] [blame] | 1591 | } |
| 1592 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1593 | return $this->_send_command('hello'); |
| 1594 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1595 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1596 | // -------------------------------------------------------------------- |
| 1597 | |
| 1598 | /** |
| 1599 | * Send SMTP command |
| 1600 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1601 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1602 | * @param string |
| 1603 | * @param string |
| 1604 | * @return string |
| 1605 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1606 | protected function _send_command($cmd, $data = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1607 | { |
| 1608 | switch ($cmd) |
| 1609 | { |
| 1610 | case 'hello' : |
| 1611 | |
| 1612 | if ($this->_smtp_auth OR $this->_get_encoding() == '8bit') |
| 1613 | $this->_send_data('EHLO '.$this->_get_hostname()); |
| 1614 | else |
| 1615 | $this->_send_data('HELO '.$this->_get_hostname()); |
| 1616 | |
| 1617 | $resp = 250; |
| 1618 | break; |
Radu Potop | bbf04b0 | 2011-09-28 13:57:51 +0300 | [diff] [blame] | 1619 | case 'starttls' : |
| 1620 | |
| 1621 | $this->_send_data('STARTTLS'); |
| 1622 | |
| 1623 | $resp = 220; |
| 1624 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1625 | case 'from' : |
| 1626 | |
| 1627 | $this->_send_data('MAIL FROM:<'.$data.'>'); |
| 1628 | |
| 1629 | $resp = 250; |
| 1630 | break; |
| 1631 | case 'to' : |
| 1632 | |
| 1633 | $this->_send_data('RCPT TO:<'.$data.'>'); |
| 1634 | |
| 1635 | $resp = 250; |
| 1636 | break; |
| 1637 | case 'data' : |
| 1638 | |
| 1639 | $this->_send_data('DATA'); |
| 1640 | |
| 1641 | $resp = 354; |
| 1642 | break; |
| 1643 | case 'quit' : |
| 1644 | |
| 1645 | $this->_send_data('QUIT'); |
| 1646 | |
| 1647 | $resp = 221; |
| 1648 | break; |
| 1649 | } |
| 1650 | |
| 1651 | $reply = $this->_get_smtp_data(); |
| 1652 | |
| 1653 | $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>"; |
| 1654 | |
| 1655 | if (substr($reply, 0, 3) != $resp) |
| 1656 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1657 | $this->_set_error_message('lang:email_smtp_error', $reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1658 | return FALSE; |
| 1659 | } |
| 1660 | |
| 1661 | if ($cmd == 'quit') |
| 1662 | { |
| 1663 | fclose($this->_smtp_connect); |
| 1664 | } |
| 1665 | |
| 1666 | return TRUE; |
| 1667 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1668 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1669 | // -------------------------------------------------------------------- |
| 1670 | |
| 1671 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1672 | * SMTP Authenticate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1673 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1674 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1675 | * @return bool |
| 1676 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1677 | protected function _smtp_authenticate() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1678 | { |
| 1679 | if ( ! $this->_smtp_auth) |
| 1680 | { |
| 1681 | return TRUE; |
| 1682 | } |
| 1683 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1684 | if ($this->smtp_user == "" AND $this->smtp_pass == "") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1685 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1686 | $this->_set_error_message('lang:email_no_smtp_unpw'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1687 | return FALSE; |
| 1688 | } |
| 1689 | |
| 1690 | $this->_send_data('AUTH LOGIN'); |
| 1691 | |
| 1692 | $reply = $this->_get_smtp_data(); |
| 1693 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1694 | if (strncmp($reply, '334', 3) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1695 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1696 | $this->_set_error_message('lang:email_failed_smtp_login', $reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1697 | return FALSE; |
| 1698 | } |
| 1699 | |
| 1700 | $this->_send_data(base64_encode($this->smtp_user)); |
| 1701 | |
| 1702 | $reply = $this->_get_smtp_data(); |
| 1703 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1704 | if (strncmp($reply, '334', 3) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1705 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1706 | $this->_set_error_message('lang:email_smtp_auth_un', $reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1707 | return FALSE; |
| 1708 | } |
| 1709 | |
| 1710 | $this->_send_data(base64_encode($this->smtp_pass)); |
| 1711 | |
| 1712 | $reply = $this->_get_smtp_data(); |
| 1713 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1714 | if (strncmp($reply, '235', 3) !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1715 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1716 | $this->_set_error_message('lang:email_smtp_auth_pw', $reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1717 | return FALSE; |
| 1718 | } |
| 1719 | |
| 1720 | return TRUE; |
| 1721 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1722 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1723 | // -------------------------------------------------------------------- |
| 1724 | |
| 1725 | /** |
| 1726 | * Send SMTP data |
| 1727 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1728 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1729 | * @return bool |
| 1730 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1731 | protected function _send_data($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1732 | { |
| 1733 | if ( ! fwrite($this->_smtp_connect, $data . $this->newline)) |
| 1734 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1735 | $this->_set_error_message('lang:email_smtp_data_failure', $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1736 | return FALSE; |
| 1737 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1738 | |
| 1739 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1740 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1741 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1742 | // -------------------------------------------------------------------- |
| 1743 | |
| 1744 | /** |
| 1745 | * Get SMTP data |
| 1746 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1747 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1748 | * @return string |
| 1749 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1750 | protected function _get_smtp_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1751 | { |
| 1752 | $data = ""; |
| 1753 | |
| 1754 | while ($str = fgets($this->_smtp_connect, 512)) |
| 1755 | { |
| 1756 | $data .= $str; |
| 1757 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1758 | if ($str[3] == " ") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1759 | { |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | return $data; |
| 1765 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1766 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1767 | // -------------------------------------------------------------------- |
| 1768 | |
| 1769 | /** |
| 1770 | * Get Hostname |
| 1771 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1772 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1773 | * @return string |
| 1774 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1775 | protected function _get_hostname() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1776 | { |
| 1777 | return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain'; |
| 1778 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1779 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1780 | // -------------------------------------------------------------------- |
| 1781 | |
| 1782 | /** |
| 1783 | * Get IP |
| 1784 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1785 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1786 | * @return string |
| 1787 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1788 | protected function _get_ip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1789 | { |
| 1790 | if ($this->_IP !== FALSE) |
| 1791 | { |
| 1792 | return $this->_IP; |
| 1793 | } |
| 1794 | |
| 1795 | $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE; |
| 1796 | $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1797 | if ($cip) $this->_IP = $cip; |
| 1798 | elseif ($rip) $this->_IP = $rip; |
| 1799 | else |
| 1800 | { |
| 1801 | $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE; |
| 1802 | if ($fip) |
| 1803 | { |
| 1804 | $this->_IP = $fip; |
| 1805 | } |
| 1806 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1807 | |
Robin Sowell | 76b369e | 2010-03-19 11:15:28 -0400 | [diff] [blame] | 1808 | if (strpos($this->_IP, ',') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1809 | { |
| 1810 | $x = explode(',', $this->_IP); |
| 1811 | $this->_IP = end($x); |
| 1812 | } |
| 1813 | |
| 1814 | if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP)) |
| 1815 | { |
| 1816 | $this->_IP = '0.0.0.0'; |
| 1817 | } |
| 1818 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1819 | return $this->_IP; |
| 1820 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1821 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1822 | // -------------------------------------------------------------------- |
| 1823 | |
| 1824 | /** |
| 1825 | * Get Debug Message |
| 1826 | * |
| 1827 | * @access public |
| 1828 | * @return string |
| 1829 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1830 | public function print_debugger() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1831 | { |
| 1832 | $msg = ''; |
| 1833 | |
| 1834 | if (count($this->_debug_msg) > 0) |
| 1835 | { |
| 1836 | foreach ($this->_debug_msg as $val) |
| 1837 | { |
| 1838 | $msg .= $val; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>'; |
| 1843 | return $msg; |
| 1844 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1845 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1846 | // -------------------------------------------------------------------- |
| 1847 | |
| 1848 | /** |
| 1849 | * Set Message |
| 1850 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1851 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1852 | * @param string |
| 1853 | * @return string |
| 1854 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1855 | protected function _set_error_message($msg, $val = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1856 | { |
| 1857 | $CI =& get_instance(); |
| 1858 | $CI->lang->load('email'); |
| 1859 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1860 | if (substr($msg, 0, 5) !== 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5)))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1861 | { |
| 1862 | $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />"; |
| 1863 | } |
| 1864 | else |
| 1865 | { |
| 1866 | $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />"; |
| 1867 | } |
| 1868 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1869 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1870 | // -------------------------------------------------------------------- |
| 1871 | |
| 1872 | /** |
| 1873 | * Mime Types |
| 1874 | * |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1875 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1876 | * @param string |
| 1877 | * @return string |
| 1878 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1879 | protected function _mime_types($ext = "") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1880 | { |
| 1881 | $mimes = array( 'hqx' => 'application/mac-binhex40', |
| 1882 | 'cpt' => 'application/mac-compactpro', |
| 1883 | 'doc' => 'application/msword', |
| 1884 | 'bin' => 'application/macbinary', |
| 1885 | 'dms' => 'application/octet-stream', |
| 1886 | 'lha' => 'application/octet-stream', |
| 1887 | 'lzh' => 'application/octet-stream', |
| 1888 | 'exe' => 'application/octet-stream', |
| 1889 | 'class' => 'application/octet-stream', |
| 1890 | 'psd' => 'application/octet-stream', |
| 1891 | 'so' => 'application/octet-stream', |
| 1892 | 'sea' => 'application/octet-stream', |
| 1893 | 'dll' => 'application/octet-stream', |
| 1894 | 'oda' => 'application/oda', |
| 1895 | 'pdf' => 'application/pdf', |
| 1896 | 'ai' => 'application/postscript', |
| 1897 | 'eps' => 'application/postscript', |
| 1898 | 'ps' => 'application/postscript', |
| 1899 | 'smi' => 'application/smil', |
| 1900 | 'smil' => 'application/smil', |
| 1901 | 'mif' => 'application/vnd.mif', |
| 1902 | 'xls' => 'application/vnd.ms-excel', |
| 1903 | 'ppt' => 'application/vnd.ms-powerpoint', |
| 1904 | 'wbxml' => 'application/vnd.wap.wbxml', |
| 1905 | 'wmlc' => 'application/vnd.wap.wmlc', |
| 1906 | 'dcr' => 'application/x-director', |
| 1907 | 'dir' => 'application/x-director', |
| 1908 | 'dxr' => 'application/x-director', |
| 1909 | 'dvi' => 'application/x-dvi', |
| 1910 | 'gtar' => 'application/x-gtar', |
| 1911 | 'php' => 'application/x-httpd-php', |
| 1912 | 'php4' => 'application/x-httpd-php', |
| 1913 | 'php3' => 'application/x-httpd-php', |
| 1914 | 'phtml' => 'application/x-httpd-php', |
| 1915 | 'phps' => 'application/x-httpd-php-source', |
| 1916 | 'js' => 'application/x-javascript', |
| 1917 | 'swf' => 'application/x-shockwave-flash', |
| 1918 | 'sit' => 'application/x-stuffit', |
| 1919 | 'tar' => 'application/x-tar', |
| 1920 | 'tgz' => 'application/x-tar', |
| 1921 | 'xhtml' => 'application/xhtml+xml', |
| 1922 | 'xht' => 'application/xhtml+xml', |
| 1923 | 'zip' => 'application/zip', |
| 1924 | 'mid' => 'audio/midi', |
| 1925 | 'midi' => 'audio/midi', |
| 1926 | 'mpga' => 'audio/mpeg', |
| 1927 | 'mp2' => 'audio/mpeg', |
| 1928 | 'mp3' => 'audio/mpeg', |
| 1929 | 'aif' => 'audio/x-aiff', |
| 1930 | 'aiff' => 'audio/x-aiff', |
| 1931 | 'aifc' => 'audio/x-aiff', |
| 1932 | 'ram' => 'audio/x-pn-realaudio', |
| 1933 | 'rm' => 'audio/x-pn-realaudio', |
| 1934 | 'rpm' => 'audio/x-pn-realaudio-plugin', |
| 1935 | 'ra' => 'audio/x-realaudio', |
| 1936 | 'rv' => 'video/vnd.rn-realvideo', |
| 1937 | 'wav' => 'audio/x-wav', |
| 1938 | 'bmp' => 'image/bmp', |
| 1939 | 'gif' => 'image/gif', |
| 1940 | 'jpeg' => 'image/jpeg', |
| 1941 | 'jpg' => 'image/jpeg', |
| 1942 | 'jpe' => 'image/jpeg', |
| 1943 | 'png' => 'image/png', |
| 1944 | 'tiff' => 'image/tiff', |
| 1945 | 'tif' => 'image/tiff', |
| 1946 | 'css' => 'text/css', |
| 1947 | 'html' => 'text/html', |
| 1948 | 'htm' => 'text/html', |
| 1949 | 'shtml' => 'text/html', |
| 1950 | 'txt' => 'text/plain', |
| 1951 | 'text' => 'text/plain', |
| 1952 | 'log' => 'text/plain', |
| 1953 | 'rtx' => 'text/richtext', |
| 1954 | 'rtf' => 'text/rtf', |
| 1955 | 'xml' => 'text/xml', |
| 1956 | 'xsl' => 'text/xml', |
| 1957 | 'mpeg' => 'video/mpeg', |
| 1958 | 'mpg' => 'video/mpeg', |
| 1959 | 'mpe' => 'video/mpeg', |
| 1960 | 'qt' => 'video/quicktime', |
| 1961 | 'mov' => 'video/quicktime', |
| 1962 | 'avi' => 'video/x-msvideo', |
| 1963 | 'movie' => 'video/x-sgi-movie', |
| 1964 | 'doc' => 'application/msword', |
| 1965 | 'word' => 'application/msword', |
| 1966 | 'xl' => 'application/excel', |
| 1967 | 'eml' => 'message/rfc822' |
| 1968 | ); |
| 1969 | |
| 1970 | return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)]; |
| 1971 | } |
| 1972 | |
| 1973 | } |
| 1974 | // END CI_Email class |
| 1975 | |
| 1976 | /* End of file Email.php */ |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1977 | /* Location: ./system/libraries/Email.php */ |