PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/cron/class.phpmailer.php

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