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