3 * PHPMailer - PHP email class
5 * Class for sending email using either sendmail, PHP mail(), or SMTP. Methods
6 * are based upon the standard AspEmail(tm) classes.
8 * @copyright 2001 - 2003 Brent R. Matzelle
14 * PHPMailer - PHP email transport class
16 * @author Brent R. Matzelle
17 * @copyright 2001 - 2003 Brent R. Matzelle
21 /////////////////////////////////////////////////
23 /////////////////////////////////////////////////
26 * Email priority (1 = High, 3 = Normal, 5 = low).
32 * Sets the CharSet of the message.
35 var $CharSet = "UTF-8";
38 * Sets the Content-type of the message.
41 var $ContentType = "text/plain";
44 * Sets the Encoding of the message. Options for this are "8bit",
45 * "7bit", "binary", "base64", and "quoted-printable".
48 var $Encoding = "8bit";
51 * Holds the most recent mailer error message.
57 * Sets the From email address for the message.
60 var $From = "localhost.localdomain";
63 * Sets the From name of the message.
66 var $FromName = "Support";
69 * Sets the Sender email (Return-Path) of the message. If not empty,
70 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
76 * Sets the Subject of the message.
82 * Sets the Body of the message. This can be either an HTML or text body.
83 * If HTML then run IsHTML(true).
89 * Sets the text-only body of the message. This automatically sets the
90 * email to multipart/alternative. This body can be read by mail
91 * clients that do not have HTML email capability such as mutt. Clients
92 * that can read HTML will view the normal Body.
98 * Sets word wrapping on the body of the message to a given number of
105 * Method to send mail: ("mail", "sendmail", or "smtp").
108 var $Mailer = "mail";
111 * Sets the path of the sendmail program.
114 var $Sendmail = "/usr/sbin/sendmail";
117 * Path to PHPMailer plugins. This is now only useful if the SMTP class
118 * is in a different directory than the PHP include path.
124 * Holds PHPMailer version.
127 var $Version = "1.73";
130 * Sets the email address that a reading confirmation will be sent.
133 var $ConfirmReadingTo = "";
136 * Sets the hostname to use in Message-Id and Received headers
137 * and as default HELO string. If empty, the value returned
138 * by SERVER_NAME is used or 'localhost.localdomain'.
143 /////////////////////////////////////////////////
145 /////////////////////////////////////////////////
148 * Sets the SMTP hosts. All hosts must be separated by a
149 * semicolon. You can also specify a different port
150 * for each host by using this format: [hostname:port]
151 * (e.g. "smtp1.example.com:25;smtp2.example.com").
152 * Hosts will be tried in order.
155 var $Host = "localhost";
158 * Sets the default SMTP server port.
164 * Sets the SMTP HELO of the message (Default is $Hostname).
170 * Sets SMTP authentication. Utilizes the Username and Password variables.
173 var $SMTPAuth = false;
176 * Sets SMTP username.
182 * Sets SMTP password.
188 * Sets the SMTP server timeout in seconds. This function will not
189 * work with the win32 version.
195 * Sets SMTP class debugging on or off.
198 var $SMTPDebug = false;
201 * Prevents the SMTP connection from being closed after each mail
202 * sending. If this is set to true then to close the connection
203 * requires an explicit call to SmtpClose().
206 var $SMTPKeepAlive = false;
215 var $ReplyTo = array();
216 var $attachment = array();
217 var $CustomHeader = array();
218 var $message_type = "";
219 var $boundary = array();
220 var $language = array();
221 var $error_count = 0;
225 /////////////////////////////////////////////////
227 /////////////////////////////////////////////////
230 * Sets message type to HTML.
234 function IsHTML($bool) {
236 $this->ContentType = "text/html";
238 $this->ContentType = "text/plain";
242 * Sets Mailer to send message using SMTP.
246 $this->Mailer = "smtp";
250 * Sets Mailer to send message using PHP mail() function.
254 $this->Mailer = "mail";
258 * Sets Mailer to send message using the $Sendmail program.
261 function IsSendmail() {
262 $this->Mailer = "sendmail";
266 * Sets Mailer to send message using the qmail MTA.
270 $this->Sendmail = "/var/qmail/bin/sendmail";
271 $this->Mailer = "sendmail";
275 /////////////////////////////////////////////////
277 /////////////////////////////////////////////////
280 * Adds a "To" address.
281 * @param string $address
282 * @param string $name
285 function AddAddress($address, $name = "") {
286 $cur = count($this->to);
287 $this->to[$cur][0] = trim($address);
288 $this->to[$cur][1] = $name;
292 * Adds a "Cc" address. Note: this function works
293 * with the SMTP mailer on win32, not with the "mail"
295 * @param string $address
296 * @param string $name
299 function AddCC($address, $name = "") {
300 $cur = count($this->cc);
301 $this->cc[$cur][0] = trim($address);
302 $this->cc[$cur][1] = $name;
306 * Adds a "Bcc" address. Note: this function works
307 * with the SMTP mailer on win32, not with the "mail"
309 * @param string $address
310 * @param string $name
313 function AddBCC($address, $name = "") {
314 $cur = count($this->bcc);
315 $this->bcc[$cur][0] = trim($address);
316 $this->bcc[$cur][1] = $name;
320 * Adds a "Reply-to" address.
321 * @param string $address
322 * @param string $name
325 function AddReplyTo($address, $name = "") {
326 $cur = count($this->ReplyTo);
327 $this->ReplyTo[$cur][0] = trim($address);
328 $this->ReplyTo[$cur][1] = $name;
332 /////////////////////////////////////////////////
333 // MAIL SENDING METHODS
334 /////////////////////////////////////////////////
337 * Creates message and assigns Mailer. If the message is
338 * not sent successfully then it returns false. Use the ErrorInfo
339 * variable to view description of the error.
347 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
349 $this->SetError($this->Lang("provide_address"));
353 // Set whether the message is multipart/alternative
354 if(!empty($this->AltBody))
355 $this->ContentType = "multipart/alternative";
357 $this->error_count = 0; // reset errors
358 $this->SetMessageType();
359 $header .= $this->CreateHeader();
360 $body = $this->CreateBody();
362 if($body == "") { return false; }
365 switch($this->Mailer)
368 $result = $this->SendmailSend($header, $body);
371 $result = $this->MailSend($header, $body);
374 $result = $this->SmtpSend($header, $body);
377 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
386 * Sends mail using the $Sendmail program.
390 function SendmailSend($header, $body) {
391 if ($this->Sender != "")
392 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, escapeshellarg($this->Sender));
394 $sendmail = sprintf("%s -oi -t", $this->Sendmail);
396 if(!@$mail = popen($sendmail, "w"))
398 $this->SetError($this->Lang("execute") . $this->Sendmail);
402 fputs($mail, $header);
405 $result = pclose($mail) >> 8 & 0xFF;
408 $this->SetError($this->Lang("execute") . $this->Sendmail);
416 * Sends mail using the PHP mail() function.
420 function MailSend($header, $body) {
422 for($i = 0; $i < count($this->to); $i++)
424 if($i != 0) { $to .= ", "; }
425 $to .= $this->to[$i][0];
428 if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
430 $old_from = ini_get("sendmail_from");
431 ini_set("sendmail_from", $this->Sender);
432 $params = sprintf("-oi -f %s", $this->Sender);
433 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body,
437 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
439 if (isset($old_from))
440 ini_set("sendmail_from", $old_from);
444 $this->SetError($this->Lang("instantiate"));
452 * Sends mail via SMTP using PhpSMTP (Author:
453 * Chris Ryan). Returns bool. Returns false if there is a
454 * bad MAIL FROM, RCPT, or DATA input.
458 function SmtpSend($header, $body) {
459 include_once($this->PluginDir . "class-smtp.php");
463 if(!$this->SmtpConnect())
466 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
467 if(!$this->smtp->Mail($smtp_from))
469 $error = $this->Lang("from_failed") . $smtp_from;
470 $this->SetError($error);
471 $this->smtp->Reset();
475 // Attempt to send attach all recipients
476 for($i = 0; $i < count($this->to); $i++)
478 if(!$this->smtp->Recipient($this->to[$i][0]))
479 $bad_rcpt[] = $this->to[$i][0];
481 for($i = 0; $i < count($this->cc); $i++)
483 if(!$this->smtp->Recipient($this->cc[$i][0]))
484 $bad_rcpt[] = $this->cc[$i][0];
486 for($i = 0; $i < count($this->bcc); $i++)
488 if(!$this->smtp->Recipient($this->bcc[$i][0]))
489 $bad_rcpt[] = $this->bcc[$i][0];
492 if(count($bad_rcpt) > 0) // Create error message
494 for($i = 0; $i < count($bad_rcpt); $i++)
496 if($i != 0) { $error .= ", "; }
497 $error .= $bad_rcpt[$i];
499 $error = $this->Lang("recipients_failed") . $error;
500 $this->SetError($error);
501 $this->smtp->Reset();
505 if(!$this->smtp->Data($header . $body))
507 $this->SetError($this->Lang("data_not_accepted"));
508 $this->smtp->Reset();
511 if($this->SMTPKeepAlive == true)
512 $this->smtp->Reset();
520 * Initiates a connection to an SMTP server. Returns false if the
525 function SmtpConnect() {
526 if($this->smtp == NULL) { $this->smtp = new SMTP(); }
528 $this->smtp->do_debug = $this->SMTPDebug;
529 $hosts = explode(";", $this->Host);
531 $connection = ($this->smtp->Connected());
533 // Retry while there is no connection
534 while($index < count($hosts) && $connection == false)
536 if(strstr($hosts[$index], ":"))
537 list($host, $port) = explode(":", $hosts[$index]);
540 $host = $hosts[$index];
544 if($this->smtp->Connect($host, $port, $this->Timeout))
546 if ($this->Helo != '')
547 $this->smtp->Hello($this->Helo);
549 $this->smtp->Hello($this->ServerHostname());
553 if(!$this->smtp->Authenticate($this->Username,
556 $this->SetError($this->Lang("authenticate"));
557 $this->smtp->Reset();
566 $this->SetError($this->Lang("connect_host"));
572 * Closes the active SMTP session if one exists.
575 function SmtpClose() {
576 if($this->smtp != NULL)
578 if($this->smtp->Connected())
581 $this->smtp->Close();
587 * Sets the language for all class error messages. Returns false
588 * if it cannot load the language file. The default language type
590 * @param string $lang_type Type of language (e.g. Portuguese: "br")
591 * @param string $lang_path Path to the language file directory
595 function SetLanguage($lang_type, $lang_path = "language/") {
596 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
597 include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
598 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
599 include($lang_path.'phpmailer.lang-en.php');
602 $this->SetError("Could not load language file");
605 $this->language = $PHPMAILER_LANG;
610 /////////////////////////////////////////////////
611 // MESSAGE CREATION METHODS
612 /////////////////////////////////////////////////
615 * Creates recipient headers.
619 function AddrAppend($type, $addr) {
620 $addr_str = $type . ": ";
621 $addr_str .= $this->AddrFormat($addr[0]);
624 for($i = 1; $i < count($addr); $i++)
625 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
627 $addr_str .= $this->LE;
633 * Formats an address correctly.
637 function AddrFormat($addr) {
639 $formatted = $addr[0];
642 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
650 * Wraps message for use with mailers that do not
651 * automatically perform wrapping and for quoted-printable.
652 * Original written by philippe.
656 function WrapText($message, $length, $qp_mode = false) {
657 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
659 $message = $this->FixEOL($message);
660 if (substr($message, -1) == $this->LE)
661 $message = substr($message, 0, -1);
663 $line = explode($this->LE, $message);
665 for ($i=0 ;$i < count($line); $i++)
667 $line_part = explode(" ", $line[$i]);
669 for ($e = 0; $e<count($line_part); $e++)
671 $word = $line_part[$e];
672 if ($qp_mode and (strlen($word) > $length))
674 $space_left = $length - strlen($buf) - 1;
677 if ($space_left > 20)
680 if (substr($word, $len - 1, 1) == "=")
682 elseif (substr($word, $len - 2, 1) == "=")
684 $part = substr($word, 0, $len);
685 $word = substr($word, $len);
687 $message .= $buf . sprintf("=%s", $this->LE);
691 $message .= $buf . $soft_break;
695 while (strlen($word) > 0)
698 if (substr($word, $len - 1, 1) == "=")
700 elseif (substr($word, $len - 2, 1) == "=")
702 $part = substr($word, 0, $len);
703 $word = substr($word, $len);
705 if (strlen($word) > 0)
706 $message .= $part . sprintf("=%s", $this->LE);
714 $buf .= ($e == 0) ? $word : (" " . $word);
716 if (strlen($buf) > $length and $buf_o != "")
718 $message .= $buf_o . $soft_break;
723 $message .= $buf . $this->LE;
730 * Set the body wrapping.
734 function SetWordWrap() {
735 if($this->WordWrap < 1)
738 switch($this->message_type)
742 case "alt_attachments":
743 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
746 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
752 * Assembles message header.
756 function CreateHeader() {
759 // Set the boundaries
760 $uniq_id = md5(uniqid(time()));
761 $this->boundary[1] = "b1_" . $uniq_id;
762 $this->boundary[2] = "b2_" . $uniq_id;
764 $result .= $this->HeaderLine("Date", $this->RFCDate());
765 if($this->Sender == "")
766 $result .= $this->HeaderLine("Return-Path", trim($this->From));
768 $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
770 // To be created automatically by mail()
771 if($this->Mailer != "mail")
773 if(count($this->to) > 0)
774 $result .= $this->AddrAppend("To", $this->to);
775 else if (count($this->cc) == 0)
776 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
777 if(count($this->cc) > 0)
778 $result .= $this->AddrAppend("Cc", $this->cc);
782 $from[0][0] = trim($this->From);
783 $from[0][1] = $this->FromName;
784 $result .= $this->AddrAppend("From", $from);
786 // sendmail and mail() extract Bcc from the header before sending
787 if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
788 $result .= $this->AddrAppend("Bcc", $this->bcc);
790 if(count($this->ReplyTo) > 0)
791 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
793 // mail() sets the subject itself
794 if($this->Mailer != "mail")
795 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
797 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
798 $result .= $this->HeaderLine("X-Priority", $this->Priority);
800 if($this->ConfirmReadingTo != "")
802 $result .= $this->HeaderLine("Disposition-Notification-To",
803 "<" . trim($this->ConfirmReadingTo) . ">");
806 // Add custom headers
807 for($index = 0; $index < count($this->CustomHeader); $index++)
809 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
810 $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
812 $result .= $this->HeaderLine("MIME-Version", "1.0");
814 switch($this->message_type)
817 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
818 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
819 $this->ContentType, $this->CharSet);
823 case "alt_attachments":
824 if($this->InlineImageExists())
826 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
827 "multipart/related", $this->LE, $this->LE,
828 $this->boundary[1], $this->LE);
832 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
833 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
837 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
838 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
842 if($this->Mailer != "mail")
843 $result .= $this->LE.$this->LE;
849 * Assembles the message body. Returns an empty string on failure.
853 function CreateBody() {
856 $this->SetWordWrap();
858 switch($this->message_type)
861 $result .= $this->GetBoundary($this->boundary[1], "",
863 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
864 $result .= $this->LE.$this->LE;
865 $result .= $this->GetBoundary($this->boundary[1], "",
868 $result .= $this->EncodeString($this->Body, $this->Encoding);
869 $result .= $this->LE.$this->LE;
871 $result .= $this->EndBoundary($this->boundary[1]);
874 $result .= $this->EncodeString($this->Body, $this->Encoding);
877 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
878 $result .= $this->EncodeString($this->Body, $this->Encoding);
879 $result .= $this->LE;
881 $result .= $this->AttachAll();
883 case "alt_attachments":
884 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
885 $result .= sprintf("Content-Type: %s;%s" .
886 "\tboundary=\"%s\"%s",
887 "multipart/alternative", $this->LE,
888 $this->boundary[2], $this->LE.$this->LE);
891 $result .= $this->GetBoundary($this->boundary[2], "",
892 "text/plain", "") . $this->LE;
894 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
895 $result .= $this->LE.$this->LE;
897 // Create the HTML body
898 $result .= $this->GetBoundary($this->boundary[2], "",
899 "text/html", "") . $this->LE;
901 $result .= $this->EncodeString($this->Body, $this->Encoding);
902 $result .= $this->LE.$this->LE;
904 $result .= $this->EndBoundary($this->boundary[2]);
906 $result .= $this->AttachAll();
916 * Returns the start of a message boundary.
919 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
921 if($charSet == "") { $charSet = $this->CharSet; }
922 if($contentType == "") { $contentType = $this->ContentType; }
923 if($encoding == "") { $encoding = $this->Encoding; }
925 $result .= $this->TextLine("--" . $boundary);
926 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
927 $contentType, $charSet);
928 $result .= $this->LE;
929 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
930 $result .= $this->LE;
936 * Returns the end of a message boundary.
939 function EndBoundary($boundary) {
940 return $this->LE . "--" . $boundary . "--" . $this->LE;
944 * Sets the message type.
948 function SetMessageType() {
949 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
950 $this->message_type = "plain";
953 if(count($this->attachment) > 0)
954 $this->message_type = "attachments";
955 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
956 $this->message_type = "alt";
957 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
958 $this->message_type = "alt_attachments";
963 * Returns a formatted header line.
967 function HeaderLine($name, $value) {
968 return $name . ": " . $value . $this->LE;
972 * Returns a formatted mail line.
976 function TextLine($value) {
977 return $value . $this->LE;
980 /////////////////////////////////////////////////
981 // ATTACHMENT METHODS
982 /////////////////////////////////////////////////
985 * Adds an attachment from a path on the filesystem.
986 * Returns false if the file could not be found
988 * @param string $path Path to the attachment.
989 * @param string $name Overrides the attachment name.
990 * @param string $encoding File encoding (see $Encoding).
991 * @param string $type File extension (MIME) type.
994 function AddAttachment($path, $name = "", $encoding = "base64",
995 $type = "application/octet-stream") {
998 $this->SetError($this->Lang("file_access") . $path);
1002 $filename = basename($path);
1006 $cur = count($this->attachment);
1007 $this->attachment[$cur][0] = $path;
1008 $this->attachment[$cur][1] = $filename;
1009 $this->attachment[$cur][2] = $name;
1010 $this->attachment[$cur][3] = $encoding;
1011 $this->attachment[$cur][4] = $type;
1012 $this->attachment[$cur][5] = false; // isStringAttachment
1013 $this->attachment[$cur][6] = "attachment";
1014 $this->attachment[$cur][7] = 0;
1020 * Attaches all fs, string, and binary attachments to the message.
1021 * Returns an empty string on failure.
1025 function AttachAll() {
1026 // Return text of body
1029 // Add all attachments
1030 for($i = 0; $i < count($this->attachment); $i++)
1032 // Check for string attachment
1033 $bString = $this->attachment[$i][5];
1035 $string = $this->attachment[$i][0];
1037 $path = $this->attachment[$i][0];
1039 $filename = $this->attachment[$i][1];
1040 $name = $this->attachment[$i][2];
1041 $encoding = $this->attachment[$i][3];
1042 $type = $this->attachment[$i][4];
1043 $disposition = $this->attachment[$i][6];
1044 $cid = $this->attachment[$i][7];
1046 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
1047 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
1048 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
1050 if($disposition == "inline")
1051 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
1053 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1054 $disposition, $name, $this->LE.$this->LE);
1056 // Encode as string attachment
1059 $mime[] = $this->EncodeString($string, $encoding);
1060 if($this->IsError()) { return ""; }
1061 $mime[] = $this->LE.$this->LE;
1065 $mime[] = $this->EncodeFile($path, $encoding);
1066 if($this->IsError()) { return ""; }
1067 $mime[] = $this->LE.$this->LE;
1071 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
1073 return join("", $mime);
1077 * Encodes attachment in requested format. Returns an
1078 * empty string on failure.
1082 function EncodeFile ($path, $encoding = "base64") {
1083 if(!@$fd = fopen($path, "rb"))
1085 $this->SetError($this->Lang("file_open") . $path);
1088 $magic_quotes = get_magic_quotes_runtime();
1089 set_magic_quotes_runtime(0);
1090 $file_buffer = fread($fd, filesize($path));
1091 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1093 set_magic_quotes_runtime($magic_quotes);
1095 return $file_buffer;
1099 * Encodes string to requested format. Returns an
1100 * empty string on failure.
1104 function EncodeString ($str, $encoding = "base64") {
1106 switch(strtolower($encoding)) {
1108 // chunk_split is found in PHP >= 3.0.6
1109 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
1113 $encoded = $this->FixEOL($str);
1114 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1115 $encoded .= $this->LE;
1120 case "quoted-printable":
1121 $encoded = $this->EncodeQP($str);
1124 $this->SetError($this->Lang("encoding") . $encoding);
1131 * Encode a header string to best of Q, B, quoted or none.
1135 function EncodeHeader ($str, $position = 'text') {
1138 switch (strtolower($position)) {
1140 if (!preg_match('/[\200-\377]/', $str)) {
1141 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1142 $encoded = addcslashes($str, "\0..\37\177\\\"");
1144 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1147 return ("\"$encoded\"");
1149 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1152 $x = preg_match_all('/[()"]/', $str, $matches);
1156 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1163 $maxlen = 75 - 7 - strlen($this->CharSet);
1164 // Try to select the encoding which should produce the shortest output
1165 if (strlen($str)/3 < $x) {
1167 $encoded = base64_encode($str);
1168 $maxlen -= $maxlen % 4;
1169 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1172 $encoded = $this->EncodeQ($str, $position);
1173 $encoded = $this->WrapText($encoded, $maxlen, true);
1174 $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
1177 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
1178 $encoded = trim(str_replace("\n", $this->LE, $encoded));
1184 * Encode string to quoted-printable.
1188 function EncodeQP ($str) {
1189 $encoded = $this->FixEOL($str);
1190 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1191 $encoded .= $this->LE;
1193 // Replace every high ascii, control and = characters
1194 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
1195 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1196 // Replace every spaces and tabs when it's the last character on a line
1197 $encoded = preg_replace("/([\011\040])".$this->LE."/e",
1198 "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
1200 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1201 $encoded = $this->WrapText($encoded, 74, true);
1207 * Encode string to q encoding.
1211 function EncodeQ ($str, $position = "text") {
1212 // There should not be any EOL in the string
1213 $encoded = preg_replace("[\r\n]", "", $str);
1215 switch (strtolower($position)) {
1217 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1220 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1223 // Replace every high ascii, control =, ? and _ characters
1224 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1225 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1229 // Replace every spaces to _ (more readable than =20)
1230 $encoded = str_replace(" ", "_", $encoded);
1236 * Adds a string or binary attachment (non-filesystem) to the list.
1237 * This method can be used to attach ascii or binary data,
1238 * such as a BLOB record from a database.
1239 * @param string $string String attachment data.
1240 * @param string $filename Name of the attachment.
1241 * @param string $encoding File encoding (see $Encoding).
1242 * @param string $type File extension (MIME) type.
1245 function AddStringAttachment($string, $filename, $encoding = "base64",
1246 $type = "application/octet-stream") {
1247 // Append to $attachment array
1248 $cur = count($this->attachment);
1249 $this->attachment[$cur][0] = $string;
1250 $this->attachment[$cur][1] = $filename;
1251 $this->attachment[$cur][2] = $filename;
1252 $this->attachment[$cur][3] = $encoding;
1253 $this->attachment[$cur][4] = $type;
1254 $this->attachment[$cur][5] = true; // isString
1255 $this->attachment[$cur][6] = "attachment";
1256 $this->attachment[$cur][7] = 0;
1260 * Adds an embedded attachment. This can include images, sounds, and
1261 * just about any other document. Make sure to set the $type to an
1262 * image type. For JPEG images use "image/jpeg" and for GIF images
1264 * @param string $path Path to the attachment.
1265 * @param string $cid Content ID of the attachment. Use this to identify
1266 * the Id for accessing the image in an HTML form.
1267 * @param string $name Overrides the attachment name.
1268 * @param string $encoding File encoding (see $Encoding).
1269 * @param string $type File extension (MIME) type.
1272 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1273 $type = "application/octet-stream") {
1275 if(!@is_file($path))
1277 $this->SetError($this->Lang("file_access") . $path);
1281 $filename = basename($path);
1285 // Append to $attachment array
1286 $cur = count($this->attachment);
1287 $this->attachment[$cur][0] = $path;
1288 $this->attachment[$cur][1] = $filename;
1289 $this->attachment[$cur][2] = $name;
1290 $this->attachment[$cur][3] = $encoding;
1291 $this->attachment[$cur][4] = $type;
1292 $this->attachment[$cur][5] = false; // isStringAttachment
1293 $this->attachment[$cur][6] = "inline";
1294 $this->attachment[$cur][7] = $cid;
1300 * Returns true if an inline attachment is present.
1304 function InlineImageExists() {
1306 for($i = 0; $i < count($this->attachment); $i++)
1308 if($this->attachment[$i][6] == "inline")
1318 /////////////////////////////////////////////////
1319 // MESSAGE RESET METHODS
1320 /////////////////////////////////////////////////
1323 * Clears all recipients assigned in the TO array. Returns void.
1326 function ClearAddresses() {
1327 $this->to = array();
1331 * Clears all recipients assigned in the CC array. Returns void.
1334 function ClearCCs() {
1335 $this->cc = array();
1339 * Clears all recipients assigned in the BCC array. Returns void.
1342 function ClearBCCs() {
1343 $this->bcc = array();
1347 * Clears all recipients assigned in the ReplyTo array. Returns void.
1350 function ClearReplyTos() {
1351 $this->ReplyTo = array();
1355 * Clears all recipients assigned in the TO, CC and BCC
1356 * array. Returns void.
1359 function ClearAllRecipients() {
1360 $this->to = array();
1361 $this->cc = array();
1362 $this->bcc = array();
1366 * Clears all previously set filesystem, string, and binary
1367 * attachments. Returns void.
1370 function ClearAttachments() {
1371 $this->attachment = array();
1375 * Clears all custom headers. Returns void.
1378 function ClearCustomHeaders() {
1379 $this->CustomHeader = array();
1383 /////////////////////////////////////////////////
1384 // MISCELLANEOUS METHODS
1385 /////////////////////////////////////////////////
1388 * Adds the error message to the error container.
1393 function SetError($msg) {
1394 $this->error_count++;
1395 $this->ErrorInfo = $msg;
1399 * Returns the proper RFC 822 formatted date.
1403 function RFCDate() {
1405 $tzs = ($tz < 0) ? "-" : "+";
1407 $tz = ($tz/3600)*100 + ($tz%3600)/60;
1408 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1414 * Returns the appropriate server variable. Should work with both
1415 * PHP 4.1.0+ as well as older versions. Returns an empty string
1416 * if nothing is found.
1420 function ServerVar($varName) {
1421 global $HTTP_SERVER_VARS;
1422 global $HTTP_ENV_VARS;
1424 if(!isset($_SERVER))
1426 $_SERVER = $HTTP_SERVER_VARS;
1427 if(!isset($_SERVER["REMOTE_ADDR"]))
1428 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1431 if(isset($_SERVER[$varName]))
1432 return $_SERVER[$varName];
1438 * Returns the server hostname or 'localhost.localdomain' if unknown.
1442 function ServerHostname() {
1443 if ($this->Hostname != "")
1444 $result = $this->Hostname;
1445 elseif ($this->ServerVar('SERVER_NAME') != "")
1446 $result = $this->ServerVar('SERVER_NAME');
1448 $result = "localhost.localdomain";
1454 * Returns a message in the appropriate language.
1458 function Lang($key) {
1459 if(count($this->language) < 1)
1460 $this->SetLanguage("en"); // set the default language
1462 if(isset($this->language[$key]))
1463 return $this->language[$key];
1465 return "Language string failed to load: " . $key;
1469 * Returns true if an error occurred.
1472 function IsError() {
1473 return ($this->error_count > 0);
1477 * Changes every end of line from CR or LF to CRLF.
1481 function FixEOL($str) {
1482 $str = str_replace("\r\n", "\n", $str);
1483 $str = str_replace("\r", "\n", $str);
1484 $str = str_replace("\n", $this->LE, $str);
1489 * Adds a custom header.
1492 function AddCustomHeader($custom_header) {
1493 $this->CustomHeader[] = explode(":", $custom_header, 2);