Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter Email Class |
| 31 | * |
| 32 | * Permits email to be sent using Mail, Sendmail, or SMTP. |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/email.html |
| 39 | */ |
trit | a15dd4f | 2011-11-23 07:40:05 -0500 | [diff] [blame] | 40 | class CI_Email { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 42 | /** |
| 43 | * Used as the User-Agent and X-Mailer headers' value. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 47 | public $useragent = 'CodeIgniter'; |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 48 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Path to the Sendmail binary. |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | public $mailpath = '/usr/sbin/sendmail'; // Sendmail path |
| 55 | |
| 56 | /** |
| 57 | * Which method to use for sending e-mails. |
| 58 | * |
| 59 | * @var string 'mail', 'sendmail' or 'smtp' |
| 60 | */ |
| 61 | public $protocol = 'mail'; // mail/sendmail/smtp |
| 62 | |
| 63 | /** |
| 64 | * STMP Server host |
| 65 | * |
| 66 | * @var string |
| 67 | */ |
| 68 | public $smtp_host = ''; |
| 69 | |
| 70 | /** |
| 71 | * SMTP Username |
| 72 | * |
| 73 | * @var string |
| 74 | */ |
| 75 | public $smtp_user = ''; |
| 76 | |
| 77 | /** |
| 78 | * SMTP Password |
| 79 | * |
| 80 | * @var string |
| 81 | */ |
| 82 | public $smtp_pass = ''; |
| 83 | |
| 84 | /** |
| 85 | * SMTP Server port |
| 86 | * |
| 87 | * @var int |
| 88 | */ |
| 89 | public $smtp_port = 25; |
| 90 | |
| 91 | /** |
| 92 | * SMTP connection timeout in seconds |
| 93 | * |
| 94 | * @var int |
| 95 | */ |
| 96 | public $smtp_timeout = 5; |
vlakoff | f399425 | 2013-02-19 01:45:23 +0100 | [diff] [blame] | 97 | |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 98 | /** |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 99 | * SMTP persistent connection |
| 100 | * |
| 101 | * @var bool |
| 102 | */ |
| 103 | public $smtp_keepalive = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * SMTP Encryption |
| 107 | * |
Andrey Andreev | 89b67b4 | 2013-03-12 19:34:23 +0200 | [diff] [blame] | 108 | * @var string empty, 'tls' or 'ssl' |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 109 | */ |
Andrey Andreev | 89b67b4 | 2013-03-12 19:34:23 +0200 | [diff] [blame] | 110 | public $smtp_crypto = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Whether to apply word-wrapping to the message body. |
| 114 | * |
| 115 | * @var bool |
| 116 | */ |
| 117 | public $wordwrap = TRUE; |
| 118 | |
| 119 | /** |
| 120 | * Number of characters to wrap at. |
| 121 | * |
| 122 | * @see CI_Email::$wordwrap |
| 123 | * @var int |
| 124 | */ |
| 125 | public $wrapchars = 76; |
| 126 | |
| 127 | /** |
| 128 | * Message format. |
| 129 | * |
| 130 | * @var string 'text' or 'html' |
| 131 | */ |
| 132 | public $mailtype = 'text'; |
| 133 | |
| 134 | /** |
| 135 | * Character set (default: utf-8) |
| 136 | * |
| 137 | * @var string |
| 138 | */ |
| 139 | public $charset = 'utf-8'; |
| 140 | |
| 141 | /** |
| 142 | * Multipart message |
| 143 | * |
| 144 | * @var string 'mixed' (in the body) or 'related' (separate) |
| 145 | */ |
| 146 | public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate) |
| 147 | |
| 148 | /** |
| 149 | * Alternative message (for HTML messages only) |
| 150 | * |
| 151 | * @var string |
| 152 | */ |
| 153 | public $alt_message = ''; |
| 154 | |
| 155 | /** |
| 156 | * Whether to validate e-mail addresses. |
| 157 | * |
| 158 | * @var bool |
| 159 | */ |
| 160 | public $validate = FALSE; |
| 161 | |
| 162 | /** |
| 163 | * X-Priority header value. |
| 164 | * |
| 165 | * @var int 1-5 |
| 166 | */ |
| 167 | public $priority = 3; // Default priority (1 - 5) |
| 168 | |
| 169 | /** |
| 170 | * Newline character sequence. |
| 171 | * Use "\r\n" to comply with RFC 822. |
| 172 | * |
| 173 | * @link http://www.ietf.org/rfc/rfc822.txt |
| 174 | * @var string "\r\n" or "\n" |
| 175 | */ |
| 176 | public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822) |
| 177 | |
| 178 | /** |
| 179 | * CRLF character sequence |
| 180 | * |
| 181 | * RFC 2045 specifies that for 'quoted-printable' encoding, |
| 182 | * "\r\n" must be used. However, it appears that some servers |
| 183 | * (even on the receiving end) don't handle it properly and |
| 184 | * switching to "\n", while improper, is the only solution |
| 185 | * that seems to work for all environments. |
| 186 | * |
| 187 | * @link http://www.ietf.org/rfc/rfc822.txt |
| 188 | * @var string |
| 189 | */ |
| 190 | public $crlf = "\n"; |
| 191 | |
| 192 | /** |
| 193 | * Whether to use Delivery Status Notification. |
| 194 | * |
| 195 | * @var bool |
| 196 | */ |
| 197 | public $dsn = FALSE; |
| 198 | |
| 199 | /** |
| 200 | * Whether to send multipart alternatives. |
| 201 | * Yahoo! doesn't seem to like these. |
| 202 | * |
| 203 | * @var bool |
| 204 | */ |
| 205 | public $send_multipart = TRUE; |
| 206 | |
| 207 | /** |
| 208 | * Whether to send messages to BCC recipients in batches. |
| 209 | * |
| 210 | * @var bool |
| 211 | */ |
| 212 | public $bcc_batch_mode = FALSE; |
| 213 | |
| 214 | /** |
| 215 | * BCC Batch max number size. |
| 216 | * |
| 217 | * @see CI_Email::$bcc_batch_mode |
| 218 | * @var int |
| 219 | */ |
| 220 | public $bcc_batch_size = 200; |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
| 225 | * Whether PHP is running in safe mode. Initialized by the class constructor. |
| 226 | * |
| 227 | * @var bool |
| 228 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 229 | protected $_safe_mode = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Subject header |
| 233 | * |
| 234 | * @var string |
| 235 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 236 | protected $_subject = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * Message body |
| 240 | * |
| 241 | * @var string |
| 242 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 243 | protected $_body = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * Final message body to be sent. |
| 247 | * |
| 248 | * @var string |
| 249 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 250 | protected $_finalbody = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * multipart/alternative boundary |
| 254 | * |
| 255 | * @var string |
| 256 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 257 | protected $_alt_boundary = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Attachment boundary |
| 261 | * |
| 262 | * @var string |
| 263 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 264 | protected $_atc_boundary = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Final headers to send |
| 268 | * |
| 269 | * @var string |
| 270 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 271 | protected $_header_str = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * SMTP Connection socket placeholder |
| 275 | * |
| 276 | * @var resource |
| 277 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 278 | protected $_smtp_connect = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * Mail encoding |
| 282 | * |
| 283 | * @var string '8bit' or '7bit' |
| 284 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 285 | protected $_encoding = '8bit'; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 286 | |
| 287 | /** |
| 288 | * Whether to perform SMTP authentication |
| 289 | * |
| 290 | * @var bool |
| 291 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 292 | protected $_smtp_auth = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Whether to send a Reply-To header |
| 296 | * |
| 297 | * @var bool |
| 298 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 299 | protected $_replyto_flag = FALSE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * Debug messages |
| 303 | * |
| 304 | * @see CI_Email::print_debugger() |
| 305 | * @var string |
| 306 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 307 | protected $_debug_msg = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 308 | |
| 309 | /** |
| 310 | * Recipients |
| 311 | * |
| 312 | * @var string[] |
| 313 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 314 | protected $_recipients = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * CC Recipients |
| 318 | * |
| 319 | * @var string[] |
| 320 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 321 | protected $_cc_array = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 322 | |
| 323 | /** |
| 324 | * BCC Recipients |
| 325 | * |
| 326 | * @var string[] |
| 327 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 328 | protected $_bcc_array = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * Message headers |
| 332 | * |
| 333 | * @var string[] |
| 334 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 335 | protected $_headers = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * Attachment data |
| 339 | * |
| 340 | * @var array |
| 341 | */ |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 342 | protected $_attachments = array(); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * Valid $protocol values |
| 346 | * |
| 347 | * @see CI_Email::$protocol |
| 348 | * @var string[] |
| 349 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 350 | protected $_protocols = array('mail', 'sendmail', 'smtp'); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * Base charsets |
| 354 | * |
| 355 | * Character sets valid for 7-bit encoding, |
| 356 | * excluding language suffix. |
| 357 | * |
| 358 | * @var string[] |
| 359 | */ |
| 360 | protected $_base_charsets = array('us-ascii', 'iso-2022-'); |
| 361 | |
| 362 | /** |
| 363 | * Bit depths |
| 364 | * |
| 365 | * Valid mail encodings |
| 366 | * |
| 367 | * @see CI_Email::$_encoding |
| 368 | * @var string[] |
| 369 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 370 | protected $_bit_depths = array('7bit', '8bit'); |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 371 | |
| 372 | /** |
| 373 | * $priority translations |
| 374 | * |
| 375 | * Actual values to send with the X-Priority header |
| 376 | * |
| 377 | * @var string[] |
| 378 | */ |
Andrey Andreev | 5081409 | 2012-03-01 12:36:12 +0200 | [diff] [blame] | 379 | 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] | 380 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 381 | // -------------------------------------------------------------------- |
| 382 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | /** |
| 384 | * Constructor - Sets Email Preferences |
| 385 | * |
| 386 | * The constructor can be passed an array of config values |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 387 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 388 | * @param array $config = array() |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 389 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 391 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 393 | $this->charset = config_item('charset'); |
Andrey Andreev | 9f44c21 | 2012-10-10 16:07:17 +0300 | [diff] [blame] | 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | if (count($config) > 0) |
| 396 | { |
| 397 | $this->initialize($config); |
| 398 | } |
| 399 | else |
| 400 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 401 | $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Andrey Andreev | 5932a73 | 2013-09-13 14:45:53 +0300 | [diff] [blame] | 404 | $this->_safe_mode = ( ! is_php('5.4') && (bool) @ini_get('safe_mode')); |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 405 | $this->charset = strtoupper($this->charset); |
| 406 | |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 407 | log_message('debug', 'Email Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | } |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 409 | |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 410 | // -------------------------------------------------------------------- |
vlakoff | f399425 | 2013-02-19 01:45:23 +0100 | [diff] [blame] | 411 | |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 412 | /** |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 413 | * Destructor - Releases Resources |
| 414 | * |
| 415 | * @return void |
| 416 | */ |
| 417 | public function __destruct() |
| 418 | { |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 419 | if (is_resource($this->_smtp_connect)) |
| 420 | { |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 421 | $this->_send_command('quit'); |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 422 | } |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 423 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | |
| 425 | // -------------------------------------------------------------------- |
| 426 | |
| 427 | /** |
| 428 | * Initialize preferences |
| 429 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * @param array |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 431 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 433 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | foreach ($config as $key => $val) |
| 436 | { |
| 437 | if (isset($this->$key)) |
| 438 | { |
| 439 | $method = 'set_'.$key; |
| 440 | |
| 441 | if (method_exists($this, $method)) |
| 442 | { |
| 443 | $this->$method($val); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | $this->$key = $val; |
| 448 | } |
| 449 | } |
| 450 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 451 | $this->clear(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 453 | $this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === ''); |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 454 | |
| 455 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | // -------------------------------------------------------------------- |
| 459 | |
| 460 | /** |
| 461 | * Initialize the Email Data |
| 462 | * |
Bo-Yi Wu | 83320eb | 2011-09-15 13:28:02 +0800 | [diff] [blame] | 463 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 464 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 466 | public function clear($clear_attachments = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 468 | $this->_subject = ''; |
| 469 | $this->_body = ''; |
| 470 | $this->_finalbody = ''; |
| 471 | $this->_header_str = ''; |
| 472 | $this->_replyto_flag = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | $this->_recipients = array(); |
Derek Jones | d160635 | 2010-09-01 11:16:07 -0500 | [diff] [blame] | 474 | $this->_cc_array = array(); |
| 475 | $this->_bcc_array = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | $this->_headers = array(); |
| 477 | $this->_debug_msg = array(); |
| 478 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 479 | $this->set_header('User-Agent', $this->useragent); |
| 480 | $this->set_header('Date', $this->_set_date()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | |
| 482 | if ($clear_attachments !== FALSE) |
| 483 | { |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 484 | $this->_attachments = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 486 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 487 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | // -------------------------------------------------------------------- |
| 491 | |
| 492 | /** |
| 493 | * Set FROM |
| 494 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 495 | * @param string $from |
| 496 | * @param string $name |
| 497 | * @param string $return_path = NULL Return-Path |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 498 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | */ |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 500 | public function from($from, $name = '', $return_path = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 502 | if (preg_match('/\<(.*)\>/', $from, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 504 | $from = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if ($this->validate) |
| 508 | { |
| 509 | $this->validate_email($this->_str_to_array($from)); |
Andrey Andreev | ccd01c7 | 2012-10-05 17:12:55 +0300 | [diff] [blame] | 510 | if ($return_path) |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 511 | { |
| 512 | $this->validate_email($this->_str_to_array($return_path)); |
| 513 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // prepare the display name |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 517 | if ($name !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | { |
| 519 | // only use Q encoding if there are characters that would require it |
| 520 | if ( ! preg_match('/[\200-\377]/', $name)) |
| 521 | { |
| 522 | // 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] | 523 | $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | } |
| 525 | else |
| 526 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 527 | $name = $this->_prep_q_encoding($name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 531 | $this->set_header('From', $name.' <'.$from.'>'); |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 532 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 533 | isset($return_path) OR $return_path = $from; |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 534 | $this->set_header('Return-Path', '<'.$return_path.'>'); |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 535 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 536 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | // -------------------------------------------------------------------- |
| 540 | |
| 541 | /** |
| 542 | * Set Reply-to |
| 543 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | * @param string |
| 545 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 546 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 548 | public function reply_to($replyto, $name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 550 | if (preg_match('/\<(.*)\>/', $replyto, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 552 | $replyto = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if ($this->validate) |
| 556 | { |
| 557 | $this->validate_email($this->_str_to_array($replyto)); |
| 558 | } |
| 559 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 560 | if ($name === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | { |
| 562 | $name = $replyto; |
| 563 | } |
| 564 | |
Andrey Andreev | 3dad2e7 | 2012-06-14 15:10:56 +0300 | [diff] [blame] | 565 | if (strpos($name, '"') !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | { |
| 567 | $name = '"'.$name.'"'; |
| 568 | } |
| 569 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 570 | $this->set_header('Reply-To', $name.' <'.$replyto.'>'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | $this->_replyto_flag = TRUE; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 572 | |
| 573 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 575 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | // -------------------------------------------------------------------- |
| 577 | |
| 578 | /** |
| 579 | * Set Recipients |
| 580 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 582 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 584 | public function to($to) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | { |
| 586 | $to = $this->_str_to_array($to); |
| 587 | $to = $this->clean_email($to); |
| 588 | |
| 589 | if ($this->validate) |
| 590 | { |
| 591 | $this->validate_email($to); |
| 592 | } |
| 593 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 594 | if ($this->_get_protocol() !== 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | { |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 596 | $this->set_header('To', implode(', ', $to)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Andrey Andreev | 8a7078b | 2012-10-17 10:52:49 +0300 | [diff] [blame] | 599 | $this->_recipients = $to; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 600 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 601 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | // -------------------------------------------------------------------- |
| 605 | |
| 606 | /** |
| 607 | * Set CC |
| 608 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 609 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 610 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 612 | public function cc($cc) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 614 | $cc = $this->clean_email($this->_str_to_array($cc)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | |
| 616 | if ($this->validate) |
| 617 | { |
| 618 | $this->validate_email($cc); |
| 619 | } |
| 620 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 621 | $this->set_header('Cc', implode(', ', $cc)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 623 | if ($this->_get_protocol() === 'smtp') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | { |
| 625 | $this->_cc_array = $cc; |
| 626 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 627 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 628 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 630 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | // -------------------------------------------------------------------- |
| 632 | |
| 633 | /** |
| 634 | * Set BCC |
| 635 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | * @param string |
| 637 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 638 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 640 | public function bcc($bcc, $limit = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 642 | if ($limit !== '' && is_numeric($limit)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 643 | { |
| 644 | $this->bcc_batch_mode = TRUE; |
| 645 | $this->bcc_batch_size = $limit; |
| 646 | } |
| 647 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 648 | $bcc = $this->clean_email($this->_str_to_array($bcc)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | |
| 650 | if ($this->validate) |
| 651 | { |
| 652 | $this->validate_email($bcc); |
| 653 | } |
| 654 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 655 | 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] | 656 | { |
| 657 | $this->_bcc_array = $bcc; |
| 658 | } |
| 659 | else |
| 660 | { |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 661 | $this->set_header('Bcc', implode(', ', $bcc)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 663 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 664 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | // -------------------------------------------------------------------- |
| 668 | |
| 669 | /** |
| 670 | * Set Email Subject |
| 671 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 673 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 675 | public function subject($subject) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 676 | { |
| 677 | $subject = $this->_prep_q_encoding($subject); |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 678 | $this->set_header('Subject', $subject); |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 679 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 681 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | // -------------------------------------------------------------------- |
| 683 | |
| 684 | /** |
| 685 | * Set Body |
| 686 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 688 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 689 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 690 | public function message($body) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 692 | $this->_body = rtrim(str_replace("\r", '', $body)); |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 693 | |
Andrey Andreev | af72862 | 2011-10-20 10:11:59 +0300 | [diff] [blame] | 694 | /* strip slashes only if magic quotes is ON |
| 695 | if we do it with magic quotes OFF, it strips real, user-inputted chars. |
| 696 | |
| 697 | NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 698 | it will probably not exist in future versions at all. |
| 699 | */ |
| 700 | if ( ! is_php('5.4') && get_magic_quotes_gpc()) |
diegorivera | 9fca615 | 2011-10-19 11:18:45 -0200 | [diff] [blame] | 701 | { |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 702 | $this->_body = stripslashes($this->_body); |
diegorivera | 9fca615 | 2011-10-19 11:18:45 -0200 | [diff] [blame] | 703 | } |
diegorivera | 33c3280 | 2011-10-19 02:56:15 -0200 | [diff] [blame] | 704 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 705 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 706 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 707 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 708 | // -------------------------------------------------------------------- |
| 709 | |
| 710 | /** |
| 711 | * Assign file attachments |
| 712 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 713 | * @param string $filename |
| 714 | * @param string $disposition = 'attachment' |
| 715 | * @param string $newname = NULL |
| 716 | * @param string $mime = '' |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 717 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 718 | */ |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 719 | public function attach($filename, $disposition = '', $newname = NULL, $mime = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 720 | { |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 721 | if ($mime === '') |
| 722 | { |
| 723 | if ( ! file_exists($filename)) |
| 724 | { |
| 725 | $this->_set_error_message('lang:email_attachment_missing', $filename); |
| 726 | return FALSE; |
| 727 | } |
| 728 | |
| 729 | if ( ! $fp = fopen($filename, FOPEN_READ)) |
| 730 | { |
| 731 | $this->_set_error_message('lang:email_attachment_unreadable', $filename); |
| 732 | return FALSE; |
| 733 | } |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 734 | |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 735 | $file_content = stream_get_contents($fp); |
| 736 | $mime = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION)); |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 737 | fclose($fp); |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 738 | } |
| 739 | else |
| 740 | { |
| 741 | $file_content =& $filename; // buffered file |
| 742 | } |
| 743 | |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 744 | $this->_attachments[] = array( |
| 745 | 'name' => array($filename, $newname), |
| 746 | 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 747 | 'type' => $mime, |
| 748 | 'content' => chunk_split(base64_encode($file_content)) |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 749 | ); |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 750 | |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 751 | |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 752 | |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 753 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 754 | } |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 755 | |
| 756 | // -------------------------------------------------------------------- |
| 757 | |
| 758 | /** |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 759 | * Set and return id of attachment |
| 760 | * |
| 761 | * useful for attached inline pictures |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 762 | * |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 763 | * @param string $filename |
| 764 | * @return string |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 765 | */ |
| 766 | public function attach_cid($filename) |
| 767 | { |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 768 | if ($this->multipart !== 'related') |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 769 | { |
| 770 | $this->multipart = 'related'; // Thunderbird need this for inline images |
| 771 | } |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 772 | |
| 773 | for ($i = 0, $c = count($this->_attachments); $i < $c; $i++) |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 774 | { |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 775 | if ($attach['name'][0] === $filename) |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 776 | { |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 777 | $this->_attachments[$ind]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@'); |
| 778 | return $this->_attachments[$i]['cid']; |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 779 | } |
| 780 | } |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 781 | |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 782 | return FALSE; |
| 783 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | |
| 785 | // -------------------------------------------------------------------- |
| 786 | |
| 787 | /** |
| 788 | * Add a Header Item |
| 789 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | * @param string |
| 791 | * @param string |
| 792 | * @return void |
| 793 | */ |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 794 | public function set_header($header, $value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 795 | { |
florisluiten | f55d514 | 2013-06-07 17:20:06 +0300 | [diff] [blame] | 796 | $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 797 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 798 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 799 | // -------------------------------------------------------------------- |
| 800 | |
| 801 | /** |
| 802 | * Convert a String to an Array |
| 803 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 804 | * @param string |
| 805 | * @return array |
| 806 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 807 | protected function _str_to_array($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 808 | { |
| 809 | if ( ! is_array($email)) |
| 810 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 811 | return (strpos($email, ',') !== FALSE) |
| 812 | ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY) |
| 813 | : (array) trim($email); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 814 | } |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 815 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 816 | return $email; |
| 817 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 818 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 819 | // -------------------------------------------------------------------- |
| 820 | |
| 821 | /** |
| 822 | * Set Multipart Value |
| 823 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 824 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 825 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 826 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 827 | public function set_alt_message($str = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 828 | { |
Dan Horrigan | d0ddeaf | 2011-08-21 09:07:27 -0400 | [diff] [blame] | 829 | $this->alt_message = (string) $str; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 830 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 831 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 832 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | // -------------------------------------------------------------------- |
| 834 | |
| 835 | /** |
| 836 | * Set Mailtype |
| 837 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 838 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 839 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 840 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 841 | public function set_mailtype($type = 'text') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 843 | $this->mailtype = ($type === 'html') ? 'html' : 'text'; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 844 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 845 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 846 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | // -------------------------------------------------------------------- |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 848 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 849 | /** |
| 850 | * Set Wordwrap |
| 851 | * |
Dan Horrigan | 628e660 | 2011-08-21 09:08:31 -0400 | [diff] [blame] | 852 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 853 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 854 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 855 | public function set_wordwrap($wordwrap = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | { |
Dan Horrigan | 628e660 | 2011-08-21 09:08:31 -0400 | [diff] [blame] | 857 | $this->wordwrap = (bool) $wordwrap; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 858 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 860 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 861 | // -------------------------------------------------------------------- |
| 862 | |
| 863 | /** |
| 864 | * Set Protocol |
| 865 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 867 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 868 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 869 | public function set_protocol($protocol = 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 871 | $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail'; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 872 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 873 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 874 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | // -------------------------------------------------------------------- |
| 876 | |
| 877 | /** |
| 878 | * Set Priority |
| 879 | * |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 880 | * @param int |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 881 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 882 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 883 | public function set_priority($n = 3) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 884 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 885 | $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 886 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 887 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 888 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 889 | // -------------------------------------------------------------------- |
| 890 | |
| 891 | /** |
| 892 | * Set Newline Character |
| 893 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 894 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 895 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 897 | public function set_newline($newline = "\n") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 898 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 899 | $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n"; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 900 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 901 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 902 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 903 | // -------------------------------------------------------------------- |
| 904 | |
| 905 | /** |
| 906 | * Set CRLF |
| 907 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 908 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 909 | * @return CI_Email |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 911 | public function set_crlf($crlf = "\n") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 912 | { |
Andrey Andreev | 76f15c9 | 2012-03-01 13:05:07 +0200 | [diff] [blame] | 913 | $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf; |
Greg Aker | a769deb | 2010-11-10 15:47:29 -0600 | [diff] [blame] | 914 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 916 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | // -------------------------------------------------------------------- |
| 918 | |
| 919 | /** |
| 920 | * Set Message Boundary |
| 921 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | * @return void |
| 923 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 924 | protected function _set_boundaries() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 925 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 926 | $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative |
| 927 | $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 929 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | // -------------------------------------------------------------------- |
| 931 | |
| 932 | /** |
| 933 | * Get the Message ID |
| 934 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 935 | * @return string |
| 936 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 937 | protected function _get_message_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 939 | $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 940 | return '<'.uniqid('').strstr($from, '@').'>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 941 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 942 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 943 | // -------------------------------------------------------------------- |
| 944 | |
| 945 | /** |
| 946 | * Get Mail Protocol |
| 947 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | * @param bool |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 949 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 950 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 951 | protected function _get_protocol($return = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 952 | { |
| 953 | $this->protocol = strtolower($this->protocol); |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 954 | in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 955 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 956 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | { |
| 958 | return $this->protocol; |
| 959 | } |
| 960 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 961 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | // -------------------------------------------------------------------- |
| 963 | |
| 964 | /** |
| 965 | * Get Mail Encoding |
| 966 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | * @param bool |
| 968 | * @return string |
| 969 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 970 | protected function _get_encoding($return = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 971 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 972 | in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 973 | |
| 974 | foreach ($this->_base_charsets as $charset) |
| 975 | { |
Andrey Andreev | 3dad2e7 | 2012-06-14 15:10:56 +0300 | [diff] [blame] | 976 | if (strpos($charset, $this->charset) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 977 | { |
| 978 | $this->_encoding = '7bit'; |
| 979 | } |
| 980 | } |
| 981 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 982 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 983 | { |
| 984 | return $this->_encoding; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // -------------------------------------------------------------------- |
| 989 | |
| 990 | /** |
| 991 | * Get content type (text/html/attachment) |
| 992 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 993 | * @return string |
| 994 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 995 | protected function _get_content_type() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 997 | if ($this->mailtype === 'html') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 998 | { |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 999 | return (count($this->_attachments) === 0) ? 'html' : 'html-attach'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1000 | } |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 1001 | elseif ($this->mailtype === 'text' && count($this->_attachments) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | { |
| 1003 | return 'plain-attach'; |
| 1004 | } |
| 1005 | else |
| 1006 | { |
| 1007 | return 'plain'; |
| 1008 | } |
| 1009 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1010 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1011 | // -------------------------------------------------------------------- |
| 1012 | |
| 1013 | /** |
| 1014 | * Set RFC 822 Date |
| 1015 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1016 | * @return string |
| 1017 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1018 | protected function _set_date() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1019 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1020 | $timezone = date('Z'); |
Andrey Andreev | 3dad2e7 | 2012-06-14 15:10:56 +0300 | [diff] [blame] | 1021 | $operator = ($timezone[0] === '-') ? '-' : '+'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1022 | $timezone = abs($timezone); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1023 | $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1024 | |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1025 | return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1026 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1027 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1028 | // -------------------------------------------------------------------- |
| 1029 | |
| 1030 | /** |
| 1031 | * Mime message |
| 1032 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | * @return string |
| 1034 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1035 | protected function _get_mime_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1037 | return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1038 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1039 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1040 | // -------------------------------------------------------------------- |
| 1041 | |
| 1042 | /** |
| 1043 | * Validate Email Address |
| 1044 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1045 | * @param string |
| 1046 | * @return bool |
| 1047 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1048 | public function validate_email($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1049 | { |
| 1050 | if ( ! is_array($email)) |
| 1051 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1052 | $this->_set_error_message('lang:email_must_be_array'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1053 | return FALSE; |
| 1054 | } |
| 1055 | |
| 1056 | foreach ($email as $val) |
| 1057 | { |
| 1058 | if ( ! $this->valid_email($val)) |
| 1059 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1060 | $this->_set_error_message('lang:email_invalid_address', $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | return FALSE; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | return TRUE; |
| 1066 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1067 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1068 | // -------------------------------------------------------------------- |
| 1069 | |
| 1070 | /** |
| 1071 | * Email Validation |
| 1072 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1073 | * @param string |
| 1074 | * @return bool |
| 1075 | */ |
Timothy Warren | d37f0e9 | 2012-06-27 08:21:59 -0400 | [diff] [blame] | 1076 | public function valid_email($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1077 | { |
Andrey Andreev | 580388b | 2012-06-27 15:43:46 +0300 | [diff] [blame] | 1078 | return (bool) filter_var($email, FILTER_VALIDATE_EMAIL); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1079 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1080 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1081 | // -------------------------------------------------------------------- |
| 1082 | |
| 1083 | /** |
| 1084 | * Clean Extended Email Address: Joe Smith <joe@smith.com> |
| 1085 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1086 | * @param string |
| 1087 | * @return string |
| 1088 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1089 | public function clean_email($email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1090 | { |
| 1091 | if ( ! is_array($email)) |
| 1092 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1093 | return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | $clean_email = array(); |
| 1097 | |
| 1098 | foreach ($email as $addy) |
| 1099 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1100 | $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | return $clean_email; |
| 1104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1106 | // -------------------------------------------------------------------- |
| 1107 | |
| 1108 | /** |
| 1109 | * Build alternative plain text message |
| 1110 | * |
Andrey Andreev | 8df1ae2 | 2012-10-19 11:20:54 +0300 | [diff] [blame] | 1111 | * Provides the raw message for use in plain-text headers of |
| 1112 | * HTML-formatted emails. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1113 | * If the user hasn't specified his own alternative message |
| 1114 | * it creates one by stripping the HTML |
| 1115 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1116 | * @return string |
| 1117 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1118 | protected function _get_alt_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1119 | { |
Andrey Andreev | 8df1ae2 | 2012-10-19 11:20:54 +0300 | [diff] [blame] | 1120 | if ( ! empty($this->alt_message)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1121 | { |
Andrey Andreev | 8df1ae2 | 2012-10-19 11:20:54 +0300 | [diff] [blame] | 1122 | return ($this->wordwrap) |
| 1123 | ? $this->word_wrap($this->alt_message, 76) |
| 1124 | : $this->alt_message; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1127 | $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1128 | $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1129 | |
| 1130 | for ($i = 20; $i >= 3; $i--) |
| 1131 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1132 | $body = str_replace(str_repeat("\n", $i), "\n\n", $body); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
GDmac | 9bea4be | 2012-10-30 06:14:19 +0100 | [diff] [blame] | 1135 | // Reduce multiple spaces |
Andrey Andreev | 8a203f6 | 2012-11-02 20:40:43 +0200 | [diff] [blame] | 1136 | $body = preg_replace('| +|', ' ', $body); |
GDmac | 9bea4be | 2012-10-30 06:14:19 +0100 | [diff] [blame] | 1137 | |
Andrey Andreev | 8df1ae2 | 2012-10-19 11:20:54 +0300 | [diff] [blame] | 1138 | return ($this->wordwrap) |
| 1139 | ? $this->word_wrap($body, 76) |
| 1140 | : $body; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1141 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1143 | // -------------------------------------------------------------------- |
| 1144 | |
| 1145 | /** |
| 1146 | * Word Wrap |
| 1147 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1148 | * @param string |
Andrey Andreev | 8df1ae2 | 2012-10-19 11:20:54 +0300 | [diff] [blame] | 1149 | * @param int line-length limit |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1150 | * @return string |
| 1151 | */ |
Andrey Andreev | 00ea2a9 | 2012-10-18 14:59:29 +0300 | [diff] [blame] | 1152 | public function word_wrap($str, $charlim = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1153 | { |
Andrey Andreev | 00ea2a9 | 2012-10-18 14:59:29 +0300 | [diff] [blame] | 1154 | // Set the character limit, if not already present |
| 1155 | if (empty($charlim)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1156 | { |
Andrey Andreev | 00ea2a9 | 2012-10-18 14:59:29 +0300 | [diff] [blame] | 1157 | $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1160 | // Standardize newlines |
| 1161 | if (strpos($str, "\r") !== FALSE) |
| 1162 | { |
Andrey Andreev | b71e06b | 2011-12-22 19:22:50 +0200 | [diff] [blame] | 1163 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
GDmac | 9bea4be | 2012-10-30 06:14:19 +0100 | [diff] [blame] | 1166 | // Reduce multiple spaces at end of line |
| 1167 | $str = preg_replace('| +\n|', "\n", $str); |
| 1168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1169 | // If the current word is surrounded by {unwrap} tags we'll |
| 1170 | // strip the entire chunk and replace it with a marker. |
| 1171 | $unwrap = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1172 | if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1173 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1174 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1175 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1176 | $unwrap[] = $matches[1][$i]; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1177 | $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1181 | // Use PHP's native public function to do the initial wordwrap. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1182 | // We set the cut flag to FALSE so that any individual words that are |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1183 | // 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] | 1184 | $str = wordwrap($str, $charlim, "\n", FALSE); |
| 1185 | |
| 1186 | // Split the string into individual lines of text and cycle through them |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1187 | $output = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1188 | foreach (explode("\n", $str) as $line) |
| 1189 | { |
| 1190 | // Is the line within the allowed character count? |
| 1191 | // If so we'll join it to the output and continue |
| 1192 | if (strlen($line) <= $charlim) |
| 1193 | { |
| 1194 | $output .= $line.$this->newline; |
| 1195 | continue; |
| 1196 | } |
| 1197 | |
| 1198 | $temp = ''; |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1199 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1200 | { |
| 1201 | // If the over-length word is a URL we won't wrap it |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1202 | if (preg_match('!\[url.+\]|://|wwww.!', $line)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1203 | { |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | // Trim the word down |
| 1208 | $temp .= substr($line, 0, $charlim-1); |
| 1209 | $line = substr($line, $charlim-1); |
| 1210 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1211 | while (strlen($line) > $charlim); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1212 | |
| 1213 | // If $temp contains data it means we had to split up an over-length |
| 1214 | // word into smaller chunks so we'll add it back to our current line |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1215 | if ($temp !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1216 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1217 | $output .= $temp.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1220 | $output .= $line.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | // Put our markers back |
| 1224 | if (count($unwrap) > 0) |
| 1225 | { |
| 1226 | foreach ($unwrap as $key => $val) |
| 1227 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1228 | $output = str_replace('{{unwrapped'.$key.'}}', $val, $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | return $output; |
| 1233 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1235 | // -------------------------------------------------------------------- |
| 1236 | |
| 1237 | /** |
| 1238 | * Build final headers |
| 1239 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1240 | * @return string |
| 1241 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1242 | protected function _build_headers() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1243 | { |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 1244 | $this->set_header('X-Sender', $this->clean_email($this->_headers['From'])); |
| 1245 | $this->set_header('X-Mailer', $this->useragent); |
| 1246 | $this->set_header('X-Priority', $this->_priorities[$this->priority - 1]); |
| 1247 | $this->set_header('Message-ID', $this->_get_message_id()); |
| 1248 | $this->set_header('Mime-Version', '1.0'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1249 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1250 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1251 | // -------------------------------------------------------------------- |
| 1252 | |
| 1253 | /** |
| 1254 | * Write Headers as a string |
| 1255 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | * @return void |
| 1257 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1258 | protected function _write_headers() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1259 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1260 | if ($this->protocol === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1261 | { |
nisheeth-barthwal | 6bb9890 | 2013-02-19 18:11:14 +0530 | [diff] [blame] | 1262 | if (isset($this->_headers['Subject'])) |
| 1263 | { |
| 1264 | $this->_subject = $this->_headers['Subject']; |
| 1265 | unset($this->_headers['Subject']); |
| 1266 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | reset($this->_headers); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1270 | $this->_header_str = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1271 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1272 | foreach ($this->_headers as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1273 | { |
| 1274 | $val = trim($val); |
| 1275 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1276 | if ($val !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1277 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1278 | $this->_header_str .= $key.': '.$val.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1282 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1283 | { |
Derek Jones | 1d89088 | 2009-02-10 20:32:14 +0000 | [diff] [blame] | 1284 | $this->_header_str = rtrim($this->_header_str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1288 | // -------------------------------------------------------------------- |
| 1289 | |
| 1290 | /** |
| 1291 | * Build Final Body and attachments |
| 1292 | * |
buhay | c6da1ef | 2013-04-18 09:06:49 -0700 | [diff] [blame] | 1293 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1294 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1295 | protected function _build_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1296 | { |
Andrey Andreev | 76f15c9 | 2012-03-01 13:05:07 +0200 | [diff] [blame] | 1297 | if ($this->wordwrap === TRUE && $this->mailtype !== 'html') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1298 | { |
| 1299 | $this->_body = $this->word_wrap($this->_body); |
| 1300 | } |
| 1301 | |
| 1302 | $this->_set_boundaries(); |
| 1303 | $this->_write_headers(); |
| 1304 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1305 | $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : ''; |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1306 | $body = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1307 | |
| 1308 | switch ($this->_get_content_type()) |
| 1309 | { |
| 1310 | case 'plain' : |
| 1311 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1312 | $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline |
| 1313 | .'Content-Transfer-Encoding: '.$this->_get_encoding(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1314 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1315 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1316 | { |
| 1317 | $this->_header_str .= $hdr; |
| 1318 | $this->_finalbody = $this->_body; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1319 | } |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1320 | else |
| 1321 | { |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1322 | $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body; |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1323 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | case 'html' : |
| 1328 | |
| 1329 | if ($this->send_multipart === FALSE) |
| 1330 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1331 | $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1332 | .'Content-Transfer-Encoding: quoted-printable'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1333 | } |
| 1334 | else |
| 1335 | { |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1336 | $hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1337 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1338 | $body .= $this->_get_mime_message().$this->newline.$this->newline |
| 1339 | .'--'.$this->_alt_boundary.$this->newline |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1340 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1341 | .'Content-Type: text/plain; charset='.$this->charset.$this->newline |
| 1342 | .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline |
| 1343 | .$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] | 1344 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1345 | .'Content-Type: text/html; charset='.$this->charset.$this->newline |
| 1346 | .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1347 | } |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1348 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1349 | $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] | 1350 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1351 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1352 | { |
| 1353 | $this->_header_str .= $hdr; |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1354 | } |
| 1355 | else |
| 1356 | { |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1357 | $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1360 | if ($this->send_multipart !== FALSE) |
| 1361 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1362 | $this->_finalbody .= '--'.$this->_alt_boundary.'--'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1367 | case 'plain-attach' : |
| 1368 | |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1369 | $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1370 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1371 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1372 | { |
| 1373 | $this->_header_str .= $hdr; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1374 | } |
| 1375 | |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1376 | $body .= $this->_get_mime_message().$this->newline |
| 1377 | .$this->newline |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1378 | .'--'.$this->_atc_boundary.$this->newline |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1379 | .'Content-Type: text/plain; charset='.$this->charset.$this->newline |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1380 | .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline |
| 1381 | .$this->newline |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1382 | .$this->_body.$this->newline.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1383 | |
| 1384 | break; |
| 1385 | case 'html-attach' : |
| 1386 | |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1387 | $hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"'; |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1388 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1389 | if ($this->_get_protocol() === 'mail') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1390 | { |
| 1391 | $this->_header_str .= $hdr; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1394 | $body .= $this->_get_mime_message().$this->newline.$this->newline |
| 1395 | .'--'.$this->_atc_boundary.$this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1396 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1397 | .'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline |
| 1398 | .'--'.$this->_alt_boundary.$this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1399 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1400 | .'Content-Type: text/plain; charset='.$this->charset.$this->newline |
| 1401 | .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline |
| 1402 | .$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] | 1403 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1404 | .'Content-Type: text/html; charset='.$this->charset.$this->newline |
| 1405 | .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline |
Brandon Jones | 485d741 | 2010-11-09 16:38:17 -0500 | [diff] [blame] | 1406 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1407 | .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline |
| 1408 | .'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1409 | |
| 1410 | break; |
| 1411 | } |
| 1412 | |
| 1413 | $attachment = array(); |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 1414 | for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1415 | { |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 1416 | $filename = $this->_attachments[$i]['name'][0]; |
vlakoff | 912f1bc | 2013-01-15 03:34:12 +0100 | [diff] [blame] | 1417 | $basename = ($this->_attachments[$i]['name'][1] === NULL) |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 1418 | ? basename($filename) : $this->_attachments[$i]['name'][1]; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1419 | |
| 1420 | $attachment[$z++] = '--'.$this->_atc_boundary.$this->newline |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 1421 | .'Content-type: '.$this->_attachments[$i]['type'].'; ' |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1422 | .'name="'.$basename.'"'.$this->newline |
Andrey Andreev | b8f9a15 | 2012-10-27 01:36:51 +0300 | [diff] [blame] | 1423 | .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 1424 | .'Content-Transfer-Encoding: base64'.$this->newline |
Petr Heralecky | de88615 | 2014-01-10 12:52:56 +0100 | [diff] [blame^] | 1425 | .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1426 | |
Petr Heralecky | 300e3f0 | 2014-01-10 11:49:11 +0100 | [diff] [blame] | 1427 | $attachment[$z++] = $this->_attachments[$i]['content']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1430 | $body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--'; |
Andrey Andreev | 2b956af | 2013-08-07 14:32:56 +0300 | [diff] [blame] | 1431 | $this->_finalbody = ($this->_get_protocol() === 'mail') |
| 1432 | ? $body |
| 1433 | : $hdr.$this->newline.$this->newline.$body; |
buhay | 466e808 | 2013-04-17 14:25:34 -0700 | [diff] [blame] | 1434 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1437 | // -------------------------------------------------------------------- |
| 1438 | |
| 1439 | /** |
| 1440 | * Prep Quoted Printable |
| 1441 | * |
| 1442 | * Prepares string for Quoted-Printable Content-Transfer-Encoding |
| 1443 | * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt |
| 1444 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1445 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1446 | * @return string |
| 1447 | */ |
Andrey Andreev | 683b34d | 2012-10-09 15:00:00 +0300 | [diff] [blame] | 1448 | protected function _prep_quoted_printable($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1449 | { |
Andrey Andreev | 00ea2a9 | 2012-10-18 14:59:29 +0300 | [diff] [blame] | 1450 | // We are intentionally wrapping so mail servers will encode characters |
| 1451 | // properly and MUAs will behave, so {unwrap} must go! |
| 1452 | $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str); |
| 1453 | |
Andrey Andreev | 683b34d | 2012-10-09 15:00:00 +0300 | [diff] [blame] | 1454 | // RFC 2045 specifies CRLF as "\r\n". |
| 1455 | // However, many developers choose to override that and violate |
| 1456 | // the RFC rules due to (apparently) a bug in MS Exchange, |
| 1457 | // which only works with "\n". |
Andrey Andreev | 26f0cf9 | 2012-10-11 13:52:39 +0300 | [diff] [blame] | 1458 | if ($this->crlf === "\r\n") |
Andrey Andreev | 683b34d | 2012-10-09 15:00:00 +0300 | [diff] [blame] | 1459 | { |
Andrey Andreev | 26f0cf9 | 2012-10-11 13:52:39 +0300 | [diff] [blame] | 1460 | if (is_php('5.3')) |
| 1461 | { |
| 1462 | return quoted_printable_encode($str); |
| 1463 | } |
| 1464 | elseif (function_exists('imap_8bit')) |
| 1465 | { |
| 1466 | return imap_8bit($str); |
| 1467 | } |
Andrey Andreev | 683b34d | 2012-10-09 15:00:00 +0300 | [diff] [blame] | 1468 | } |
| 1469 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1470 | // Reduce multiple spaces & remove nulls |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1471 | $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Standardize newlines |
| 1474 | if (strpos($str, "\r") !== FALSE) |
| 1475 | { |
| 1476 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 1477 | } |
| 1478 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1479 | $escape = '='; |
| 1480 | $output = ''; |
| 1481 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1482 | foreach (explode("\n", $str) as $line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1483 | { |
| 1484 | $length = strlen($line); |
| 1485 | $temp = ''; |
| 1486 | |
| 1487 | // Loop through each character in the line to add soft-wrap |
| 1488 | // characters at the end of a line " =\r\n" and add the newly |
| 1489 | // processed line(s) to the output (see comment on $crlf class property) |
| 1490 | for ($i = 0; $i < $length; $i++) |
| 1491 | { |
| 1492 | // Grab the next character |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1493 | $char = $line[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1494 | $ascii = ord($char); |
| 1495 | |
| 1496 | // 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] | 1497 | if ($i === ($length - 1) && ($ascii === 32 OR $ascii === 9)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1498 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1499 | $char = $escape.sprintf('%02s', dechex($ascii)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1500 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1501 | elseif ($ascii === 61) // encode = signs |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1502 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1503 | $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | // If we're at the character limit, add the line to the output, |
| 1507 | // reset our temp variable, and keep on chuggin' |
Andrey Andreev | e8bc5f4 | 2012-10-10 11:13:59 +0300 | [diff] [blame] | 1508 | if ((strlen($temp) + strlen($char)) >= 76) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1509 | { |
| 1510 | $output .= $temp.$escape.$this->crlf; |
| 1511 | $temp = ''; |
| 1512 | } |
| 1513 | |
| 1514 | // Add the character to our temporary line |
| 1515 | $temp .= $char; |
| 1516 | } |
| 1517 | |
| 1518 | // Add our completed line to the output |
| 1519 | $output .= $temp.$this->crlf; |
| 1520 | } |
| 1521 | |
| 1522 | // get rid of extra CRLF tacked onto the end |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1523 | return substr($output, 0, strlen($this->crlf) * -1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1527 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1528 | /** |
| 1529 | * Prep Q Encoding |
| 1530 | * |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1531 | * Performs "Q Encoding" on a string for use in email headers. |
| 1532 | * It's related but not identical to quoted-printable, so it has its |
| 1533 | * own method. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1534 | * |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 1535 | * @param string |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 1536 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1537 | */ |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1538 | protected function _prep_q_encoding($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1539 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1540 | $str = str_replace(array("\r", "\n"), '', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1541 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1542 | if ($this->charset === 'UTF-8') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1543 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1544 | if (MB_ENABLED === TRUE) |
| 1545 | { |
| 1546 | return mb_encode_mimeheader($str, $this->charset, 'Q', $this->crlf); |
| 1547 | } |
| 1548 | elseif (extension_loaded('iconv')) |
| 1549 | { |
| 1550 | $output = @iconv_mime_encode('', $str, |
| 1551 | array( |
| 1552 | 'scheme' => 'Q', |
| 1553 | 'line-length' => 76, |
| 1554 | 'input-charset' => $this->charset, |
| 1555 | 'output-charset' => $this->charset, |
| 1556 | 'line-break-chars' => $this->crlf |
| 1557 | ) |
| 1558 | ); |
| 1559 | |
| 1560 | // There are reports that iconv_mime_encode() might fail and return FALSE |
| 1561 | if ($output !== FALSE) |
| 1562 | { |
| 1563 | // iconv_mime_encode() will always put a header field name. |
| 1564 | // We've passed it an empty one, but it still prepends our |
| 1565 | // encoded string with ': ', so we need to strip it. |
| 1566 | return substr($output, 2); |
| 1567 | } |
| 1568 | |
| 1569 | $chars = iconv_strlen($str, 'UTF-8'); |
| 1570 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1573 | // We might already have this set for UTF-8 |
| 1574 | isset($chars) OR $chars = strlen($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1575 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1576 | $output = '=?'.$this->charset.'?Q?'; |
| 1577 | for ($i = 0, $length = strlen($output), $iconv = extension_loaded('iconv'); $i < $chars; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1578 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1579 | $chr = ($this->charset === 'UTF-8' && $iconv === TRUE) |
| 1580 | ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2)) |
| 1581 | : '='.strtoupper(bin2hex($str[$i])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1582 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1583 | // RFC 2045 sets a limit of 76 characters per line. |
| 1584 | // We'll append ?= to the end of each line though. |
| 1585 | if ($length + ($l = strlen($chr)) > 74) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1586 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1587 | $output .= '?='.$this->crlf // EOL |
| 1588 | .' =?'.$this->charset.'?Q?'.$chr; // New line |
| 1589 | $length = 6 + strlen($this->charset) + $l; // Reset the length for the new line |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1590 | } |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1591 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1592 | { |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1593 | $output .= $chr; |
| 1594 | $length += $l; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1595 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Andrey Andreev | 925dd90 | 2012-10-19 11:06:31 +0300 | [diff] [blame] | 1598 | // End the header |
| 1599 | return $output.'?='; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1603 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1604 | /** |
| 1605 | * Send Email |
| 1606 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1607 | * @param bool $auto_clear = TRUE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1608 | * @return bool |
| 1609 | */ |
Alex Bilbie | d7bc8d0 | 2012-07-30 09:46:20 +0100 | [diff] [blame] | 1610 | public function send($auto_clear = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1611 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1612 | if ($this->_replyto_flag === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1613 | { |
| 1614 | $this->reply_to($this->_headers['From']); |
| 1615 | } |
| 1616 | |
Andrey Andreev | 76f15c9 | 2012-03-01 13:05:07 +0200 | [diff] [blame] | 1617 | if ( ! isset($this->_recipients) && ! isset($this->_headers['To']) |
| 1618 | && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc']) |
| 1619 | && ! isset($this->_headers['Cc'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1620 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1621 | $this->_set_error_message('lang:email_no_recipients'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1622 | return FALSE; |
| 1623 | } |
| 1624 | |
| 1625 | $this->_build_headers(); |
| 1626 | |
Andrey Andreev | 76f15c9 | 2012-03-01 13:05:07 +0200 | [diff] [blame] | 1627 | 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] | 1628 | { |
Alex Bilbie | b901e73 | 2012-07-30 09:44:57 +0100 | [diff] [blame] | 1629 | $result = $this->batch_bcc_send(); |
Alex Bilbie | d7bc8d0 | 2012-07-30 09:46:20 +0100 | [diff] [blame] | 1630 | |
Alex Bilbie | a87aab3 | 2012-07-30 09:50:37 +0100 | [diff] [blame] | 1631 | if ($result && $auto_clear) |
Alex Bilbie | d7bc8d0 | 2012-07-30 09:46:20 +0100 | [diff] [blame] | 1632 | { |
| 1633 | $this->clear(); |
| 1634 | } |
| 1635 | |
Alex Bilbie | b901e73 | 2012-07-30 09:44:57 +0100 | [diff] [blame] | 1636 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
buhay | 466e808 | 2013-04-17 14:25:34 -0700 | [diff] [blame] | 1639 | if ($this->_build_message() === FALSE) |
| 1640 | { |
| 1641 | return FALSE; |
| 1642 | } |
buhay | c6da1ef | 2013-04-18 09:06:49 -0700 | [diff] [blame] | 1643 | |
Alex Bilbie | b901e73 | 2012-07-30 09:44:57 +0100 | [diff] [blame] | 1644 | $result = $this->_spool_email(); |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 1645 | |
Alex Bilbie | a87aab3 | 2012-07-30 09:50:37 +0100 | [diff] [blame] | 1646 | if ($result && $auto_clear) |
Alex Bilbie | d7bc8d0 | 2012-07-30 09:46:20 +0100 | [diff] [blame] | 1647 | { |
| 1648 | $this->clear(); |
| 1649 | } |
Alex Bilbie | a87aab3 | 2012-07-30 09:50:37 +0100 | [diff] [blame] | 1650 | |
Alex Bilbie | b901e73 | 2012-07-30 09:44:57 +0100 | [diff] [blame] | 1651 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1652 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1653 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1654 | // -------------------------------------------------------------------- |
| 1655 | |
| 1656 | /** |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 1657 | * Batch Bcc Send. Sends groups of BCCs in batches |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1658 | * |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 1659 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1660 | */ |
Phil Sturgeon | a0f980e | 2011-01-13 10:59:12 +0000 | [diff] [blame] | 1661 | public function batch_bcc_send() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1662 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1663 | $float = $this->bcc_batch_size - 1; |
| 1664 | $set = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1665 | $chunk = array(); |
| 1666 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1667 | for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1668 | { |
| 1669 | if (isset($this->_bcc_array[$i])) |
| 1670 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1671 | $set .= ', '.$this->_bcc_array[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1674 | if ($i === $float) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1675 | { |
| 1676 | $chunk[] = substr($set, 1); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1677 | $float += $this->bcc_batch_size; |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 1678 | $set = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1681 | if ($i === $c-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1682 | { |
| 1683 | $chunk[] = substr($set, 1); |
| 1684 | } |
| 1685 | } |
| 1686 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1687 | for ($i = 0, $c = count($chunk); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1688 | { |
| 1689 | unset($this->_headers['Bcc']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1690 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1691 | $bcc = $this->clean_email($this->_str_to_array($chunk[$i])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1692 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1693 | if ($this->protocol !== 'smtp') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1694 | { |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 1695 | $this->set_header('Bcc', implode(', ', $bcc)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1696 | } |
| 1697 | else |
| 1698 | { |
| 1699 | $this->_bcc_array = $bcc; |
| 1700 | } |
| 1701 | |
buhay | 466e808 | 2013-04-17 14:25:34 -0700 | [diff] [blame] | 1702 | if ($this->_build_message() === FALSE) |
| 1703 | { |
| 1704 | return FALSE; |
| 1705 | } |
buhay | c6da1ef | 2013-04-18 09:06:49 -0700 | [diff] [blame] | 1706 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1707 | $this->_spool_email(); |
| 1708 | } |
| 1709 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1710 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1711 | // -------------------------------------------------------------------- |
| 1712 | |
| 1713 | /** |
| 1714 | * Unwrap special elements |
| 1715 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1716 | * @return void |
| 1717 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1718 | protected function _unwrap_specials() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1719 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1720 | $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1721 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1722 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1723 | // -------------------------------------------------------------------- |
| 1724 | |
| 1725 | /** |
| 1726 | * Strip line-breaks via callback |
| 1727 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1728 | * @param string $matches |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1729 | * @return string |
| 1730 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1731 | protected function _remove_nl_callback($matches) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1732 | { |
| 1733 | if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE) |
| 1734 | { |
| 1735 | $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]); |
| 1736 | } |
| 1737 | |
| 1738 | return $matches[1]; |
| 1739 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1740 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1741 | // -------------------------------------------------------------------- |
| 1742 | |
| 1743 | /** |
| 1744 | * Spool mail to the mail server |
| 1745 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1746 | * @return bool |
| 1747 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1748 | protected function _spool_email() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1749 | { |
| 1750 | $this->_unwrap_specials(); |
| 1751 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1752 | $method = '_send_with_'.$this->_get_protocol(); |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1753 | if ( ! $this->$method()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1754 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1755 | $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol())); |
Diogo Osório | 7fcc7bd | 2012-03-02 16:54:52 +0000 | [diff] [blame] | 1756 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1759 | $this->_set_error_message('lang:email_sent', $this->_get_protocol()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1760 | return TRUE; |
| 1761 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1762 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1763 | // -------------------------------------------------------------------- |
| 1764 | |
| 1765 | /** |
| 1766 | * Send using mail() |
| 1767 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1768 | * @return bool |
| 1769 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1770 | protected function _send_with_mail() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1771 | { |
Andrey Andreev | 8a7078b | 2012-10-17 10:52:49 +0300 | [diff] [blame] | 1772 | if (is_array($this->_recipients)) |
| 1773 | { |
| 1774 | $this->_recipients = implode(', ', $this->_recipients); |
| 1775 | } |
| 1776 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1777 | if ($this->_safe_mode === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1778 | { |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1779 | return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else |
| 1782 | { |
| 1783 | // most documentation of sendmail using the "-f" flag lacks a space after it, however |
| 1784 | // we've encountered servers that seem to require it to be in place. |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 1785 | return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path'])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1786 | } |
| 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 | * Send using Sendmail |
| 1793 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1794 | * @return bool |
| 1795 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1796 | protected function _send_with_sendmail() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1797 | { |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 1798 | // is popen() enabled? |
| 1799 | if ( ! function_usable('popen') |
| 1800 | OR FALSE === ($fp = @popen( |
| 1801 | $this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']) |
| 1802 | .' -t -r '.$this->clean_email($this->_headers['Return-Path']) |
| 1803 | , 'w')) |
| 1804 | ) // server probably has popen disabled, so nothing we can do to get a verbose error. |
Derek Jones | 4cefaa4 | 2009-04-29 19:13:56 +0000 | [diff] [blame] | 1805 | { |
Derek Jones | 4cefaa4 | 2009-04-29 19:13:56 +0000 | [diff] [blame] | 1806 | return FALSE; |
| 1807 | } |
Derek Jones | 71141ce | 2010-03-02 16:41:20 -0600 | [diff] [blame] | 1808 | |
Derek Jones | c630bcf | 2008-11-17 21:09:45 +0000 | [diff] [blame] | 1809 | fputs($fp, $this->_header_str); |
| 1810 | fputs($fp, $this->_finalbody); |
| 1811 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1812 | $status = pclose($fp); |
Eric Barnes | 6113f54 | 2010-12-29 13:36:12 -0500 | [diff] [blame] | 1813 | |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 1814 | if ($status !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1815 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 1816 | $this->_set_error_message('lang:email_exit_status', $status); |
| 1817 | $this->_set_error_message('lang:email_no_socket'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1818 | return FALSE; |
| 1819 | } |
| 1820 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1821 | return TRUE; |
| 1822 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1823 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1824 | // -------------------------------------------------------------------- |
| 1825 | |
| 1826 | /** |
| 1827 | * Send using SMTP |
| 1828 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1829 | * @return bool |
| 1830 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 1831 | protected function _send_with_smtp() |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1832 | { |
| 1833 | if ($this->smtp_host === '') |
| 1834 | { |
| 1835 | $this->_set_error_message('lang:email_no_hostname'); |
| 1836 | return FALSE; |
| 1837 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1838 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1839 | if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate()) |
| 1840 | { |
| 1841 | return FALSE; |
| 1842 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1843 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1844 | $this->_send_command('from', $this->clean_email($this->_headers['From'])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1845 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1846 | foreach ($this->_recipients as $val) |
| 1847 | { |
| 1848 | $this->_send_command('to', $val); |
| 1849 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1850 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1851 | if (count($this->_cc_array) > 0) |
| 1852 | { |
| 1853 | foreach ($this->_cc_array as $val) |
| 1854 | { |
| 1855 | if ($val !== '') |
| 1856 | { |
| 1857 | $this->_send_command('to', $val); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1861 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1862 | if (count($this->_bcc_array) > 0) |
| 1863 | { |
| 1864 | foreach ($this->_bcc_array as $val) |
| 1865 | { |
| 1866 | if ($val !== '') |
| 1867 | { |
| 1868 | $this->_send_command('to', $val); |
| 1869 | } |
| 1870 | } |
| 1871 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1872 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1873 | $this->_send_command('data'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1874 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1875 | // perform dot transformation on any lines that begin with a dot |
| 1876 | $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1877 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1878 | $this->_send_data('.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1879 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1880 | $reply = $this->_get_smtp_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1881 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1882 | $this->_set_error_message($reply); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1883 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1884 | if (strpos($reply, '250') !== 0) |
| 1885 | { |
| 1886 | $this->_set_error_message('lang:email_smtp_error', $reply); |
| 1887 | return FALSE; |
| 1888 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1889 | |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 1890 | if ($this->smtp_keepalive) |
| 1891 | { |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1892 | $this->_send_command('reset'); |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 1893 | } |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1894 | else |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 1895 | { |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1896 | $this->_send_command('quit'); |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 1897 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1898 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1899 | return TRUE; |
| 1900 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1901 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1902 | // -------------------------------------------------------------------- |
Radu Potop | 4c589ae | 2011-09-29 10:19:55 +0300 | [diff] [blame] | 1903 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1904 | /** |
| 1905 | * SMTP Connect |
| 1906 | * |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1907 | * @return string |
| 1908 | */ |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1909 | protected function _smtp_connect() |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1910 | { |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1911 | if (is_resource($this->_smtp_connect)) |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1912 | { |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1913 | return TRUE; |
| 1914 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1915 | |
Andrey Andreev | 89b67b4 | 2013-03-12 19:34:23 +0200 | [diff] [blame] | 1916 | $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1917 | |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1918 | $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, |
| 1919 | $this->smtp_port, |
| 1920 | $errno, |
| 1921 | $errstr, |
| 1922 | $this->smtp_timeout); |
| 1923 | |
| 1924 | if ( ! is_resource($this->_smtp_connect)) |
| 1925 | { |
| 1926 | $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); |
| 1927 | return FALSE; |
| 1928 | } |
| 1929 | |
| 1930 | stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); |
| 1931 | $this->_set_error_message($this->_get_smtp_data()); |
| 1932 | |
| 1933 | if ($this->smtp_crypto === 'tls') |
| 1934 | { |
| 1935 | $this->_send_command('hello'); |
| 1936 | $this->_send_command('starttls'); |
| 1937 | |
| 1938 | $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); |
| 1939 | |
| 1940 | if ($crypto !== TRUE) |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1941 | { |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1942 | $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1943 | return FALSE; |
| 1944 | } |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1945 | } |
nisheeth-barthwal | a44e691 | 2013-02-18 18:07:03 +0530 | [diff] [blame] | 1946 | |
| 1947 | return $this->_send_command('hello'); |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1948 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1949 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1950 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1951 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1952 | /** |
| 1953 | * Send SMTP command |
| 1954 | * |
| 1955 | * @param string |
| 1956 | * @param string |
| 1957 | * @return string |
| 1958 | */ |
| 1959 | protected function _send_command($cmd, $data = '') |
| 1960 | { |
| 1961 | switch ($cmd) |
| 1962 | { |
| 1963 | case 'hello' : |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1964 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1965 | if ($this->_smtp_auth OR $this->_get_encoding() === '8bit') |
| 1966 | { |
| 1967 | $this->_send_data('EHLO '.$this->_get_hostname()); |
| 1968 | } |
| 1969 | else |
| 1970 | { |
| 1971 | $this->_send_data('HELO '.$this->_get_hostname()); |
| 1972 | } |
Radu Potop | bbf04b0 | 2011-09-28 13:57:51 +0300 | [diff] [blame] | 1973 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1974 | $resp = 250; |
| 1975 | break; |
| 1976 | case 'starttls' : |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1977 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1978 | $this->_send_data('STARTTLS'); |
| 1979 | $resp = 220; |
| 1980 | break; |
| 1981 | case 'from' : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1982 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1983 | $this->_send_data('MAIL FROM:<'.$data.'>'); |
| 1984 | $resp = 250; |
| 1985 | break; |
| 1986 | case 'to' : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1987 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1988 | if ($this->dsn) |
| 1989 | { |
| 1990 | $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data); |
| 1991 | } |
| 1992 | else |
| 1993 | { |
| 1994 | $this->_send_data('RCPT TO:<'.$data.'>'); |
| 1995 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1996 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 1997 | $resp = 250; |
| 1998 | break; |
| 1999 | case 'data' : |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2000 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2001 | $this->_send_data('DATA'); |
| 2002 | $resp = 354; |
| 2003 | break; |
| 2004 | case 'reset': |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2005 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2006 | $this->_send_data('RSET'); |
| 2007 | $resp = 250; |
| 2008 | break; |
| 2009 | case 'quit' : |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2010 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2011 | $this->_send_data('QUIT'); |
| 2012 | $resp = 221; |
| 2013 | break; |
| 2014 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2015 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2016 | $reply = $this->_get_smtp_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2017 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2018 | $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2019 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2020 | if ((int) substr($reply, 0, 3) !== $resp) |
| 2021 | { |
| 2022 | $this->_set_error_message('lang:email_smtp_error', $reply); |
| 2023 | return FALSE; |
| 2024 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2025 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2026 | if ($cmd === 'quit') |
| 2027 | { |
| 2028 | fclose($this->_smtp_connect); |
| 2029 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2030 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2031 | return TRUE; |
| 2032 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2033 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2034 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2035 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2036 | /** |
| 2037 | * SMTP Authenticate |
| 2038 | * |
| 2039 | * @return bool |
| 2040 | */ |
| 2041 | protected function _smtp_authenticate() |
| 2042 | { |
| 2043 | if ( ! $this->_smtp_auth) |
| 2044 | { |
| 2045 | return TRUE; |
| 2046 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2047 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2048 | if ($this->smtp_user === '' && $this->smtp_pass === '') |
| 2049 | { |
| 2050 | $this->_set_error_message('lang:email_no_smtp_unpw'); |
| 2051 | return FALSE; |
| 2052 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2053 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2054 | $this->_send_data('AUTH LOGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2055 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2056 | $reply = $this->_get_smtp_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2057 | |
Andrey Andreev | fa01ae4 | 2013-03-04 14:40:18 +0200 | [diff] [blame] | 2058 | if (strpos($reply, '503') === 0) // Already authenticated |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 2059 | { |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2060 | return TRUE; |
nisheeth-barthwal | 758f40c | 2013-02-18 17:18:51 +0530 | [diff] [blame] | 2061 | } |
Andrey Andreev | fa01ae4 | 2013-03-04 14:40:18 +0200 | [diff] [blame] | 2062 | elseif (strpos($reply, '334') !== 0) |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2063 | { |
| 2064 | $this->_set_error_message('lang:email_failed_smtp_login', $reply); |
| 2065 | return FALSE; |
| 2066 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2067 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2068 | $this->_send_data(base64_encode($this->smtp_user)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2069 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2070 | $reply = $this->_get_smtp_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2071 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2072 | if (strpos($reply, '334') !== 0) |
| 2073 | { |
| 2074 | $this->_set_error_message('lang:email_smtp_auth_un', $reply); |
| 2075 | return FALSE; |
| 2076 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2077 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2078 | $this->_send_data(base64_encode($this->smtp_pass)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2079 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2080 | $reply = $this->_get_smtp_data(); |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 2081 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2082 | if (strpos($reply, '235') !== 0) |
| 2083 | { |
| 2084 | $this->_set_error_message('lang:email_smtp_auth_pw', $reply); |
| 2085 | return FALSE; |
| 2086 | } |
nisheeth-barthwal | cf22557 | 2013-02-18 01:08:04 +0530 | [diff] [blame] | 2087 | |
nisheeth-barthwal | 59209de | 2013-02-18 17:02:13 +0530 | [diff] [blame] | 2088 | return TRUE; |
| 2089 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2090 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2091 | // -------------------------------------------------------------------- |
| 2092 | |
| 2093 | /** |
| 2094 | * Send SMTP data |
| 2095 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 2096 | * @param string $data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2097 | * @return bool |
| 2098 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 2099 | protected function _send_data($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2100 | { |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 2101 | if ( ! fwrite($this->_smtp_connect, $data.$this->newline)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2102 | { |
patwork | b070798 | 2011-04-08 15:10:05 +0200 | [diff] [blame] | 2103 | $this->_set_error_message('lang:email_smtp_data_failure', $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2104 | return FALSE; |
| 2105 | } |
Andrey Andreev | 1bd3d88 | 2011-12-22 15:38:20 +0200 | [diff] [blame] | 2106 | |
| 2107 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2108 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2109 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2110 | // -------------------------------------------------------------------- |
| 2111 | |
| 2112 | /** |
| 2113 | * Get SMTP data |
| 2114 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2115 | * @return string |
| 2116 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 2117 | protected function _get_smtp_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2118 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 2119 | $data = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2120 | |
| 2121 | while ($str = fgets($this->_smtp_connect, 512)) |
| 2122 | { |
| 2123 | $data .= $str; |
| 2124 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 2125 | if ($str[3] === ' ') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2126 | { |
| 2127 | break; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | return $data; |
| 2132 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2134 | // -------------------------------------------------------------------- |
| 2135 | |
| 2136 | /** |
| 2137 | * Get Hostname |
| 2138 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2139 | * @return string |
| 2140 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 2141 | protected function _get_hostname() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2142 | { |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 2143 | return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2146 | // -------------------------------------------------------------------- |
| 2147 | |
| 2148 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2149 | * Get Debug Message |
| 2150 | * |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 2151 | * @param array $include List of raw data chunks to include in the output |
| 2152 | * Valid options are: 'headers', 'subject', 'body' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2153 | * @return string |
| 2154 | */ |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 2155 | public function print_debugger($include = array('headers', 'subject', 'body')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2156 | { |
| 2157 | $msg = ''; |
| 2158 | |
| 2159 | if (count($this->_debug_msg) > 0) |
| 2160 | { |
| 2161 | foreach ($this->_debug_msg as $val) |
| 2162 | { |
| 2163 | $msg .= $val; |
| 2164 | } |
| 2165 | } |
| 2166 | |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 2167 | // Determine which parts of our raw data needs to be printed |
| 2168 | $raw_data = ''; |
| 2169 | is_array($include) OR $include = array($include); |
| 2170 | |
| 2171 | if (in_array('headers', $include, TRUE)) |
| 2172 | { |
Andrey Andreev | 58f677f | 2013-07-16 11:01:37 +0300 | [diff] [blame] | 2173 | $raw_data = htmlspecialchars($this->_header_str)."\n"; |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | if (in_array('subject', $include, TRUE)) |
| 2177 | { |
| 2178 | $raw_data .= htmlspecialchars($this->_subject)."\n"; |
| 2179 | } |
| 2180 | |
| 2181 | if (in_array('body', $include, TRUE)) |
| 2182 | { |
| 2183 | $raw_data .= htmlspecialchars($this->_finalbody); |
| 2184 | } |
| 2185 | |
| 2186 | return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2189 | // -------------------------------------------------------------------- |
| 2190 | |
| 2191 | /** |
| 2192 | * Set Message |
| 2193 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 2194 | * @param string $msg |
| 2195 | * @param string $val = '' |
Andrey Andreev | 081c946 | 2012-03-01 12:58:11 +0200 | [diff] [blame] | 2196 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2197 | */ |
Phil Sturgeon | 6d2f13a | 2011-07-20 10:04:52 -0600 | [diff] [blame] | 2198 | protected function _set_error_message($msg, $val = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2199 | { |
| 2200 | $CI =& get_instance(); |
| 2201 | $CI->lang->load('email'); |
| 2202 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 2203 | if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2204 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 2205 | $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2206 | } |
| 2207 | else |
| 2208 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 2209 | $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2210 | } |
| 2211 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2212 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2213 | // -------------------------------------------------------------------- |
| 2214 | |
| 2215 | /** |
| 2216 | * Mime Types |
| 2217 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2218 | * @param string |
| 2219 | * @return string |
| 2220 | */ |
Andrey Andreev | 59c5b53 | 2012-03-01 14:09:51 +0200 | [diff] [blame] | 2221 | protected function _mime_types($ext = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2222 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 2223 | static $mimes; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2224 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 2225 | $ext = strtolower($ext); |
| 2226 | |
| 2227 | if ( ! is_array($mimes)) |
| 2228 | { |
| 2229 | $mimes =& get_mimes(); |
| 2230 | } |
| 2231 | |
| 2232 | if (isset($mimes[$ext])) |
| 2233 | { |
| 2234 | return is_array($mimes[$ext]) |
| 2235 | ? current($mimes[$ext]) |
| 2236 | : $mimes[$ext]; |
| 2237 | } |
| 2238 | |
| 2239 | return 'application/x-unknown-content-type'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2243 | |
| 2244 | /* End of file Email.php */ |
Andrey Andreev | 5098859 | 2012-10-08 20:46:04 +0300 | [diff] [blame] | 2245 | /* Location: ./system/libraries/Email.php */ |