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