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