PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/pmfv2-1-0-alpha-1/inc/class.phpmailer.php

https://github.com/redbugz/rootstech2013
PHP | 1506 lines | 970 code | 154 blank | 382 comment | 138 complexity | 3bd0f3a65294424a573c36d277e5ac39 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
  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. $connection = true;
  476. $Authconnection = true;
  477. if ($this->Helo != '')
  478. $this->smtp->Hello($this->Helo);
  479. else
  480. $this->smtp->Hello($this->ServerHostname());
  481. if($this->SMTPAuth)
  482. {
  483. if(!$this->smtp->Authenticate($this->Username,
  484. $this->Password))
  485. {
  486. $this->SetError($this->Lang("authenticate"));
  487. $this->smtp->Reset();
  488. $Authconnection = false;
  489. }
  490. }
  491. //$connection = true;
  492. }
  493. $index++;
  494. }
  495. if(!$connection)
  496. $this->SetError($this->Lang("connect_host"));
  497. if(!$Authconnection)
  498. {
  499. $this->SetError($this->Lang("Authentication failed"));
  500. $connection = $Authconnection;
  501. }
  502. return $connection;
  503. }
  504. /**
  505. * Closes the active SMTP session if one exists.
  506. * @return void
  507. */
  508. function SmtpClose() {
  509. if($this->smtp != NULL)
  510. {
  511. if($this->smtp->Connected())
  512. {
  513. $this->smtp->Quit();
  514. $this->smtp->Close();
  515. }
  516. }
  517. }
  518. /**
  519. * Sets the language for all class error messages. Returns false
  520. * if it cannot load the language file. The default language type
  521. * is English.
  522. * @param string $lang_type Type of language (e.g. Portuguese: "br")
  523. * @param string $lang_path Path to the language file directory
  524. * @access public
  525. * @return bool
  526. */
  527. function SetLanguage($lang_type, $lang_path = "language/") {
  528. if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
  529. include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  530. else if(file_exists($lang_path.'phpmailer.lang-en.php'))
  531. include($lang_path.'phpmailer.lang-en.php');
  532. else
  533. {
  534. $this->SetError("Could not load language file");
  535. return false;
  536. }
  537. $this->language = $PHPMAILER_LANG;
  538. return true;
  539. }
  540. /////////////////////////////////////////////////
  541. // MESSAGE CREATION METHODS
  542. /////////////////////////////////////////////////
  543. /**
  544. * Creates recipient headers.
  545. * @access private
  546. * @return string
  547. */
  548. function AddrAppend($type, $addr) {
  549. $addr_str = $type . ": ";
  550. $addr_str .= $this->AddrFormat($addr[0]);
  551. if(count($addr) > 1)
  552. {
  553. for($i = 1; $i < count($addr); $i++)
  554. $addr_str .= ", " . $this->AddrFormat($addr[$i]);
  555. }
  556. $addr_str .= $this->LE;
  557. return $addr_str;
  558. }
  559. /**
  560. * Formats an address correctly.
  561. * @access private
  562. * @return string
  563. */
  564. function AddrFormat($addr) {
  565. if(empty($addr[1]))
  566. $formatted = $addr[0];
  567. else
  568. {
  569. $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
  570. $addr[0] . ">";
  571. }
  572. return $formatted;
  573. }
  574. /**
  575. * Wraps message for use with mailers that do not
  576. * automatically perform wrapping and for quoted-printable.
  577. * Original written by philippe.
  578. * @access private
  579. * @return string
  580. */
  581. function WrapText($message, $length, $qp_mode = false) {
  582. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  583. $message = $this->FixEOL($message);
  584. if (substr($message, -1) == $this->LE)
  585. $message = substr($message, 0, -1);
  586. $line = explode($this->LE, $message);
  587. $message = "";
  588. for ($i=0 ;$i < count($line); $i++)
  589. {
  590. $line_part = explode(" ", $line[$i]);
  591. $buf = "";
  592. for ($e = 0; $e<count($line_part); $e++)
  593. {
  594. $word = $line_part[$e];
  595. if ($qp_mode and (strlen($word) > $length))
  596. {
  597. $space_left = $length - strlen($buf) - 1;
  598. if ($e != 0)
  599. {
  600. if ($space_left > 20)
  601. {
  602. $len = $space_left;
  603. if (substr($word, $len - 1, 1) == "=")
  604. $len--;
  605. elseif (substr($word, $len - 2, 1) == "=")
  606. $len -= 2;
  607. $part = substr($word, 0, $len);
  608. $word = substr($word, $len);
  609. $buf .= " " . $part;
  610. $message .= $buf . sprintf("=%s", $this->LE);
  611. }
  612. else
  613. {
  614. $message .= $buf . $soft_break;
  615. }
  616. $buf = "";
  617. }
  618. while (strlen($word) > 0)
  619. {
  620. $len = $length;
  621. if (substr($word, $len - 1, 1) == "=")
  622. $len--;
  623. elseif (substr($word, $len - 2, 1) == "=")
  624. $len -= 2;
  625. $part = substr($word, 0, $len);
  626. $word = substr($word, $len);
  627. if (strlen($word) > 0)
  628. $message .= $part . sprintf("=%s", $this->LE);
  629. else
  630. $buf = $part;
  631. }
  632. }
  633. else
  634. {
  635. $buf_o = $buf;
  636. $buf .= ($e == 0) ? $word : (" " . $word);
  637. if (strlen($buf) > $length and $buf_o != "")
  638. {
  639. $message .= $buf_o . $soft_break;
  640. $buf = $word;
  641. }
  642. }
  643. }
  644. $message .= $buf . $this->LE;
  645. }
  646. return $message;
  647. }
  648. /**
  649. * Set the body wrapping.
  650. * @access private
  651. * @return void
  652. */
  653. function SetWordWrap() {
  654. if($this->WordWrap < 1)
  655. return;
  656. switch($this->message_type)
  657. {
  658. case "alt":
  659. // fall through
  660. case "alt_attachments":
  661. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  662. break;
  663. default:
  664. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  665. break;
  666. }
  667. }
  668. /**
  669. * Assembles message header.
  670. * @access private
  671. * @return string
  672. */
  673. function CreateHeader() {
  674. $result = "";
  675. // Set the boundaries
  676. $uniq_id = md5(uniqid(time()));
  677. $this->boundary[1] = "b1_" . $uniq_id;
  678. $this->boundary[2] = "b2_" . $uniq_id;
  679. $result .= $this->HeaderLine("Date", $this->RFCDate());
  680. if($this->Sender == "")
  681. $result .= $this->HeaderLine("Return-Path", trim($this->From));
  682. else
  683. $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
  684. // To be created automatically by mail()
  685. if($this->Mailer != "mail")
  686. {
  687. if(count($this->to) > 0)
  688. $result .= $this->AddrAppend("To", $this->to);
  689. else if (count($this->cc) == 0)
  690. $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
  691. if(count($this->cc) > 0)
  692. $result .= $this->AddrAppend("Cc", $this->cc);
  693. }
  694. $from = array();
  695. $from[0][0] = trim($this->From);
  696. $from[0][1] = $this->FromName;
  697. $result .= $this->AddrAppend("From", $from);
  698. // sendmail and mail() extract Bcc from the header before sending
  699. if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
  700. $result .= $this->AddrAppend("Bcc", $this->bcc);
  701. if(count($this->ReplyTo) > 0)
  702. $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
  703. // mail() sets the subject itself
  704. if($this->Mailer != "mail")
  705. $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
  706. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  707. $result .= $this->HeaderLine("X-Priority", $this->Priority);
  708. $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]");
  709. if($this->ConfirmReadingTo != "")
  710. {
  711. $result .= $this->HeaderLine("Disposition-Notification-To",
  712. "<" . trim($this->ConfirmReadingTo) . ">");
  713. }
  714. // Add custom headers
  715. for($index = 0; $index < count($this->CustomHeader); $index++)
  716. {
  717. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
  718. $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  719. }
  720. $result .= $this->HeaderLine("MIME-Version", "1.0");
  721. switch($this->message_type)
  722. {
  723. case "plain":
  724. $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
  725. $result .= sprintf("Content-Type: %s; charset=\"%s\"",
  726. $this->ContentType, $this->CharSet);
  727. break;
  728. case "attachments":
  729. // fall through
  730. case "alt_attachments":
  731. if($this->InlineImageExists())
  732. {
  733. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
  734. "multipart/related", $this->LE, $this->LE,
  735. $this->boundary[1], $this->LE);
  736. }
  737. else
  738. {
  739. $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
  740. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  741. }
  742. break;
  743. case "alt":
  744. $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
  745. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  746. break;
  747. }
  748. if($this->Mailer != "mail")
  749. $result .= $this->LE.$this->LE;
  750. return $result;
  751. }
  752. /**
  753. * Assembles the message body. Returns an empty string on failure.
  754. * @access private
  755. * @return string
  756. */
  757. function CreateBody() {
  758. $result = "";
  759. $this->SetWordWrap();
  760. switch($this->message_type)
  761. {
  762. case "alt":
  763. $result .= $this->GetBoundary($this->boundary[1], "",
  764. "text/plain", "");
  765. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  766. $result .= $this->LE.$this->LE;
  767. $result .= $this->GetBoundary($this->boundary[1], "",
  768. "text/html", "");
  769. $result .= $this->EncodeString($this->Body, $this->Encoding);
  770. $result .= $this->LE.$this->LE;
  771. $result .= $this->EndBoundary($this->boundary[1]);
  772. break;
  773. case "plain":
  774. $result .= $this->EncodeString($this->Body, $this->Encoding);
  775. break;
  776. case "attachments":
  777. $result .= $this->GetBoundary($this->boundary[1], "", "", "");
  778. $result .= $this->EncodeString($this->Body, $this->Encoding);
  779. $result .= $this->LE;
  780. $result .= $this->AttachAll();
  781. break;
  782. case "alt_attachments":
  783. $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  784. $result .= sprintf("Content-Type: %s;%s" .
  785. "\tboundary=\"%s\"%s",
  786. "multipart/alternative", $this->LE,
  787. $this->boundary[2], $this->LE.$this->LE);
  788. // Create text body
  789. $result .= $this->GetBoundary($this->boundary[2], "",
  790. "text/plain", "") . $this->LE;
  791. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  792. $result .= $this->LE.$this->LE;
  793. // Create the HTML body
  794. $result .= $this->GetBoundary($this->boundary[2], "",
  795. "text/html", "") . $this->LE;
  796. $result .= $this->EncodeString($this->Body, $this->Encoding);
  797. $result .= $this->LE.$this->LE;
  798. $result .= $this->EndBoundary($this->boundary[2]);
  799. $result .= $this->AttachAll();
  800. break;
  801. }
  802. if($this->IsError())
  803. $result = "";
  804. return $result;
  805. }
  806. /**
  807. * Returns the start of a message boundary.
  808. * @access private
  809. */
  810. function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  811. $result = "";
  812. if($charSet == "") { $charSet = $this->CharSet; }
  813. if($contentType == "") { $contentType = $this->ContentType; }
  814. if($encoding == "") { $encoding = $this->Encoding; }
  815. $result .= $this->TextLine("--" . $boundary);
  816. $result .= sprintf("Content-Type: %s; charset = \"%s\"",
  817. $contentType, $charSet);
  818. $result .= $this->LE;
  819. $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
  820. $result .= $this->LE;
  821. return $result;
  822. }
  823. /**
  824. * Returns the end of a message boundary.
  825. * @access private
  826. */
  827. function EndBoundary($boundary) {
  828. return $this->LE . "--" . $boundary . "--" . $this->LE;
  829. }
  830. /**
  831. * Sets the message type.
  832. * @access private
  833. * @return void
  834. */
  835. function SetMessageType() {
  836. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
  837. $this->message_type = "plain";
  838. else
  839. {
  840. if(count($this->attachment) > 0)
  841. $this->message_type = "attachments";
  842. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
  843. $this->message_type = "alt";
  844. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
  845. $this->message_type = "alt_attachments";
  846. }
  847. }
  848. /**
  849. * Returns a formatted header line.
  850. * @access private
  851. * @return string
  852. */
  853. function HeaderLine($name, $value) {
  854. return $name . ": " . $value . $this->LE;
  855. }
  856. /**
  857. * Returns a formatted mail line.
  858. * @access private
  859. * @return string
  860. */
  861. function TextLine($value) {
  862. return $value . $this->LE;
  863. }
  864. /////////////////////////////////////////////////
  865. // ATTACHMENT METHODS
  866. /////////////////////////////////////////////////
  867. /**
  868. * Adds an attachment from a path on the filesystem.
  869. * Returns false if the file could not be found
  870. * or accessed.
  871. * @param string $path Path to the attachment.
  872. * @param string $name Overrides the attachment name.
  873. * @param string $encoding File encoding (see $Encoding).
  874. * @param string $type File extension (MIME) type.
  875. * @return bool
  876. */
  877. function AddAttachment($path, $name = "", $encoding = "base64",
  878. $type = "application/octet-stream") {
  879. if(!@is_file($path))
  880. {
  881. $this->SetError($this->Lang("file_access") . $path);
  882. return false;
  883. }
  884. $filename = basename($path);
  885. if($name == "")
  886. $name = $filename;
  887. $cur = count($this->attachment);
  888. $this->attachment[$cur][0] = $path;
  889. $this->attachment[$cur][1] = $filename;
  890. $this->attachment[$cur][2] = $name;
  891. $this->attachment[$cur][3] = $encoding;
  892. $this->attachment[$cur][4] = $type;
  893. $this->attachment[$cur][5] = false; // isStringAttachment
  894. $this->attachment[$cur][6] = "attachment";
  895. $this->attachment[$cur][7] = 0;
  896. return true;
  897. }
  898. /**
  899. * Attaches all fs, string, and binary attachments to the message.
  900. * Returns an empty string on failure.
  901. * @access private
  902. * @return string
  903. */
  904. function AttachAll() {
  905. // Return text of body
  906. $mime = array();
  907. // Add all attachments
  908. for($i = 0; $i < count($this->attachment); $i++)
  909. {
  910. // Check for string attachment
  911. $bString = $this->attachment[$i][5];
  912. if ($bString)
  913. $string = $this->attachment[$i][0];
  914. else
  915. $path = $this->attachment[$i][0];
  916. $filename = $this->attachment[$i][1];
  917. $name = $this->attachment[$i][2];
  918. $encoding = $this->attachment[$i][3];
  919. $type = $this->attachment[$i][4];
  920. $disposition = $this->attachment[$i][6];
  921. $cid = $this->attachment[$i][7];
  922. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  923. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
  924. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  925. if($disposition == "inline")
  926. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  927. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
  928. $disposition, $name, $this->LE.$this->LE);
  929. // Encode as string attachment
  930. if($bString)
  931. {
  932. $mime[] = $this->EncodeString($string, $encoding);
  933. if($this->IsError()) { return ""; }
  934. $mime[] = $this->LE.$this->LE;
  935. }
  936. else
  937. {
  938. $mime[] = $this->EncodeFile($path, $encoding);
  939. if($this->IsError()) { return ""; }
  940. $mime[] = $this->LE.$this->LE;
  941. }
  942. }
  943. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  944. return join("", $mime);
  945. }
  946. /**
  947. * Encodes attachment in requested format. Returns an
  948. * empty string on failure.
  949. * @access private
  950. * @return string
  951. */
  952. function EncodeFile ($path, $encoding = "base64") {
  953. if(!@$fd = fopen($path, "rb"))
  954. {
  955. $this->SetError($this->Lang("file_open") . $path);
  956. return "";
  957. }
  958. $magic_quotes = get_magic_quotes_runtime();
  959. set_magic_quotes_runtime(0);
  960. $file_buffer = fread($fd, filesize($path));
  961. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  962. fclose($fd);
  963. set_magic_quotes_runtime($magic_quotes);
  964. return $file_buffer;
  965. }
  966. /**
  967. * Encodes string to requested format. Returns an
  968. * empty string on failure.
  969. * @access private
  970. * @return string
  971. */
  972. function EncodeString ($str, $encoding = "base64") {
  973. $encoded = "";
  974. switch(strtolower($encoding)) {
  975. case "base64":
  976. // chunk_split is found in PHP >= 3.0.6
  977. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  978. break;
  979. case "7bit":
  980. case "8bit":
  981. $encoded = $this->FixEOL($str);
  982. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  983. $encoded .= $this->LE;
  984. break;
  985. case "binary":
  986. $encoded = $str;
  987. break;
  988. case "quoted-printable":
  989. $encoded = $this->EncodeQP($str);
  990. break;
  991. default:
  992. $this->SetError($this->Lang("encoding") . $encoding);
  993. break;
  994. }
  995. return $encoded;
  996. }
  997. /**
  998. * Encode a header string to best of Q, B, quoted or none.
  999. * @access private
  1000. * @return string
  1001. */
  1002. function EncodeHeader ($str, $position = 'text') {
  1003. $x = 0;
  1004. switch (strtolower($position)) {
  1005. case 'phrase':
  1006. if (!preg_match('/[\200-\377]/', $str)) {
  1007. // Can't use addslashes as we don't know what value has magic_quotes_sybase.
  1008. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1009. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
  1010. return ($encoded);
  1011. else
  1012. return ("\"$encoded\"");
  1013. }
  1014. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1015. break;
  1016. case 'comment':
  1017. $x = preg_match_all('/[()"]/', $str, $matches);
  1018. // Fall-through
  1019. case 'text':
  1020. default:
  1021. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1022. break;
  1023. }
  1024. if ($x == 0)
  1025. return ($str);
  1026. $maxlen = 75 - 7 - strlen($this->CharSet);
  1027. // Try to select the encoding which should produce the shortest output
  1028. if (strlen($str)/3 < $x) {
  1029. $encoding = 'B';
  1030. $encoded = base64_encode($str);
  1031. $maxlen -= $maxlen % 4;
  1032. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1033. } else {
  1034. $encoding = 'Q';
  1035. $encoded = $this->EncodeQ($str, $position);
  1036. $encoded = $this->WrapText($encoded, $maxlen, true);
  1037. $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
  1038. }
  1039. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1040. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1041. return $encoded;
  1042. }
  1043. /**
  1044. * Encode string to quoted-printable.
  1045. * @access private
  1046. * @return string
  1047. */
  1048. function EncodeQP ($str) {
  1049. $encoded = $this->FixEOL($str);
  1050. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1051. $encoded .= $this->LE;
  1052. // Replace every high ascii, control and = characters
  1053. $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
  1054. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1055. // Replace every spaces and tabs when it's the last character on a line
  1056. $encoded = preg_replace("/([\011\040])".$this->LE."/e",
  1057. "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
  1058. // Maximum line length of 76 characters before CRLF (74 + space + '=')
  1059. $encoded = $this->WrapText($encoded, 74, true);
  1060. return $encoded;
  1061. }
  1062. /**
  1063. * Encode string to q encoding.
  1064. * @access private
  1065. * @return string
  1066. */
  1067. function EncodeQ ($str, $position = "text") {
  1068. // There should not be any EOL in the string
  1069. $encoded = preg_replace("[\r\n]", "", $str);
  1070. switch (strtolower($position)) {
  1071. case "phrase":
  1072. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1073. break;
  1074. case "comment":
  1075. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1076. case "text":
  1077. default:
  1078. // Replace every high ascii, control =, ? and _ characters
  1079. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1080. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1081. break;
  1082. }
  1083. // Replace every spaces to _ (more readable than =20)
  1084. $encoded = str_replace(" ", "_", $encoded);
  1085. return $encoded;
  1086. }
  1087. /**
  1088. * Adds a string or binary attachment (non-filesystem) to the list.
  1089. * This method can be used to attach ascii or binary data,
  1090. * such as a BLOB record from a database.
  1091. * @param string $string String attachment data.
  1092. * @param string $filename Name of the attachment.
  1093. * @param string $encoding File encoding (see $Encoding).
  1094. * @param string $type File extension (MIME) type.
  1095. * @return void
  1096. */
  1097. function AddStringAttachment($string, $filename, $encoding = "base64",
  1098. $type = "application/octet-stream") {
  1099. // Append to $attachment array
  1100. $cur = count($this->attachment);
  1101. $this->attachment[$cur][0] = $string;
  1102. $this->attachment[$cur][1] = $filename;
  1103. $this->attachment[$cur][2] = $filename;
  1104. $this->attachment[$cur][3] = $encoding;
  1105. $this->attachment[$cur][4] = $type;
  1106. $this->attachment[$cur][5] = true; // isString
  1107. $this->attachment[$cur][6] = "attachment";
  1108. $this->attachment[$cur][7] = 0;
  1109. }
  1110. /**
  1111. * Adds an embedded attachment. This can include images, sounds, and
  1112. * just about any other document. Make sure to set the $type to an
  1113. * image type. For JPEG images use "image/jpeg" and for GIF images
  1114. * use "image/gif".
  1115. * @param string $path Path to the attachment.
  1116. * @param string $cid Content ID of the attachment. Use this to identify
  1117. * the Id for accessing the image in an HTML form.
  1118. * @param string $name Overrides the attachment name.
  1119. * @param string $encoding File encoding (see $Encoding).
  1120. * @param string $type File extension (MIME) type.
  1121. * @return bool
  1122. */
  1123. function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
  1124. $type = "application/octet-stream") {
  1125. if(!@is_file($path))
  1126. {
  1127. $this->SetError($this->Lang("file_access") . $path);
  1128. return false;
  1129. }
  1130. $filename = basename($path);
  1131. if($name == "")
  1132. $name = $filename;
  1133. // Append to $attachment array
  1134. $cur = count($this->attachment);
  1135. $this->attachment[$cur][0] = $path;
  1136. $this->attachment[$cur][1] = $filename;
  1137. $this->attachment[$cur][2] = $name;
  1138. $this->attachment[$cur][3] = $encoding;
  1139. $this->attachment[$cur][4] = $type;
  1140. $this->attachment[$cur][5] = false; // isStringAttachment
  1141. $this->attachment[$cur][6] = "inline";
  1142. $this->attachment[$cur][7] = $cid;
  1143. return true;
  1144. }
  1145. /**
  1146. * Returns true if an inline attachment is present.
  1147. * @access private
  1148. * @return bool
  1149. */
  1150. function InlineImageExists() {
  1151. $result = false;
  1152. for($i = 0; $i < count($this->attachment); $i++)
  1153. {
  1154. if($this->attachment[$i][6] == "inline")
  1155. {
  1156. $result = true;
  1157. break;
  1158. }
  1159. }
  1160. return $result;
  1161. }
  1162. /////////////////////////////////////////////////
  1163. // MESSAGE RESET METHODS
  1164. /////////////////////////////////////////////////
  1165. /**
  1166. * Clears all recipients assigned in the TO array. Returns void.
  1167. * @return void
  1168. */
  1169. function ClearAddresses() {
  1170. $this->to = array();
  1171. }
  1172. /**
  1173. * Clears all recipients assigned in the CC array. Returns void.
  1174. * @return void
  1175. */
  1176. function ClearCCs() {
  1177. $this->cc = array();
  1178. }
  1179. /**
  1180. * Clears all recipients assigned in the BCC array. Returns void.
  1181. * @return void
  1182. */
  1183. function ClearBCCs() {
  1184. $this->bcc = array();
  1185. }
  1186. /**
  1187. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1188. * @return void
  1189. */
  1190. function ClearReplyTos() {
  1191. $this->ReplyTo = array();
  1192. }
  1193. /**
  1194. * Clears all recipients assigned in the TO, CC and BCC
  1195. * array. Returns void.
  1196. * @return void
  1197. */
  1198. function ClearAllRecipients() {
  1199. $this->to = array();
  1200. $this->cc = array();
  1201. $this->bcc = array();
  1202. }
  1203. /**
  1204. * Clears all previously set filesystem, string, and binary
  1205. * attachments. Returns void.
  1206. * @return void
  1207. */
  1208. function ClearAttachments() {
  1209. $this->attachment = array();
  1210. }
  1211. /**
  1212. * Clears all custom headers. Returns void.
  1213. * @return void
  1214. */
  1215. function ClearCustomHeaders() {
  1216. $this->CustomHeader = array();
  1217. }
  1218. /////////////////////////////////////////////////
  1219. // MISCELLANEOUS METHODS
  1220. /////////////////////////////////////////////////
  1221. /**
  1222. * Adds the error message to the error container.
  1223. * Returns void.
  1224. * @access private
  1225. * @return void
  1226. */
  1227. function SetError($msg) {
  1228. $this->error_count++;
  1229. $this->ErrorInfo = $msg;
  1230. }
  1231. /**
  1232. * Returns the proper RFC 822 formatted date.
  1233. * @access private
  1234. * @return string
  1235. */
  1236. function RFCDate() {
  1237. $tz = date("Z");
  1238. $tzs = ($tz < 0) ? "-" : "+";
  1239. $tz = abs($tz);
  1240. $tz = ($tz/3600)*100 + ($tz%3600)/60;
  1241. $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
  1242. return $result;
  1243. }
  1244. /**
  1245. * Returns the appropriate server variable. Should work with both
  1246. * PHP 4.1.0+ as well as older versions. Returns an empty string
  1247. * if nothing is found.
  1248. * @access private
  1249. * @return mixed
  1250. */
  1251. function ServerVar($varName) {
  1252. global $HTTP_SERVER_VARS;
  1253. global $HTTP_ENV_VARS;
  1254. if(!isset($_SERVER))
  1255. {
  1256. $_SERVER = $HTTP_SERVER_VARS;
  1257. if(!isset($_SERVER["REMOTE_ADDR"]))
  1258. $_SERVER = $HTTP_ENV_VARS; // must be Apache
  1259. }
  1260. if(isset($_SERVER[$varName]))
  1261. return $_SERVER[$varName];
  1262. else
  1263. return "";
  1264. }
  1265. /**
  1266. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1267. * @access private
  1268. * @return string
  1269. */
  1270. function ServerHostname() {
  1271. if ($this->Hostname != "")
  1272. $result = $this->Hostname;
  1273. elseif ($this->ServerVar('SERVER_NAME') != "")
  1274. $result = $this->ServerVar('SERVER_NAME');
  1275. else
  1276. $result = "localhost.localdomain";
  1277. return $result;
  1278. }
  1279. /**
  1280. * Returns a message in the appropriate language.
  1281. * @access private
  1282. * @return string
  1283. */
  1284. function Lang($key) {
  1285. if(count($this->language) < 1)
  1286. $this->SetLanguage("en"); // set the default language
  1287. if(isset($this->language[$key]))
  1288. return $this->language[$key];
  1289. else
  1290. return "Language string failed to load: " . $key;
  1291. }
  1292. /**
  1293. * Returns true if an error occurred.
  1294. * @return bool
  1295. */
  1296. function IsError() {
  1297. return ($this->error_count > 0);
  1298. }
  1299. /**
  1300. * Changes every end of line from CR or LF to CRLF.
  1301. * @access private
  1302. * @return string
  1303. */
  1304. function FixEOL($str) {
  1305. $str = str_replace("\r\n", "\n", $str);
  1306. $str = str_replace("\r", "\n", $str);
  1307. $str = str_replace("\n", $this->LE, $str);
  1308. return $str;
  1309. }
  1310. /**
  1311. * Adds a custom header.
  1312. * @return void
  1313. */
  1314. function AddCustomHeader($custom_header) {
  1315. $this->CustomHeader[] = explode(":", $custom_header, 2);
  1316. }
  1317. }
  1318. ?>