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

/includes/phpmailer/class.phpmailer.php

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