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