PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phplist/admin/phpmailer/class.phpmailer.php

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