PageRenderTime 68ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/classes/class.phpmailer.php

https://bitbucket.org/flth/xtcm
PHP | 1909 lines | 1302 code | 157 blank | 450 comment | 199 complexity | 8a9655f43173b6ee18b364fb9b0824ed MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  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. * PHPMailer - PHP email transport class
  28. * @package PHPMailer
  29. * @author Andy Prevost
  30. * @copyright 2004 - 2009 Andy Prevost
  31. */
  32. class PHPMailer {
  33. /////////////////////////////////////////////////
  34. // PROPERTIES, PUBLIC
  35. /////////////////////////////////////////////////
  36. /**
  37. * Email priority (1 = High, 3 = Normal, 5 = low).
  38. * @var int
  39. */
  40. var $Priority = 3;
  41. /**
  42. * Sets the CharSet of the message.
  43. * @var string
  44. */
  45. var $CharSet = 'iso-8859-1';
  46. /**
  47. * Sets the Content-type of the message.
  48. * @var string
  49. */
  50. var $ContentType = 'text/plain';
  51. /**
  52. * Sets the Encoding of the message. Options for this are "8bit",
  53. * "7bit", "binary", "base64", and "quoted-printable".
  54. * @var string
  55. */
  56. var $Encoding = '8bit';
  57. /**
  58. * Holds the most recent mailer error message.
  59. * @var string
  60. */
  61. var $ErrorInfo = '';
  62. /**
  63. * Sets the From email address for the message.
  64. * @var string
  65. */
  66. var $From = 'root@localhost';
  67. /**
  68. * Sets the From name of the message.
  69. * @var string
  70. */
  71. var $FromName = 'Root User';
  72. /**
  73. * Sets the Sender email (Return-Path) of the message. If not empty,
  74. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  75. * @var string
  76. */
  77. var $Sender = '';
  78. /**
  79. * Sets the Subject of the message.
  80. * @var string
  81. */
  82. var $Subject = '';
  83. /**
  84. * Sets the Body of the message. This can be either an HTML or text body.
  85. * If HTML then run IsHTML(true).
  86. * @var string
  87. */
  88. var $Body = '';
  89. /**
  90. * Sets the text-only body of the message. This automatically sets the
  91. * email to multipart/alternative. This body can be read by mail
  92. * clients that do not have HTML email capability such as mutt. Clients
  93. * that can read HTML will view the normal Body.
  94. * @var string
  95. */
  96. var $AltBody = '';
  97. /**
  98. * Sets word wrapping on the body of the message to a given number of
  99. * characters.
  100. * @var int
  101. */
  102. var $WordWrap = 0;
  103. /**
  104. * Method to send mail: ("mail", "sendmail", or "smtp").
  105. * @var string
  106. */
  107. var $Mailer = 'mail';
  108. /**
  109. * Sets the path of the sendmail program.
  110. * @var string
  111. */
  112. var $Sendmail = '/usr/sbin/sendmail';
  113. /**
  114. * Path to PHPMailer plugins. This is now only useful if the SMTP class
  115. * is in a different directory than the PHP include path.
  116. * @var string
  117. */
  118. var $PluginDir = '';
  119. /**
  120. * Holds PHPMailer version.
  121. * @var string
  122. */
  123. var $Version = "2.0.4";
  124. /**
  125. * Sets the email address that a reading confirmation will be sent.
  126. * @var string
  127. */
  128. var $ConfirmReadingTo = '';
  129. /**
  130. * Sets the hostname to use in Message-Id and Received headers
  131. * and as default HELO string. If empty, the value returned
  132. * by SERVER_NAME is used or 'localhost.localdomain'.
  133. * @var string
  134. */
  135. var $Hostname = '';
  136. /**
  137. * Sets the message ID to be used in the Message-Id header.
  138. * If empty, a unique id will be generated.
  139. * @var string
  140. */
  141. var $MessageID = '';
  142. /////////////////////////////////////////////////
  143. // PROPERTIES FOR SMTP
  144. /////////////////////////////////////////////////
  145. /**
  146. * Sets the SMTP hosts. All hosts must be separated by a
  147. * semicolon. You can also specify a different port
  148. * for each host by using this format: [hostname:port]
  149. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  150. * Hosts will be tried in order.
  151. * @var string
  152. */
  153. var $Host = 'localhost';
  154. /**
  155. * Sets the default SMTP server port.
  156. * @var int
  157. */
  158. var $Port = 25;
  159. /**
  160. * Sets the SMTP HELO of the message (Default is $Hostname).
  161. * @var string
  162. */
  163. var $Helo = '';
  164. /**
  165. * Sets connection prefix.
  166. * Options are "", "ssl" or "tls"
  167. * @var string
  168. */
  169. var $SMTPSecure = "";
  170. /**
  171. * Sets SMTP authentication. Utilizes the Username and Password variables.
  172. * @var bool
  173. */
  174. var $SMTPAuth = false;
  175. /**
  176. * Sets SMTP username.
  177. * @var string
  178. */
  179. var $Username = '';
  180. /**
  181. * Sets SMTP password.
  182. * @var string
  183. */
  184. var $Password = '';
  185. /**
  186. * Sets the SMTP server timeout in seconds. This function will not
  187. * work with the win32 version.
  188. * @var int
  189. */
  190. var $Timeout = 10;
  191. /**
  192. * Sets SMTP class debugging on or off.
  193. * @var bool
  194. */
  195. var $SMTPDebug = false;
  196. /**
  197. * Prevents the SMTP connection from being closed after each mail
  198. * sending. If this is set to true then to close the connection
  199. * requires an explicit call to SmtpClose().
  200. * @var bool
  201. */
  202. var $SMTPKeepAlive = false;
  203. /**
  204. * Provides the ability to have the TO field process individual
  205. * emails, instead of sending to entire TO addresses
  206. * @var bool
  207. */
  208. var $SingleTo = false;
  209. /////////////////////////////////////////////////
  210. // PROPERTIES, PRIVATE
  211. /////////////////////////////////////////////////
  212. var $smtp = NULL;
  213. var $to = array();
  214. var $cc = array();
  215. var $bcc = array();
  216. var $ReplyTo = array();
  217. var $attachment = array();
  218. var $CustomHeader = array();
  219. var $message_type = '';
  220. var $boundary = array();
  221. var $language = array();
  222. var $error_count = 0;
  223. var $LE = "\n";
  224. var $sign_cert_file = "";
  225. var $sign_key_file = "";
  226. var $sign_key_pass = "";
  227. /////////////////////////////////////////////////
  228. // METHODS, VARIABLES
  229. /////////////////////////////////////////////////
  230. /**
  231. * Sets message type to HTML.
  232. * @param bool $bool
  233. * @return void
  234. */
  235. function IsHTML($bool) {
  236. if($bool == true) {
  237. $this->ContentType = 'text/html';
  238. } else {
  239. $this->ContentType = 'text/plain';
  240. }
  241. }
  242. /**
  243. * Sets Mailer to send message using SMTP.
  244. * @return void
  245. */
  246. function IsSMTP() {
  247. $this->Mailer = 'smtp';
  248. }
  249. /**
  250. * Sets Mailer to send message using PHP mail() function.
  251. * @return void
  252. */
  253. function IsMail() {
  254. $this->Mailer = 'mail';
  255. }
  256. /**
  257. * Sets Mailer to send message using the $Sendmail program.
  258. * @return void
  259. */
  260. function IsSendmail() {
  261. $this->Mailer = 'sendmail';
  262. }
  263. /**
  264. * Sets Mailer to send message using the qmail MTA.
  265. * @return void
  266. */
  267. function IsQmail() {
  268. $this->Sendmail = '/var/qmail/bin/sendmail';
  269. $this->Mailer = 'sendmail';
  270. }
  271. /////////////////////////////////////////////////
  272. // METHODS, RECIPIENTS
  273. /////////////////////////////////////////////////
  274. /**
  275. * Adds a "To" address.
  276. * @param string $address
  277. * @param string $name
  278. * @return void
  279. */
  280. function AddAddress($address, $name = '') {
  281. $cur = count($this->to);
  282. $this->to[$cur][0] = trim($address);
  283. $this->to[$cur][1] = $name;
  284. }
  285. /**
  286. * Adds a "Cc" address. Note: this function works
  287. * with the SMTP mailer on win32, not with the "mail"
  288. * mailer.
  289. * @param string $address
  290. * @param string $name
  291. * @return void
  292. */
  293. function AddCC($address, $name = '') {
  294. $cur = count($this->cc);
  295. $this->cc[$cur][0] = trim($address);
  296. $this->cc[$cur][1] = $name;
  297. }
  298. /**
  299. * Adds a "Bcc" address. Note: this function works
  300. * with the SMTP mailer on win32, not with the "mail"
  301. * mailer.
  302. * @param string $address
  303. * @param string $name
  304. * @return void
  305. */
  306. function AddBCC($address, $name = '') {
  307. $cur = count($this->bcc);
  308. $this->bcc[$cur][0] = trim($address);
  309. $this->bcc[$cur][1] = $name;
  310. }
  311. /**
  312. * Adds a "Reply-To" address.
  313. * @param string $address
  314. * @param string $name
  315. * @return void
  316. */
  317. function AddReplyTo($address, $name = '') {
  318. $cur = count($this->ReplyTo);
  319. $this->ReplyTo[$cur][0] = trim($address);
  320. $this->ReplyTo[$cur][1] = $name;
  321. }
  322. /////////////////////////////////////////////////
  323. // METHODS, MAIL SENDING
  324. /////////////////////////////////////////////////
  325. /**
  326. * Creates message and assigns Mailer. If the message is
  327. * not sent successfully then it returns false. Use the ErrorInfo
  328. * variable to view description of the error.
  329. * @return bool
  330. */
  331. function Send() {
  332. $header = '';
  333. $body = '';
  334. $result = true;
  335. if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  336. $this->SetError($this->Lang('provide_address'));
  337. return false;
  338. }
  339. /* Set whether the message is multipart/alternative */
  340. if(!empty($this->AltBody)) {
  341. $this->ContentType = 'multipart/alternative';
  342. }
  343. $this->error_count = 0; // reset errors
  344. $this->SetMessageType();
  345. $header .= $this->CreateHeader();
  346. $body = $this->CreateBody();
  347. if($body == '') {
  348. return false;
  349. }
  350. /* Choose the mailer */
  351. switch($this->Mailer) {
  352. case 'sendmail':
  353. $result = $this->SendmailSend($header, $body);
  354. break;
  355. case 'smtp':
  356. $result = $this->SmtpSend($header, $body);
  357. break;
  358. case 'mail':
  359. $result = $this->MailSend($header, $body);
  360. break;
  361. default:
  362. $result = $this->MailSend($header, $body);
  363. break;
  364. //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
  365. //$result = false;
  366. //break;
  367. }
  368. return $result;
  369. }
  370. /**
  371. * Sends mail using the $Sendmail program.
  372. * @access private
  373. * @return bool
  374. */
  375. function SendmailSend($header, $body) {
  376. if ($this->Sender != '') {
  377. $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  378. } else {
  379. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  380. }
  381. if(!@$mail = popen($sendmail, 'w')) {
  382. $this->SetError($this->Lang('execute') . $this->Sendmail);
  383. return false;
  384. }
  385. fputs($mail, $header);
  386. fputs($mail, $body);
  387. $result = pclose($mail);
  388. if (version_compare(phpversion(), '4.2.3') == -1) {
  389. $result = $result >> 8 & 0xFF;
  390. }
  391. if($result != 0) {
  392. $this->SetError($this->Lang('execute') . $this->Sendmail);
  393. return false;
  394. }
  395. return true;
  396. }
  397. /**
  398. * Sends mail using the PHP mail() function.
  399. * @access private
  400. * @return bool
  401. */
  402. function MailSend($header, $body) {
  403. $to = '';
  404. for($i = 0; $i < count($this->to); $i++) {
  405. if($i != 0) { $to .= ', '; }
  406. $to .= $this->AddrFormat($this->to[$i]);
  407. }
  408. $toArr = explode(',', $to); // Dokuman - 2010-02-15 - replaced deprecated function split with explode to be ready for PHP >= 5.3
  409. $params = sprintf("-oi -f %s", $this->Sender);
  410. if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
  411. $old_from = ini_get('sendmail_from');
  412. ini_set('sendmail_from', $this->Sender);
  413. if ($this->SingleTo === true && count($toArr) > 1) {
  414. foreach ($toArr as $key => $val) {
  415. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  416. }
  417. } else {
  418. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  419. }
  420. } else {
  421. if ($this->SingleTo === true && count($toArr) > 1) {
  422. foreach ($toArr as $key => $val) {
  423. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  424. }
  425. } else {
  426. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
  427. }
  428. }
  429. if (isset($old_from)) {
  430. ini_set('sendmail_from', $old_from);
  431. }
  432. if(!$rt) {
  433. $this->SetError($this->Lang('instantiate'));
  434. return false;
  435. }
  436. return true;
  437. }
  438. /**
  439. * Sends mail via SMTP using PhpSMTP (Author:
  440. * Chris Ryan). Returns bool. Returns false if there is a
  441. * bad MAIL FROM, RCPT, or DATA input.
  442. * @access private
  443. * @return bool
  444. */
  445. function SmtpSend($header, $body) {
  446. include_once($this->PluginDir . 'class.smtp.php');
  447. $error = '';
  448. $bad_rcpt = array();
  449. if(!$this->SmtpConnect()) {
  450. return false;
  451. }
  452. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  453. if(!$this->smtp->Mail($smtp_from)) {
  454. $error = $this->Lang('from_failed') . $smtp_from;
  455. $this->SetError($error);
  456. $this->smtp->Reset();
  457. return false;
  458. }
  459. /* Attempt to send attach all recipients */
  460. for($i = 0; $i < count($this->to); $i++) {
  461. if(!$this->smtp->Recipient($this->to[$i][0])) {
  462. $bad_rcpt[] = $this->to[$i][0];
  463. }
  464. }
  465. for($i = 0; $i < count($this->cc); $i++) {
  466. if(!$this->smtp->Recipient($this->cc[$i][0])) {
  467. $bad_rcpt[] = $this->cc[$i][0];
  468. }
  469. }
  470. for($i = 0; $i < count($this->bcc); $i++) {
  471. if(!$this->smtp->Recipient($this->bcc[$i][0])) {
  472. $bad_rcpt[] = $this->bcc[$i][0];
  473. }
  474. }
  475. if(count($bad_rcpt) > 0) { // Create error message
  476. for($i = 0; $i < count($bad_rcpt); $i++) {
  477. if($i != 0) {
  478. $error .= ', ';
  479. }
  480. $error .= $bad_rcpt[$i];
  481. }
  482. $error = $this->Lang('recipients_failed') . $error;
  483. $this->SetError($error);
  484. $this->smtp->Reset();
  485. return false;
  486. }
  487. if(!$this->smtp->Data($header . $body)) {
  488. $this->SetError($this->Lang('data_not_accepted'));
  489. $this->smtp->Reset();
  490. return false;
  491. }
  492. if($this->SMTPKeepAlive == true) {
  493. $this->smtp->Reset();
  494. } else {
  495. $this->SmtpClose();
  496. }
  497. return true;
  498. }
  499. /**
  500. * Initiates a connection to an SMTP server. Returns false if the
  501. * operation failed.
  502. * @access private
  503. * @return bool
  504. */
  505. function SmtpConnect() {
  506. if($this->smtp == NULL) {
  507. $this->smtp = new SMTP();
  508. }
  509. $this->smtp->do_debug = $this->SMTPDebug;
  510. $hosts = explode(';', $this->Host);
  511. $index = 0;
  512. $connection = ($this->smtp->Connected());
  513. /* Retry while there is no connection */
  514. while($index < count($hosts) && $connection == false) {
  515. $hostinfo = array();
  516. if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) { // web28 - 2010-07-10 - replaced deprecated function eregi with preg_match to be ready for PHP >= 5.3
  517. $host = $hostinfo[1];
  518. $port = $hostinfo[2];
  519. } else {
  520. $host = $hosts[$index];
  521. $port = $this->Port;
  522. }
  523. if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {
  524. if ($this->Helo != '') {
  525. $this->smtp->Hello($this->Helo);
  526. } else {
  527. $this->smtp->Hello($this->ServerHostname());
  528. }
  529. $connection = true;
  530. if($this->SMTPAuth) {
  531. if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
  532. $this->SetError($this->Lang('authenticate'));
  533. $this->smtp->Reset();
  534. $connection = false;
  535. }
  536. }
  537. }
  538. $index++;
  539. }
  540. if(!$connection) {
  541. $this->SetError($this->Lang('connect_host'));
  542. }
  543. return $connection;
  544. }
  545. /**
  546. * Closes the active SMTP session if one exists.
  547. * @return void
  548. */
  549. function SmtpClose() {
  550. if($this->smtp != NULL) {
  551. if($this->smtp->Connected()) {
  552. $this->smtp->Quit();
  553. $this->smtp->Close();
  554. }
  555. }
  556. }
  557. /**
  558. * Sets the language for all class error messages. Returns false
  559. * if it cannot load the language file. The default language type
  560. * is English.
  561. * @param string $lang_type Type of language (e.g. Portuguese: "br")
  562. * @param string $lang_path Path to the language file directory
  563. * @access public
  564. * @return bool
  565. */
  566. function SetLanguage($lang_type, $lang_path = 'language/') {
  567. if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {
  568. include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  569. } elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {
  570. include($lang_path.'phpmailer.lang-en.php');
  571. } else {
  572. $PHPMAILER_LANG = array();
  573. $PHPMAILER_LANG["provide_address"] = 'You must provide at least one ' .
  574. $PHPMAILER_LANG["mailer_not_supported"] = ' mailer is not supported.';
  575. $PHPMAILER_LANG["execute"] = 'Could not execute: ';
  576. $PHPMAILER_LANG["instantiate"] = 'Could not instantiate mail function.';
  577. $PHPMAILER_LANG["authenticate"] = 'SMTP Error: Could not authenticate.';
  578. $PHPMAILER_LANG["from_failed"] = 'The following From address failed: ';
  579. $PHPMAILER_LANG["recipients_failed"] = 'SMTP Error: The following ' .
  580. $PHPMAILER_LANG["data_not_accepted"] = 'SMTP Error: Data not accepted.';
  581. $PHPMAILER_LANG["connect_host"] = 'SMTP Error: Could not connect to SMTP host.';
  582. $PHPMAILER_LANG["file_access"] = 'Could not access file: ';
  583. $PHPMAILER_LANG["file_open"] = 'File Error: Could not open file: ';
  584. $PHPMAILER_LANG["encoding"] = 'Unknown encoding: ';
  585. $PHPMAILER_LANG["signing"] = 'Signing Error: ';
  586. }
  587. $this->language = $PHPMAILER_LANG;
  588. return true;
  589. }
  590. /////////////////////////////////////////////////
  591. // METHODS, MESSAGE CREATION
  592. /////////////////////////////////////////////////
  593. /**
  594. * Creates recipient headers.
  595. * @access private
  596. * @return string
  597. */
  598. function AddrAppend($type, $addr) {
  599. $addr_str = $type . ': ';
  600. $addr_str .= $this->AddrFormat($addr[0]);
  601. if(count($addr) > 1) {
  602. for($i = 1; $i < count($addr); $i++) {
  603. $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
  604. }
  605. }
  606. $addr_str .= $this->LE;
  607. return $addr_str;
  608. }
  609. /**
  610. * Formats an address correctly.
  611. * @access private
  612. * @return string
  613. */
  614. function AddrFormat($addr) {
  615. if(empty($addr[1])) {
  616. $formatted = $this->SecureHeader($addr[0]);
  617. } else {
  618. $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  619. }
  620. return $formatted;
  621. }
  622. /**
  623. * Wraps message for use with mailers that do not
  624. * automatically perform wrapping and for quoted-printable.
  625. * Original written by philippe.
  626. * @access private
  627. * @return string
  628. */
  629. function WrapText($message, $length, $qp_mode = false) {
  630. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  631. // If utf-8 encoding is used, we will need to make sure we don't
  632. // split multibyte characters when we wrap
  633. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  634. $message = $this->FixEOL($message);
  635. if (substr($message, -1) == $this->LE) {
  636. $message = substr($message, 0, -1);
  637. }
  638. $line = explode($this->LE, $message);
  639. $message = '';
  640. for ($i=0 ;$i < count($line); $i++) {
  641. $line_part = explode(' ', $line[$i]);
  642. $buf = '';
  643. for ($e = 0; $e<count($line_part); $e++) {
  644. $word = $line_part[$e];
  645. if ($qp_mode and (strlen($word) > $length)) {
  646. $space_left = $length - strlen($buf) - 1;
  647. if ($e != 0) {
  648. if ($space_left > 20) {
  649. $len = $space_left;
  650. if ($is_utf8) {
  651. $len = $this->UTF8CharBoundary($word, $len);
  652. } elseif (substr($word, $len - 1, 1) == "=") {
  653. $len--;
  654. } elseif (substr($word, $len - 2, 1) == "=") {
  655. $len -= 2;
  656. }
  657. $part = substr($word, 0, $len);
  658. $word = substr($word, $len);
  659. $buf .= ' ' . $part;
  660. $message .= $buf . sprintf("=%s", $this->LE);
  661. } else {
  662. $message .= $buf . $soft_break;
  663. }
  664. $buf = '';
  665. }
  666. while (strlen($word) > 0) {
  667. $len = $length;
  668. if ($is_utf8) {
  669. $len = $this->UTF8CharBoundary($word, $len);
  670. } elseif (substr($word, $len - 1, 1) == "=") {
  671. $len--;
  672. } elseif (substr($word, $len - 2, 1) == "=") {
  673. $len -= 2;
  674. }
  675. $part = substr($word, 0, $len);
  676. $word = substr($word, $len);
  677. if (strlen($word) > 0) {
  678. $message .= $part . sprintf("=%s", $this->LE);
  679. } else {
  680. $buf = $part;
  681. }
  682. }
  683. } else {
  684. $buf_o = $buf;
  685. $buf .= ($e == 0) ? $word : (' ' . $word);
  686. if (strlen($buf) > $length and $buf_o != '') {
  687. $message .= $buf_o . $soft_break;
  688. $buf = $word;
  689. }
  690. }
  691. }
  692. $message .= $buf . $this->LE;
  693. }
  694. return $message;
  695. }
  696. /**
  697. * Finds last character boundary prior to maxLength in a utf-8
  698. * quoted (printable) encoded string.
  699. * Original written by Colin Brown.
  700. * @access private
  701. * @param string $encodedText utf-8 QP text
  702. * @param int $maxLength find last character boundary prior to this length
  703. * @return int
  704. */
  705. function UTF8CharBoundary($encodedText, $maxLength) {
  706. $foundSplitPos = false;
  707. $lookBack = 3;
  708. while (!$foundSplitPos) {
  709. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  710. $encodedCharPos = strpos($lastChunk, "=");
  711. if ($encodedCharPos !== false) {
  712. // Found start of encoded character byte within $lookBack block.
  713. // Check the encoded byte value (the 2 chars after the '=')
  714. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  715. $dec = hexdec($hex);
  716. if ($dec < 128) { // Single byte character.
  717. // If the encoded char was found at pos 0, it will fit
  718. // otherwise reduce maxLength to start of the encoded char
  719. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  720. $maxLength - ($lookBack - $encodedCharPos);
  721. $foundSplitPos = true;
  722. } elseif ($dec >= 192) { // First byte of a multi byte character
  723. // Reduce maxLength to split at start of character
  724. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  725. $foundSplitPos = true;
  726. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  727. $lookBack += 3;
  728. }
  729. } else {
  730. // No encoded character found
  731. $foundSplitPos = true;
  732. }
  733. }
  734. return $maxLength;
  735. }
  736. /**
  737. * Set the body wrapping.
  738. * @access private
  739. * @return void
  740. */
  741. function SetWordWrap() {
  742. if($this->WordWrap < 1) {
  743. return;
  744. }
  745. switch($this->message_type) {
  746. case 'alt':
  747. /* fall through */
  748. case 'alt_attachments':
  749. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  750. break;
  751. default:
  752. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  753. break;
  754. }
  755. }
  756. /**
  757. * Assembles message header.
  758. * @access private
  759. * @return string
  760. */
  761. function CreateHeader() {
  762. $result = '';
  763. /* Set the boundaries */
  764. $uniq_id = md5(uniqid(time()));
  765. $this->boundary[1] = 'b1_' . $uniq_id;
  766. $this->boundary[2] = 'b2_' . $uniq_id;
  767. $result .= $this->HeaderLine('Date', $this->RFCDate());
  768. if($this->Sender == '') {
  769. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  770. } else {
  771. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  772. }
  773. /* To be created automatically by mail() */
  774. if($this->Mailer != 'mail') {
  775. if(count($this->to) > 0) {
  776. $result .= $this->AddrAppend('To', $this->to);
  777. } elseif (count($this->cc) == 0) {
  778. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  779. }
  780. }
  781. $from = array();
  782. $from[0][0] = trim($this->From);
  783. $from[0][1] = $this->FromName;
  784. $result .= $this->AddrAppend('From', $from);
  785. /* sendmail and mail() extract Cc from the header before sending */
  786. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
  787. $result .= $this->AddrAppend('Cc', $this->cc);
  788. }
  789. /* sendmail and mail() extract Bcc from the header before sending */
  790. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  791. $result .= $this->AddrAppend('Bcc', $this->bcc);
  792. }
  793. if(count($this->ReplyTo) > 0) {
  794. $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
  795. }
  796. /* mail() sets the subject itself */
  797. if($this->Mailer != 'mail') {
  798. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  799. }
  800. if($this->MessageID != '') {
  801. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  802. } else {
  803. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  804. }
  805. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  806. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');
  807. if($this->ConfirmReadingTo != '') {
  808. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  809. }
  810. // Add custom headers
  811. for($index = 0; $index < count($this->CustomHeader); $index++) {
  812. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  813. }
  814. if (!$this->sign_key_file) {
  815. $result .= $this->HeaderLine('MIME-Version', '1.0');
  816. $result .= $this->GetMailMIME();
  817. }
  818. return $result;
  819. }
  820. /**
  821. * Returns the message MIME.
  822. * @access private
  823. * @return string
  824. */
  825. function GetMailMIME() {
  826. $result = '';
  827. switch($this->message_type) {
  828. case 'plain':
  829. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  830. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
  831. break;
  832. case 'attachments':
  833. /* fall through */
  834. case 'alt_attachments':
  835. if($this->InlineImageExists()){
  836. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
  837. } else {
  838. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  839. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  840. }
  841. break;
  842. case 'alt':
  843. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  844. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  845. break;
  846. }
  847. if($this->Mailer != 'mail') {
  848. $result .= $this->LE.$this->LE;
  849. }
  850. return $result;
  851. }
  852. /**
  853. * Assembles the message body. Returns an empty string on failure.
  854. * @access private
  855. * @return string
  856. */
  857. function CreateBody() {
  858. $result = '';
  859. if ($this->sign_key_file) {
  860. $result .= $this->GetMailMIME();
  861. }
  862. $this->SetWordWrap();
  863. switch($this->message_type) {
  864. case 'alt':
  865. $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  866. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  867. $result .= $this->LE.$this->LE;
  868. $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  869. $result .= $this->EncodeString($this->Body, $this->Encoding);
  870. $result .= $this->LE.$this->LE;
  871. $result .= $this->EndBoundary($this->boundary[1]);
  872. break;
  873. case 'plain':
  874. $result .= $this->EncodeString($this->Body, $this->Encoding);
  875. break;
  876. case 'attachments':
  877. $result .= $this->GetBoundary($this->boundary[1], '', '', '');
  878. $result .= $this->EncodeString($this->Body, $this->Encoding);
  879. $result .= $this->LE;
  880. $result .= $this->AttachAll();
  881. break;
  882. case 'alt_attachments':
  883. $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  884. $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
  885. $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
  886. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  887. $result .= $this->LE.$this->LE;
  888. $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
  889. $result .= $this->EncodeString($this->Body, $this->Encoding);
  890. $result .= $this->LE.$this->LE;
  891. $result .= $this->EndBoundary($this->boundary[2]);
  892. $result .= $this->AttachAll();
  893. break;
  894. }
  895. if($this->IsError()) {
  896. $result = '';
  897. } else if ($this->sign_key_file) {
  898. $file = tempnam("", "mail");
  899. $fp = fopen($file, "w");
  900. fwrite($fp, $result);
  901. fclose($fp);
  902. $signed = tempnam("", "signed");
  903. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
  904. $fp = fopen($signed, "r");
  905. $result = fread($fp, filesize($this->sign_key_file));
  906. $result = '';
  907. while(!feof($fp)){
  908. $result = $result . fread($fp, 1024);
  909. }
  910. fclose($fp);
  911. } else {
  912. $this->SetError($this->Lang("signing").openssl_error_string());
  913. $result = '';
  914. }
  915. unlink($file);
  916. unlink($signed);
  917. }
  918. return $result;
  919. }
  920. /**
  921. * Returns the start of a message boundary.
  922. * @access private
  923. */
  924. function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  925. $result = '';
  926. if($charSet == '') {
  927. $charSet = $this->CharSet;
  928. }
  929. if($contentType == '') {
  930. $contentType = $this->ContentType;
  931. }
  932. if($encoding == '') {
  933. $encoding = $this->Encoding;
  934. }
  935. $result .= $this->TextLine('--' . $boundary);
  936. $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
  937. $result .= $this->LE;
  938. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  939. $result .= $this->LE;
  940. return $result;
  941. }
  942. /**
  943. * Returns the end of a message boundary.
  944. * @access private
  945. */
  946. function EndBoundary($boundary) {
  947. return $this->LE . '--' . $boundary . '--' . $this->LE;
  948. }
  949. /**
  950. * Sets the message type.
  951. * @access private
  952. * @return void
  953. */
  954. function SetMessageType() {
  955. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
  956. $this->message_type = 'plain';
  957. } else {
  958. if(count($this->attachment) > 0) {
  959. $this->message_type = 'attachments';
  960. }
  961. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
  962. $this->message_type = 'alt';
  963. }
  964. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
  965. $this->message_type = 'alt_attachments';
  966. }
  967. }
  968. }
  969. /* Returns a formatted header line.
  970. * @access private
  971. * @return string
  972. */
  973. function HeaderLine($name, $value) {
  974. return $name . ': ' . $value . $this->LE;
  975. }
  976. /**
  977. * Returns a formatted mail line.
  978. * @access private
  979. * @return string
  980. */
  981. function TextLine($value) {
  982. return $value . $this->LE;
  983. }
  984. /////////////////////////////////////////////////
  985. // CLASS METHODS, ATTACHMENTS
  986. /////////////////////////////////////////////////
  987. /**
  988. * Adds an attachment from a path on the filesystem.
  989. * Returns false if the file could not be found
  990. * or accessed.
  991. * @param string $path Path to the attachment.
  992. * @param string $name Overrides the attachment name.
  993. * @param string $encoding File encoding (see $Encoding).
  994. * @param string $type File extension (MIME) type.
  995. * @return bool
  996. */
  997. function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  998. if(!@is_file($path)) {
  999. $this->SetError($this->Lang('file_access') . $path);
  1000. return false;
  1001. }
  1002. $filename = basename($path);
  1003. if($name == '') {
  1004. $name = $filename;
  1005. }
  1006. $cur = count($this->attachment);
  1007. $this->attachment[$cur][0] = $path;
  1008. $this->attachment[$cur][1] = $filename;
  1009. $this->attachment[$cur][2] = $name;
  1010. $this->attachment[$cur][3] = $encoding;
  1011. $this->attachment[$cur][4] = $type;
  1012. $this->attachment[$cur][5] = false; // isStringAttachment
  1013. $this->attachment[$cur][6] = 'attachment';
  1014. $this->attachment[$cur][7] = 0;
  1015. return true;
  1016. }
  1017. /**
  1018. * Attaches all fs, string, and binary attachments to the message.
  1019. * Returns an empty string on failure.
  1020. * @access private
  1021. * @return string
  1022. */
  1023. function AttachAll() {
  1024. /* Return text of body */
  1025. $mime = array();
  1026. /* Add all attachments */
  1027. for($i = 0; $i < count($this->attachment); $i++) {
  1028. /* Check for string attachment */
  1029. $bString = $this->attachment[$i][5];
  1030. if ($bString) {
  1031. $string = $this->attachment[$i][0];
  1032. } else {
  1033. $path = $this->attachment[$i][0];
  1034. }
  1035. $filename = $this->attachment[$i][1];
  1036. $name = $this->attachment[$i][2];
  1037. $encoding = $this->attachment[$i][3];
  1038. $type = $this->attachment[$i][4];
  1039. $disposition = $this->attachment[$i][6];
  1040. $cid = $this->attachment[$i][7];
  1041. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  1042. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
  1043. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1044. if($disposition == 'inline') {
  1045. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1046. }
  1047. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
  1048. /* Encode as string attachment */
  1049. if($bString) {
  1050. $mime[] = $this->EncodeString($string, $encoding);
  1051. if($this->IsError()) {
  1052. return '';
  1053. }
  1054. $mime[] = $this->LE.$this->LE;
  1055. } else {
  1056. $mime[] = $this->EncodeFile($path, $encoding);
  1057. if($this->IsError()) {
  1058. return '';
  1059. }
  1060. $mime[] = $this->LE.$this->LE;
  1061. }
  1062. }
  1063. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  1064. return join('', $mime);
  1065. }
  1066. /**
  1067. * Encodes attachment in requested format. Returns an
  1068. * empty string on failure.
  1069. * @access private
  1070. * @return string
  1071. */
  1072. function EncodeFile ($path, $encoding = 'base64') {
  1073. if(!@$fd = fopen($path, 'rb')) {
  1074. $this->SetError($this->Lang('file_open') . $path);
  1075. return '';
  1076. }
  1077. $magic_quotes = get_magic_quotes_runtime();
  1078. @set_magic_quotes_runtime(0); // Hetfield - 2009-11-19 - deprecated function set_magic_quotes_runtime to be ready for PHP >= 5.3
  1079. $file_buffer = fread($fd, filesize($path));
  1080. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1081. fclose($fd);
  1082. @set_magic_quotes_runtime($magic_quotes); // Hetfield - 2009-11-19 - deprecated function set_magic_quotes_runtime to be ready for PHP >= 5.3
  1083. return $file_buffer;
  1084. }
  1085. /**
  1086. * Encodes string to requested format. Returns an
  1087. * empty string on failure.
  1088. * @access private
  1089. * @return string
  1090. */
  1091. function EncodeString ($str, $encoding = 'base64') {
  1092. $encoded = '';
  1093. switch(strtolower($encoding)) {
  1094. case 'base64':
  1095. /* chunk_split is found in PHP >= 3.0.6 */
  1096. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1097. break;
  1098. case '7bit':
  1099. case '8bit':
  1100. $encoded = $this->FixEOL($str);
  1101. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1102. $encoded .= $this->LE;
  1103. break;
  1104. case 'binary':
  1105. $encoded = $str;
  1106. break;
  1107. case 'quoted-printable':
  1108. $encoded = $this->EncodeQP($str);
  1109. break;
  1110. default:
  1111. $this->SetError($this->Lang('encoding') . $encoding);
  1112. break;
  1113. }
  1114. return $encoded;
  1115. }
  1116. /**
  1117. * Encode a header string to best of Q, B, quoted or none.
  1118. * @access private
  1119. * @return string
  1120. */
  1121. function EncodeHeader ($str, $position = 'text') {
  1122. $x = 0;
  1123. switch (strtolower($position)) {
  1124. case 'phrase':
  1125. if (!preg_match('/[\200-\377]/', $str)) {
  1126. /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
  1127. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1128. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1129. return ($encoded);
  1130. } else {
  1131. return ("\"$encoded\"");
  1132. }
  1133. }
  1134. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1135. break;
  1136. case 'comment':
  1137. $x = preg_match_all('/[()"]/', $str, $matches);
  1138. /* Fall-through */
  1139. case 'text':
  1140. default:
  1141. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1142. break;
  1143. }
  1144. if ($x == 0) {
  1145. return ($str);
  1146. }
  1147. $maxlen = 75 - 7 - strlen($this->CharSet);
  1148. /* Try to select the encoding which should produce the shortest output */
  1149. if (strlen($str)/3 < $x) {
  1150. $encoding = 'B';
  1151. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1152. // Use a custom function which correctly encodes and wraps long
  1153. // multibyte strings without breaking lines within a character
  1154. $encoded = $this->Base64EncodeWrapMB($str);
  1155. } else {
  1156. $encoded = base64_encode($str);
  1157. $maxlen -= $maxlen % 4;
  1158. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1159. }
  1160. } else {
  1161. $encoding = 'Q';
  1162. $encoded = $this->EncodeQ($str, $position);
  1163. $encoded = $this->WrapText($encoded, $maxlen, true);
  1164. $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
  1165. }
  1166. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1167. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1168. return $encoded;
  1169. }
  1170. /**
  1171. * Checks if a string contains multibyte characters.
  1172. * @access private
  1173. * @param string $str multi-byte text to wrap encode
  1174. * @return bool
  1175. */
  1176. function HasMultiBytes($str) {
  1177. if (function_exists('mb_strlen')) {
  1178. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1179. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1180. return False;
  1181. }
  1182. }
  1183. /**
  1184. * Correctly encodes and wraps long multibyte strings for mail headers
  1185. * without breaking lines within a character.
  1186. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1187. * @access private
  1188. * @param string $str multi-byte text to wrap encode
  1189. * @return string
  1190. */
  1191. function Base64EncodeWrapMB($str) {
  1192. $start = "=?".$this->CharSet."?B?";
  1193. $end = "?=";
  1194. $encoded = "";
  1195. $mb_length = mb_strlen($str, $this->CharSet);
  1196. // Each line must have length <= 75, including $start and $end
  1197. $length = 75 - strlen($start) - strlen($end);
  1198. // Average multi-byte ratio
  1199. $ratio = $mb_length / strlen($str);
  1200. // Base64 has a 4:3 ratio
  1201. $offset = $avgLength = floor($length * $ratio * .75);
  1202. for ($i = 0; $i < $mb_length; $i += $offset) {
  1203. $lookBack = 0;
  1204. do {
  1205. $offset = $avgLength - $lookBack;
  1206. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1207. $chunk = base64_encode($chunk);
  1208. $lookBack++;
  1209. }
  1210. while (strlen($chunk) > $length);
  1211. $encoded .= $chunk . $this->LE;
  1212. }
  1213. // Chomp the last linefeed
  1214. $encoded = substr($encoded, 0, -strlen($this->LE));
  1215. return $encoded;
  1216. }
  1217. /**
  1218. * Encode string to quoted-printable.
  1219. * @access private
  1220. * @return string
  1221. */
  1222. function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
  1223. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1224. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1225. $eol = "\r\n";
  1226. $escape = '=';
  1227. $output = '';
  1228. while( list(, $line) = each($lines) ) {
  1229. $linlen = strlen($line);
  1230. $newline = '';
  1231. for($i = 0; $i < $linlen; $i++) {
  1232. $c = substr( $line, $i, 1 );
  1233. $dec = ord( $c );
  1234. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1235. $c = '=2E';
  1236. }
  1237. if ( $dec == 32 ) {
  1238. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1239. $c = '=20';
  1240. } else if ( $space_conv ) {
  1241. $c = '=20';
  1242. }
  1243. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1244. $h2 = floor($dec/16);
  1245. $h1 = floor($dec%16);
  1246. $c = $escape.$hex[$h2].$hex[$h1];
  1247. }
  1248. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1249. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1250. $newline = '';
  1251. // check if newline first character will be point or not
  1252. if ( $dec == 46 ) {
  1253. $c = '=2E';
  1254. }
  1255. }
  1256. $newline .= $c;
  1257. } // end of for
  1258. $output .= $newline.$eol;
  1259. } // end of while
  1260. return $output;
  1261. }
  1262. /**
  1263. * Encode string to q encoding.
  1264. * @access private
  1265. * @return string
  1266. */
  1267. function EncodeQ ($str, $position = 'text') {
  1268. /* There should not be any EOL in the string */
  1269. $encoded = preg_replace("[\r\n]", '', $str);
  1270. switch (strtolower($position)) {
  1271. case 'phrase':
  1272. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1273. break;
  1274. case 'comment':
  1275. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1276. case 'text':
  1277. default:
  1278. /* Replace every high ascii, control =, ? and _ characters */
  1279. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1280. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1281. break;
  1282. }
  1283. /* Replace every spaces to _ (more readable than =20) */
  1284. $encoded = str_replace(' ', '_', $encoded);
  1285. return $encoded;
  1286. }
  1287. /**
  1288. * Adds a string or binary attachment (non-filesystem) to the list.
  1289. * This method can be used to attach ascii or binary data,
  1290. * such as a BLOB record from a database.
  1291. * @param string $string String attachment data.
  1292. * @param string $filename Name of the attachment.
  1293. * @param string $encoding File encoding (see $Encoding).
  1294. * @param string $type File extension (MIME) type.
  1295. * @return void
  1296. */
  1297. function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1298. /* Append to $attachment array */
  1299. $cur = count($this->attachment);
  1300. $this->attachment[$cur][0] = $string;
  1301. $this->attachment[$cur][1] = $filename;
  1302. $this->attachment[$cur][2] = $filename;
  1303. $this->attachment[$cur][3] = $encoding;
  1304. $this->attachment[$cur][4] = $type;
  1305. $this->attachment[$cur][5] = true; // isString
  1306. $this->attachment[$cur][6] = 'attachment';
  1307. $this->attachment[$cur][7] = 0;
  1308. }
  1309. /**
  1310. * Adds an embedded attachment. This can include images, sounds, and
  1311. * just about any other document. Make sure to set the $type to an
  1312. * image type. For JPEG images use "image/jpeg" and for GIF images
  1313. * use "image/gif".
  1314. * @param string $path Path to the attachment.
  1315. * @param string $cid Content ID of the attachment. Use this to identify
  1316. * the Id for accessing the image in an HTML form.
  1317. * @param string $name Overrides the attachment name.
  1318. * @param string $encoding File encoding (see $Encoding).
  1319. * @param string $type File extension (MIME) type.
  1320. * @return bool
  1321. */
  1322. function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1323. if(!@is_file($path)) {
  1324. $this->SetError($this->Lang('file_access') . $path);
  1325. return false;
  1326. }
  1327. $filename = basename($path);
  1328. if($name == '') {
  1329. $name = $filename;
  1330. }
  1331. /* Append to $attachment array */
  1332. $cur = count($this->attachment);
  1333. $this->attachment[$cur][0] = $path;
  1334. $this->attachment[$cur][1] = $filename;
  1335. $this->attachment[$cur][2] = $name;
  1336. $this->attachment[$cur][3] = $encoding;
  1337. $this->attachment[$cur][4] = $type;
  1338. $this->attachment[$cur][5] = false;
  1339. $this->attachment[$cur][6] = 'inline';
  1340. $this->attachment[$cur][7] = $cid;
  1341. return true;
  1342. }
  1343. /**
  1344. * Returns true if an inline attachment is present.
  1345. * @access private
  1346. * @return bool
  1347. */
  1348. function InlineImageExists() {
  1349. $result = false;
  1350. for($i = 0; $i < count($this->attachment); $i++) {
  1351. if($this->attachment[$i][6] == 'inline') {
  1352. $result = true;
  1353. break;
  1354. }
  1355. }
  1356. return $result;
  1357. }
  1358. /////////////////////////////////////////////////
  1359. // CLASS METHODS, MESSAGE RESET
  1360. /////////////////////////////////////////////////
  1361. /**
  1362. * Clears all recipients assigned in the TO array. Returns void.
  1363. * @return void
  1364. */
  1365. function ClearAddresses() {
  1366. $this->to = array();
  1367. }
  1368. /**
  1369. * Clears all recipients assigned in the CC array. Returns void.
  1370. * @return void
  1371. */
  1372. function ClearCCs() {
  1373. $this->cc = array();
  1374. }
  1375. /**
  1376. * Clears all recipients assigned in the BCC array. Returns void.
  1377. * @return void
  1378. */
  1379. function ClearBCCs() {
  1380. $this->bcc = array();
  1381. }
  1382. /**
  1383. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1384. * @return void
  1385. */
  1386. function ClearReplyTos() {
  1387. $this->ReplyTo = array();
  1388. }
  1389. /**
  1390. * Clears all recipients assigned in the TO, CC and BCC
  1391. * array. Returns void.
  1392. * @return void
  1393. */
  1394. function ClearAllRecipients() {
  1395. $this->to = array();
  1396. $this->cc = array();
  1397. $this->bcc = array();
  1398. }
  1399. /**
  1400. * Clears all previously set filesystem, string, and binary
  1401. * attachments. Returns void.
  1402. * @return void
  1403. */
  1404. function ClearAttachments() {
  1405. $this->attachment = array();
  1406. }
  1407. /**
  1408. * Clears all custom headers. Returns void.
  1409. * @return void
  1410. */
  1411. function ClearCustomHeaders() {
  1412. $this->CustomHeader = array();
  1413. }
  1414. /////////////////////////////////////////////////
  1415. // CLASS METHODS, MISCELLANEOUS
  1416. /////////////////////////////////////////////////
  1417. /**
  1418. * Adds the error message to the error container.
  1419. * Returns void.
  1420. * @access private
  1421. * @return void
  1422. */
  1423. function SetError($msg) {
  1424. $this->error_count++;
  1425. $this->ErrorInfo = $msg;
  1426. }
  1427. /**
  1428. * Returns the proper RFC 822 formatted date.
  1429. * @access private
  1430. * @return string
  1431. */
  1432. function RFCDate() {
  1433. $tz = date('Z');
  1434. $tzs = ($tz < 0) ? '-' : '+';
  1435. $tz = abs($tz);
  1436. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  1437. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  1438. return $result;
  1439. }
  1440. /**
  1441. * Returns the appropriate server variable. Should work with both
  1442. * PHP 4.1.0+ as well as older versions. Returns an empty string
  1443. * if nothing is found.
  1444. * @access private
  1445. * @return mixed
  1446. */
  1447. function ServerVar($varName) {
  1448. global $HTTP_SERVER_VARS;
  1449. global $HTTP_ENV_VARS;
  1450. if(!isset($_SERVER)) {
  1451. $_SERVER = $HTTP_SERVER_VARS;
  1452. if(!isset($_SERVER['REMOTE_ADDR'])) {
  1453. $_SERVER = $HTTP_ENV_VARS; // must be Apache
  1454. }
  1455. }
  1456. if(isset($_SERVER[$varName])) {
  1457. return $_SERVER[$varName];
  1458. } else {
  1459. return '';
  1460. }
  1461. }
  1462. /**
  1463. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1464. * @access private
  1465. * @return string
  1466. */
  1467. function ServerHostname() {
  1468. if ($this->Hostname != '') {
  1469. $result = $this->Hostname;
  1470. } elseif ($this->ServerVar('SERVER_NAME') != '') {
  1471. $result = $this->ServerVar('SERVER_NAME');
  1472. } else {
  1473. $result = 'localhost.localdomain';
  1474. }
  1475. return $result;
  1476. }
  1477. /**
  1478. * Returns a message in the appropriate language.
  1479. * @access private
  1480. * @return string
  1481. */
  1482. function Lang($key) {
  1483. if(count($this->language) < 1) {
  1484. $this->SetLanguage('en'); // set the default language
  1485. }
  1486. if(isset($this->language[$key])) {
  1487. return $this->language[$key];

Large files files are truncated, but you can click here to view the full file