2 ////////////////////////////////////////////////////
3 // PHPMailer - PHP email class
5 // Class for sending email using either
6 // sendmail, PHP mail(), or SMTP. Methods are
7 // based upon the standard AspEmail(tm) classes.
9 // Copyright (C) 2001 - 2003 Brent R. Matzelle
11 // License: LGPL, see LICENSE
12 ////////////////////////////////////////////////////
15 * PHPMailer - PHP email transport class
17 * @author Brent R. Matzelle
18 * @copyright 2001 - 2003 Brent R. Matzelle
22 /////////////////////////////////////////////////
24 /////////////////////////////////////////////////
27 * Email priority (1 = High, 3 = Normal, 5 = low).
33 * Sets the CharSet of the message.
36 var $CharSet = "UTF-8";
39 * Sets the Content-type of the message.
42 var $ContentType = "text/plain";
45 * Sets the Encoding of the message. Options for this are "8bit",
46 * "7bit", "binary", "base64", and "quoted-printable".
49 var $Encoding = "8bit";
52 * Holds the most recent mailer error message.
58 * Sets the From email address for the message.
61 var $From = "localhost.localdomain";
64 * Sets the From name of the message.
67 var $FromName = "Support";
70 * Sets the Sender email (Return-Path) of the message. If not empty,
71 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
77 * Sets the Subject of the message.
83 * Sets the Body of the message. This can be either an HTML or text body.
84 * If HTML then run IsHTML(true).
90 * Sets the text-only body of the message. This automatically sets the
91 * email to multipart/alternative. This body can be read by mail
92 * clients that do not have HTML email capability such as mutt. Clients
93 * that can read HTML will view the normal Body.
99 * Sets word wrapping on the body of the message to a given number of
106 * Method to send mail: ("mail", "sendmail", or "smtp").
109 var $Mailer = "mail";
112 * Sets the path of the sendmail program.
115 var $Sendmail = "/usr/sbin/sendmail";
118 * Path to PHPMailer plugins. This is now only useful if the SMTP class
119 * is in a different directory than the PHP include path.
125 * Holds PHPMailer version.
128 var $Version = "1.73";
131 * Sets the email address that a reading confirmation will be sent.
134 var $ConfirmReadingTo = "";
137 * Sets the hostname to use in Message-Id and Received headers
138 * and as default HELO string. If empty, the value returned
139 * by SERVER_NAME is used or 'localhost.localdomain'.
144 /////////////////////////////////////////////////
146 /////////////////////////////////////////////////
149 * Sets the SMTP hosts. All hosts must be separated by a
150 * semicolon. You can also specify a different port
151 * for each host by using this format: [hostname:port]
152 * (e.g. "smtp1.example.com:25;smtp2.example.com").
153 * Hosts will be tried in order.
156 var $Host = "localhost";
159 * Sets the default SMTP server port.
165 * Sets the SMTP HELO of the message (Default is $Hostname).
171 * Sets SMTP authentication. Utilizes the Username and Password variables.
174 var $SMTPAuth = false;
177 * Sets SMTP username.
183 * Sets SMTP password.
189 * Sets the SMTP server timeout in seconds. This function will not
190 * work with the win32 version.
196 * Sets SMTP class debugging on or off.
199 var $SMTPDebug = false;
202 * Prevents the SMTP connection from being closed after each mail
203 * sending. If this is set to true then to close the connection
204 * requires an explicit call to SmtpClose().
207 var $SMTPKeepAlive = false;
216 var $ReplyTo = array();
217 var $attachment = array();
218 var $CustomHeader = array();
219 var $message_type = "";
220 var $boundary = array();
221 var $language = array();
222 var $error_count = 0;
226 /////////////////////////////////////////////////
228 /////////////////////////////////////////////////
231 * Sets message type to HTML.
235 function IsHTML($bool) {
237 $this->ContentType = "text/html";
239 $this->ContentType = "text/plain";
243 * Sets Mailer to send message using SMTP.
247 $this->Mailer = "smtp";
251 * Sets Mailer to send message using PHP mail() function.
255 $this->Mailer = "mail";
259 * Sets Mailer to send message using the $Sendmail program.
262 function IsSendmail() {
263 $this->Mailer = "sendmail";
267 * Sets Mailer to send message using the qmail MTA.
271 $this->Sendmail = "/var/qmail/bin/sendmail";
272 $this->Mailer = "sendmail";
276 /////////////////////////////////////////////////
278 /////////////////////////////////////////////////
281 * Adds a "To" address.
282 * @param string $address
283 * @param string $name
286 function AddAddress($address, $name = "") {
287 $cur = count($this->to);
288 $this->to[$cur][0] = trim($address);
289 $this->to[$cur][1] = $name;
293 * Adds a "Cc" address. Note: this function works
294 * with the SMTP mailer on win32, not with the "mail"
296 * @param string $address
297 * @param string $name
300 function AddCC($address, $name = "") {
301 $cur = count($this->cc);
302 $this->cc[$cur][0] = trim($address);
303 $this->cc[$cur][1] = $name;
307 * Adds a "Bcc" address. Note: this function works
308 * with the SMTP mailer on win32, not with the "mail"
310 * @param string $address
311 * @param string $name
314 function AddBCC($address, $name = "") {
315 $cur = count($this->bcc);
316 $this->bcc[$cur][0] = trim($address);
317 $this->bcc[$cur][1] = $name;
321 * Adds a "Reply-to" address.
322 * @param string $address
323 * @param string $name
326 function AddReplyTo($address, $name = "") {
327 $cur = count($this->ReplyTo);
328 $this->ReplyTo[$cur][0] = trim($address);
329 $this->ReplyTo[$cur][1] = $name;
333 /////////////////////////////////////////////////
334 // MAIL SENDING METHODS
335 /////////////////////////////////////////////////
338 * Creates message and assigns Mailer. If the message is
339 * not sent successfully then it returns false. Use the ErrorInfo
340 * variable to view description of the error.
348 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
350 $this->SetError($this->Lang("provide_address"));
354 // Set whether the message is multipart/alternative
355 if(!empty($this->AltBody))
356 $this->ContentType = "multipart/alternative";
358 $this->error_count = 0; // reset errors
359 $this->SetMessageType();
360 $header .= $this->CreateHeader();
361 $body = $this->CreateBody();
363 if($body == "") { return false; }
366 switch($this->Mailer)
369 $result = $this->SendmailSend($header, $body);
372 $result = $this->MailSend($header, $body);
375 $result = $this->SmtpSend($header, $body);
378 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
387 * Sends mail using the $Sendmail program.
391 function SendmailSend($header, $body) {
392 if ($this->Sender != "")
393 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, escapeshellarg($this->Sender));
395 $sendmail = sprintf("%s -oi -t", $this->Sendmail);
397 if(!@$mail = popen($sendmail, "w"))
399 $this->SetError($this->Lang("execute") . $this->Sendmail);
403 fputs($mail, $header);
406 $result = pclose($mail) >> 8 & 0xFF;
409 $this->SetError($this->Lang("execute") . $this->Sendmail);
417 * Sends mail using the PHP mail() function.
421 function MailSend($header, $body) {
423 for($i = 0; $i < count($this->to); $i++)
425 if($i != 0) { $to .= ", "; }
426 $to .= $this->to[$i][0];
429 if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
431 $old_from = ini_get("sendmail_from");
432 ini_set("sendmail_from", $this->Sender);
433 $params = sprintf("-oi -f %s", $this->Sender);
434 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body,
438 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
440 if (isset($old_from))
441 ini_set("sendmail_from", $old_from);
445 $this->SetError($this->Lang("instantiate"));
453 * Sends mail via SMTP using PhpSMTP (Author:
454 * Chris Ryan). Returns bool. Returns false if there is a
455 * bad MAIL FROM, RCPT, or DATA input.
459 function SmtpSend($header, $body) {
460 include_once($this->PluginDir . "class-smtp.php");
464 if(!$this->SmtpConnect())
467 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
468 if(!$this->smtp->Mail($smtp_from))
470 $error = $this->Lang("from_failed") . $smtp_from;
471 $this->SetError($error);
472 $this->smtp->Reset();
476 // Attempt to send attach all recipients
477 for($i = 0; $i < count($this->to); $i++)
479 if(!$this->smtp->Recipient($this->to[$i][0]))
480 $bad_rcpt[] = $this->to[$i][0];
482 for($i = 0; $i < count($this->cc); $i++)
484 if(!$this->smtp->Recipient($this->cc[$i][0]))
485 $bad_rcpt[] = $this->cc[$i][0];
487 for($i = 0; $i < count($this->bcc); $i++)
489 if(!$this->smtp->Recipient($this->bcc[$i][0]))
490 $bad_rcpt[] = $this->bcc[$i][0];
493 if(count($bad_rcpt) > 0) // Create error message
495 for($i = 0; $i < count($bad_rcpt); $i++)
497 if($i != 0) { $error .= ", "; }
498 $error .= $bad_rcpt[$i];
500 $error = $this->Lang("recipients_failed") . $error;
501 $this->SetError($error);
502 $this->smtp->Reset();
506 if(!$this->smtp->Data($header . $body))
508 $this->SetError($this->Lang("data_not_accepted"));
509 $this->smtp->Reset();
512 if($this->SMTPKeepAlive == true)
513 $this->smtp->Reset();
521 * Initiates a connection to an SMTP server. Returns false if the
526 function SmtpConnect() {
527 if($this->smtp == NULL) { $this->smtp = new SMTP(); }
529 $this->smtp->do_debug = $this->SMTPDebug;
530 $hosts = explode(";", $this->Host);
532 $connection = ($this->smtp->Connected());
534 // Retry while there is no connection
535 while($index < count($hosts) && $connection == false)
537 if(strstr($hosts[$index], ":"))
538 list($host, $port) = explode(":", $hosts[$index]);
541 $host = $hosts[$index];
545 if($this->smtp->Connect($host, $port, $this->Timeout))
547 if ($this->Helo != '')
548 $this->smtp->Hello($this->Helo);
550 $this->smtp->Hello($this->ServerHostname());
554 if(!$this->smtp->Authenticate($this->Username,
557 $this->SetError($this->Lang("authenticate"));
558 $this->smtp->Reset();
567 $this->SetError($this->Lang("connect_host"));
573 * Closes the active SMTP session if one exists.
576 function SmtpClose() {
577 if($this->smtp != NULL)
579 if($this->smtp->Connected())
582 $this->smtp->Close();
588 * Sets the language for all class error messages. Returns false
589 * if it cannot load the language file. The default language type
591 * @param string $lang_type Type of language (e.g. Portuguese: "br")
592 * @param string $lang_path Path to the language file directory
596 function SetLanguage($lang_type, $lang_path = "language/") {
597 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
598 include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
599 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
600 include($lang_path.'phpmailer.lang-en.php');
603 $this->SetError("Could not load language file");
606 $this->language = $PHPMAILER_LANG;
611 /////////////////////////////////////////////////
612 // MESSAGE CREATION METHODS
613 /////////////////////////////////////////////////
616 * Creates recipient headers.
620 function AddrAppend($type, $addr) {
621 $addr_str = $type . ": ";
622 $addr_str .= $this->AddrFormat($addr[0]);
625 for($i = 1; $i < count($addr); $i++)
626 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
628 $addr_str .= $this->LE;
634 * Formats an address correctly.
638 function AddrFormat($addr) {
640 $formatted = $addr[0];
643 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
651 * Wraps message for use with mailers that do not
652 * automatically perform wrapping and for quoted-printable.
653 * Original written by philippe.
657 function WrapText($message, $length, $qp_mode = false) {
658 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
660 $message = $this->FixEOL($message);
661 if (substr($message, -1) == $this->LE)
662 $message = substr($message, 0, -1);
664 $line = explode($this->LE, $message);
666 for ($i=0 ;$i < count($line); $i++)
668 $line_part = explode(" ", $line[$i]);
670 for ($e = 0; $e<count($line_part); $e++)
672 $word = $line_part[$e];
673 if ($qp_mode and (strlen($word) > $length))
675 $space_left = $length - strlen($buf) - 1;
678 if ($space_left > 20)
681 if (substr($word, $len - 1, 1) == "=")
683 elseif (substr($word, $len - 2, 1) == "=")
685 $part = substr($word, 0, $len);
686 $word = substr($word, $len);
688 $message .= $buf . sprintf("=%s", $this->LE);
692 $message .= $buf . $soft_break;
696 while (strlen($word) > 0)
699 if (substr($word, $len - 1, 1) == "=")
701 elseif (substr($word, $len - 2, 1) == "=")
703 $part = substr($word, 0, $len);
704 $word = substr($word, $len);
706 if (strlen($word) > 0)
707 $message .= $part . sprintf("=%s", $this->LE);
715 $buf .= ($e == 0) ? $word : (" " . $word);
717 if (strlen($buf) > $length and $buf_o != "")
719 $message .= $buf_o . $soft_break;
724 $message .= $buf . $this->LE;
731 * Set the body wrapping.
735 function SetWordWrap() {
736 if($this->WordWrap < 1)
739 switch($this->message_type)
743 case "alt_attachments":
744 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
747 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
753 * Assembles message header.
757 function CreateHeader() {
760 // Set the boundaries
761 $uniq_id = md5(uniqid(time()));
762 $this->boundary[1] = "b1_" . $uniq_id;
763 $this->boundary[2] = "b2_" . $uniq_id;
765 $result .= $this->HeaderLine("Date", $this->RFCDate());
766 if($this->Sender == "")
767 $result .= $this->HeaderLine("Return-Path", trim($this->From));
769 $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
771 // To be created automatically by mail()
772 if($this->Mailer != "mail")
774 if(count($this->to) > 0)
775 $result .= $this->AddrAppend("To", $this->to);
776 else if (count($this->cc) == 0)
777 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
778 if(count($this->cc) > 0)
779 $result .= $this->AddrAppend("Cc", $this->cc);
783 $from[0][0] = trim($this->From);
784 $from[0][1] = $this->FromName;
785 $result .= $this->AddrAppend("From", $from);
787 // sendmail and mail() extract Bcc from the header before sending
788 if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
789 $result .= $this->AddrAppend("Bcc", $this->bcc);
791 if(count($this->ReplyTo) > 0)
792 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
794 // mail() sets the subject itself
795 if($this->Mailer != "mail")
796 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
798 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
799 $result .= $this->HeaderLine("X-Priority", $this->Priority);
801 if($this->ConfirmReadingTo != "")
803 $result .= $this->HeaderLine("Disposition-Notification-To",
804 "<" . trim($this->ConfirmReadingTo) . ">");
807 // Add custom headers
808 for($index = 0; $index < count($this->CustomHeader); $index++)
810 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
811 $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
813 $result .= $this->HeaderLine("MIME-Version", "1.0");
815 switch($this->message_type)
818 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
819 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
820 $this->ContentType, $this->CharSet);
824 case "alt_attachments":
825 if($this->InlineImageExists())
827 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
828 "multipart/related", $this->LE, $this->LE,
829 $this->boundary[1], $this->LE);
833 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
834 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
838 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
839 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
843 if($this->Mailer != "mail")
844 $result .= $this->LE.$this->LE;
850 * Assembles the message body. Returns an empty string on failure.
854 function CreateBody() {
857 $this->SetWordWrap();
859 switch($this->message_type)
862 $result .= $this->GetBoundary($this->boundary[1], "",
864 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
865 $result .= $this->LE.$this->LE;
866 $result .= $this->GetBoundary($this->boundary[1], "",
869 $result .= $this->EncodeString($this->Body, $this->Encoding);
870 $result .= $this->LE.$this->LE;
872 $result .= $this->EndBoundary($this->boundary[1]);
875 $result .= $this->EncodeString($this->Body, $this->Encoding);
878 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
879 $result .= $this->EncodeString($this->Body, $this->Encoding);
880 $result .= $this->LE;
882 $result .= $this->AttachAll();
884 case "alt_attachments":
885 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
886 $result .= sprintf("Content-Type: %s;%s" .
887 "\tboundary=\"%s\"%s",
888 "multipart/alternative", $this->LE,
889 $this->boundary[2], $this->LE.$this->LE);
892 $result .= $this->GetBoundary($this->boundary[2], "",
893 "text/plain", "") . $this->LE;
895 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
896 $result .= $this->LE.$this->LE;
898 // Create the HTML body
899 $result .= $this->GetBoundary($this->boundary[2], "",
900 "text/html", "") . $this->LE;
902 $result .= $this->EncodeString($this->Body, $this->Encoding);
903 $result .= $this->LE.$this->LE;
905 $result .= $this->EndBoundary($this->boundary[2]);
907 $result .= $this->AttachAll();
917 * Returns the start of a message boundary.
920 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
922 if($charSet == "") { $charSet = $this->CharSet; }
923 if($contentType == "") { $contentType = $this->ContentType; }
924 if($encoding == "") { $encoding = $this->Encoding; }
926 $result .= $this->TextLine("--" . $boundary);
927 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
928 $contentType, $charSet);
929 $result .= $this->LE;
930 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
931 $result .= $this->LE;
937 * Returns the end of a message boundary.
940 function EndBoundary($boundary) {
941 return $this->LE . "--" . $boundary . "--" . $this->LE;
945 * Sets the message type.
949 function SetMessageType() {
950 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
951 $this->message_type = "plain";
954 if(count($this->attachment) > 0)
955 $this->message_type = "attachments";
956 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
957 $this->message_type = "alt";
958 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
959 $this->message_type = "alt_attachments";
964 * Returns a formatted header line.
968 function HeaderLine($name, $value) {
969 return $name . ": " . $value . $this->LE;
973 * Returns a formatted mail line.
977 function TextLine($value) {
978 return $value . $this->LE;
981 /////////////////////////////////////////////////
982 // ATTACHMENT METHODS
983 /////////////////////////////////////////////////
986 * Adds an attachment from a path on the filesystem.
987 * Returns false if the file could not be found
989 * @param string $path Path to the attachment.
990 * @param string $name Overrides the attachment name.
991 * @param string $encoding File encoding (see $Encoding).
992 * @param string $type File extension (MIME) type.
995 function AddAttachment($path, $name = "", $encoding = "base64",
996 $type = "application/octet-stream") {
999 $this->SetError($this->Lang("file_access") . $path);
1003 $filename = basename($path);
1007 $cur = count($this->attachment);
1008 $this->attachment[$cur][0] = $path;
1009 $this->attachment[$cur][1] = $filename;
1010 $this->attachment[$cur][2] = $name;
1011 $this->attachment[$cur][3] = $encoding;
1012 $this->attachment[$cur][4] = $type;
1013 $this->attachment[$cur][5] = false; // isStringAttachment
1014 $this->attachment[$cur][6] = "attachment";
1015 $this->attachment[$cur][7] = 0;
1021 * Attaches all fs, string, and binary attachments to the message.
1022 * Returns an empty string on failure.
1026 function AttachAll() {
1027 // Return text of body
1030 // Add all attachments
1031 for($i = 0; $i < count($this->attachment); $i++)
1033 // Check for string attachment
1034 $bString = $this->attachment[$i][5];
1036 $string = $this->attachment[$i][0];
1038 $path = $this->attachment[$i][0];
1040 $filename = $this->attachment[$i][1];
1041 $name = $this->attachment[$i][2];
1042 $encoding = $this->attachment[$i][3];
1043 $type = $this->attachment[$i][4];
1044 $disposition = $this->attachment[$i][6];
1045 $cid = $this->attachment[$i][7];
1047 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
1048 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
1049 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
1051 if($disposition == "inline")
1052 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
1054 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1055 $disposition, $name, $this->LE.$this->LE);
1057 // Encode as string attachment
1060 $mime[] = $this->EncodeString($string, $encoding);
1061 if($this->IsError()) { return ""; }
1062 $mime[] = $this->LE.$this->LE;
1066 $mime[] = $this->EncodeFile($path, $encoding);
1067 if($this->IsError()) { return ""; }
1068 $mime[] = $this->LE.$this->LE;
1072 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
1074 return join("", $mime);
1078 * Encodes attachment in requested format. Returns an
1079 * empty string on failure.
1083 function EncodeFile ($path, $encoding = "base64") {
1084 if(!@$fd = fopen($path, "rb"))
1086 $this->SetError($this->Lang("file_open") . $path);
1089 $magic_quotes = get_magic_quotes_runtime();
1090 set_magic_quotes_runtime(0);
1091 $file_buffer = fread($fd, filesize($path));
1092 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1094 set_magic_quotes_runtime($magic_quotes);
1096 return $file_buffer;
1100 * Encodes string to requested format. Returns an
1101 * empty string on failure.
1105 function EncodeString ($str, $encoding = "base64") {
1107 switch(strtolower($encoding)) {
1109 // chunk_split is found in PHP >= 3.0.6
1110 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
1114 $encoded = $this->FixEOL($str);
1115 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1116 $encoded .= $this->LE;
1121 case "quoted-printable":
1122 $encoded = $this->EncodeQP($str);
1125 $this->SetError($this->Lang("encoding") . $encoding);
1132 * Encode a header string to best of Q, B, quoted or none.
1136 function EncodeHeader ($str, $position = 'text') {
1139 switch (strtolower($position)) {
1141 if (!preg_match('/[\200-\377]/', $str)) {
1142 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1143 $encoded = addcslashes($str, "\0..\37\177\\\"");
1145 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1148 return ("\"$encoded\"");
1150 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1153 $x = preg_match_all('/[()"]/', $str, $matches);
1157 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1164 $maxlen = 75 - 7 - strlen($this->CharSet);
1165 // Try to select the encoding which should produce the shortest output
1166 if (strlen($str)/3 < $x) {
1168 $encoded = base64_encode($str);
1169 $maxlen -= $maxlen % 4;
1170 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1173 $encoded = $this->EncodeQ($str, $position);
1174 $encoded = $this->WrapText($encoded, $maxlen, true);
1175 $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
1178 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
1179 $encoded = trim(str_replace("\n", $this->LE, $encoded));
1185 * Encode string to quoted-printable.
1189 function EncodeQP ($str) {
1190 $encoded = $this->FixEOL($str);
1191 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1192 $encoded .= $this->LE;
1194 // Replace every high ascii, control and = characters
1195 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
1196 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1197 // Replace every spaces and tabs when it's the last character on a line
1198 $encoded = preg_replace("/([\011\040])".$this->LE."/e",
1199 "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
1201 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1202 $encoded = $this->WrapText($encoded, 74, true);
1208 * Encode string to q encoding.
1212 function EncodeQ ($str, $position = "text") {
1213 // There should not be any EOL in the string
1214 $encoded = preg_replace("[\r\n]", "", $str);
1216 switch (strtolower($position)) {
1218 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1221 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1224 // Replace every high ascii, control =, ? and _ characters
1225 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1226 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1230 // Replace every spaces to _ (more readable than =20)
1231 $encoded = str_replace(" ", "_", $encoded);
1237 * Adds a string or binary attachment (non-filesystem) to the list.
1238 * This method can be used to attach ascii or binary data,
1239 * such as a BLOB record from a database.
1240 * @param string $string String attachment data.
1241 * @param string $filename Name of the attachment.
1242 * @param string $encoding File encoding (see $Encoding).
1243 * @param string $type File extension (MIME) type.
1246 function AddStringAttachment($string, $filename, $encoding = "base64",
1247 $type = "application/octet-stream") {
1248 // Append to $attachment array
1249 $cur = count($this->attachment);
1250 $this->attachment[$cur][0] = $string;
1251 $this->attachment[$cur][1] = $filename;
1252 $this->attachment[$cur][2] = $filename;
1253 $this->attachment[$cur][3] = $encoding;
1254 $this->attachment[$cur][4] = $type;
1255 $this->attachment[$cur][5] = true; // isString
1256 $this->attachment[$cur][6] = "attachment";
1257 $this->attachment[$cur][7] = 0;
1261 * Adds an embedded attachment. This can include images, sounds, and
1262 * just about any other document. Make sure to set the $type to an
1263 * image type. For JPEG images use "image/jpeg" and for GIF images
1265 * @param string $path Path to the attachment.
1266 * @param string $cid Content ID of the attachment. Use this to identify
1267 * the Id for accessing the image in an HTML form.
1268 * @param string $name Overrides the attachment name.
1269 * @param string $encoding File encoding (see $Encoding).
1270 * @param string $type File extension (MIME) type.
1273 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1274 $type = "application/octet-stream") {
1276 if(!@is_file($path))
1278 $this->SetError($this->Lang("file_access") . $path);
1282 $filename = basename($path);
1286 // Append to $attachment array
1287 $cur = count($this->attachment);
1288 $this->attachment[$cur][0] = $path;
1289 $this->attachment[$cur][1] = $filename;
1290 $this->attachment[$cur][2] = $name;
1291 $this->attachment[$cur][3] = $encoding;
1292 $this->attachment[$cur][4] = $type;
1293 $this->attachment[$cur][5] = false; // isStringAttachment
1294 $this->attachment[$cur][6] = "inline";
1295 $this->attachment[$cur][7] = $cid;
1301 * Returns true if an inline attachment is present.
1305 function InlineImageExists() {
1307 for($i = 0; $i < count($this->attachment); $i++)
1309 if($this->attachment[$i][6] == "inline")
1319 /////////////////////////////////////////////////
1320 // MESSAGE RESET METHODS
1321 /////////////////////////////////////////////////
1324 * Clears all recipients assigned in the TO array. Returns void.
1327 function ClearAddresses() {
1328 $this->to = array();
1332 * Clears all recipients assigned in the CC array. Returns void.
1335 function ClearCCs() {
1336 $this->cc = array();
1340 * Clears all recipients assigned in the BCC array. Returns void.
1343 function ClearBCCs() {
1344 $this->bcc = array();
1348 * Clears all recipients assigned in the ReplyTo array. Returns void.
1351 function ClearReplyTos() {
1352 $this->ReplyTo = array();
1356 * Clears all recipients assigned in the TO, CC and BCC
1357 * array. Returns void.
1360 function ClearAllRecipients() {
1361 $this->to = array();
1362 $this->cc = array();
1363 $this->bcc = array();
1367 * Clears all previously set filesystem, string, and binary
1368 * attachments. Returns void.
1371 function ClearAttachments() {
1372 $this->attachment = array();
1376 * Clears all custom headers. Returns void.
1379 function ClearCustomHeaders() {
1380 $this->CustomHeader = array();
1384 /////////////////////////////////////////////////
1385 // MISCELLANEOUS METHODS
1386 /////////////////////////////////////////////////
1389 * Adds the error message to the error container.
1394 function SetError($msg) {
1395 $this->error_count++;
1396 $this->ErrorInfo = $msg;
1400 * Returns the proper RFC 822 formatted date.
1404 function RFCDate() {
1406 $tzs = ($tz < 0) ? "-" : "+";
1408 $tz = ($tz/3600)*100 + ($tz%3600)/60;
1409 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1415 * Returns the appropriate server variable. Should work with both
1416 * PHP 4.1.0+ as well as older versions. Returns an empty string
1417 * if nothing is found.
1421 function ServerVar($varName) {
1422 global $HTTP_SERVER_VARS;
1423 global $HTTP_ENV_VARS;
1425 if(!isset($_SERVER))
1427 $_SERVER = $HTTP_SERVER_VARS;
1428 if(!isset($_SERVER["REMOTE_ADDR"]))
1429 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1432 if(isset($_SERVER[$varName]))
1433 return $_SERVER[$varName];
1439 * Returns the server hostname or 'localhost.localdomain' if unknown.
1443 function ServerHostname() {
1444 if ($this->Hostname != "")
1445 $result = $this->Hostname;
1446 elseif ($this->ServerVar('SERVER_NAME') != "")
1447 $result = $this->ServerVar('SERVER_NAME');
1449 $result = "localhost.localdomain";
1455 * Returns a message in the appropriate language.
1459 function Lang($key) {
1460 if(count($this->language) < 1)
1461 $this->SetLanguage("en"); // set the default language
1463 if(isset($this->language[$key]))
1464 return $this->language[$key];
1466 return "Language string failed to load: " . $key;
1470 * Returns true if an error occurred.
1473 function IsError() {
1474 return ($this->error_count > 0);
1478 * Changes every end of line from CR or LF to CRLF.
1482 function FixEOL($str) {
1483 $str = str_replace("\r\n", "\n", $str);
1484 $str = str_replace("\r", "\n", $str);
1485 $str = str_replace("\n", $this->LE, $str);
1490 * Adds a custom header.
1493 function AddCustomHeader($custom_header) {
1494 $this->CustomHeader[] = explode(":", $custom_header, 2);