Source for file class.phpmailer.php

Documentation is available at class.phpmailer.php

  1. <?php
  2. /*~ class.phpmailer.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 2.0.4 |
  6. | Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
  7. | Info: http://phpmailer.sourceforge.net |
  8. | Support: http://sourceforge.net/projects/phpmailer/ |
  9. | ------------------------------------------------------------------------- |
  10. | Author: Andy Prevost (project admininistrator) |
  11. | Author: Brent R. Matzelle (original founder) |
  12. | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
  13. | Copyright (c) 2001-2003, Brent R. Matzelle |
  14. | ------------------------------------------------------------------------- |
  15. | License: Distributed under the Lesser General Public License (LGPL) |
  16. | http://www.gnu.org/copyleft/lesser.html |
  17. | This program is distributed in the hope that it will be useful - WITHOUT |
  18. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  19. | FITNESS FOR A PARTICULAR PURPOSE. |
  20. | ------------------------------------------------------------------------- |
  21. | We offer a number of paid services (www.codeworxtech.com): |
  22. | - Web Hosting on highly optimized fast and secure servers |
  23. | - Technology Consulting |
  24. | - Oursourcing (highly qualified programmers and graphic designers) |
  25. '---------------------------------------------------------------------------'
  26.  
  27.  
  28.  
  29. /**
  30. * PHPMailer - PHP email transport class
  31. * @package PHPMailer
  32. * @author Andy Prevost
  33. * @copyright 2004 - 2009 Andy Prevost
  34. */
  35.  
  36. class PHPMailer {
  37.  
  38. /////////////////////////////////////////////////
  39. // PROPERTIES, PUBLIC
  40. /////////////////////////////////////////////////
  41.  
  42.  
  43. /**
  44. * Email priority (1 = High, 3 = Normal, 5 = low).
  45. * @var int
  46. */
  47. var $Priority = 3;
  48.  
  49. /**
  50. * Sets the CharSet of the message.
  51. * @var string
  52. */
  53. var $CharSet = 'iso-8859-1';
  54.  
  55. /**
  56. * Sets the Content-type of the message.
  57. * @var string
  58. */
  59. var $ContentType = 'text/plain';
  60.  
  61. /**
  62. * Sets the Encoding of the message. Options for this are "8bit",
  63. * "7bit", "binary", "base64", and "quoted-printable".
  64. * @var string
  65. */
  66. var $Encoding = '8bit';
  67.  
  68. /**
  69. * Holds the most recent mailer error message.
  70. * @var string
  71. */
  72. var $ErrorInfo = '';
  73.  
  74. /**
  75. * Sets the From email address for the message.
  76. * @var string
  77. */
  78. var $From = 'root@localhost';
  79.  
  80. /**
  81. * Sets the From name of the message.
  82. * @var string
  83. */
  84. var $FromName = 'Root User';
  85.  
  86. /**
  87. * Sets the Sender email (Return-Path) of the message. If not empty,
  88. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  89. * @var string
  90. */
  91. var $Sender = '';
  92.  
  93. /**
  94. * Sets the Subject of the message.
  95. * @var string
  96. */
  97. var $Subject = '';
  98.  
  99. /**
  100. * Sets the Body of the message. This can be either an HTML or text body.
  101. * If HTML then run IsHTML(true).
  102. * @var string
  103. */
  104. var $Body = '';
  105.  
  106. /**
  107. * Sets the text-only body of the message. This automatically sets the
  108. * email to multipart/alternative. This body can be read by mail
  109. * clients that do not have HTML email capability such as mutt. Clients
  110. * that can read HTML will view the normal Body.
  111. * @var string
  112. */
  113. var $AltBody = '';
  114.  
  115. /**
  116. * Sets word wrapping on the body of the message to a given number of
  117. * characters.
  118. * @var int
  119. */
  120. var $WordWrap = 0;
  121.  
  122. /**
  123. * Method to send mail: ("mail", "sendmail", or "smtp").
  124. * @var string
  125. */
  126. var $Mailer = 'mail';
  127.  
  128. /**
  129. * Sets the path of the sendmail program.
  130. * @var string
  131. */
  132. var $Sendmail = '/usr/sbin/sendmail';
  133.  
  134. /**
  135. * Path to PHPMailer plugins. This is now only useful if the SMTP class
  136. * is in a different directory than the PHP include path.
  137. * @var string
  138. */
  139. var $PluginDir = '';
  140.  
  141. /**
  142. * Holds PHPMailer version.
  143. * @var string
  144. */
  145. var $Version = "2.0.4";
  146.  
  147. /**
  148. * Sets the email address that a reading confirmation will be sent.
  149. * @var string
  150. */
  151. var $ConfirmReadingTo = '';
  152.  
  153. /**
  154. * Sets the hostname to use in Message-Id and Received headers
  155. * and as default HELO string. If empty, the value returned
  156. * by SERVER_NAME is used or 'localhost.localdomain'.
  157. * @var string
  158. */
  159. var $Hostname = '';
  160.  
  161. /**
  162. * Sets the message ID to be used in the Message-Id header.
  163. * If empty, a unique id will be generated.
  164. * @var string
  165. */
  166. var $MessageID = '';
  167.  
  168. /////////////////////////////////////////////////
  169. // PROPERTIES FOR SMTP
  170. /////////////////////////////////////////////////
  171.  
  172.  
  173. /**
  174. * Sets the SMTP hosts. All hosts must be separated by a
  175. * semicolon. You can also specify a different port
  176. * for each host by using this format: [hostname:port]
  177. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  178. * Hosts will be tried in order.
  179. * @var string
  180. */
  181. var $Host = 'localhost';
  182.  
  183. /**
  184. * Sets the default SMTP server port.
  185. * @var int
  186. */
  187. var $Port = 25;
  188.  
  189. /**
  190. * Sets the SMTP HELO of the message (Default is $Hostname).
  191. * @var string
  192. */
  193. var $Helo = '';
  194.  
  195. /**
  196. * Sets connection prefix.
  197. * Options are "", "ssl" or "tls"
  198. * @var string
  199. */
  200. var $SMTPSecure = "";
  201.  
  202. /**
  203. * Sets SMTP authentication. Utilizes the Username and Password variables.
  204. * @var bool
  205. */
  206. var $SMTPAuth = false;
  207.  
  208. /**
  209. * Sets SMTP username.
  210. * @var string
  211. */
  212. var $Username = '';
  213.  
  214. /**
  215. * Sets SMTP password.
  216. * @var string
  217. */
  218. var $Password = '';
  219.  
  220. /**
  221. * Sets the SMTP server timeout in seconds. This function will not
  222. * work with the win32 version.
  223. * @var int
  224. */
  225. var $Timeout = 10;
  226.  
  227. /**
  228. * Sets SMTP class debugging on or off.
  229. * @var bool
  230. */
  231. var $SMTPDebug = false;
  232.  
  233. /**
  234. * Prevents the SMTP connection from being closed after each mail
  235. * sending. If this is set to true then to close the connection
  236. * requires an explicit call to SmtpClose().
  237. * @var bool
  238. */
  239. var $SMTPKeepAlive = false;
  240.  
  241. /**
  242. * Provides the ability to have the TO field process individual
  243. * emails, instead of sending to entire TO addresses
  244. * @var bool
  245. */
  246. var $SingleTo = false;
  247.  
  248. /////////////////////////////////////////////////
  249. // PROPERTIES, PRIVATE
  250. /////////////////////////////////////////////////
  251.  
  252.  
  253. var $smtp = NULL;
  254. var $to = array();
  255. var $cc = array();
  256. var $bcc = array();
  257. var $ReplyTo = array();
  258. var $attachment = array();
  259. var $CustomHeader = array();
  260. var $message_type = '';
  261. var $boundary = array();
  262. var $language = array();
  263. var $error_count = 0;
  264. var $LE = "\n";
  265. var $sign_cert_file = "";
  266. var $sign_key_file = "";
  267. var $sign_key_pass = "";
  268.  
  269. /////////////////////////////////////////////////
  270. // METHODS, VARIABLES
  271. /////////////////////////////////////////////////
  272.  
  273.  
  274. /**
  275. * Sets message type to HTML.
  276. * @param bool $bool
  277. * @return void
  278. */
  279. function IsHTML($bool) {
  280. if($bool == true) {
  281. $this->ContentType = 'text/html';
  282. } else {
  283. $this->ContentType = 'text/plain';
  284. }
  285. }
  286.  
  287. /**
  288. * Sets Mailer to send message using SMTP.
  289. * @return void
  290. */
  291. function IsSMTP() {
  292. $this->Mailer = 'smtp';
  293. }
  294.  
  295. /**
  296. * Sets Mailer to send message using PHP mail() function.
  297. * @return void
  298. */
  299. function IsMail() {
  300. $this->Mailer = 'mail';
  301. }
  302.  
  303. /**
  304. * Sets Mailer to send message using the $Sendmail program.
  305. * @return void
  306. */
  307. function IsSendmail() {
  308. $this->Mailer = 'sendmail';
  309. }
  310.  
  311. /**
  312. * Sets Mailer to send message using the qmail MTA.
  313. * @return void
  314. */
  315. function IsQmail() {
  316. $this->Sendmail = '/var/qmail/bin/sendmail';
  317. $this->Mailer = 'sendmail';
  318. }
  319.  
  320. /////////////////////////////////////////////////
  321. // METHODS, RECIPIENTS
  322. /////////////////////////////////////////////////
  323.  
  324.  
  325. /**
  326. * Adds a "To" address.
  327. * @param string $address
  328. * @param string $name
  329. * @return void
  330. */
  331. function AddAddress($address, $name = '') {
  332. $cur = count($this->to);
  333. $this->to[$cur][0] = trim($address);
  334. $this->to[$cur][1] = $name;
  335. }
  336.  
  337. /**
  338. * Adds a "Cc" address. Note: this function works
  339. * with the SMTP mailer on win32, not with the "mail"
  340. * mailer.
  341. * @param string $address
  342. * @param string $name
  343. * @return void
  344. */
  345. function AddCC($address, $name = '') {
  346. $cur = count($this->cc);
  347. $this->cc[$cur][0] = trim($address);
  348. $this->cc[$cur][1] = $name;
  349. }
  350.  
  351. /**
  352. * Adds a "Bcc" address. Note: this function works
  353. * with the SMTP mailer on win32, not with the "mail"
  354. * mailer.
  355. * @param string $address
  356. * @param string $name
  357. * @return void
  358. */
  359. function AddBCC($address, $name = '') {
  360. $cur = count($this->bcc);
  361. $this->bcc[$cur][0] = trim($address);
  362. $this->bcc[$cur][1] = $name;
  363. }
  364.  
  365. /**
  366. * Adds a "Reply-To" address.
  367. * @param string $address
  368. * @param string $name
  369. * @return void
  370. */
  371. function AddReplyTo($address, $name = '') {
  372. $cur = count($this->ReplyTo);
  373. $this->ReplyTo[$cur][0] = trim($address);
  374. $this->ReplyTo[$cur][1] = $name;
  375. }
  376.  
  377. /////////////////////////////////////////////////
  378. // METHODS, MAIL SENDING
  379. /////////////////////////////////////////////////
  380.  
  381.  
  382. /**
  383. * Creates message and assigns Mailer. If the message is
  384. * not sent successfully then it returns false. Use the ErrorInfo
  385. * variable to view description of the error.
  386. * @return bool
  387. */
  388. function Send() {
  389. $header = '';
  390. $body = '';
  391. $result = true;
  392.  
  393. if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  394. $this->SetError($this->Lang('provide_address'));
  395. return false;
  396. }
  397.  
  398. /* Set whether the message is multipart/alternative */
  399. if(!empty($this->AltBody)) {
  400. $this->ContentType = 'multipart/alternative';
  401. }
  402.  
  403. $this->error_count = 0; // reset errors
  404. $this->SetMessageType();
  405. $header .= $this->CreateHeader();
  406. $body = $this->CreateBody();
  407.  
  408. if($body == '') {
  409. return false;
  410. }
  411.  
  412. /* Choose the mailer */
  413. switch($this->Mailer) {
  414. case 'sendmail':
  415. $result = $this->SendmailSend($header, $body);
  416. break;
  417. case 'smtp':
  418. $result = $this->SmtpSend($header, $body);
  419. break;
  420. case 'mail':
  421. $result = $this->MailSend($header, $body);
  422. break;
  423. default:
  424. $result = $this->MailSend($header, $body);
  425. break;
  426. //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
  427. //$result = false;
  428. //break;
  429. }
  430.  
  431. return $result;
  432. }
  433.  
  434. /**
  435. * Sends mail using the $Sendmail program.
  436. * @access private
  437. * @return bool
  438. */
  439. function SendmailSend($header, $body) {
  440. if ($this->Sender != '') {
  441. $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  442. } else {
  443. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  444. }
  445.  
  446. if(!@$mail = popen($sendmail, 'w')) {
  447. $this->SetError($this->Lang('execute') . $this->Sendmail);
  448. return false;
  449. }
  450.  
  451. fputs($mail, $header);
  452. fputs($mail, $body);
  453.  
  454. $result = pclose($mail);
  455. if (version_compare(phpversion(), '4.2.3') == -1) {
  456. $result = $result >> 8 & 0xFF;
  457. }
  458. if($result != 0) {
  459. $this->SetError($this->Lang('execute') . $this->Sendmail);
  460. return false;
  461. }
  462. return true;
  463. }
  464.  
  465. /**
  466. * Sends mail using the PHP mail() function.
  467. * @access private
  468. * @return bool
  469. */
  470. function MailSend($header, $body) {
  471.  
  472. $to = '';
  473. for($i = 0; $i < count($this->to); $i++) {
  474. if($i != 0) { $to .= ', '; }
  475. $to .= $this->AddrFormat($this->to[$i]);
  476. }
  477.  
  478. $toArr = split(',', $to);
  479.  
  480. $params = sprintf("-oi -f %s", $this->Sender);
  481. if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
  482. $old_from = ini_get('sendmail_from');
  483. ini_set('sendmail_from', $this->Sender);
  484. if ($this->SingleTo === true && count($toArr) > 1) {
  485. foreach ($toArr as $key => $val) {
  486. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  487. }
  488. } else {
  489. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  490. }
  491. } else {
  492. if ($this->SingleTo === true && count($toArr) > 1) {
  493. foreach ($toArr as $key => $val) {
  494. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  495. }
  496. } else {
  497. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
  498. }
  499. }
  500.  
  501. if (isset($old_from)) {
  502. ini_set('sendmail_from', $old_from);
  503. }
  504.  
  505. if(!$rt) {
  506. $this->SetError($this->Lang('instantiate'));
  507. return false;
  508. }
  509.  
  510. return true;
  511. }
  512.  
  513. /**
  514. * Sends mail via SMTP using PhpSMTP (Author:
  515. * Chris Ryan). Returns bool. Returns false if there is a
  516. * bad MAIL FROM, RCPT, or DATA input.
  517. * @access private
  518. * @return bool
  519. */
  520. function SmtpSend($header, $body) {
  521. include_once($this->PluginDir . 'class.smtp.php');
  522. $error = '';
  523. $bad_rcpt = array();
  524.  
  525. if(!$this->SmtpConnect()) {
  526. return false;
  527. }
  528.  
  529. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  530. if(!$this->smtp->Mail($smtp_from)) {
  531. $error = $this->Lang('from_failed') . $smtp_from;
  532. $this->SetError($error);
  533. $this->smtp->Reset();
  534. return false;
  535. }
  536.  
  537. /* Attempt to send attach all recipients */
  538. for($i = 0; $i < count($this->to); $i++) {
  539. if(!$this->smtp->Recipient($this->to[$i][0])) {
  540. $bad_rcpt[] = $this->to[$i][0];
  541. }
  542. }
  543. for($i = 0; $i < count($this->cc); $i++) {
  544. if(!$this->smtp->Recipient($this->cc[$i][0])) {
  545. $bad_rcpt[] = $this->cc[$i][0];
  546. }
  547. }
  548. for($i = 0; $i < count($this->bcc); $i++) {
  549. if(!$this->smtp->Recipient($this->bcc[$i][0])) {
  550. $bad_rcpt[] = $this->bcc[$i][0];
  551. }
  552. }
  553.  
  554. if(count($bad_rcpt) > 0) { // Create error message
  555. for($i = 0; $i < count($bad_rcpt); $i++) {
  556. if($i != 0) {
  557. $error .= ', ';
  558. }
  559. $error .= $bad_rcpt[$i];
  560. }
  561. $error = $this->Lang('recipients_failed') . $error;
  562. $this->SetError($error);
  563. $this->smtp->Reset();
  564. return false;
  565. }
  566.  
  567. if(!$this->smtp->Data($header . $body)) {
  568. $this->SetError($this->Lang('data_not_accepted'));
  569. $this->smtp->Reset();
  570. return false;
  571. }
  572. if($this->SMTPKeepAlive == true) {
  573. $this->smtp->Reset();
  574. } else {
  575. $this->SmtpClose();
  576. }
  577.  
  578. return true;
  579. }
  580.  
  581. /**
  582. * Initiates a connection to an SMTP server. Returns false if the
  583. * operation failed.
  584. * @access private
  585. * @return bool
  586. */
  587. function SmtpConnect() {
  588. if($this->smtp == NULL) {
  589. $this->smtp = new SMTP();
  590. }
  591.  
  592. $this->smtp->do_debug = $this->SMTPDebug;
  593. $hosts = explode(';', $this->Host);
  594. $index = 0;
  595. $connection = ($this->smtp->Connected());
  596.  
  597. /* Retry while there is no connection */
  598. while($index < count($hosts) && $connection == false) {
  599. $hostinfo = array();
  600. if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
  601. $host = $hostinfo[1];
  602. $port = $hostinfo[2];
  603. } else {
  604. $host = $hosts[$index];
  605. $port = $this->Port;
  606. }
  607.  
  608. if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {
  609. if ($this->Helo != '') {
  610. $this->smtp->Hello($this->Helo);
  611. } else {
  612. $this->smtp->Hello($this->ServerHostname());
  613. }
  614.  
  615. $connection = true;
  616. if($this->SMTPAuth) {
  617. if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
  618. $this->SetError($this->Lang('authenticate'));
  619. $this->smtp->Reset();
  620. $connection = false;
  621. }
  622. }
  623. }
  624. $index++;
  625. }
  626. if(!$connection) {
  627. $this->SetError($this->Lang('connect_host'));
  628. }
  629.  
  630. return $connection;
  631. }
  632.  
  633. /**
  634. * Closes the active SMTP session if one exists.
  635. * @return void
  636. */
  637. function SmtpClose() {
  638. if($this->smtp != NULL) {
  639. if($this->smtp->Connected()) {
  640. $this->smtp->Quit();
  641. $this->smtp->Close();
  642. }
  643. }
  644. }
  645.  
  646. /**
  647. * Sets the language for all class error messages. Returns false
  648. * if it cannot load the language file. The default language type
  649. * is English.
  650. * @param string $lang_type Type of language (e.g. Portuguese: "br")
  651. * @param string $lang_path Path to the language file directory
  652. * @access public
  653. * @return bool
  654. */
  655. function SetLanguage($lang_type, $lang_path = 'language/') {
  656. if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {
  657. include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  658. } elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {
  659. include($lang_path.'phpmailer.lang-en.php');
  660. } else {
  661. $PHPMAILER_LANG = array();
  662. $PHPMAILER_LANG["provide_address"] = 'You must provide at least one ' .
  663. $PHPMAILER_LANG["mailer_not_supported"] = ' mailer is not supported.';
  664. $PHPMAILER_LANG["execute"] = 'Could not execute: ';
  665. $PHPMAILER_LANG["instantiate"] = 'Could not instantiate mail function.';
  666. $PHPMAILER_LANG["authenticate"] = 'SMTP Error: Could not authenticate.';
  667. $PHPMAILER_LANG["from_failed"] = 'The following From address failed: ';
  668. $PHPMAILER_LANG["recipients_failed"] = 'SMTP Error: The following ' .
  669. $PHPMAILER_LANG["data_not_accepted"] = 'SMTP Error: Data not accepted.';
  670. $PHPMAILER_LANG["connect_host"] = 'SMTP Error: Could not connect to SMTP host.';
  671. $PHPMAILER_LANG["file_access"] = 'Could not access file: ';
  672. $PHPMAILER_LANG["file_open"] = 'File Error: Could not open file: ';
  673. $PHPMAILER_LANG["encoding"] = 'Unknown encoding: ';
  674. $PHPMAILER_LANG["signing"] = 'Signing Error: ';
  675. }
  676. $this->language = $PHPMAILER_LANG;
  677.  
  678. return true;
  679. }
  680.  
  681. /////////////////////////////////////////////////
  682. // METHODS, MESSAGE CREATION
  683. /////////////////////////////////////////////////
  684.  
  685.  
  686. /**
  687. * Creates recipient headers.
  688. * @access private
  689. * @return string
  690. */
  691. function AddrAppend($type, $addr) {
  692. $addr_str = $type . ': ';
  693. $addr_str .= $this->AddrFormat($addr[0]);
  694. if(count($addr) > 1) {
  695. for($i = 1; $i < count($addr); $i++) {
  696. $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
  697. }
  698. }
  699. $addr_str .= $this->LE;
  700.  
  701. return $addr_str;
  702. }
  703.  
  704. /**
  705. * Formats an address correctly.
  706. * @access private
  707. * @return string
  708. */
  709. function AddrFormat($addr) {
  710. if(empty($addr[1])) {
  711. $formatted = $this->SecureHeader($addr[0]);
  712. } else {
  713. $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  714. }
  715.  
  716. return $formatted;
  717. }
  718.  
  719. /**
  720. * Wraps message for use with mailers that do not
  721. * automatically perform wrapping and for quoted-printable.
  722. * Original written by philippe.
  723. * @access private
  724. * @return string
  725. */
  726. function WrapText($message, $length, $qp_mode = false) {
  727. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  728. // If utf-8 encoding is used, we will need to make sure we don't
  729. // split multibyte characters when we wrap
  730. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  731.  
  732. $message = $this->FixEOL($message);
  733. if (substr($message, -1) == $this->LE) {
  734. $message = substr($message, 0, -1);
  735. }
  736.  
  737. $line = explode($this->LE, $message);
  738. $message = '';
  739. for ($i=0 ;$i < count($line); $i++) {
  740. $line_part = explode(' ', $line[$i]);
  741. $buf = '';
  742. for ($e = 0; $e<count($line_part); $e++) {
  743. $word = $line_part[$e];
  744. if ($qp_mode and (strlen($word) > $length)) {
  745. $space_left = $length - strlen($buf) - 1;
  746. if ($e != 0) {
  747. if ($space_left > 20) {
  748. $len = $space_left;
  749. if ($is_utf8) {
  750. $len = $this->UTF8CharBoundary($word, $len);
  751. } elseif (substr($word, $len - 1, 1) == "=") {
  752. $len--;
  753. } elseif (substr($word, $len - 2, 1) == "=") {
  754. $len -= 2;
  755. }
  756. $part = substr($word, 0, $len);
  757. $word = substr($word, $len);
  758. $buf .= ' ' . $part;
  759. $message .= $buf . sprintf("=%s", $this->LE);
  760. } else {
  761. $message .= $buf . $soft_break;
  762. }
  763. $buf = '';
  764. }
  765. while (strlen($word) > 0) {
  766. $len = $length;
  767. if ($is_utf8) {
  768. $len = $this->UTF8CharBoundary($word, $len);
  769. } elseif (substr($word, $len - 1, 1) == "=") {
  770. $len--;
  771. } elseif (substr($word, $len - 2, 1) == "=") {
  772. $len -= 2;
  773. }
  774. $part = substr($word, 0, $len);
  775. $word = substr($word, $len);
  776.  
  777. if (strlen($word) > 0) {
  778. $message .= $part . sprintf("=%s", $this->LE);
  779. } else {
  780. $buf = $part;
  781. }
  782. }
  783. } else {
  784. $buf_o = $buf;
  785. $buf .= ($e == 0) ? $word : (' ' . $word);
  786.  
  787. if (strlen($buf) > $length and $buf_o != '') {
  788. $message .= $buf_o . $soft_break;
  789. $buf = $word;
  790. }
  791. }
  792. }
  793. $message .= $buf . $this->LE;
  794. }
  795.  
  796. return $message;
  797. }
  798.  
  799. /**
  800. * Finds last character boundary prior to maxLength in a utf-8
  801. * quoted (printable) encoded string.
  802. * Original written by Colin Brown.
  803. * @access private
  804. * @param string $encodedText utf-8 QP text
  805. * @param int $maxLength find last character boundary prior to this length
  806. * @return int
  807. */
  808. function UTF8CharBoundary($encodedText, $maxLength) {
  809. $foundSplitPos = false;
  810. $lookBack = 3;
  811. while (!$foundSplitPos) {
  812. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  813. $encodedCharPos = strpos($lastChunk, "=");
  814. if ($encodedCharPos !== false) {
  815. // Found start of encoded character byte within $lookBack block.
  816. // Check the encoded byte value (the 2 chars after the '=')
  817. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  818. $dec = hexdec($hex);
  819. if ($dec < 128) { // Single byte character.
  820. // If the encoded char was found at pos 0, it will fit
  821. // otherwise reduce maxLength to start of the encoded char
  822. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  823. $maxLength - ($lookBack - $encodedCharPos);
  824. $foundSplitPos = true;
  825. } elseif ($dec >= 192) { // First byte of a multi byte character
  826. // Reduce maxLength to split at start of character
  827. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  828. $foundSplitPos = true;
  829. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  830. $lookBack += 3;
  831. }
  832. } else {
  833. // No encoded character found
  834. $foundSplitPos = true;
  835. }
  836. }
  837. return $maxLength;
  838. }
  839.  
  840. /**
  841. * Set the body wrapping.
  842. * @access private
  843. * @return void
  844. */
  845. function SetWordWrap() {
  846. if($this->WordWrap < 1) {
  847. return;
  848. }
  849.  
  850. switch($this->message_type) {
  851. case 'alt':
  852. /* fall through */
  853. case 'alt_attachments':
  854. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  855. break;
  856. default:
  857. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  858. break;
  859. }
  860. }
  861.  
  862. /**
  863. * Assembles message header.
  864. * @access private
  865. * @return string
  866. */
  867. function CreateHeader() {
  868. $result = '';
  869.  
  870. /* Set the boundaries */
  871. $uniq_id = md5(uniqid(time()));
  872. $this->boundary[1] = 'b1_' . $uniq_id;
  873. $this->boundary[2] = 'b2_' . $uniq_id;
  874.  
  875. $result .= $this->HeaderLine('Date', $this->RFCDate());
  876. if($this->Sender == '') {
  877. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  878. } else {
  879. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  880. }
  881.  
  882. /* To be created automatically by mail() */
  883. if($this->Mailer != 'mail') {
  884. if(count($this->to) > 0) {
  885. $result .= $this->AddrAppend('To', $this->to);
  886. } elseif (count($this->cc) == 0) {
  887. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  888. }
  889. }
  890.  
  891. $from = array();
  892. $from[0][0] = trim($this->From);
  893. $from[0][1] = $this->FromName;
  894. $result .= $this->AddrAppend('From', $from);
  895.  
  896. /* sendmail and mail() extract Cc from the header before sending */
  897. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
  898. $result .= $this->AddrAppend('Cc', $this->cc);
  899. }
  900.  
  901. /* sendmail and mail() extract Bcc from the header before sending */
  902. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  903. $result .= $this->AddrAppend('Bcc', $this->bcc);
  904. }
  905.  
  906. if(count($this->ReplyTo) > 0) {
  907. $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
  908. }
  909.  
  910. /* mail() sets the subject itself */
  911. if($this->Mailer != 'mail') {
  912. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  913. }
  914.  
  915. if($this->MessageID != '') {
  916. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  917. } else {
  918. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  919. }
  920. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  921. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');
  922.  
  923. if($this->ConfirmReadingTo != '') {
  924. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  925. }
  926.  
  927. // Add custom headers
  928. for($index = 0; $index < count($this->CustomHeader); $index++) {
  929. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  930. }
  931. if (!$this->sign_key_file) {
  932. $result .= $this->HeaderLine('MIME-Version', '1.0');
  933. $result .= $this->GetMailMIME();
  934. }
  935.  
  936. return $result;
  937. }
  938.  
  939. /**
  940. * Returns the message MIME.
  941. * @access private
  942. * @return string
  943. */
  944. function GetMailMIME() {
  945. $result = '';
  946. switch($this->message_type) {
  947. case 'plain':
  948. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  949. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
  950. break;
  951. case 'attachments':
  952. /* fall through */
  953. case 'alt_attachments':
  954. if($this->InlineImageExists()){
  955. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
  956. } else {
  957. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  958. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  959. }
  960. break;
  961. case 'alt':
  962. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  963. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  964. break;
  965. }
  966.  
  967. if($this->Mailer != 'mail') {
  968. $result .= $this->LE.$this->LE;
  969. }
  970.  
  971. return $result;
  972. }
  973.  
  974. /**
  975. * Assembles the message body. Returns an empty string on failure.
  976. * @access private
  977. * @return string
  978. */
  979. function CreateBody() {
  980. $result = '';
  981. if ($this->sign_key_file) {
  982. $result .= $this->GetMailMIME();
  983. }
  984.  
  985. $this->SetWordWrap();
  986.  
  987. switch($this->message_type) {
  988. case 'alt':
  989. $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  990. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  991. $result .= $this->LE.$this->LE;
  992. $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  993. $result .= $this->EncodeString($this->Body, $this->Encoding);
  994. $result .= $this->LE.$this->LE;
  995. $result .= $this->EndBoundary($this->boundary[1]);
  996. break;
  997. case 'plain':
  998. $result .= $this->EncodeString($this->Body, $this->Encoding);
  999. break;
  1000. case 'attachments':
  1001. $result .= $this->GetBoundary($this->boundary[1], '', '', '');
  1002. $result .= $this->EncodeString($this->Body, $this->Encoding);
  1003. $result .= $this->LE;
  1004. $result .= $this->AttachAll();
  1005. break;
  1006. case 'alt_attachments':
  1007. $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  1008. $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
  1009. $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
  1010. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  1011. $result .= $this->LE.$this->LE;
  1012. $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
  1013. $result .= $this->EncodeString($this->Body, $this->Encoding);
  1014. $result .= $this->LE.$this->LE;
  1015. $result .= $this->EndBoundary($this->boundary[2]);
  1016. $result .= $this->AttachAll();
  1017. break;
  1018. }
  1019.  
  1020. if($this->IsError()) {
  1021. $result = '';
  1022. } else if ($this->sign_key_file) {
  1023. $file = tempnam("", "mail");
  1024. $fp = fopen($file, "w");
  1025. fwrite($fp, $result);
  1026. fclose($fp);
  1027. $signed = tempnam("", "signed");
  1028.  
  1029. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
  1030. $fp = fopen($signed, "r");
  1031. $result = fread($fp, filesize($this->sign_key_file));
  1032. $result = '';
  1033. while(!feof($fp)){
  1034. $result = $result . fread($fp, 1024);
  1035. }
  1036. fclose($fp);
  1037. } else {
  1038. $this->SetError($this->Lang("signing").openssl_error_string());
  1039. $result = '';
  1040. }
  1041.  
  1042. unlink($file);
  1043. unlink($signed);
  1044. }
  1045.  
  1046. return $result;
  1047. }
  1048.  
  1049. /**
  1050. * Returns the start of a message boundary.
  1051. * @access private
  1052. */
  1053. function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  1054. $result = '';
  1055. if($charSet == '') {
  1056. $charSet = $this->CharSet;
  1057. }
  1058. if($contentType == '') {
  1059. $contentType = $this->ContentType;
  1060. }
  1061. if($encoding == '') {
  1062. $encoding = $this->Encoding;
  1063. }
  1064. $result .= $this->TextLine('--' . $boundary);
  1065. $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
  1066. $result .= $this->LE;
  1067. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  1068. $result .= $this->LE;
  1069.  
  1070. return $result;
  1071. }
  1072.  
  1073. /**
  1074. * Returns the end of a message boundary.
  1075. * @access private
  1076. */
  1077. function EndBoundary($boundary) {
  1078. return $this->LE . '--' . $boundary . '--' . $this->LE;
  1079. }
  1080.  
  1081. /**
  1082. * Sets the message type.
  1083. * @access private
  1084. * @return void
  1085. */
  1086. function SetMessageType() {
  1087. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
  1088. $this->message_type = 'plain';
  1089. } else {
  1090. if(count($this->attachment) > 0) {
  1091. $this->message_type = 'attachments';
  1092. }
  1093. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
  1094. $this->message_type = 'alt';
  1095. }
  1096. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
  1097. $this->message_type = 'alt_attachments';
  1098. }
  1099. }
  1100. }
  1101.  
  1102. /* Returns a formatted header line.
  1103. * @access private
  1104. * @return string
  1105. */
  1106. function HeaderLine($name, $value) {
  1107. return $name . ': ' . $value . $this->LE;
  1108. }
  1109.  
  1110. /**
  1111. * Returns a formatted mail line.
  1112. * @access private
  1113. * @return string
  1114. */
  1115. function TextLine($value) {
  1116. return $value . $this->LE;
  1117. }
  1118.  
  1119. /////////////////////////////////////////////////
  1120. // CLASS METHODS, ATTACHMENTS
  1121. /////////////////////////////////////////////////
  1122.  
  1123.  
  1124. /**
  1125. * Adds an attachment from a path on the filesystem.
  1126. * Returns false if the file could not be found
  1127. * or accessed.
  1128. * @param string $path Path to the attachment.
  1129. * @param string $name Overrides the attachment name.
  1130. * @param string $encoding File encoding (see $Encoding).
  1131. * @param string $type File extension (MIME) type.
  1132. * @return bool
  1133. */
  1134. function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1135. if(!@is_file($path)) {
  1136. $this->SetError($this->Lang('file_access') . $path);
  1137. return false;
  1138. }
  1139.  
  1140. $filename = basename($path);
  1141. if($name == '') {
  1142. $name = $filename;
  1143. }
  1144.  
  1145. $cur = count($this->attachment);
  1146. $this->attachment[$cur][0] = $path;
  1147. $this->attachment[$cur][1] = $filename;
  1148. $this->attachment[$cur][2] = $name;
  1149. $this->attachment[$cur][3] = $encoding;
  1150. $this->attachment[$cur][4] = $type;
  1151. $this->attachment[$cur][5] = false; // isStringAttachment
  1152. $this->attachment[$cur][6] = 'attachment';
  1153. $this->attachment[$cur][7] = 0;
  1154.  
  1155. return true;
  1156. }
  1157.  
  1158. /**
  1159. * Attaches all fs, string, and binary attachments to the message.
  1160. * Returns an empty string on failure.
  1161. * @access private
  1162. * @return string
  1163. */
  1164. function AttachAll() {
  1165. /* Return text of body */
  1166. $mime = array();
  1167.  
  1168. /* Add all attachments */
  1169. for($i = 0; $i < count($this->attachment); $i++) {
  1170. /* Check for string attachment */
  1171. $bString = $this->attachment[$i][5];
  1172. if ($bString) {
  1173. $string = $this->attachment[$i][0];
  1174. } else {
  1175. $path = $this->attachment[$i][0];
  1176. }
  1177.  
  1178. $filename = $this->attachment[$i][1];
  1179. $name = $this->attachment[$i][2];
  1180. $encoding = $this->attachment[$i][3];
  1181. $type = $this->attachment[$i][4];
  1182. $disposition = $this->attachment[$i][6];
  1183. $cid = $this->attachment[$i][7];
  1184.  
  1185. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  1186. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
  1187. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1188.  
  1189. if($disposition == 'inline') {
  1190. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1191. }
  1192.  
  1193. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
  1194.  
  1195. /* Encode as string attachment */
  1196. if($bString) {
  1197. $mime[] = $this->EncodeString($string, $encoding);
  1198. if($this->IsError()) {
  1199. return '';
  1200. }
  1201. $mime[] = $this->LE.$this->LE;
  1202. } else {
  1203. $mime[] = $this->EncodeFile($path, $encoding);
  1204. if($this->IsError()) {
  1205. return '';
  1206. }
  1207. $mime[] = $this->LE.$this->LE;
  1208. }
  1209. }
  1210.  
  1211. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  1212.  
  1213. return join('', $mime);
  1214. }
  1215.  
  1216. /**
  1217. * Encodes attachment in requested format. Returns an
  1218. * empty string on failure.
  1219. * @access private
  1220. * @return string
  1221. */
  1222. function EncodeFile ($path, $encoding = 'base64') {
  1223. if(!@$fd = fopen($path, 'rb')) {
  1224. $this->SetError($this->Lang('file_open') . $path);
  1225. return '';
  1226. }
  1227. $magic_quotes = get_magic_quotes_runtime();
  1228. set_magic_quotes_runtime(0);
  1229. $file_buffer = fread($fd, filesize($path));
  1230. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1231. fclose($fd);
  1232. set_magic_quotes_runtime($magic_quotes);
  1233.  
  1234. return $file_buffer;
  1235. }
  1236.  
  1237. /**
  1238. * Encodes string to requested format. Returns an
  1239. * empty string on failure.
  1240. * @access private
  1241. * @return string
  1242. */
  1243. function EncodeString ($str, $encoding = 'base64') {
  1244. $encoded = '';
  1245. switch(strtolower($encoding)) {
  1246. case 'base64':
  1247. /* chunk_split is found in PHP >= 3.0.6 */
  1248. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1249. break;
  1250. case '7bit':
  1251. case '8bit':
  1252. $encoded = $this->FixEOL($str);
  1253. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1254. $encoded .= $this->LE;
  1255. break;
  1256. case 'binary':
  1257. $encoded = $str;
  1258. break;
  1259. case 'quoted-printable':
  1260. $encoded = $this->EncodeQP($str);
  1261. break;
  1262. default:
  1263. $this->SetError($this->Lang('encoding') . $encoding);
  1264. break;
  1265. }
  1266. return $encoded;
  1267. }
  1268.  
  1269. /**
  1270. * Encode a header string to best of Q, B, quoted or none.
  1271. * @access private
  1272. * @return string
  1273. */
  1274. function EncodeHeader ($str, $position = 'text') {
  1275. $x = 0;
  1276.  
  1277. switch (strtolower($position)) {
  1278. case 'phrase':
  1279. if (!preg_match('/[\200-\377]/', $str)) {
  1280. /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
  1281. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1282. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1283. return ($encoded);
  1284. } else {
  1285. return ("\"$encoded\"");
  1286. }
  1287. }
  1288. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1289. break;
  1290. case 'comment':
  1291. $x = preg_match_all('/[()"]/', $str, $matches);
  1292. /* Fall-through */
  1293. case 'text':
  1294. default:
  1295. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1296. break;
  1297. }
  1298.  
  1299. if ($x == 0) {
  1300. return ($str);
  1301. }
  1302.  
  1303. $maxlen = 75 - 7 - strlen($this->CharSet);
  1304. /* Try to select the encoding which should produce the shortest output */
  1305. if (strlen($str)/3 < $x) {
  1306. $encoding = 'B';
  1307. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1308. // Use a custom function which correctly encodes and wraps long
  1309. // multibyte strings without breaking lines within a character
  1310. $encoded = $this->Base64EncodeWrapMB($str);
  1311. } else {
  1312. $encoded = base64_encode($str);
  1313. $maxlen -= $maxlen % 4;
  1314. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1315. }
  1316. } else {
  1317. $encoding = 'Q';
  1318. $encoded = $this->EncodeQ($str, $position);
  1319. $encoded = $this->WrapText($encoded, $maxlen, true);
  1320. $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
  1321. }
  1322.  
  1323. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1324. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1325.  
  1326. return $encoded;
  1327. }
  1328.  
  1329. /**
  1330. * Checks if a string contains multibyte characters.
  1331. * @access private
  1332. * @param string $str multi-byte text to wrap encode
  1333. * @return bool
  1334. */
  1335. function HasMultiBytes($str) {
  1336. if (function_exists('mb_strlen')) {
  1337. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1338. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1339. return False;
  1340. }
  1341. }
  1342.  
  1343. /**
  1344. * Correctly encodes and wraps long multibyte strings for mail headers
  1345. * without breaking lines within a character.
  1346. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1347. * @access private
  1348. * @param string $str multi-byte text to wrap encode
  1349. * @return string
  1350. */
  1351. function Base64EncodeWrapMB($str) {
  1352. $start = "=?".$this->CharSet."?B?";
  1353. $end = "?=";
  1354. $encoded = "";
  1355.  
  1356. $mb_length = mb_strlen($str, $this->CharSet);
  1357. // Each line must have length <= 75, including $start and $end
  1358. $length = 75 - strlen($start) - strlen($end);
  1359. // Average multi-byte ratio
  1360. $ratio = $mb_length / strlen($str);
  1361. // Base64 has a 4:3 ratio
  1362. $offset = $avgLength = floor($length * $ratio * .75);
  1363.  
  1364. for ($i = 0; $i < $mb_length; $i += $offset) {
  1365. $lookBack = 0;
  1366.  
  1367. do {
  1368. $offset = $avgLength - $lookBack;
  1369. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1370. $chunk = base64_encode($chunk);
  1371. $lookBack++;
  1372. }
  1373. while (strlen($chunk) > $length);
  1374.  
  1375. $encoded .= $chunk . $this->LE;
  1376. }
  1377.  
  1378. // Chomp the last linefeed
  1379. $encoded = substr($encoded, 0, -strlen($this->LE));
  1380. return $encoded;
  1381. }
  1382.  
  1383. /**
  1384. * Encode string to quoted-printable.
  1385. * @access private
  1386. * @return string
  1387. */
  1388. function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
  1389. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1390. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1391. $eol = "\r\n";
  1392. $escape = '=';
  1393. $output = '';
  1394. while( list(, $line) = each($lines) ) {
  1395. $linlen = strlen($line);
  1396. $newline = '';
  1397. for($i = 0; $i < $linlen; $i++) {
  1398. $c = substr( $line, $i, 1 );
  1399. $dec = ord( $c );
  1400. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1401. $c = '=2E';
  1402. }
  1403. if ( $dec == 32 ) {
  1404. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1405. $c = '=20';
  1406. } else if ( $space_conv ) {
  1407. $c = '=20';
  1408. }
  1409. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1410. $h2 = floor($dec/16);
  1411. $h1 = floor($dec%16);
  1412. $c = $escape.$hex[$h2].$hex[$h1];
  1413. }
  1414. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1415. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1416. $newline = '';
  1417. // check if newline first character will be point or not
  1418. if ( $dec == 46 ) {
  1419. $c = '=2E';
  1420. }
  1421. }
  1422. $newline .= $c;
  1423. } // end of for
  1424. $output .= $newline.$eol;
  1425. } // end of while
  1426. return $output;
  1427. }
  1428.  
  1429. /**
  1430. * Encode string to q encoding.
  1431. * @access private
  1432. * @return string
  1433. */
  1434. function EncodeQ ($str, $position = 'text') {
  1435. /* There should not be any EOL in the string */
  1436. $encoded = preg_replace("[\r\n]", '', $str);
  1437.  
  1438. switch (strtolower($position)) {
  1439. case 'phrase':
  1440. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1441. break;
  1442. case 'comment':
  1443. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1444. case 'text':
  1445. default:
  1446. /* Replace every high ascii, control =, ? and _ characters */
  1447. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1448. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1449. break;
  1450. }
  1451.  
  1452. /* Replace every spaces to _ (more readable than =20) */
  1453. $encoded = str_replace(' ', '_', $encoded);
  1454.  
  1455. return $encoded;
  1456. }
  1457.  
  1458. /**
  1459. * Adds a string or binary attachment (non-filesystem) to the list.
  1460. * This method can be used to attach ascii or binary data,
  1461. * such as a BLOB record from a database.
  1462. * @param string $string String attachment data.
  1463. * @param string $filename Name of the attachment.
  1464. * @param string $encoding File encoding (see $Encoding).
  1465. * @param string $type File extension (MIME) type.
  1466. * @return void
  1467. */
  1468. function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1469. /* Append to $attachment array */
  1470. $cur = count($this->attachment);
  1471. $this->attachment[$cur][0] = $string;
  1472. $this->attachment[$cur][1] = $filename;
  1473. $this->attachment[$cur][2] = $filename;
  1474. $this->attachment[$cur][3] = $encoding;
  1475. $this->attachment[$cur][4] = $type;
  1476. $this->attachment[$cur][5] = true; // isString
  1477. $this->attachment[$cur][6] = 'attachment';
  1478. $this->attachment[$cur][7] = 0;
  1479. }
  1480.  
  1481. /**
  1482. * Adds an embedded attachment. This can include images, sounds, and
  1483. * just about any other document. Make sure to set the $type to an
  1484. * image type. For JPEG images use "image/jpeg" and for GIF images
  1485. * use "image/gif".
  1486. * @param string $path Path to the attachment.
  1487. * @param string $cid Content ID of the attachment. Use this to identify
  1488. * the Id for accessing the image in an HTML form.
  1489. * @param string $name Overrides the attachment name.
  1490. * @param string $encoding File encoding (see $Encoding).
  1491. * @param string $type File extension (MIME) type.
  1492. * @return bool
  1493. */
  1494. function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1495.  
  1496. if(!@is_file($path)) {
  1497. $this->SetError($this->Lang('file_access') . $path);
  1498. return false;
  1499. }
  1500.  
  1501. $filename = basename($path);
  1502. if($name == '') {
  1503. $name = $filename;
  1504. }
  1505.  
  1506. /* Append to $attachment array */
  1507. $cur = count($this->attachment);
  1508. $this->attachment[$cur][0] = $path;
  1509. $this->attachment[$cur][1] = $filename;
  1510. $this->attachment[$cur][2] = $name;
  1511. $this->attachment[$cur][3] = $encoding;
  1512. $this->attachment[$cur][4] = $type;
  1513. $this->attachment[$cur][5] = false;
  1514. $this->attachment[$cur][6] = 'inline';
  1515. $this->attachment[$cur][7] = $cid;
  1516.  
  1517. return true;
  1518. }
  1519.  
  1520. /**
  1521. * Returns true if an inline attachment is present.
  1522. * @access private
  1523. * @return bool
  1524. */
  1525. function InlineImageExists() {
  1526. $result = false;
  1527. for($i = 0; $i < count($this->attachment); $i++) {
  1528. if($this->attachment[$i][6] == 'inline') {
  1529. $result = true;
  1530. break;
  1531. }
  1532. }
  1533.  
  1534. return $result;
  1535. }
  1536.  
  1537. /////////////////////////////////////////////////
  1538. // CLASS METHODS, MESSAGE RESET
  1539. /////////////////////////////////////////////////
  1540.  
  1541.  
  1542. /**
  1543. * Clears all recipients assigned in the TO array. Returns void.
  1544. * @return void
  1545. */
  1546. function ClearAddresses() {
  1547. $this->to = array();
  1548. }
  1549.  
  1550. /**
  1551. * Clears all recipients assigned in the CC array. Returns void.
  1552. * @return void
  1553. */
  1554. function ClearCCs() {
  1555. $this->cc = array();
  1556. }
  1557.  
  1558. /**
  1559. * Clears all recipients assigned in the BCC array. Returns void.
  1560. * @return void
  1561. */
  1562. function ClearBCCs() {
  1563. $this->bcc = array();
  1564. }
  1565.  
  1566. /**
  1567. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1568. * @return void
  1569. */
  1570. function ClearReplyTos() {
  1571. $this->ReplyTo = array();
  1572. }
  1573.  
  1574. /**
  1575. * Clears all recipients assigned in the TO, CC and BCC
  1576. * array. Returns void.
  1577. * @return void
  1578. */
  1579. function ClearAllRecipients() {
  1580. $this->to = array();
  1581. $this->cc = array();
  1582. $this->bcc = array();
  1583. }
  1584.  
  1585. /**
  1586. * Clears all previously set filesystem, string, and binary
  1587. * attachments. Returns void.
  1588. * @return void
  1589. */
  1590. function ClearAttachments() {
  1591. $this->attachment = array();
  1592. }
  1593.  
  1594. /**
  1595. * Clears all custom headers. Returns void.
  1596. * @return void
  1597. */
  1598. function ClearCustomHeaders() {
  1599. $this->CustomHeader = array();
  1600. }
  1601.  
  1602. /////////////////////////////////////////////////
  1603. // CLASS METHODS, MISCELLANEOUS
  1604. /////////////////////////////////////////////////
  1605.  
  1606.  
  1607. /**
  1608. * Adds the error message to the error container.
  1609. * Returns void.
  1610. * @access private
  1611. * @return void
  1612. */
  1613. function SetError($msg) {
  1614. $this->error_count++;
  1615. $this->ErrorInfo = $msg;
  1616. }
  1617.  
  1618. /**
  1619. * Returns the proper RFC 822 formatted date.
  1620. * @access private
  1621. * @return string
  1622. */
  1623. function RFCDate() {
  1624. $tz = date('Z');
  1625. $tzs = ($tz < 0) ? '-' : '+';
  1626. $tz = abs($tz);
  1627. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  1628. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  1629.  
  1630. return $result;
  1631. }
  1632.  
  1633. /**
  1634. * Returns the appropriate server variable. Should work with both
  1635. * PHP 4.1.0+ as well as older versions. Returns an empty string
  1636. * if nothing is found.
  1637. * @access private
  1638. * @return mixed
  1639. */
  1640. function ServerVar($varName) {
  1641. global $HTTP_SERVER_VARS;
  1642. global $HTTP_ENV_VARS;
  1643.  
  1644. if(!isset($_SERVER)) {
  1645. $_SERVER = $HTTP_SERVER_VARS;
  1646. if(!isset($_SERVER['REMOTE_ADDR'])) {
  1647. $_SERVER = $HTTP_ENV_VARS; // must be Apache
  1648. }
  1649. }
  1650.  
  1651. if(isset($_SERVER[$varName])) {
  1652. return $_SERVER[$varName];
  1653. } else {
  1654. return '';
  1655. }
  1656. }
  1657.  
  1658. /**
  1659. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1660. * @access private
  1661. * @return string
  1662. */
  1663. function ServerHostname() {
  1664. if ($this->Hostname != '') {
  1665. $result = $this->Hostname;
  1666. } elseif ($this->ServerVar('SERVER_NAME') != '') {
  1667. $result = $this->ServerVar('SERVER_NAME');
  1668. } else {
  1669. $result = 'localhost.localdomain';
  1670. }
  1671.  
  1672. return $result;
  1673. }
  1674.  
  1675. /**
  1676. * Returns a message in the appropriate language.
  1677. * @access private
  1678. * @return string
  1679. */
  1680. function Lang($key) {
  1681. if(count($this->language) < 1) {
  1682. $this->SetLanguage('en'); // set the default language
  1683. }
  1684.  
  1685. if(isset($this->language[$key])) {
  1686. return $this->language[$key];
  1687. } else {
  1688. return 'Language string failed to load: ' . $key;
  1689. }
  1690. }
  1691.  
  1692. /**
  1693. * Returns true if an error occurred.
  1694. * @return bool
  1695. */
  1696. function IsError() {
  1697. return ($this->error_count > 0);
  1698. }
  1699.  
  1700. /**
  1701. * Changes every end of line from CR or LF to CRLF.
  1702. * @access private
  1703. * @return string
  1704. */
  1705. function FixEOL($str) {
  1706. $str = str_replace("\r\n", "\n", $str);
  1707. $str = str_replace("\r", "\n", $str);
  1708. $str = str_replace("\n", $this->LE, $str);
  1709. return $str;
  1710. }
  1711.  
  1712. /**
  1713. * Adds a custom header.
  1714. * @return void
  1715. */
  1716. function AddCustomHeader($custom_header) {
  1717. $this->CustomHeader[] = explode(':', $custom_header, 2);
  1718. }
  1719.  
  1720. /**
  1721. * Evaluates the message and returns modifications for inline images and backgrounds
  1722. * @access public
  1723. * @return $message
  1724. */
  1725. function MsgHTML($message,$basedir='') {
  1726. preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
  1727. if(isset($images[2])) {
  1728. foreach($images[2] as $i => $url) {
  1729. // do not change urls for absolute images (thanks to corvuscorax)
  1730. if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
  1731. $filename = basename($url);
  1732. $directory = dirname($url);
  1733. ($directory == '.')?$directory='':'';
  1734. $cid = 'cid:' . md5($filename);
  1735. $fileParts = split("\.", $filename);
  1736. $ext = $fileParts[1];
  1737. $mimeType = $this->_mime_types($ext);
  1738. if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
  1739. if ( strlen($directory) > 1 && substr($directory,-1) != '/') { $directory .= '/'; }
  1740. if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
  1741. $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
  1742. }
  1743. }
  1744. }
  1745. }
  1746. $this->IsHTML(true);
  1747. $this->Body = $message;
  1748. $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
  1749. if ( !empty($textMsg) && empty($this->AltBody) ) {
  1750. $this->AltBody = html_entity_decode($textMsg);
  1751. }
  1752. if ( empty($this->AltBody) ) {
  1753. $this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";
  1754. }
  1755. }
  1756.  
  1757. /**
  1758. * Gets the mime type of the embedded or inline image
  1759. * @access private
  1760. * @return mime type of ext
  1761. */
  1762. function _mime_types($ext = '') {
  1763. $mimes = array(
  1764. 'ai' => 'application/postscript',
  1765. 'aif' => 'audio/x-aiff',
  1766. 'aifc' => 'audio/x-aiff',
  1767. 'aiff' => 'audio/x-aiff',
  1768. 'avi' => 'video/x-msvideo',
  1769. 'bin' => 'application/macbinary',
  1770. 'bmp' => 'image/bmp',
  1771. 'class' => 'application/octet-stream',
  1772. 'cpt' => 'application/mac-compactpro',
  1773. 'css' => 'text/css',
  1774. 'dcr' => 'application/x-director',
  1775. 'dir' => 'application/x-director',
  1776. 'dll' => 'application/octet-stream',
  1777. 'dms' => 'application/octet-stream',
  1778. 'doc' => 'application/msword',
  1779. 'dvi' => 'application/x-dvi',
  1780. 'dxr' => 'application/x-director',
  1781. 'eml' => 'message/rfc822',
  1782. 'eps' => 'application/postscript',
  1783. 'exe' => 'application/octet-stream',
  1784. 'gif' => 'image/gif',
  1785. 'gtar' => 'application/x-gtar',
  1786. 'htm' => 'text/html',
  1787. 'html' => 'text/html',
  1788. 'jpe' => 'image/jpeg',
  1789. 'jpeg' => 'image/jpeg',
  1790. 'jpg' => 'image/jpeg',
  1791. 'hqx' => 'application/mac-binhex40',
  1792. 'js' => 'application/x-javascript',
  1793. 'lha' => 'application/octet-stream',
  1794. 'log' => 'text/plain',
  1795. 'lzh' => 'application/octet-stream',
  1796. 'mid' => 'audio/midi',
  1797. 'midi' => 'audio/midi',
  1798. 'mif' => 'application/vnd.mif',
  1799. 'mov' => 'video/quicktime',
  1800. 'movie' => 'video/x-sgi-movie',
  1801. 'mp2' => 'audio/mpeg',
  1802. 'mp3' => 'audio/mpeg',
  1803. 'mpe' => 'video/mpeg',
  1804. 'mpeg' => 'video/mpeg',
  1805. 'mpg' => 'video/mpeg',
  1806. 'mpga' => 'audio/mpeg',
  1807. 'oda' => 'application/oda',
  1808. 'pdf' => 'application/pdf',
  1809. 'php' => 'application/x-httpd-php',
  1810. 'php3' => 'application/x-httpd-php',
  1811. 'php4' => 'application/x-httpd-php',
  1812. 'phps' => 'application/x-httpd-php-source',
  1813. 'phtml' => 'application/x-httpd-php',
  1814. 'png' => 'image/png',
  1815. 'ppt' => 'application/vnd.ms-powerpoint',
  1816. 'ps' => 'application/postscript',
  1817. 'psd' => 'application/octet-stream',
  1818. 'qt' => 'video/quicktime',
  1819. 'ra' => 'audio/x-realaudio',
  1820. 'ram' => 'audio/x-pn-realaudio',
  1821. 'rm' => 'audio/x-pn-realaudio',
  1822. 'rpm' => 'audio/x-pn-realaudio-plugin',
  1823. 'rtf' => 'text/rtf',
  1824. 'rtx' => 'text/richtext',
  1825. 'rv' => 'video/vnd.rn-realvideo',
  1826. 'sea' => 'application/octet-stream',
  1827. 'shtml' => 'text/html',
  1828. 'sit' => 'application/x-stuffit',
  1829. 'so' => 'application/octet-stream',
  1830. 'smi' => 'application/smil',
  1831. 'smil' => 'application/smil',
  1832. 'swf' => 'application/x-shockwave-flash',
  1833. 'tar' => 'application/x-tar',
  1834. 'text' => 'text/plain',
  1835. 'txt' => 'text/plain',
  1836. 'tgz' => 'application/x-tar',
  1837. 'tif' => 'image/tiff',
  1838. 'tiff' => 'image/tiff',
  1839. 'wav' => 'audio/x-wav',
  1840. 'wbxml' => 'application/vnd.wap.wbxml',
  1841. 'wmlc' => 'application/vnd.wap.wmlc',
  1842. 'word' => 'application/msword',
  1843. 'xht' => 'application/xhtml+xml',
  1844. 'xhtml' => 'application/xhtml+xml',
  1845. 'xl' => 'application/excel',
  1846. 'xls' => 'application/vnd.ms-excel',
  1847. 'xml' => 'text/xml',
  1848. 'xsl' => 'text/xml',
  1849. 'zip' => 'application/zip'
  1850. );
  1851. return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
  1852. }
  1853.  
  1854. /**
  1855. * Set (or reset) Class Objects (variables)
  1856. *
  1857. * Usage Example:
  1858. * $page->set('X-Priority', '3');
  1859. *
  1860. * @access public
  1861. * @param string $name Parameter Name
  1862. * @param mixed $value Parameter Value
  1863. * NOTE: will not work with arrays, there are no arrays to set/reset
  1864. */
  1865. function set ( $name, $value = '' ) {
  1866. if ( isset($this->$name) ) {
  1867. $this->$name = $value;
  1868. } else {
  1869. $this->SetError('Cannot set or reset variable ' . $name);
  1870. return false;
  1871. }
  1872. }
  1873.  
  1874. /**
  1875. * Read a file from a supplied filename and return it.
  1876. *
  1877. * @access public
  1878. * @param string $filename Parameter File Name
  1879. */
  1880. function getFile($filename) {
  1881. $return = '';
  1882. if ($fp = fopen($filename, 'rb')) {
  1883. while (!feof($fp)) {
  1884. $return .= fread($fp, 1024);
  1885. }
  1886. fclose($fp);
  1887. return $return;
  1888. } else {
  1889. return false;
  1890. }
  1891. }
  1892.  
  1893. /**
  1894. * Strips newlines to prevent header injection.
  1895. * @access private
  1896. * @param string $str String
  1897. * @return string
  1898. */
  1899. function SecureHeader($str) {
  1900. $str = trim($str);
  1901. $str = str_replace("\r", "", $str);
  1902. $str = str_replace("\n", "", $str);
  1903. return $str;
  1904. }
  1905.  
  1906. /**
  1907. * Set the private key file and password to sign the message.
  1908. *
  1909. * @access public
  1910. * @param string $key_filename Parameter File Name
  1911. * @param string $key_pass Password for private key
  1912. */
  1913. function Sign($cert_filename, $key_filename, $key_pass) {
  1914. $this->sign_cert_file = $cert_filename;
  1915. $this->sign_key_file = $key_filename;
  1916. $this->sign_key_pass = $key_pass;
  1917. }
  1918.  
  1919. }
  1920.  
  1921. ?>

Documentation generated on Thu, 02 Apr 2009 21:19:47 -0400 by phpDocumentor 1.3.0RC3