2 ////////////////////////////////////////////////////
\r
3 // PHPMailer - PHP email class
\r
5 // Class for sending email using either
\r
6 // sendmail, PHP mail(), or SMTP. Methods are
\r
7 // based upon the standard AspEmail(tm) classes.
\r
9 // Copyright (C) 2001 - 2003 Brent R. Matzelle
\r
11 // License: LGPL, see LICENSE
\r
12 ////////////////////////////////////////////////////
\r
15 * PHPMailer - PHP email transport class
\r
16 * @package PHPMailer
\r
17 * @author Brent R. Matzelle
\r
18 * @copyright 2001 - 2003 Brent R. Matzelle
\r
22 /////////////////////////////////////////////////
\r
24 /////////////////////////////////////////////////
\r
27 * Email priority (1 = High, 3 = Normal, 5 = low).
\r
33 * Sets the CharSet of the message.
\r
36 var $CharSet = "iso-8859-1";
\r
39 * Sets the Content-type of the message.
\r
42 var $ContentType = "text/plain";
\r
45 * Sets the Encoding of the message. Options for this are "8bit",
\r
46 * "7bit", "binary", "base64", and "quoted-printable".
\r
49 var $Encoding = "8bit";
\r
52 * Holds the most recent mailer error message.
\r
55 var $ErrorInfo = "";
\r
58 * Sets the From email address for the message.
\r
61 var $From = "root@localhost";
\r
64 * Sets the From name of the message.
\r
67 var $FromName = "Root User";
\r
70 * Sets the Sender email (Return-Path) of the message. If not empty,
\r
71 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
\r
77 * Sets the Subject of the message.
\r
83 * Sets the Body of the message. This can be either an HTML or text body.
\r
84 * If HTML then run IsHTML(true).
\r
90 * Sets the text-only body of the message. This automatically sets the
\r
91 * email to multipart/alternative. This body can be read by mail
\r
92 * clients that do not have HTML email capability such as mutt. Clients
\r
93 * that can read HTML will view the normal Body.
\r
99 * Sets word wrapping on the body of the message to a given number of
\r
106 * Method to send mail: ("mail", "sendmail", or "smtp").
\r
109 var $Mailer = "mail";
\r
112 * Sets the path of the sendmail program.
\r
115 var $Sendmail = "/usr/sbin/sendmail";
\r
118 * Path to PHPMailer plugins. This is now only useful if the SMTP class
\r
119 * is in a different directory than the PHP include path.
\r
122 var $PluginDir = "";
\r
125 * Holds PHPMailer version.
\r
128 var $Version = "1.73";
\r
131 * Sets the email address that a reading confirmation will be sent.
\r
134 var $ConfirmReadingTo = "";
\r
137 * Sets the hostname to use in Message-Id and Received headers
\r
138 * and as default HELO string. If empty, the value returned
\r
139 * by SERVER_NAME is used or 'localhost.localdomain'.
\r
142 var $Hostname = "";
\r
144 /////////////////////////////////////////////////
\r
146 /////////////////////////////////////////////////
\r
149 * Sets the SMTP hosts. All hosts must be separated by a
\r
150 * semicolon. You can also specify a different port
\r
151 * for each host by using this format: [hostname:port]
\r
152 * (e.g. "smtp1.example.com:25;smtp2.example.com").
\r
153 * Hosts will be tried in order.
\r
156 var $Host = "localhost";
\r
159 * Sets the default SMTP server port.
\r
165 * Sets the SMTP HELO of the message (Default is $Hostname).
\r
171 * Sets SMTP authentication. Utilizes the Username and Password variables.
\r
174 var $SMTPAuth = false;
\r
177 * Sets SMTP username.
\r
180 var $Username = "";
\r
183 * Sets SMTP password.
\r
186 var $Password = "";
\r
189 * Sets the SMTP server timeout in seconds. This function will not
\r
190 * work with the win32 version.
\r
196 * Sets SMTP class debugging on or off.
\r
199 var $SMTPDebug = false;
\r
202 * Prevents the SMTP connection from being closed after each mail
\r
203 * sending. If this is set to true then to close the connection
\r
204 * requires an explicit call to SmtpClose().
\r
207 var $SMTPKeepAlive = false;
\r
215 var $bcc = array();
\r
216 var $ReplyTo = array();
\r
217 var $attachment = array();
\r
218 var $CustomHeader = array();
\r
219 var $message_type = "";
\r
220 var $boundary = array();
\r
221 var $language = array();
\r
222 var $error_count = 0;
\r
226 /////////////////////////////////////////////////
\r
227 // VARIABLE METHODS
\r
228 /////////////////////////////////////////////////
\r
231 * Sets message type to HTML.
\r
232 * @param bool $bool
\r
235 function IsHTML($bool) {
\r
237 $this->ContentType = "text/html";
\r
239 $this->ContentType = "text/plain";
\r
243 * Sets Mailer to send message using SMTP.
\r
246 function IsSMTP() {
\r
247 $this->Mailer = "smtp";
\r
251 * Sets Mailer to send message using PHP mail() function.
\r
254 function IsMail() {
\r
255 $this->Mailer = "mail";
\r
259 * Sets Mailer to send message using the $Sendmail program.
\r
262 function IsSendmail() {
\r
263 $this->Mailer = "sendmail";
\r
267 * Sets Mailer to send message using the qmail MTA.
\r
270 function IsQmail() {
\r
271 $this->Sendmail = "/var/qmail/bin/sendmail";
\r
272 $this->Mailer = "sendmail";
\r
276 /////////////////////////////////////////////////
\r
277 // RECIPIENT METHODS
\r
278 /////////////////////////////////////////////////
\r
281 * Adds a "To" address.
\r
282 * @param string $address
\r
283 * @param string $name
\r
286 function AddAddress($address, $name = "") {
\r
287 $cur = count($this->to);
\r
288 $this->to[$cur][0] = trim($address);
\r
289 $this->to[$cur][1] = $name;
\r
293 * Adds a "Cc" address. Note: this function works
\r
294 * with the SMTP mailer on win32, not with the "mail"
\r
296 * @param string $address
\r
297 * @param string $name
\r
300 function AddCC($address, $name = "") {
\r
301 $cur = count($this->cc);
\r
302 $this->cc[$cur][0] = trim($address);
\r
303 $this->cc[$cur][1] = $name;
\r
307 * Adds a "Bcc" address. Note: this function works
\r
308 * with the SMTP mailer on win32, not with the "mail"
\r
310 * @param string $address
\r
311 * @param string $name
\r
314 function AddBCC($address, $name = "") {
\r
315 $cur = count($this->bcc);
\r
316 $this->bcc[$cur][0] = trim($address);
\r
317 $this->bcc[$cur][1] = $name;
\r
321 * Adds a "Reply-to" address.
\r
322 * @param string $address
\r
323 * @param string $name
\r
326 function AddReplyTo($address, $name = "") {
\r
327 $cur = count($this->ReplyTo);
\r
328 $this->ReplyTo[$cur][0] = trim($address);
\r
329 $this->ReplyTo[$cur][1] = $name;
\r
333 /////////////////////////////////////////////////
\r
334 // MAIL SENDING METHODS
\r
335 /////////////////////////////////////////////////
\r
338 * Creates message and assigns Mailer. If the message is
\r
339 * not sent successfully then it returns false. Use the ErrorInfo
\r
340 * variable to view description of the error.
\r
348 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
\r
350 $this->SetError($this->Lang("provide_address"));
\r
354 // Set whether the message is multipart/alternative
\r
355 if(!empty($this->AltBody))
\r
356 $this->ContentType = "multipart/alternative";
\r
358 $this->error_count = 0; // reset errors
\r
359 $this->SetMessageType();
\r
360 $header .= $this->CreateHeader();
\r
361 $body = $this->CreateBody();
\r
363 if($body == "") { return false; }
\r
365 // Choose the mailer
\r
366 switch($this->Mailer)
\r
369 $result = $this->SendmailSend($header, $body);
\r
372 $result = $this->MailSend($header, $body);
\r
375 $result = $this->SmtpSend($header, $body);
\r
378 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
\r
387 * Sends mail using the $Sendmail program.
\r
391 function SendmailSend($header, $body) {
\r
392 if ($this->Sender != "")
\r
393 $sendmail = sprintf("%s -oi -f %s -t",
\r
394 escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
\r
396 $sendmail = sprintf("%s -oi -t",
\r
397 escapeshellcmd($this->Sendmail));
\r
399 if(!@$mail = popen($sendmail, "w"))
\r
401 $this->SetError($this->Lang("execute") . $this->Sendmail);
\r
405 fputs($mail, $header);
\r
406 fputs($mail, $body);
\r
408 $result = pclose($mail) >> 8 & 0xFF;
\r
411 $this->SetError($this->Lang("execute") . $this->Sendmail);
\r
419 * Sends mail using the PHP mail() function.
\r
423 function MailSend($header, $body) {
\r
425 for($i = 0; $i < count($this->to); $i++)
\r
427 if($i != 0) { $to .= ", "; }
\r
428 $to .= $this->to[$i][0];
\r
431 if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
\r
433 $old_from = ini_get("sendmail_from");
\r
434 ini_set("sendmail_from", $this->Sender);
\r
435 $params = sprintf("-oi -f %s", $this->Sender);
\r
436 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body,
\r
440 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
\r
442 if (isset($old_from))
\r
443 ini_set("sendmail_from", $old_from);
\r
447 $this->SetError($this->Lang("instantiate"));
\r
455 * Sends mail via SMTP using PhpSMTP (Author:
\r
456 * Chris Ryan). Returns bool. Returns false if there is a
\r
457 * bad MAIL FROM, RCPT, or DATA input.
\r
461 function SmtpSend($header, $body) {
\r
462 include_once($this->PluginDir . "class.smtp.php");
\r
464 $bad_rcpt = array();
\r
466 if(!$this->SmtpConnect())
\r
469 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
\r
470 if(!$this->smtp->Mail($smtp_from))
\r
472 $error = $this->Lang("from_failed") . $smtp_from;
\r
473 $this->SetError($error);
\r
474 $this->smtp->Reset();
\r
478 // Attempt to send attach all recipients
\r
479 for($i = 0; $i < count($this->to); $i++)
\r
481 if(!$this->smtp->Recipient($this->to[$i][0]))
\r
482 $bad_rcpt[] = $this->to[$i][0];
\r
484 for($i = 0; $i < count($this->cc); $i++)
\r
486 if(!$this->smtp->Recipient($this->cc[$i][0]))
\r
487 $bad_rcpt[] = $this->cc[$i][0];
\r
489 for($i = 0; $i < count($this->bcc); $i++)
\r
491 if(!$this->smtp->Recipient($this->bcc[$i][0]))
\r
492 $bad_rcpt[] = $this->bcc[$i][0];
\r
495 if(count($bad_rcpt) > 0) // Create error message
\r
497 for($i = 0; $i < count($bad_rcpt); $i++)
\r
499 if($i != 0) { $error .= ", "; }
\r
500 $error .= $bad_rcpt[$i];
\r
502 $error = $this->Lang("recipients_failed") . $error;
\r
503 $this->SetError($error);
\r
504 $this->smtp->Reset();
\r
508 if(!$this->smtp->Data($header . $body))
\r
510 $this->SetError($this->Lang("data_not_accepted"));
\r
511 $this->smtp->Reset();
\r
514 if($this->SMTPKeepAlive == true)
\r
515 $this->smtp->Reset();
\r
517 $this->SmtpClose();
\r
523 * Initiates a connection to an SMTP server. Returns false if the
\r
524 * operation failed.
\r
528 function SmtpConnect() {
\r
529 if($this->smtp == NULL) { $this->smtp = new SMTP(); }
\r
531 $this->smtp->do_debug = $this->SMTPDebug;
\r
532 $hosts = explode(";", $this->Host);
\r
534 $connection = ($this->smtp->Connected());
\r
536 // Retry while there is no connection
\r
537 while($index < count($hosts) && $connection == false)
\r
539 if(strstr($hosts[$index], ":"))
\r
540 list($host, $port) = explode(":", $hosts[$index]);
\r
543 $host = $hosts[$index];
\r
544 $port = $this->Port;
\r
547 if($this->smtp->Connect($host, $port, $this->Timeout))
\r
549 if ($this->Helo != '')
\r
550 $this->smtp->Hello($this->Helo);
\r
552 $this->smtp->Hello($this->ServerHostname());
\r
554 if($this->SMTPAuth)
\r
556 if(!$this->smtp->Authenticate($this->Username,
\r
559 $this->SetError($this->Lang("authenticate"));
\r
560 $this->smtp->Reset();
\r
561 $connection = false;
\r
564 $connection = true;
\r
569 $this->SetError($this->Lang("connect_host"));
\r
571 return $connection;
\r
575 * Closes the active SMTP session if one exists.
\r
578 function SmtpClose() {
\r
579 if($this->smtp != NULL)
\r
581 if($this->smtp->Connected())
\r
583 $this->smtp->Quit();
\r
584 $this->smtp->Close();
\r
590 * Sets the language for all class error messages. Returns false
\r
591 * if it cannot load the language file. The default language type
\r
593 * @param string $lang_type Type of language (e.g. Portuguese: "br")
\r
594 * @param string $lang_path Path to the language file directory
\r
598 function SetLanguage($lang_type, $lang_path = "language/") {
\r
599 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
\r
600 include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
\r
601 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
\r
602 include($lang_path.'phpmailer.lang-en.php');
\r
605 $this->SetError("Could not load language file");
\r
608 $this->language = $PHPMAILER_LANG;
\r
613 /////////////////////////////////////////////////
\r
614 // MESSAGE CREATION METHODS
\r
615 /////////////////////////////////////////////////
\r
618 * Creates recipient headers.
\r
622 function AddrAppend($type, $addr) {
\r
623 $addr_str = $type . ": ";
\r
624 $addr_str .= $this->AddrFormat($addr[0]);
\r
625 if(count($addr) > 1)
\r
627 for($i = 1; $i < count($addr); $i++)
\r
628 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
\r
630 $addr_str .= $this->LE;
\r
636 * Formats an address correctly.
\r
640 function AddrFormat($addr) {
\r
641 if(empty($addr[1]))
\r
642 $formatted = $addr[0];
\r
645 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
\r
653 * Wraps message for use with mailers that do not
\r
654 * automatically perform wrapping and for quoted-printable.
\r
655 * Original written by philippe.
\r
659 function WrapText($message, $length, $qp_mode = false) {
\r
660 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
\r
662 $message = $this->FixEOL($message);
\r
663 if (substr($message, -1) == $this->LE)
\r
664 $message = substr($message, 0, -1);
\r
666 $line = explode($this->LE, $message);
\r
668 for ($i=0 ;$i < count($line); $i++)
\r
670 $line_part = explode(" ", $line[$i]);
\r
672 for ($e = 0; $e<count($line_part); $e++)
\r
674 $word = $line_part[$e];
\r
675 if ($qp_mode and (strlen($word) > $length))
\r
677 $space_left = $length - strlen($buf) - 1;
\r
680 if ($space_left > 20)
\r
682 $len = $space_left;
\r
683 if (substr($word, $len - 1, 1) == "=")
\r
685 elseif (substr($word, $len - 2, 1) == "=")
\r
687 $part = substr($word, 0, $len);
\r
688 $word = substr($word, $len);
\r
689 $buf .= " " . $part;
\r
690 $message .= $buf . sprintf("=%s", $this->LE);
\r
694 $message .= $buf . $soft_break;
\r
698 while (strlen($word) > 0)
\r
701 if (substr($word, $len - 1, 1) == "=")
\r
703 elseif (substr($word, $len - 2, 1) == "=")
\r
705 $part = substr($word, 0, $len);
\r
706 $word = substr($word, $len);
\r
708 if (strlen($word) > 0)
\r
709 $message .= $part . sprintf("=%s", $this->LE);
\r
717 $buf .= ($e == 0) ? $word : (" " . $word);
\r
719 if (strlen($buf) > $length and $buf_o != "")
\r
721 $message .= $buf_o . $soft_break;
\r
726 $message .= $buf . $this->LE;
\r
733 * Set the body wrapping.
\r
737 function SetWordWrap() {
\r
738 if($this->WordWrap < 1)
\r
741 switch($this->message_type)
\r
745 case "alt_attachments":
\r
746 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
\r
749 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
\r
755 * Assembles message header.
\r
759 function CreateHeader() {
\r
762 // Set the boundaries
\r
763 $uniq_id = md5(uniqid(time()));
\r
764 $this->boundary[1] = "b1_" . $uniq_id;
\r
765 $this->boundary[2] = "b2_" . $uniq_id;
\r
767 $result .= $this->HeaderLine("Date", $this->RFCDate());
\r
768 if($this->Sender == "")
\r
769 $result .= $this->HeaderLine("Return-Path", trim($this->From));
\r
771 $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
\r
773 // To be created automatically by mail()
\r
774 if($this->Mailer != "mail")
\r
776 if(count($this->to) > 0)
\r
777 $result .= $this->AddrAppend("To", $this->to);
\r
778 else if (count($this->cc) == 0)
\r
779 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
\r
780 if(count($this->cc) > 0)
\r
781 $result .= $this->AddrAppend("Cc", $this->cc);
\r
785 $from[0][0] = trim($this->From);
\r
786 $from[0][1] = $this->FromName;
\r
787 $result .= $this->AddrAppend("From", $from);
\r
789 // sendmail and mail() extract Bcc from the header before sending
\r
790 if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
\r
791 $result .= $this->AddrAppend("Bcc", $this->bcc);
\r
793 if(count($this->ReplyTo) > 0)
\r
794 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
\r
796 // mail() sets the subject itself
\r
797 if($this->Mailer != "mail")
\r
798 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
\r
800 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
\r
801 $result .= $this->HeaderLine("X-Priority", $this->Priority);
\r
802 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]");
\r
804 if($this->ConfirmReadingTo != "")
\r
806 $result .= $this->HeaderLine("Disposition-Notification-To",
\r
807 "<" . trim($this->ConfirmReadingTo) . ">");
\r
810 // Add custom headers
\r
811 for($index = 0; $index < count($this->CustomHeader); $index++)
\r
813 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
\r
814 $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
\r
816 $result .= $this->HeaderLine("MIME-Version", "1.0");
\r
818 switch($this->message_type)
\r
821 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
\r
822 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
\r
823 $this->ContentType, $this->CharSet);
\r
825 case "attachments":
\r
827 case "alt_attachments":
\r
828 if($this->InlineImageExists())
\r
830 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
\r
831 "multipart/related", $this->LE, $this->LE,
\r
832 $this->boundary[1], $this->LE);
\r
836 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
\r
837 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
\r
841 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
\r
842 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
\r
846 if($this->Mailer != "mail")
\r
847 $result .= $this->LE.$this->LE;
\r
853 * Assembles the message body. Returns an empty string on failure.
\r
857 function CreateBody() {
\r
860 $this->SetWordWrap();
\r
862 switch($this->message_type)
\r
865 $result .= $this->GetBoundary($this->boundary[1], "",
\r
867 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
\r
868 $result .= $this->LE.$this->LE;
\r
869 $result .= $this->GetBoundary($this->boundary[1], "",
\r
872 $result .= $this->EncodeString($this->Body, $this->Encoding);
\r
873 $result .= $this->LE.$this->LE;
\r
875 $result .= $this->EndBoundary($this->boundary[1]);
\r
878 $result .= $this->EncodeString($this->Body, $this->Encoding);
\r
880 case "attachments":
\r
881 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
\r
882 $result .= $this->EncodeString($this->Body, $this->Encoding);
\r
883 $result .= $this->LE;
\r
885 $result .= $this->AttachAll();
\r
887 case "alt_attachments":
\r
888 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
\r
889 $result .= sprintf("Content-Type: %s;%s" .
\r
890 "\tboundary=\"%s\"%s",
\r
891 "multipart/alternative", $this->LE,
\r
892 $this->boundary[2], $this->LE.$this->LE);
\r
894 // Create text body
\r
895 $result .= $this->GetBoundary($this->boundary[2], "",
\r
896 "text/plain", "") . $this->LE;
\r
898 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
\r
899 $result .= $this->LE.$this->LE;
\r
901 // Create the HTML body
\r
902 $result .= $this->GetBoundary($this->boundary[2], "",
\r
903 "text/html", "") . $this->LE;
\r
905 $result .= $this->EncodeString($this->Body, $this->Encoding);
\r
906 $result .= $this->LE.$this->LE;
\r
908 $result .= $this->EndBoundary($this->boundary[2]);
\r
910 $result .= $this->AttachAll();
\r
913 if($this->IsError())
\r
920 * Returns the start of a message boundary.
\r
923 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
\r
925 if($charSet == "") { $charSet = $this->CharSet; }
\r
926 if($contentType == "") { $contentType = $this->ContentType; }
\r
927 if($encoding == "") { $encoding = $this->Encoding; }
\r
929 $result .= $this->TextLine("--" . $boundary);
\r
930 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
\r
931 $contentType, $charSet);
\r
932 $result .= $this->LE;
\r
933 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
\r
934 $result .= $this->LE;
\r
940 * Returns the end of a message boundary.
\r
943 function EndBoundary($boundary) {
\r
944 return $this->LE . "--" . $boundary . "--" . $this->LE;
\r
948 * Sets the message type.
\r
952 function SetMessageType() {
\r
953 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
\r
954 $this->message_type = "plain";
\r
957 if(count($this->attachment) > 0)
\r
958 $this->message_type = "attachments";
\r
959 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
\r
960 $this->message_type = "alt";
\r
961 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
\r
962 $this->message_type = "alt_attachments";
\r
967 * Returns a formatted header line.
\r
971 function HeaderLine($name, $value) {
\r
972 return $name . ": " . $value . $this->LE;
\r
976 * Returns a formatted mail line.
\r
980 function TextLine($value) {
\r
981 return $value . $this->LE;
\r
984 /////////////////////////////////////////////////
\r
985 // ATTACHMENT METHODS
\r
986 /////////////////////////////////////////////////
\r
989 * Adds an attachment from a path on the filesystem.
\r
990 * Returns false if the file could not be found
\r
992 * @param string $path Path to the attachment.
\r
993 * @param string $name Overrides the attachment name.
\r
994 * @param string $encoding File encoding (see $Encoding).
\r
995 * @param string $type File extension (MIME) type.
\r
998 function AddAttachment($path, $name = "", $encoding = "base64",
\r
999 $type = "application/octet-stream") {
\r
1000 if(!@is_file($path))
\r
1002 $this->SetError($this->Lang("file_access") . $path);
\r
1006 $filename = basename($path);
\r
1008 $name = $filename;
\r
1010 $cur = count($this->attachment);
\r
1011 $this->attachment[$cur][0] = $path;
\r
1012 $this->attachment[$cur][1] = $filename;
\r
1013 $this->attachment[$cur][2] = $name;
\r
1014 $this->attachment[$cur][3] = $encoding;
\r
1015 $this->attachment[$cur][4] = $type;
\r
1016 $this->attachment[$cur][5] = false; // isStringAttachment
\r
1017 $this->attachment[$cur][6] = "attachment";
\r
1018 $this->attachment[$cur][7] = 0;
\r
1024 * Attaches all fs, string, and binary attachments to the message.
\r
1025 * Returns an empty string on failure.
\r
1029 function AttachAll() {
\r
1030 // Return text of body
\r
1033 // Add all attachments
\r
1034 for($i = 0; $i < count($this->attachment); $i++)
\r
1036 // Check for string attachment
\r
1037 $bString = $this->attachment[$i][5];
\r
1039 $string = $this->attachment[$i][0];
\r
1041 $path = $this->attachment[$i][0];
\r
1043 $filename = $this->attachment[$i][1];
\r
1044 $name = $this->attachment[$i][2];
\r
1045 $encoding = $this->attachment[$i][3];
\r
1046 $type = $this->attachment[$i][4];
\r
1047 $disposition = $this->attachment[$i][6];
\r
1048 $cid = $this->attachment[$i][7];
\r
1050 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
\r
1051 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
\r
1052 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
\r
1054 if($disposition == "inline")
\r
1055 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
\r
1057 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
\r
1058 $disposition, $name, $this->LE.$this->LE);
\r
1060 // Encode as string attachment
\r
1063 $mime[] = $this->EncodeString($string, $encoding);
\r
1064 if($this->IsError()) { return ""; }
\r
1065 $mime[] = $this->LE.$this->LE;
\r
1069 $mime[] = $this->EncodeFile($path, $encoding);
\r
1070 if($this->IsError()) { return ""; }
\r
1071 $mime[] = $this->LE.$this->LE;
\r
1075 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
\r
1077 return join("", $mime);
\r
1081 * Encodes attachment in requested format. Returns an
\r
1082 * empty string on failure.
\r
1086 function EncodeFile ($path, $encoding = "base64") {
\r
1087 if(!@$fd = fopen($path, "rb"))
\r
1089 $this->SetError($this->Lang("file_open") . $path);
\r
1092 $magic_quotes = get_magic_quotes_runtime();
\r
1093 set_magic_quotes_runtime(0);
\r
1094 $file_buffer = fread($fd, filesize($path));
\r
1095 $file_buffer = $this->EncodeString($file_buffer, $encoding);
\r
1097 set_magic_quotes_runtime($magic_quotes);
\r
1099 return $file_buffer;
\r
1103 * Encodes string to requested format. Returns an
\r
1104 * empty string on failure.
\r
1108 function EncodeString ($str, $encoding = "base64") {
\r
1110 switch(strtolower($encoding)) {
\r
1112 // chunk_split is found in PHP >= 3.0.6
\r
1113 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
\r
1117 $encoded = $this->FixEOL($str);
\r
1118 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
\r
1119 $encoded .= $this->LE;
\r
1124 case "quoted-printable":
\r
1125 $encoded = $this->EncodeQP($str);
\r
1128 $this->SetError($this->Lang("encoding") . $encoding);
\r
1135 * Encode a header string to best of Q, B, quoted or none.
\r
1139 function EncodeHeader ($str, $position = 'text') {
\r
1142 switch (strtolower($position)) {
\r
1144 if (!preg_match('/[\200-\377]/', $str)) {
\r
1145 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
\r
1146 $encoded = addcslashes($str, "\0..\37\177\\\"");
\r
1148 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
\r
1149 return ($encoded);
\r
1151 return ("\"$encoded\"");
\r
1153 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
\r
1156 $x = preg_match_all('/[()"]/', $str, $matches);
\r
1160 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
\r
1167 $maxlen = 75 - 7 - strlen($this->CharSet);
\r
1168 // Try to select the encoding which should produce the shortest output
\r
1169 if (strlen($str)/3 < $x) {
\r
1171 $encoded = base64_encode($str);
\r
1172 $maxlen -= $maxlen % 4;
\r
1173 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
\r
1176 $encoded = $this->EncodeQ($str, $position);
\r
1177 $encoded = $this->WrapText($encoded, $maxlen, true);
\r
1178 $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
\r
1181 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
\r
1182 $encoded = trim(str_replace("\n", $this->LE, $encoded));
\r
1188 * Encode string to quoted-printable.
\r
1192 function EncodeQP ($str) {
\r
1193 $encoded = $this->FixEOL($str);
\r
1194 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
\r
1195 $encoded .= $this->LE;
\r
1197 // Replace every high ascii, control and = characters
\r
1198 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
\r
1199 "'='.sprintf('%02X', ord('\\1'))", $encoded);
\r
1200 // Replace every spaces and tabs when it's the last character on a line
\r
1201 $encoded = preg_replace("/([\011\040])".$this->LE."/e",
\r
1202 "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
\r
1204 // Maximum line length of 76 characters before CRLF (74 + space + '=')
\r
1205 $encoded = $this->WrapText($encoded, 74, true);
\r
1211 * Encode string to q encoding.
\r
1215 function EncodeQ ($str, $position = "text") {
\r
1216 // There should not be any EOL in the string
\r
1217 $encoded = preg_replace("[\r\n]", "", $str);
\r
1219 switch (strtolower($position)) {
\r
1221 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
\r
1224 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
\r
1227 // Replace every high ascii, control =, ? and _ characters
\r
1228 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
\r
1229 "'='.sprintf('%02X', ord('\\1'))", $encoded);
\r
1233 // Replace every spaces to _ (more readable than =20)
\r
1234 $encoded = str_replace(" ", "_", $encoded);
\r
1240 * Adds a string or binary attachment (non-filesystem) to the list.
\r
1241 * This method can be used to attach ascii or binary data,
\r
1242 * such as a BLOB record from a database.
\r
1243 * @param string $string String attachment data.
\r
1244 * @param string $filename Name of the attachment.
\r
1245 * @param string $encoding File encoding (see $Encoding).
\r
1246 * @param string $type File extension (MIME) type.
\r
1249 function AddStringAttachment($string, $filename, $encoding = "base64",
\r
1250 $type = "application/octet-stream") {
\r
1251 // Append to $attachment array
\r
1252 $cur = count($this->attachment);
\r
1253 $this->attachment[$cur][0] = $string;
\r
1254 $this->attachment[$cur][1] = $filename;
\r
1255 $this->attachment[$cur][2] = $filename;
\r
1256 $this->attachment[$cur][3] = $encoding;
\r
1257 $this->attachment[$cur][4] = $type;
\r
1258 $this->attachment[$cur][5] = true; // isString
\r
1259 $this->attachment[$cur][6] = "attachment";
\r
1260 $this->attachment[$cur][7] = 0;
\r
1264 * Adds an embedded attachment. This can include images, sounds, and
\r
1265 * just about any other document. Make sure to set the $type to an
\r
1266 * image type. For JPEG images use "image/jpeg" and for GIF images
\r
1267 * use "image/gif".
\r
1268 * @param string $path Path to the attachment.
\r
1269 * @param string $cid Content ID of the attachment. Use this to identify
\r
1270 * the Id for accessing the image in an HTML form.
\r
1271 * @param string $name Overrides the attachment name.
\r
1272 * @param string $encoding File encoding (see $Encoding).
\r
1273 * @param string $type File extension (MIME) type.
\r
1276 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
\r
1277 $type = "application/octet-stream") {
\r
1279 if(!@is_file($path))
\r
1281 $this->SetError($this->Lang("file_access") . $path);
\r
1285 $filename = basename($path);
\r
1287 $name = $filename;
\r
1289 // Append to $attachment array
\r
1290 $cur = count($this->attachment);
\r
1291 $this->attachment[$cur][0] = $path;
\r
1292 $this->attachment[$cur][1] = $filename;
\r
1293 $this->attachment[$cur][2] = $name;
\r
1294 $this->attachment[$cur][3] = $encoding;
\r
1295 $this->attachment[$cur][4] = $type;
\r
1296 $this->attachment[$cur][5] = false; // isStringAttachment
\r
1297 $this->attachment[$cur][6] = "inline";
\r
1298 $this->attachment[$cur][7] = $cid;
\r
1304 * Returns true if an inline attachment is present.
\r
1308 function InlineImageExists() {
\r
1310 for($i = 0; $i < count($this->attachment); $i++)
\r
1312 if($this->attachment[$i][6] == "inline")
\r
1322 /////////////////////////////////////////////////
\r
1323 // MESSAGE RESET METHODS
\r
1324 /////////////////////////////////////////////////
\r
1327 * Clears all recipients assigned in the TO array. Returns void.
\r
1330 function ClearAddresses() {
\r
1331 $this->to = array();
\r
1335 * Clears all recipients assigned in the CC array. Returns void.
\r
1338 function ClearCCs() {
\r
1339 $this->cc = array();
\r
1343 * Clears all recipients assigned in the BCC array. Returns void.
\r
1346 function ClearBCCs() {
\r
1347 $this->bcc = array();
\r
1351 * Clears all recipients assigned in the ReplyTo array. Returns void.
\r
1354 function ClearReplyTos() {
\r
1355 $this->ReplyTo = array();
\r
1359 * Clears all recipients assigned in the TO, CC and BCC
\r
1360 * array. Returns void.
\r
1363 function ClearAllRecipients() {
\r
1364 $this->to = array();
\r
1365 $this->cc = array();
\r
1366 $this->bcc = array();
\r
1370 * Clears all previously set filesystem, string, and binary
\r
1371 * attachments. Returns void.
\r
1374 function ClearAttachments() {
\r
1375 $this->attachment = array();
\r
1379 * Clears all custom headers. Returns void.
\r
1382 function ClearCustomHeaders() {
\r
1383 $this->CustomHeader = array();
\r
1387 /////////////////////////////////////////////////
\r
1388 // MISCELLANEOUS METHODS
\r
1389 /////////////////////////////////////////////////
\r
1392 * Adds the error message to the error container.
\r
1397 function SetError($msg) {
\r
1398 $this->error_count++;
\r
1399 $this->ErrorInfo = $msg;
\r
1403 * Returns the proper RFC 822 formatted date.
\r
1407 function RFCDate() {
\r
1409 $tzs = ($tz < 0) ? "-" : "+";
\r
1411 $tz = ($tz/3600)*100 + ($tz%3600)/60;
\r
1412 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
\r
1418 * Returns the appropriate server variable. Should work with both
\r
1419 * PHP 4.1.0+ as well as older versions. Returns an empty string
\r
1420 * if nothing is found.
\r
1424 function ServerVar($varName) {
\r
1425 global $HTTP_SERVER_VARS;
\r
1426 global $HTTP_ENV_VARS;
\r
1428 if(!isset($_SERVER))
\r
1430 $_SERVER = $HTTP_SERVER_VARS;
\r
1431 if(!isset($_SERVER["REMOTE_ADDR"]))
\r
1432 $_SERVER = $HTTP_ENV_VARS; // must be Apache
\r
1435 if(isset($_SERVER[$varName]))
\r
1436 return $_SERVER[$varName];
\r
1442 * Returns the server hostname or 'localhost.localdomain' if unknown.
\r
1446 function ServerHostname() {
\r
1447 if ($this->Hostname != "")
\r
1448 $result = $this->Hostname;
\r
1449 elseif ($this->ServerVar('SERVER_NAME') != "")
\r
1450 $result = $this->ServerVar('SERVER_NAME');
\r
1452 $result = "localhost.localdomain";
\r
1458 * Returns a message in the appropriate language.
\r
1462 function Lang($key) {
\r
1463 if(count($this->language) < 1)
\r
1464 $this->SetLanguage("en"); // set the default language
\r
1466 if(isset($this->language[$key]))
\r
1467 return $this->language[$key];
\r
1469 return "Language string failed to load: " . $key;
\r
1473 * Returns true if an error occurred.
\r
1476 function IsError() {
\r
1477 return ($this->error_count > 0);
\r
1481 * Changes every end of line from CR or LF to CRLF.
\r
1485 function FixEOL($str) {
\r
1486 $str = str_replace("\r\n", "\n", $str);
\r
1487 $str = str_replace("\r", "\n", $str);
\r
1488 $str = str_replace("\n", $this->LE, $str);
\r
1493 * Adds a custom header.
\r
1496 function AddCustomHeader($custom_header) {
\r
1497 $this->CustomHeader[] = explode(":", $custom_header, 2);
\r