/halogy/libraries/Email.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 2041 lines · 1499 code · 243 blank · 299 comment · 144 complexity · 50dc761c3dbbb26113918687261e3edd MD5 · raw file

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