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 = "iso-8859-1";
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 = "root@localhost";
64 * Sets the From name of the message.
67 var $FromName = "Root User";
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.71";
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'.
145 /////////////////////////////////////////////////
147 /////////////////////////////////////////////////
150 * Sets the SMTP hosts. All hosts must be separated by a
151 * semicolon. You can also specify a different port
152 * for each host by using this format: [hostname:port]
153 * (e.g. "smtp1.example.com:25;smtp2.example.com").
154 * Hosts will be tried in order.
157 var $Host = "localhost";
160 * Sets the default SMTP server port.
166 * Sets the SMTP HELO of the message (Default is $Hostname).
172 * Sets SMTP authentication. Utilizes the Username and Password variables.
175 var $SMTPAuth = false;
178 * Sets SMTP username.
184 * Sets SMTP password.
190 * Sets the SMTP server timeout in seconds. This function will not
191 * work with the win32 version.
197 * Sets SMTP class debugging on or off.
200 var $SMTPDebug = false;
203 * Prevents the SMTP connection from being closed after each mail
204 * sending. If this is set to true then to close the connection
205 * requires an explicit call to SmtpClose().
208 var $SMTPKeepAlive = false;
217 var $ReplyTo = array();
218 var $attachment = array();
219 var $CustomHeader = array();
220 var $message_type = "";
221 var $boundary = array();
222 var $language = array();
223 var $error_count = 0;
227 /////////////////////////////////////////////////
229 /////////////////////////////////////////////////
232 * Sets message type to HTML.
236 function IsHTML($bool) {
238 $this->ContentType
= "text/html";
240 $this->ContentType
= "text/plain";
244 * Sets Mailer to send message using SMTP.
248 $this->Mailer
= "smtp";
252 * Sets Mailer to send message using PHP mail() function.
256 $this->Mailer
= "mail";
260 * Sets Mailer to send message using the $Sendmail program.
263 function IsSendmail() {
264 $this->Mailer
= "sendmail";
268 * Sets Mailer to send message using the qmail MTA.
272 $this->Sendmail
= "/var/qmail/bin/sendmail";
273 $this->Mailer
= "sendmail";
277 /////////////////////////////////////////////////
279 /////////////////////////////////////////////////
282 * Adds a "To" address.
283 * @param string $address
284 * @param string $name
287 function AddAddress($address, $name = "") {
288 $cur = count($this->to
);
289 $this->to
[$cur][0] = trim($address);
290 $this->to
[$cur][1] = $name;
294 * Adds a "Cc" address. Note: this function works
295 * with the SMTP mailer on win32, not with the "mail"
297 * @param string $address
298 * @param string $name
301 function AddCC($address, $name = "") {
302 $cur = count($this->cc
);
303 $this->cc
[$cur][0] = trim($address);
304 $this->cc
[$cur][1] = $name;
308 * Adds a "Bcc" address. Note: this function works
309 * with the SMTP mailer on win32, not with the "mail"
311 * @param string $address
312 * @param string $name
315 function AddBCC($address, $name = "") {
316 $cur = count($this->bcc
);
317 $this->bcc
[$cur][0] = trim($address);
318 $this->bcc
[$cur][1] = $name;
322 * Adds a "Reply-to" address.
323 * @param string $address
324 * @param string $name
327 function AddReplyTo($address, $name = "") {
328 $cur = count($this->ReplyTo
);
329 $this->ReplyTo
[$cur][0] = trim($address);
330 $this->ReplyTo
[$cur][1] = $name;
334 /////////////////////////////////////////////////
335 // MAIL SENDING METHODS
336 /////////////////////////////////////////////////
339 * Creates message and assigns Mailer. If the message is
340 * not sent successfully then it returns false. Use the ErrorInfo
341 * 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->SetMessageType();
359 $header .= $this->CreateHeader();
360 $body = $this->CreateBody();
362 if($body == "") { return false; }
365 if($this->Mailer
== "sendmail")
367 if(!$this->SendmailSend($header, $body))
370 elseif($this->Mailer
== "mail")
372 if(!$this->MailSend($header, $body))
375 elseif($this->Mailer
== "smtp")
377 if(!$this->SmtpSend($header, $body))
382 $this->SetError($this->Mailer
. $this->Lang("mailer_not_supported"));
390 * Sends mail using the $Sendmail program.
394 function SendmailSend($header, $body) {
395 if ($this->Sender
!= "")
396 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail
, $this->Sender
);
398 $sendmail = sprintf("%s -oi -t", $this->Sendmail
);
400 if(!@$mail = popen($sendmail, "w"))
402 $this->SetError($this->Lang("execute") . $this->Sendmail
);
406 fputs($mail, $header);
409 $result = pclose($mail) >> 8 & 0xFF;
412 $this->SetError($this->Lang("execute") . $this->Sendmail
);
420 * Sends mail using the PHP mail() function.
424 function MailSend($header, $body) {
426 for($i = 0; $i < count($this->to
); $i++
)
428 if($i != 0) { $to .= ", "; }
429 $to .= $this->to
[$i][0];
432 if ($this->Sender
!= "" && strlen(ini_get("safe_mode"))< 1)
434 $old_from = ini_get("sendmail_from");
435 ini_set("sendmail_from", $this->Sender
);
436 $params = sprintf("-oi -f %s", $this->Sender
);
437 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body,
441 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body, $header);
443 if (isset($old_from))
444 ini_set("sendmail_from", $old_from);
448 $this->SetError($this->Lang("instantiate"));
456 * Sends mail via SMTP using PhpSMTP (Author:
457 * Chris Ryan). Returns bool. Returns false if there is a
458 * bad MAIL FROM, RCPT, or DATA input.
462 function SmtpSend($header, $body) {
463 require_once($this->PluginDir
. "class.smtp.php");
467 if(!$this->SmtpConnect())
470 $smtp_from = ($this->Sender
== "") ?
$this->From
: $this->Sender
;
471 if(!$this->smtp
->Mail($smtp_from))
473 $error = $this->Lang("from_failed") . $smtp_from;
474 $this->SetError($error);
475 $this->smtp
->Reset();
479 // Attempt to send attach all recipients
480 for($i = 0; $i < count($this->to
); $i++
)
482 if(!$this->smtp
->Recipient($this->to
[$i][0]))
483 $bad_rcpt[] = $this->to
[$i][0];
485 for($i = 0; $i < count($this->cc
); $i++
)
487 if(!$this->smtp
->Recipient($this->cc
[$i][0]))
488 $bad_rcpt[] = $this->cc
[$i][0];
490 for($i = 0; $i < count($this->bcc
); $i++
)
492 if(!$this->smtp
->Recipient($this->bcc
[$i][0]))
493 $bad_rcpt[] = $this->bcc
[$i][0];
496 if(count($bad_rcpt) > 0) // Create error message
498 for($i = 0; $i < count($bad_rcpt); $i++
)
500 if($i != 0) { $error .= ", "; }
501 $error .= $bad_rcpt[$i];
503 $error = $this->Lang("recipients_failed") . $error;
504 $this->SetError($error);
505 $this->smtp
->Reset();
509 if(!$this->smtp
->Data($header . $body))
511 $this->SetError($this->Lang("data_not_accepted"));
512 $this->smtp
->Reset();
515 if($this->SMTPKeepAlive
== true)
516 $this->smtp
->Reset();
524 * Initiates a connection to an SMTP server. Returns false if the
529 function SmtpConnect() {
530 if($this->smtp
== NULL) { $this->smtp
= new SMTP(); }
532 $this->smtp
->do_debug
= $this->SMTPDebug
;
533 $hosts = explode(";", $this->Host
);
535 $connection = ($this->smtp
->Connected());
537 // Retry while there is no connection
538 while($index < count($hosts) && $connection == false)
540 if(strstr($hosts[$index], ":"))
541 list($host, $port) = explode(":", $hosts[$index]);
544 $host = $hosts[$index];
548 if($this->smtp
->Connect($host, $port, $this->Timeout
))
550 if ($this->Helo
!= '')
551 $this->smtp
->Hello($this->Helo
);
553 $this->smtp
->Hello($this->ServerHostname());
557 if(!$this->smtp
->Authenticate($this->Username
,
560 $this->SetError($this->Lang("authenticate"));
561 $this->smtp
->Reset();
570 $this->SetError($this->Lang("connect_host"));
576 * Closes the active SMTP session if one exists.
579 function SmtpClose() {
580 if($this->smtp
!= NULL)
582 if($this->smtp
->Connected())
585 $this->smtp
->Close();
591 * Sets the language for all class error messages. Returns false
592 * if it cannot load the language file. The default language type
594 * @param string $lang_type Type of language (e.g. Portuguese: "br")
595 * @param string $lang_path Path to the language file directory
599 function SetLanguage($lang_type, $lang_path = "") {
600 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
601 include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
602 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
603 include($lang_path.'phpmailer.lang-en.php');
606 $this->SetError("Could not load language file");
609 $this->language
= $PHPMAILER_LANG;
614 /////////////////////////////////////////////////
615 // MESSAGE CREATION METHODS
616 /////////////////////////////////////////////////
619 * Creates recipient headers.
623 function AddrAppend($type, $addr) {
624 $addr_str = $type . ": ";
625 $addr_str .= $this->AddrFormat($addr[0]);
628 for($i = 1; $i < count($addr); $i++
)
629 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
631 $addr_str .= $this->LE
;
637 * Formats an address correctly.
641 function AddrFormat($addr) {
643 $formatted = $addr[0];
646 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
654 * Wraps message for use with mailers that do not
655 * automatically perform wrapping and for quoted-printable.
656 * Original written by philippe.
660 function WrapText($message, $length, $qp_mode = false) {
661 $soft_break = ($qp_mode) ?
sprintf(" =%s", $this->LE
) : $this->LE
;
663 $message = $this->FixEOL($message);
664 if (substr($message, -1) == $this->LE
)
665 $message = substr($message, 0, -1);
667 $line = explode($this->LE
, $message);
669 for ($i=0 ;$i < count($line); $i++
)
671 $line_part = explode(" ", $line[$i]);
673 for ($e = 0; $e<count($line_part); $e++
)
675 $word = $line_part[$e];
676 if ($qp_mode and (strlen($word) > $length))
678 $space_left = $length - strlen($buf) - 1;
681 if ($space_left > 20)
684 if (substr($word, $len - 1, 1) == "=")
686 elseif (substr($word, $len - 2, 1) == "=")
688 $part = substr($word, 0, $len);
689 $word = substr($word, $len);
691 $message .= $buf . sprintf("=%s", $this->LE
);
695 $message .= $buf . $soft_break;
699 while (strlen($word) > 0)
702 if (substr($word, $len - 1, 1) == "=")
704 elseif (substr($word, $len - 2, 1) == "=")
706 $part = substr($word, 0, $len);
707 $word = substr($word, $len);
709 if (strlen($word) > 0)
710 $message .= $part . sprintf("=%s", $this->LE
);
718 $buf .= ($e == 0) ?
$word : (" " . $word);
720 if (strlen($buf) > $length and $buf_o != "")
722 $message .= $buf_o . $soft_break;
727 $message .= $buf . $this->LE
;
734 * Set the body wrapping.
738 function SetWordWrap() {
739 if($this->WordWrap
< 1)
742 switch($this->message_type
)
746 case "alt_attachment":
747 $this->AltBody
= $this->WrapText($this->AltBody
, $this->WordWrap
);
750 $this->Body
= $this->WrapText($this->Body
, $this->WordWrap
);
756 * Assembles message header.
760 function CreateHeader() {
763 // Set the boundaries
764 $uniq_id = md5(uniqid(time()));
765 $this->boundary
[1] = "b1_" . $uniq_id;
766 $this->boundary
[2] = "b2_" . $uniq_id;
768 $result .= $this->Received();
769 $result .= $this->HeaderLine("Date", $this->RFCDate());
770 if($this->Sender
== "")
771 $result .= $this->HeaderLine("Return-Path", trim($this->From
));
773 $result .= $this->HeaderLine("Return-Path", trim($this->Sender
));
775 // To be created automatically by mail()
776 if($this->Mailer
!= "mail")
778 if(count($this->to
) > 0)
779 $result .= $this->AddrAppend("To", $this->to
);
780 else if (count($this->cc
) == 0)
781 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
782 if(count($this->cc
) > 0)
783 $result .= $this->AddrAppend("Cc", $this->cc
);
787 $from[0][0] = trim($this->From
);
788 $from[0][1] = $this->FromName
;
789 $result .= $this->AddrAppend("From", $from);
791 // sendmail and mail() extract Bcc from the header before sending
792 if((($this->Mailer
== "sendmail") ||
($this->Mailer
== "mail")) && (count($this->bcc
) > 0))
793 $result .= $this->AddrAppend("Bcc", $this->bcc
);
795 if(count($this->ReplyTo
) > 0)
796 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo
);
798 // mail() sets the subject itself
799 if($this->Mailer
!= "mail")
800 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject
)));
802 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE
);
803 $result .= $this->HeaderLine("X-Priority", $this->Priority
);
804 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version
. "]");
806 if($this->ConfirmReadingTo
!= "")
808 $result .= $this->HeaderLine("Disposition-Notification-To",
809 "<" . trim($this->ConfirmReadingTo
) . ">");
812 // Add custom headers
813 for($index = 0; $index < count($this->CustomHeader
); $index++
)
815 $result .= $this->HeaderLine(trim($this->CustomHeader
[$index][0]),
816 $this->EncodeHeader(trim($this->CustomHeader
[$index][1])));
818 $result .= $this->HeaderLine("MIME-Version", "1.0");
820 switch($this->message_type
)
823 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding
);
824 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
825 $this->ContentType
, $this->CharSet
);
829 case "alt_attachments":
830 if($this->InlineImageExists())
832 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
833 "multipart/related", $this->LE
, $this->LE
,
834 $this->boundary
[1], $this->LE
);
838 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
839 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
843 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
844 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
848 if($this->Mailer
!= "mail")
849 $result .= $this->LE
.$this->LE
;
855 * Assembles the message body. Returns an empty string on failure.
859 function CreateBody() {
862 $this->SetWordWrap();
864 switch($this->message_type
)
867 $result .= $this->GetBoundary($this->boundary
[1], "",
869 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
870 $result .= $this->LE
.$this->LE
;
871 $result .= $this->GetBoundary($this->boundary
[1], "",
874 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
875 $result .= $this->LE
.$this->LE
;
877 $result .= $this->EndBoundary($this->boundary
[1]);
880 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
883 $result .= $this->GetBoundary($this->boundary
[1], "", "", "");
884 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
885 $result .= $this->LE
;
887 $result .= $this->AttachAll();
889 case "alt_attachments":
890 $result .= sprintf("--%s%s", $this->boundary
[1], $this->LE
);
891 $result .= sprintf("Content-Type: %s;%s" .
892 "\tboundary=\"%s\"%s",
893 "multipart/alternative", $this->LE
,
894 $this->boundary
[2], $this->LE
.$this->LE
);
897 $result .= $this->GetBoundary($this->boundary
[2], "",
898 "text/plain", "") . $this->LE
;
900 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
901 $result .= $this->LE
.$this->LE
;
903 // Create the HTML body
904 $result .= $this->GetBoundary($this->boundary
[2], "",
905 "text/html", "") . $this->LE
;
907 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
908 $result .= $this->LE
.$this->LE
;
910 $result .= $this->EndBoundary($this->boundary
[2]);
912 $result .= $this->AttachAll();
922 * Returns the start of a message boundary.
925 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
927 if($charSet == "") { $charSet = $this->CharSet
; }
928 if($contentType == "") { $contentType = $this->ContentType
; }
929 if($encoding == "") { $encoding = $this->Encoding
; }
931 $result .= $this->TextLine("--" . $boundary);
932 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
933 $contentType, $charSet);
934 $result .= $this->LE
;
935 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
936 $result .= $this->LE
;
942 * Returns the end of a message boundary.
945 function EndBoundary($boundary) {
946 return $this->LE
. "--" . $boundary . "--" . $this->LE
;
950 * Sets the message type.
954 function SetMessageType() {
955 if(count($this->attachment
) < 1 && strlen($this->AltBody
) < 1)
956 $this->message_type
= "plain";
959 if(count($this->attachment
) > 0)
960 $this->message_type
= "attachments";
961 if(strlen($this->AltBody
) > 0 && count($this->attachment
) < 1)
962 $this->message_type
= "alt";
963 if(strlen($this->AltBody
) > 0 && count($this->attachment
) > 0)
964 $this->message_type
= "alt_attachments";
969 * Returns a formatted header line.
973 function HeaderLine($name, $value) {
974 return $name . ": " . $value . $this->LE
;
978 * Returns a formatted mail line.
982 function TextLine($value) {
983 return $value . $this->LE
;
986 /////////////////////////////////////////////////
987 // ATTACHMENT METHODS
988 /////////////////////////////////////////////////
991 * Adds an attachment from a path on the filesystem.
992 * Returns false if the file could not be found
994 * @param string $path Path to the attachment.
995 * @param string $name Overrides the attachment name.
996 * @param string $encoding File encoding (see $Encoding).
997 * @param string $type File extension (MIME) type.
1000 function AddAttachment($path, $name = "", $encoding = "base64",
1001 $type = "application/octet-stream") {
1002 if(!@is_file
($path))
1004 $this->SetError($this->Lang("file_access") . $path);
1008 $filename = basename($path);
1012 $cur = count($this->attachment
);
1013 $this->attachment
[$cur][0] = $path;
1014 $this->attachment
[$cur][1] = $filename;
1015 $this->attachment
[$cur][2] = $name;
1016 $this->attachment
[$cur][3] = $encoding;
1017 $this->attachment
[$cur][4] = $type;
1018 $this->attachment
[$cur][5] = false; // isStringAttachment
1019 $this->attachment
[$cur][6] = "attachment";
1020 $this->attachment
[$cur][7] = 0;
1026 * Attaches all fs, string, and binary attachments to the message.
1027 * Returns an empty string on failure.
1031 function AttachAll() {
1032 // Return text of body
1035 // Add all attachments
1036 for($i = 0; $i < count($this->attachment
); $i++
)
1038 // Check for string attachment
1039 $bString = $this->attachment
[$i][5];
1041 $string = $this->attachment
[$i][0];
1043 $path = $this->attachment
[$i][0];
1045 $filename = $this->attachment
[$i][1];
1046 $name = $this->attachment
[$i][2];
1047 $encoding = $this->attachment
[$i][3];
1048 $type = $this->attachment
[$i][4];
1049 $disposition = $this->attachment
[$i][6];
1050 $cid = $this->attachment
[$i][7];
1052 $mime[] = sprintf("--%s%s", $this->boundary
[1], $this->LE
);
1053 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE
);
1054 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE
);
1056 if($disposition == "inline")
1057 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE
);
1059 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1060 $disposition, $name, $this->LE
.$this->LE
);
1062 // Encode as string attachment
1065 $mime[] = $this->EncodeString($string, $encoding);
1066 if($this->IsError()) { return ""; }
1067 $mime[] = $this->LE
.$this->LE
;
1071 $mime[] = $this->EncodeFile($path, $encoding);
1072 if($this->IsError()) { return ""; }
1073 $mime[] = $this->LE
.$this->LE
;
1077 $mime[] = sprintf("--%s--%s", $this->boundary
[1], $this->LE
);
1079 return join("", $mime);
1083 * Encodes attachment in requested format. Returns an
1084 * empty string on failure.
1088 function EncodeFile ($path, $encoding = "base64") {
1089 if(!@$fd = fopen($path, "rb"))
1091 $this->SetError($this->Lang("file_open") . $path);
1094 $file_buffer = fread($fd, filesize($path));
1095 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1098 return $file_buffer;
1102 * Encodes string to requested format. Returns an
1103 * empty string on failure.
1107 function EncodeString ($str, $encoding = "base64") {
1109 switch(strtolower($encoding)) {
1111 // chunk_split is found in PHP >= 3.0.6
1112 $encoded = chunk_split(base64_encode($str), 76, $this->LE
);
1116 $encoded = $this->FixEOL($str);
1117 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1118 $encoded .= $this->LE
;
1123 case "quoted-printable":
1124 $encoded = $this->EncodeQP($str);
1127 $this->SetError($this->Lang("encoding") . $encoding);
1134 * Encode a header string to best of Q, B, quoted or none.
1138 function EncodeHeader ($str, $position = 'text') {
1141 switch (strtolower($position)) {
1143 if (!preg_match('/[\200-\377]/', $str)) {
1144 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1145 $encoded = addcslashes($str, "\0..\37\177\\\"");
1147 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1150 return ("\"$encoded\"");
1152 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1155 $x = preg_match_all('/[()"]/', $str, $matches);
1159 $x +
= preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1166 $maxlen = 75 - 7 - strlen($this->CharSet
);
1167 // Try to select the encoding which should produce the shortest output
1168 if (strlen($str)/3 < $x) {
1170 $encoded = base64_encode($str);
1171 $maxlen -= $maxlen %
4;
1172 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1175 $encoded = $this->EncodeQ($str, $position);
1176 $encoded = $this->WrapText($encoded, $maxlen, true);
1177 $encoded = str_replace("=".$this->LE
, "\n", trim($encoded));
1180 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet
."?$encoding?\\1?=", $encoded);
1181 $encoded = trim(str_replace("\n", $this->LE
, $encoded));
1187 * Encode string to quoted-printable.
1191 function EncodeQP ($str) {
1192 $encoded = $this->FixEOL($str);
1193 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1194 $encoded .= $this->LE
;
1196 // Replace every high ascii, control and = characters
1197 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
1198 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1199 // Replace every spaces and tabs when it's the last character on a line
1200 $encoded = preg_replace("/([\011\040])".$this->LE
."/e",
1201 "'='.sprintf('%02X', ord('\\1')).'".$this->LE
."'", $encoded);
1203 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1204 $encoded = $this->WrapText($encoded, 74, true);
1210 * Encode string to q encoding.
1214 function EncodeQ ($str, $position = "text") {
1215 // There should not be any EOL in the string
1216 $encoded = preg_replace("[\r\n]", "", $str);
1218 switch (strtolower($position)) {
1220 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1223 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1226 // Replace every high ascii, control =, ? and _ characters
1227 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1228 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1232 // Replace every spaces to _ (more readable than =20)
1233 $encoded = str_replace(" ", "_", $encoded);
1239 * Adds a string or binary attachment (non-filesystem) to the list.
1240 * This method can be used to attach ascii or binary data,
1241 * such as a BLOB record from a database.
1242 * @param string $string String attachment data.
1243 * @param string $filename Name of the attachment.
1244 * @param string $encoding File encoding (see $Encoding).
1245 * @param string $type File extension (MIME) type.
1248 function AddStringAttachment($string, $filename, $encoding = "base64",
1249 $type = "application/octet-stream") {
1250 // Append to $attachment array
1251 $cur = count($this->attachment
);
1252 $this->attachment
[$cur][0] = $string;
1253 $this->attachment
[$cur][1] = $filename;
1254 $this->attachment
[$cur][2] = $filename;
1255 $this->attachment
[$cur][3] = $encoding;
1256 $this->attachment
[$cur][4] = $type;
1257 $this->attachment
[$cur][5] = true; // isString
1258 $this->attachment
[$cur][6] = "attachment";
1259 $this->attachment
[$cur][7] = 0;
1263 * Adds an embedded attachment. This can include images, sounds, and
1264 * just about any other document. Make sure to set the $type to an
1265 * image type. For JPEG images use "image/jpeg" and for GIF images
1267 * @param string $path Path to the attachment.
1268 * @param string $cid Content ID of the attachment. Use this to identify
1269 * the Id for accessing the image in an HTML form.
1270 * @param string $name Overrides the attachment name.
1271 * @param string $encoding File encoding (see $Encoding).
1272 * @param string $type File extension (MIME) type.
1275 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1276 $type = "application/octet-stream") {
1278 if(!@is_file
($path))
1280 $this->SetError($this->Lang("file_access") . $path);
1284 $filename = basename($path);
1288 // Append to $attachment array
1289 $cur = count($this->attachment
);
1290 $this->attachment
[$cur][0] = $path;
1291 $this->attachment
[$cur][1] = $filename;
1292 $this->attachment
[$cur][2] = $name;
1293 $this->attachment
[$cur][3] = $encoding;
1294 $this->attachment
[$cur][4] = $type;
1295 $this->attachment
[$cur][5] = false; // isStringAttachment
1296 $this->attachment
[$cur][6] = "inline";
1297 $this->attachment
[$cur][7] = $cid;
1303 * Returns true if an inline attachment is present.
1307 function InlineImageExists() {
1309 for($i = 0; $i < count($this->attachment
); $i++
)
1311 if($this->attachment
[$i][6] == "inline")
1321 /////////////////////////////////////////////////
1322 // MESSAGE RESET METHODS
1323 /////////////////////////////////////////////////
1326 * Clears all recipients assigned in the TO array. Returns void.
1329 function ClearAddresses() {
1330 $this->to
= array();
1334 * Clears all recipients assigned in the CC array. Returns void.
1337 function ClearCCs() {
1338 $this->cc
= array();
1342 * Clears all recipients assigned in the BCC array. Returns void.
1345 function ClearBCCs() {
1346 $this->bcc
= array();
1350 * Clears all recipients assigned in the ReplyTo array. Returns void.
1353 function ClearReplyTos() {
1354 $this->ReplyTo
= array();
1358 * Clears all recipients assigned in the TO, CC and BCC
1359 * array. Returns void.
1362 function ClearAllRecipients() {
1363 $this->to
= array();
1364 $this->cc
= array();
1365 $this->bcc
= array();
1369 * Clears all previously set filesystem, string, and binary
1370 * attachments. Returns void.
1373 function ClearAttachments() {
1374 $this->attachment
= array();
1378 * Clears all custom headers. Returns void.
1381 function ClearCustomHeaders() {
1382 $this->CustomHeader
= array();
1386 /////////////////////////////////////////////////
1387 // MISCELLANEOUS METHODS
1388 /////////////////////////////////////////////////
1391 * Adds the error message to the error container.
1396 function SetError($msg) {
1397 $this->error_count++
;
1398 $this->ErrorInfo
= $msg;
1402 * Returns the proper RFC 822 formatted date.
1406 function RFCDate() {
1408 $tzs = ($tz < 0) ?
"-" : "+";
1410 $tz = ($tz/3600)*100 +
($tz%3600
)/60;
1411 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1417 * Returns Received header for message tracing.
1421 function Received() {
1422 if ($this->ServerVar('SERVER_NAME') != '')
1424 $protocol = ($this->ServerVar('HTTPS') == 'on') ?
'HTTPS' : 'HTTP';
1425 $remote = $this->ServerVar('REMOTE_HOST');
1427 $remote = 'phpmailer';
1428 $remote .= ' (['.$this->ServerVar('REMOTE_ADDR').'])';
1432 $protocol = 'local';
1433 $remote = $this->ServerVar('USER');
1435 $remote = 'phpmailer';
1438 $result = sprintf("Received: from %s %s\tby %s " .
1439 "with %s (PHPMailer);%s\t%s%s", $remote, $this->LE
,
1440 $this->ServerHostname(), $protocol, $this->LE
,
1441 $this->RFCDate(), $this->LE
);
1447 * Returns the appropriate server variable. Should work with both
1448 * PHP 4.1.0+ as well as older versions. Returns an empty string
1449 * if nothing is found.
1453 function ServerVar($varName) {
1454 global $HTTP_SERVER_VARS;
1455 global $HTTP_ENV_VARS;
1457 if(!isset($_SERVER))
1459 $_SERVER = $HTTP_SERVER_VARS;
1460 if(!isset($_SERVER["REMOTE_ADDR"]))
1461 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1464 if(isset($_SERVER[$varName]))
1465 return $_SERVER[$varName];
1471 * Returns the server hostname or 'localhost.localdomain' if unknown.
1475 function ServerHostname() {
1476 if ($this->Hostname
!= "")
1477 $result = $this->Hostname
;
1478 elseif ($this->ServerVar('SERVER_NAME') != "")
1479 $result = $this->ServerVar('SERVER_NAME');
1481 $result = "localhost.localdomain";
1487 * Returns a message in the appropriate language.
1491 function Lang($key) {
1492 if(count($this->language
) < 1)
1493 $this->SetLanguage("en"); // set the default language
1495 if(isset($this->language
[$key]))
1496 return $this->language
[$key];
1498 return "Language string failed to load: " . $key;
1502 * Returns true if an error occurred.
1505 function IsError() {
1506 return ($this->error_count
> 0);
1510 * Changes every end of line from CR or LF to CRLF.
1514 function FixEOL($str) {
1515 $str = str_replace("\r\n", "\n", $str);
1516 $str = str_replace("\r", "\n", $str);
1517 $str = str_replace("\n", $this->LE
, $str);
1522 * Adds a custom header.
1525 function AddCustomHeader($custom_header) {
1526 $this->CustomHeader
[] = explode(":", $custom_header, 2);