PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/plugins/PostToQzone/phpmailer.php

http://typecho.googlecode.com/
PHP | 1896 lines | 1289 code | 157 blank | 450 comment | 200 complexity | ff4c8ba59a1c6cfa234a0fe664a85ed2 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.2 |
  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 - 2008 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.2";
  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_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 = split(',', $to);
  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 . '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(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
  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. $this->SetError('Could not load language file');
  573. return false;
  574. }
  575. $this->language = $PHPMAILER_LANG;
  576. return true;
  577. }
  578. /////////////////////////////////////////////////
  579. // METHODS, MESSAGE CREATION
  580. /////////////////////////////////////////////////
  581. /**
  582. * Creates recipient headers.
  583. * @access private
  584. * @return string
  585. */
  586. function AddrAppend($type, $addr) {
  587. $addr_str = $type . ': ';
  588. $addr_str .= $this->AddrFormat($addr[0]);
  589. if(count($addr) > 1) {
  590. for($i = 1; $i < count($addr); $i++) {
  591. $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
  592. }
  593. }
  594. $addr_str .= $this->LE;
  595. return $addr_str;
  596. }
  597. /**
  598. * Formats an address correctly.
  599. * @access private
  600. * @return string
  601. */
  602. function AddrFormat($addr) {
  603. if(empty($addr[1])) {
  604. $formatted = $this->SecureHeader($addr[0]);
  605. } else {
  606. $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  607. }
  608. return $formatted;
  609. }
  610. /**
  611. * Wraps message for use with mailers that do not
  612. * automatically perform wrapping and for quoted-printable.
  613. * Original written by philippe.
  614. * @access private
  615. * @return string
  616. */
  617. function WrapText($message, $length, $qp_mode = false) {
  618. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  619. // If utf-8 encoding is used, we will need to make sure we don't
  620. // split multibyte characters when we wrap
  621. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  622. $message = $this->FixEOL($message);
  623. if (substr($message, -1) == $this->LE) {
  624. $message = substr($message, 0, -1);
  625. }
  626. $line = explode($this->LE, $message);
  627. $message = '';
  628. for ($i=0 ;$i < count($line); $i++) {
  629. $line_part = explode(' ', $line[$i]);
  630. $buf = '';
  631. for ($e = 0; $e<count($line_part); $e++) {
  632. $word = $line_part[$e];
  633. if ($qp_mode and (strlen($word) > $length)) {
  634. $space_left = $length - strlen($buf) - 1;
  635. if ($e != 0) {
  636. if ($space_left > 20) {
  637. $len = $space_left;
  638. if ($is_utf8) {
  639. $len = $this->UTF8CharBoundary($word, $len);
  640. } elseif (substr($word, $len - 1, 1) == "=") {
  641. $len--;
  642. } elseif (substr($word, $len - 2, 1) == "=") {
  643. $len -= 2;
  644. }
  645. $part = substr($word, 0, $len);
  646. $word = substr($word, $len);
  647. $buf .= ' ' . $part;
  648. $message .= $buf . sprintf("=%s", $this->LE);
  649. } else {
  650. $message .= $buf . $soft_break;
  651. }
  652. $buf = '';
  653. }
  654. while (strlen($word) > 0) {
  655. $len = $length;
  656. if ($is_utf8) {
  657. $len = $this->UTF8CharBoundary($word, $len);
  658. } elseif (substr($word, $len - 1, 1) == "=") {
  659. $len--;
  660. } elseif (substr($word, $len - 2, 1) == "=") {
  661. $len -= 2;
  662. }
  663. $part = substr($word, 0, $len);
  664. $word = substr($word, $len);
  665. if (strlen($word) > 0) {
  666. $message .= $part . sprintf("=%s", $this->LE);
  667. } else {
  668. $buf = $part;
  669. }
  670. }
  671. } else {
  672. $buf_o = $buf;
  673. $buf .= ($e == 0) ? $word : (' ' . $word);
  674. if (strlen($buf) > $length and $buf_o != '') {
  675. $message .= $buf_o . $soft_break;
  676. $buf = $word;
  677. }
  678. }
  679. }
  680. $message .= $buf . $this->LE;
  681. }
  682. return $message;
  683. }
  684. /**
  685. * Finds last character boundary prior to maxLength in a utf-8
  686. * quoted (printable) encoded string.
  687. * Original written by Colin Brown.
  688. * @access private
  689. * @param string $encodedText utf-8 QP text
  690. * @param int $maxLength find last character boundary prior to this length
  691. * @return int
  692. */
  693. function UTF8CharBoundary($encodedText, $maxLength) {
  694. $foundSplitPos = false;
  695. $lookBack = 3;
  696. while (!$foundSplitPos) {
  697. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  698. $encodedCharPos = strpos($lastChunk, "=");
  699. if ($encodedCharPos !== false) {
  700. // Found start of encoded character byte within $lookBack block.
  701. // Check the encoded byte value (the 2 chars after the '=')
  702. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  703. $dec = hexdec($hex);
  704. if ($dec < 128) { // Single byte character.
  705. // If the encoded char was found at pos 0, it will fit
  706. // otherwise reduce maxLength to start of the encoded char
  707. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  708. $maxLength - ($lookBack - $encodedCharPos);
  709. $foundSplitPos = true;
  710. } elseif ($dec >= 192) { // First byte of a multi byte character
  711. // Reduce maxLength to split at start of character
  712. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  713. $foundSplitPos = true;
  714. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  715. $lookBack += 3;
  716. }
  717. } else {
  718. // No encoded character found
  719. $foundSplitPos = true;
  720. }
  721. }
  722. return $maxLength;
  723. }
  724. /**
  725. * Set the body wrapping.
  726. * @access private
  727. * @return void
  728. */
  729. function SetWordWrap() {
  730. if($this->WordWrap < 1) {
  731. return;
  732. }
  733. switch($this->message_type) {
  734. case 'alt':
  735. /* fall through */
  736. case 'alt_attachments':
  737. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  738. break;
  739. default:
  740. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  741. break;
  742. }
  743. }
  744. /**
  745. * Assembles message header.
  746. * @access private
  747. * @return string
  748. */
  749. function CreateHeader() {
  750. $result = '';
  751. /* Set the boundaries */
  752. $uniq_id = md5(uniqid(time()));
  753. $this->boundary[1] = 'b1_' . $uniq_id;
  754. $this->boundary[2] = 'b2_' . $uniq_id;
  755. $result .= $this->HeaderLine('Date', $this->RFCDate());
  756. if($this->Sender == '') {
  757. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  758. } else {
  759. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  760. }
  761. /* To be created automatically by mail() */
  762. if($this->Mailer != 'mail') {
  763. if(count($this->to) > 0) {
  764. $result .= $this->AddrAppend('To', $this->to);
  765. } elseif (count($this->cc) == 0) {
  766. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  767. }
  768. if(count($this->cc) > 0) {
  769. $result .= $this->AddrAppend('Cc', $this->cc);
  770. }
  771. }
  772. $from = array();
  773. $from[0][0] = trim($this->From);
  774. $from[0][1] = $this->FromName;
  775. $result .= $this->AddrAppend('From', $from);
  776. /* sendmail and mail() extract Cc from the header before sending */
  777. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
  778. $result .= $this->AddrAppend('Cc', $this->cc);
  779. }
  780. /* sendmail and mail() extract Bcc from the header before sending */
  781. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  782. $result .= $this->AddrAppend('Bcc', $this->bcc);
  783. }
  784. if(count($this->ReplyTo) > 0) {
  785. $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
  786. }
  787. /* mail() sets the subject itself */
  788. if($this->Mailer != 'mail') {
  789. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  790. }
  791. if($this->MessageID != '') {
  792. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  793. } else {
  794. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  795. }
  796. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  797. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');
  798. if($this->ConfirmReadingTo != '') {
  799. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  800. }
  801. // Add custom headers
  802. for($index = 0; $index < count($this->CustomHeader); $index++) {
  803. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  804. }
  805. if (!$this->sign_key_file) {
  806. $result .= $this->HeaderLine('MIME-Version', '1.0');
  807. $result .= $this->GetMailMIME();
  808. }
  809. return $result;
  810. }
  811. /**
  812. * Returns the message MIME.
  813. * @access private
  814. * @return string
  815. */
  816. function GetMailMIME() {
  817. $result = '';
  818. switch($this->message_type) {
  819. case 'plain':
  820. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  821. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
  822. break;
  823. case 'attachments':
  824. /* fall through */
  825. case 'alt_attachments':
  826. if($this->InlineImageExists()){
  827. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
  828. } else {
  829. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  830. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  831. }
  832. break;
  833. case 'alt':
  834. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  835. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  836. break;
  837. }
  838. if($this->Mailer != 'mail') {
  839. $result .= $this->LE.$this->LE;
  840. }
  841. return $result;
  842. }
  843. /**
  844. * Assembles the message body. Returns an empty string on failure.
  845. * @access private
  846. * @return string
  847. */
  848. function CreateBody() {
  849. $result = '';
  850. if ($this->sign_key_file) {
  851. $result .= $this->GetMailMIME();
  852. }
  853. $this->SetWordWrap();
  854. switch($this->message_type) {
  855. case 'alt':
  856. $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  857. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  858. $result .= $this->LE.$this->LE;
  859. $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  860. $result .= $this->EncodeString($this->Body, $this->Encoding);
  861. $result .= $this->LE.$this->LE;
  862. $result .= $this->EndBoundary($this->boundary[1]);
  863. break;
  864. case 'plain':
  865. $result .= $this->EncodeString($this->Body, $this->Encoding);
  866. break;
  867. case 'attachments':
  868. $result .= $this->GetBoundary($this->boundary[1], '', '', '');
  869. $result .= $this->EncodeString($this->Body, $this->Encoding);
  870. $result .= $this->LE;
  871. $result .= $this->AttachAll();
  872. break;
  873. case 'alt_attachments':
  874. $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  875. $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
  876. $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
  877. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  878. $result .= $this->LE.$this->LE;
  879. $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
  880. $result .= $this->EncodeString($this->Body, $this->Encoding);
  881. $result .= $this->LE.$this->LE;
  882. $result .= $this->EndBoundary($this->boundary[2]);
  883. $result .= $this->AttachAll();
  884. break;
  885. }
  886. if($this->IsError()) {
  887. $result = '';
  888. } else if ($this->sign_key_file) {
  889. $file = tempnam("", "mail");
  890. $fp = fopen($file, "w");
  891. fwrite($fp, $result);
  892. fclose($fp);
  893. $signed = tempnam("", "signed");
  894. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_key_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
  895. $fp = fopen($signed, "r");
  896. $result = fread($fp, filesize($this->sign_key_file));
  897. fclose($fp);
  898. } else {
  899. $this->SetError($this->Lang("signing").openssl_error_string());
  900. $result = '';
  901. }
  902. unlink($file);
  903. unlink($signed);
  904. }
  905. return $result;
  906. }
  907. /**
  908. * Returns the start of a message boundary.
  909. * @access private
  910. */
  911. function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  912. $result = '';
  913. if($charSet == '') {
  914. $charSet = $this->CharSet;
  915. }
  916. if($contentType == '') {
  917. $contentType = $this->ContentType;
  918. }
  919. if($encoding == '') {
  920. $encoding = $this->Encoding;
  921. }
  922. $result .= $this->TextLine('--' . $boundary);
  923. $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
  924. $result .= $this->LE;
  925. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  926. $result .= $this->LE;
  927. return $result;
  928. }
  929. /**
  930. * Returns the end of a message boundary.
  931. * @access private
  932. */
  933. function EndBoundary($boundary) {
  934. return $this->LE . '--' . $boundary . '--' . $this->LE;
  935. }
  936. /**
  937. * Sets the message type.
  938. * @access private
  939. * @return void
  940. */
  941. function SetMessageType() {
  942. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
  943. $this->message_type = 'plain';
  944. } else {
  945. if(count($this->attachment) > 0) {
  946. $this->message_type = 'attachments';
  947. }
  948. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
  949. $this->message_type = 'alt';
  950. }
  951. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
  952. $this->message_type = 'alt_attachments';
  953. }
  954. }
  955. }
  956. /* Returns a formatted header line.
  957. * @access private
  958. * @return string
  959. */
  960. function HeaderLine($name, $value) {
  961. return $name . ': ' . $value . $this->LE;
  962. }
  963. /**
  964. * Returns a formatted mail line.
  965. * @access private
  966. * @return string
  967. */
  968. function TextLine($value) {
  969. return $value . $this->LE;
  970. }
  971. /////////////////////////////////////////////////
  972. // CLASS METHODS, ATTACHMENTS
  973. /////////////////////////////////////////////////
  974. /**
  975. * Adds an attachment from a path on the filesystem.
  976. * Returns false if the file could not be found
  977. * or accessed.
  978. * @param string $path Path to the attachment.
  979. * @param string $name Overrides the attachment name.
  980. * @param string $encoding File encoding (see $Encoding).
  981. * @param string $type File extension (MIME) type.
  982. * @return bool
  983. */
  984. function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  985. if(!@is_file($path)) {
  986. $this->SetError($this->Lang('file_access') . $path);
  987. return false;
  988. }
  989. $filename = basename($path);
  990. if($name == '') {
  991. $name = $filename;
  992. }
  993. $cur = count($this->attachment);
  994. $this->attachment[$cur][0] = $path;
  995. $this->attachment[$cur][1] = $filename;
  996. $this->attachment[$cur][2] = $name;
  997. $this->attachment[$cur][3] = $encoding;
  998. $this->attachment[$cur][4] = $type;
  999. $this->attachment[$cur][5] = false; // isStringAttachment
  1000. $this->attachment[$cur][6] = 'attachment';
  1001. $this->attachment[$cur][7] = 0;
  1002. return true;
  1003. }
  1004. /**
  1005. * Attaches all fs, string, and binary attachments to the message.
  1006. * Returns an empty string on failure.
  1007. * @access private
  1008. * @return string
  1009. */
  1010. function AttachAll() {
  1011. /* Return text of body */
  1012. $mime = array();
  1013. /* Add all attachments */
  1014. for($i = 0; $i < count($this->attachment); $i++) {
  1015. /* Check for string attachment */
  1016. $bString = $this->attachment[$i][5];
  1017. if ($bString) {
  1018. $string = $this->attachment[$i][0];
  1019. } else {
  1020. $path = $this->attachment[$i][0];
  1021. }
  1022. $filename = $this->attachment[$i][1];
  1023. $name = $this->attachment[$i][2];
  1024. $encoding = $this->attachment[$i][3];
  1025. $type = $this->attachment[$i][4];
  1026. $disposition = $this->attachment[$i][6];
  1027. $cid = $this->attachment[$i][7];
  1028. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  1029. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
  1030. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1031. if($disposition == 'inline') {
  1032. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1033. }
  1034. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
  1035. /* Encode as string attachment */
  1036. if($bString) {
  1037. $mime[] = $this->EncodeString($string, $encoding);
  1038. if($this->IsError()) {
  1039. return '';
  1040. }
  1041. $mime[] = $this->LE.$this->LE;
  1042. } else {
  1043. $mime[] = $this->EncodeFile($path, $encoding);
  1044. if($this->IsError()) {
  1045. return '';
  1046. }
  1047. $mime[] = $this->LE.$this->LE;
  1048. }
  1049. }
  1050. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  1051. return join('', $mime);
  1052. }
  1053. /**
  1054. * Encodes attachment in requested format. Returns an
  1055. * empty string on failure.
  1056. * @access private
  1057. * @return string
  1058. */
  1059. function EncodeFile ($path, $encoding = 'base64') {
  1060. if(!@$fd = fopen($path, 'rb')) {
  1061. $this->SetError($this->Lang('file_open') . $path);
  1062. return '';
  1063. }
  1064. $magic_quotes = get_magic_quotes_runtime();
  1065. set_magic_quotes_runtime(0);
  1066. $file_buffer = fread($fd, filesize($path));
  1067. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1068. fclose($fd);
  1069. set_magic_quotes_runtime($magic_quotes);
  1070. return $file_buffer;
  1071. }
  1072. /**
  1073. * Encodes string to requested format. Returns an
  1074. * empty string on failure.
  1075. * @access private
  1076. * @return string
  1077. */
  1078. function EncodeString ($str, $encoding = 'base64') {
  1079. $encoded = '';
  1080. switch(strtolower($encoding)) {
  1081. case 'base64':
  1082. /* chunk_split is found in PHP >= 3.0.6 */
  1083. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1084. break;
  1085. case '7bit':
  1086. case '8bit':
  1087. $encoded = $this->FixEOL($str);
  1088. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1089. $encoded .= $this->LE;
  1090. break;
  1091. case 'binary':
  1092. $encoded = $str;
  1093. break;
  1094. case 'quoted-printable':
  1095. $encoded = $this->EncodeQP($str);
  1096. break;
  1097. default:
  1098. $this->SetError($this->Lang('encoding') . $encoding);
  1099. break;
  1100. }
  1101. return $encoded;
  1102. }
  1103. /**
  1104. * Encode a header string to best of Q, B, quoted or none.
  1105. * @access private
  1106. * @return string
  1107. */
  1108. function EncodeHeader ($str, $position = 'text') {
  1109. $x = 0;
  1110. switch (strtolower($position)) {
  1111. case 'phrase':
  1112. if (!preg_match('/[\200-\377]/', $str)) {
  1113. /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
  1114. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1115. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1116. return ($encoded);
  1117. } else {
  1118. return ("\"$encoded\"");
  1119. }
  1120. }
  1121. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1122. break;
  1123. case 'comment':
  1124. $x = preg_match_all('/[()"]/', $str, $matches);
  1125. /* Fall-through */
  1126. case 'text':
  1127. default:
  1128. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1129. break;
  1130. }
  1131. if ($x == 0) {
  1132. return ($str);
  1133. }
  1134. $maxlen = 75 - 7 - strlen($this->CharSet);
  1135. /* Try to select the encoding which should produce the shortest output */
  1136. if (strlen($str)/3 < $x) {
  1137. $encoding = 'B';
  1138. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1139. // Use a custom function which correctly encodes and wraps long
  1140. // multibyte strings without breaking lines within a character
  1141. $encoded = $this->Base64EncodeWrapMB($str);
  1142. } else {
  1143. $encoded = base64_encode($str);
  1144. $maxlen -= $maxlen % 4;
  1145. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1146. }
  1147. } else {
  1148. $encoding = 'Q';
  1149. $encoded = $this->EncodeQ($str, $position);
  1150. $encoded = $this->WrapText($encoded, $maxlen, true);
  1151. $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
  1152. }
  1153. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1154. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1155. return $encoded;
  1156. }
  1157. /**
  1158. * Checks if a string contains multibyte characters.
  1159. * @access private
  1160. * @param string $str multi-byte text to wrap encode
  1161. * @return bool
  1162. */
  1163. function HasMultiBytes($str) {
  1164. if (function_exists('mb_strlen')) {
  1165. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1166. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1167. return False;
  1168. }
  1169. }
  1170. /**
  1171. * Correctly encodes and wraps long multibyte strings for mail headers
  1172. * without breaking lines within a character.
  1173. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1174. * @access private
  1175. * @param string $str multi-byte text to wrap encode
  1176. * @return string
  1177. */
  1178. function Base64EncodeWrapMB($str) {
  1179. $start = "=?".$this->CharSet."?B?";
  1180. $end = "?=";
  1181. $encoded = "";
  1182. $mb_length = mb_strlen($str, $this->CharSet);
  1183. // Each line must have length <= 75, including $start and $end
  1184. $length = 75 - strlen($start) - strlen($end);
  1185. // Average multi-byte ratio
  1186. $ratio = $mb_length / strlen($str);
  1187. // Base64 has a 4:3 ratio
  1188. $offset = $avgLength = floor($length * $ratio * .75);
  1189. for ($i = 0; $i < $mb_length; $i += $offset) {
  1190. $lookBack = 0;
  1191. do {
  1192. $offset = $avgLength - $lookBack;
  1193. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1194. $chunk = base64_encode($chunk);
  1195. $lookBack++;
  1196. }
  1197. while (strlen($chunk) > $length);
  1198. $encoded .= $chunk . $this->LE;
  1199. }
  1200. // Chomp the last linefeed
  1201. $encoded = substr($encoded, 0, -strlen($this->LE));
  1202. return $encoded;
  1203. }
  1204. /**
  1205. * Encode string to quoted-printable.
  1206. * @access private
  1207. * @return string
  1208. */
  1209. function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
  1210. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1211. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1212. $eol = "\r\n";
  1213. $escape = '=';
  1214. $output = '';
  1215. while( list(, $line) = each($lines) ) {
  1216. $linlen = strlen($line);
  1217. $newline = '';
  1218. for($i = 0; $i < $linlen; $i++) {
  1219. $c = substr( $line, $i, 1 );
  1220. $dec = ord( $c );
  1221. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1222. $c = '=2E';
  1223. }
  1224. if ( $dec == 32 ) {
  1225. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1226. $c = '=20';
  1227. } else if ( $space_conv ) {
  1228. $c = '=20';
  1229. }
  1230. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1231. $h2 = floor($dec/16);
  1232. $h1 = floor($dec%16);
  1233. $c = $escape.$hex[$h2].$hex[$h1];
  1234. }
  1235. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1236. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1237. $newline = '';
  1238. // check if newline first character will be point or not
  1239. if ( $dec == 46 ) {
  1240. $c = '=2E';
  1241. }
  1242. }
  1243. $newline .= $c;
  1244. } // end of for
  1245. $output .= $newline.$eol;
  1246. } // end of while
  1247. return trim($output);
  1248. }
  1249. /**
  1250. * Encode string to q encoding.
  1251. * @access private
  1252. * @return string
  1253. */
  1254. function EncodeQ ($str, $position = 'text') {
  1255. /* There should not be any EOL in the string */
  1256. $encoded = preg_replace("[\r\n]", '', $str);
  1257. switch (strtolower($position)) {
  1258. case 'phrase':
  1259. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1260. break;
  1261. case 'comment':
  1262. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1263. case 'text':
  1264. default:
  1265. /* Replace every high ascii, control =, ? and _ characters */
  1266. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1267. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1268. break;
  1269. }
  1270. /* Replace every spaces to _ (more readable than =20) */
  1271. $encoded = str_replace(' ', '_', $encoded);
  1272. return $encoded;
  1273. }
  1274. /**
  1275. * Adds a string or binary attachment (non-filesystem) to the list.
  1276. * This method can be used to attach ascii or binary data,
  1277. * such as a BLOB record from a database.
  1278. * @param string $string String attachment data.
  1279. * @param string $filename Name of the attachment.
  1280. * @param string $encoding File encoding (see $Encoding).
  1281. * @param string $type File extension (MIME) type.
  1282. * @return void
  1283. */
  1284. function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1285. /* Append to $attachment array */
  1286. $cur = count($this->attachment);
  1287. $this->attachment[$cur][0] = $string;
  1288. $this->attachment[$cur][1] = $filename;
  1289. $this->attachment[$cur][2] = $filename;
  1290. $this->attachment[$cur][3] = $encoding;
  1291. $this->attachment[$cur][4] = $type;
  1292. $this->attachment[$cur][5] = true; // isString
  1293. $this->attachment[$cur][6] = 'attachment';
  1294. $this->attachment[$cur][7] = 0;
  1295. }
  1296. /**
  1297. * Adds an embedded attachment. This can include images, sounds, and
  1298. * just about any other document. Make sure to set the $type to an
  1299. * image type. For JPEG images use "image/jpeg" and for GIF images
  1300. * use "image/gif".
  1301. * @param string $path Path to the attachment.
  1302. * @param string $cid Content ID of the attachment. Use this to identify
  1303. * the Id for accessing the image in an HTML form.
  1304. * @param string $name Overrides the attachment name.
  1305. * @param string $encoding File encoding (see $Encoding).
  1306. * @param string $type File extension (MIME) type.
  1307. * @return bool
  1308. */
  1309. function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1310. if(!@is_file($path)) {
  1311. $this->SetError($this->Lang('file_access') . $path);
  1312. return false;
  1313. }
  1314. $filename = basename($path);
  1315. if($name == '') {
  1316. $name = $filename;
  1317. }
  1318. /* Append to $attachment array */
  1319. $cur = count($this->attachment);
  1320. $this->attachment[$cur][0] = $path;
  1321. $this->attachment[$cur][1] = $filename;
  1322. $this->attachment[$cur][2] = $name;
  1323. $this->attachment[$cur][3] = $encoding;
  1324. $this->attachment[$cur][4] = $type;
  1325. $this->attachment[$cur][5] = false;
  1326. $this->attachment[$cur][6] = 'inline';
  1327. $this->attachment[$cur][7] = $cid;
  1328. return true;
  1329. }
  1330. /**
  1331. * Returns true if an inline attachment is present.
  1332. * @access private
  1333. * @return bool
  1334. */
  1335. function InlineImageExists() {
  1336. $result = false;
  1337. for($i = 0; $i < count($this->attachment); $i++) {
  1338. if($this->attachment[$i][6] == 'inline') {
  1339. $result = true;
  1340. break;
  1341. }
  1342. }
  1343. return $result;
  1344. }
  1345. /////////////////////////////////////////////////
  1346. // CLASS METHODS, MESSAGE RESET
  1347. /////////////////////////////////////////////////
  1348. /**
  1349. * Clears all recipients assigned in the TO array. Returns void.
  1350. * @return void
  1351. */
  1352. function ClearAddresses() {
  1353. $this->to = array();
  1354. }
  1355. /**
  1356. * Clears all recipients assigned in the CC array. Returns void.
  1357. * @return void
  1358. */
  1359. function ClearCCs() {
  1360. $this->cc = array();
  1361. }
  1362. /**
  1363. * Clears all recipients assigned in the BCC array. Returns void.
  1364. * @return void
  1365. */
  1366. function ClearBCCs() {
  1367. $this->bcc = array();
  1368. }
  1369. /**
  1370. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1371. * @return void
  1372. */
  1373. function ClearReplyTos() {
  1374. $this->ReplyTo = array();
  1375. }
  1376. /**
  1377. * Clears all recipients assigned in the TO, CC and BCC
  1378. * array. Returns void.
  1379. * @return void
  1380. */
  1381. function ClearAllRecipients() {
  1382. $this->to = array();
  1383. $this->cc = array();
  1384. $this->bcc = array();
  1385. }
  1386. /**
  1387. * Clears all previously set filesystem, string, and binary
  1388. * attachments. Returns void.
  1389. * @return void
  1390. */
  1391. function ClearAttachments() {
  1392. $this->attachment = array();
  1393. }
  1394. /**
  1395. * Clears all custom headers. Returns void.
  1396. * @return void
  1397. */
  1398. function ClearCustomHeaders() {
  1399. $this->CustomHeader = array();
  1400. }
  1401. /////////////////////////////////////////////////
  1402. // CLASS METHODS, MISCELLANEOUS
  1403. /////////////////////////////////////////////////
  1404. /**
  1405. * Adds the error message to the error container.
  1406. * Returns void.
  1407. * @access private
  1408. * @return void
  1409. */
  1410. function SetError($msg) {
  1411. $this->error_count++;
  1412. $this->ErrorInfo = $msg;
  1413. }
  1414. /**
  1415. * Returns the proper RFC 822 formatted date.
  1416. * @access private
  1417. * @return string
  1418. */
  1419. function RFCDate() {
  1420. $tz = date('Z');
  1421. $tzs = ($tz < 0) ? '-' : '+';
  1422. $tz = abs($tz);
  1423. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  1424. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  1425. return $result;
  1426. }
  1427. /**
  1428. * Returns the appropriate server variable. Should work with both
  1429. * PHP 4.1.0+ as well as older versions. Returns an empty string
  1430. * if nothing is found.
  1431. * @access private
  1432. * @return mixed
  1433. */
  1434. function ServerVar($varName) {
  1435. global $HTTP_SERVER_VARS;
  1436. global $HTTP_ENV_VARS;
  1437. if(!isset($_SERVER)) {
  1438. $_SERVER = $HTTP_SERVER_VARS;
  1439. if(!isset($_SERVER['REMOTE_ADDR'])) {
  1440. $_SERVER = $HTTP_ENV_VARS; // must be Apache
  1441. }
  1442. }
  1443. if(isset($_SERVER[$varName])) {
  1444. return $_SERVER[$varName];
  1445. } else {
  1446. return '';
  1447. }
  1448. }
  1449. /**
  1450. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1451. * @access private
  1452. * @return string
  1453. */
  1454. function ServerHostname() {
  1455. if ($this->Hostname != '') {
  1456. $result = $this->Hostname;
  1457. } elseif ($this->ServerVar('SERVER_NAME') != '') {
  1458. $result = $this->ServerVar('SERVER_NAME');
  1459. } else {
  1460. $result = 'localhost.localdomain';
  1461. }
  1462. return $result;
  1463. }
  1464. /**
  1465. * Returns a message in the appropriate language.
  1466. * @access private
  1467. * @return string
  1468. */
  1469. function Lang($key) {
  1470. if(count($this->language) < 1) {
  1471. $this->SetLanguage('en'); // set the default language
  1472. }
  1473. if(isset($this->language[$key])) {
  1474. return $this->language[$key];
  1475. } else {
  1476. return 'Language string failed to load: ' . $key;
  1477. }
  1478. }
  1479. /**
  1480. * Returns true if an error occurred.
  1481. * @return bool
  1482. */
  1483. function IsError() {
  1484. return ($this->error_count > 0);
  1485. }
  1486. /**
  1487. * Changes every end of line from CR or LF to CRLF.
  1488. * @access private
  1489. * @return string
  1490. */
  1491. function FixEOL($str) {
  1492. $str = str_replace("\r\n", "\n", $str);
  1493. $str = str_replace("\r", "\n", $str);
  1494. $str = str_replace("\n", $this->LE, $str);
  1495. return $str;
  1496. }
  1497. /**
  1498. * Adds a custom header.
  1499. * @return void
  1500. */
  1501. function AddCustomHeader($custom_header) {
  1502. $this->CustomHeader[] = explode(':', $custom_header, 2);
  1503. }
  1504. /**
  1505. * Evaluates the message and returns modifications for inline images and backgrounds
  1506. * @access public
  1507. * @return $message
  1508. */
  1509. function MsgHTML($message,$basedir='') {
  1510. preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
  1511. if(isset($images[2])) {
  1512. foreach($images[2] as $i => $url) {
  1513. // do not change urls for absolute images (thanks to corvuscorax)
  1514. if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
  1515. $filename = basename($url);
  1516. $directory = dirname($url);
  1517. ($directory == '.')?$directory='':'';
  1518. $cid = 'cid:' . md5($filename);
  1519. $fileParts = split("\.", $filename);
  1520. $ext = $fileParts[1];
  1521. $mimeType = $this->_mime_types($ext);
  1522. if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
  1523. if ( strlen($directory) > 1 &

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