PageRenderTime 73ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/class-phpmailer.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 1921 lines | 1308 code | 158 blank | 455 comment | 199 complexity | 3e6657c8713841da6c94d4fb1970b016 MD5 | raw file

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

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