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