PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/system/libraries/Email.php

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