PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/system/libraries/Email.php

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