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