]>
git.wh0rd.org - tt-rss.git/blob - phpmailer/class.phpmailer.php
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.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",
394 escapeshellcmd($this->Sendmail
), escapeshellarg($this->Sender
));
396 $sendmail = sprintf("%s -oi -t",
397 escapeshellcmd($this->Sendmail
));
399 if(!@$mail = popen($sendmail, "w"))
401 $this->SetError($this->Lang("execute") . $this->Sendmail
);
405 fputs($mail, $header);
408 $result = pclose($mail) >> 8 & 0xFF;
411 $this->SetError($this->Lang("execute") . $this->Sendmail
);
419 * Sends mail using the PHP mail() function.
423 function MailSend($header, $body) {
425 for($i = 0; $i < count($this->to
); $i++
)
427 if($i != 0) { $to .= ", "; }
428 $to .= $this->to
[$i][0];
431 if ($this->Sender
!= "" && strlen(ini_get("safe_mode"))< 1)
433 $old_from = ini_get("sendmail_from");
434 ini_set("sendmail_from", $this->Sender
);
435 $params = sprintf("-oi -f %s", $this->Sender
);
436 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body,
440 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body, $header);
442 if (isset($old_from))
443 ini_set("sendmail_from", $old_from);
447 $this->SetError($this->Lang("instantiate"));
455 * Sends mail via SMTP using PhpSMTP (Author:
456 * Chris Ryan). Returns bool. Returns false if there is a
457 * bad MAIL FROM, RCPT, or DATA input.
461 function SmtpSend($header, $body) {
462 include_once($this->PluginDir
. "class.smtp.php");
466 if(!$this->SmtpConnect())
469 $smtp_from = ($this->Sender
== "") ?
$this->From
: $this->Sender
;
470 if(!$this->smtp
->Mail($smtp_from))
472 $error = $this->Lang("from_failed") . $smtp_from;
473 $this->SetError($error);
474 $this->smtp
->Reset();
478 // Attempt to send attach all recipients
479 for($i = 0; $i < count($this->to
); $i++
)
481 if(!$this->smtp
->Recipient($this->to
[$i][0]))
482 $bad_rcpt[] = $this->to
[$i][0];
484 for($i = 0; $i < count($this->cc
); $i++
)
486 if(!$this->smtp
->Recipient($this->cc
[$i][0]))
487 $bad_rcpt[] = $this->cc
[$i][0];
489 for($i = 0; $i < count($this->bcc
); $i++
)
491 if(!$this->smtp
->Recipient($this->bcc
[$i][0]))
492 $bad_rcpt[] = $this->bcc
[$i][0];
495 if(count($bad_rcpt) > 0) // Create error message
497 for($i = 0; $i < count($bad_rcpt); $i++
)
499 if($i != 0) { $error .= ", "; }
500 $error .= $bad_rcpt[$i];
502 $error = $this->Lang("recipients_failed") . $error;
503 $this->SetError($error);
504 $this->smtp
->Reset();
508 if(!$this->smtp
->Data($header . $body))
510 $this->SetError($this->Lang("data_not_accepted"));
511 $this->smtp
->Reset();
514 if($this->SMTPKeepAlive
== true)
515 $this->smtp
->Reset();
523 * Initiates a connection to an SMTP server. Returns false if the
528 function SmtpConnect() {
529 if($this->smtp
== NULL) { $this->smtp
= new SMTP(); }
531 $this->smtp
->do_debug
= $this->SMTPDebug
;
532 $hosts = explode(";", $this->Host
);
534 $connection = ($this->smtp
->Connected());
536 // Retry while there is no connection
537 while($index < count($hosts) && $connection == false)
539 if(strstr($hosts[$index], ":"))
540 list($host, $port) = explode(":", $hosts[$index]);
543 $host = $hosts[$index];
547 if($this->smtp
->Connect($host, $port, $this->Timeout
))
549 if ($this->Helo
!= '')
550 $this->smtp
->Hello($this->Helo
);
552 $this->smtp
->Hello($this->ServerHostname());
556 if(!$this->smtp
->Authenticate($this->Username
,
559 $this->SetError($this->Lang("authenticate"));
560 $this->smtp
->Reset();
569 $this->SetError($this->Lang("connect_host"));
575 * Closes the active SMTP session if one exists.
578 function SmtpClose() {
579 if($this->smtp
!= NULL)
581 if($this->smtp
->Connected())
584 $this->smtp
->Close();
590 * Sets the language for all class error messages. Returns false
591 * if it cannot load the language file. The default language type
593 * @param string $lang_type Type of language (e.g. Portuguese: "br")
594 * @param string $lang_path Path to the language file directory
598 function SetLanguage($lang_type, $lang_path = "language/") {
599 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
600 include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
601 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
602 include($lang_path.'phpmailer.lang-en.php');
605 $this->SetError("Could not load language file");
608 $this->language
= $PHPMAILER_LANG;
613 /////////////////////////////////////////////////
614 // MESSAGE CREATION METHODS
615 /////////////////////////////////////////////////
618 * Creates recipient headers.
622 function AddrAppend($type, $addr) {
623 $addr_str = $type . ": ";
624 $addr_str .= $this->AddrFormat($addr[0]);
627 for($i = 1; $i < count($addr); $i++
)
628 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
630 $addr_str .= $this->LE
;
636 * Formats an address correctly.
640 function AddrFormat($addr) {
642 $formatted = $addr[0];
645 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
653 * Wraps message for use with mailers that do not
654 * automatically perform wrapping and for quoted-printable.
655 * Original written by philippe.
659 function WrapText($message, $length, $qp_mode = false) {
660 $soft_break = ($qp_mode) ?
sprintf(" =%s", $this->LE
) : $this->LE
;
662 $message = $this->FixEOL($message);
663 if (substr($message, -1) == $this->LE
)
664 $message = substr($message, 0, -1);
666 $line = explode($this->LE
, $message);
668 for ($i=0 ;$i < count($line); $i++
)
670 $line_part = explode(" ", $line[$i]);
672 for ($e = 0; $e<count($line_part); $e++
)
674 $word = $line_part[$e];
675 if ($qp_mode and (strlen($word) > $length))
677 $space_left = $length - strlen($buf) - 1;
680 if ($space_left > 20)
683 if (substr($word, $len - 1, 1) == "=")
685 elseif (substr($word, $len - 2, 1) == "=")
687 $part = substr($word, 0, $len);
688 $word = substr($word, $len);
690 $message .= $buf . sprintf("=%s", $this->LE
);
694 $message .= $buf . $soft_break;
698 while (strlen($word) > 0)
701 if (substr($word, $len - 1, 1) == "=")
703 elseif (substr($word, $len - 2, 1) == "=")
705 $part = substr($word, 0, $len);
706 $word = substr($word, $len);
708 if (strlen($word) > 0)
709 $message .= $part . sprintf("=%s", $this->LE
);
717 $buf .= ($e == 0) ?
$word : (" " . $word);
719 if (strlen($buf) > $length and $buf_o != "")
721 $message .= $buf_o . $soft_break;
726 $message .= $buf . $this->LE
;
733 * Set the body wrapping.
737 function SetWordWrap() {
738 if($this->WordWrap
< 1)
741 switch($this->message_type
)
745 case "alt_attachments":
746 $this->AltBody
= $this->WrapText($this->AltBody
, $this->WordWrap
);
749 $this->Body
= $this->WrapText($this->Body
, $this->WordWrap
);
755 * Assembles message header.
759 function CreateHeader() {
762 // Set the boundaries
763 $uniq_id = md5(uniqid(time()));
764 $this->boundary
[1] = "b1_" . $uniq_id;
765 $this->boundary
[2] = "b2_" . $uniq_id;
767 $result .= $this->HeaderLine("Date", $this->RFCDate());
768 if($this->Sender
== "")
769 $result .= $this->HeaderLine("Return-Path", trim($this->From
));
771 $result .= $this->HeaderLine("Return-Path", trim($this->Sender
));
773 // To be created automatically by mail()
774 if($this->Mailer
!= "mail")
776 if(count($this->to
) > 0)
777 $result .= $this->AddrAppend("To", $this->to
);
778 else if (count($this->cc
) == 0)
779 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
780 if(count($this->cc
) > 0)
781 $result .= $this->AddrAppend("Cc", $this->cc
);
785 $from[0][0] = trim($this->From
);
786 $from[0][1] = $this->FromName
;
787 $result .= $this->AddrAppend("From", $from);
789 // sendmail and mail() extract Bcc from the header before sending
790 if((($this->Mailer
== "sendmail") ||
($this->Mailer
== "mail")) && (count($this->bcc
) > 0))
791 $result .= $this->AddrAppend("Bcc", $this->bcc
);
793 if(count($this->ReplyTo
) > 0)
794 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo
);
796 // mail() sets the subject itself
797 if($this->Mailer
!= "mail")
798 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject
)));
800 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE
);
801 $result .= $this->HeaderLine("X-Priority", $this->Priority
);
802 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version
. "]");
804 if($this->ConfirmReadingTo
!= "")
806 $result .= $this->HeaderLine("Disposition-Notification-To",
807 "<" . trim($this->ConfirmReadingTo
) . ">");
810 // Add custom headers
811 for($index = 0; $index < count($this->CustomHeader
); $index++
)
813 $result .= $this->HeaderLine(trim($this->CustomHeader
[$index][0]),
814 $this->EncodeHeader(trim($this->CustomHeader
[$index][1])));
816 $result .= $this->HeaderLine("MIME-Version", "1.0");
818 switch($this->message_type
)
821 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding
);
822 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
823 $this->ContentType
, $this->CharSet
);
827 case "alt_attachments":
828 if($this->InlineImageExists())
830 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
831 "multipart/related", $this->LE
, $this->LE
,
832 $this->boundary
[1], $this->LE
);
836 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
837 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
841 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
842 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
846 if($this->Mailer
!= "mail")
847 $result .= $this->LE
.$this->LE
;
853 * Assembles the message body. Returns an empty string on failure.
857 function CreateBody() {
860 $this->SetWordWrap();
862 switch($this->message_type
)
865 $result .= $this->GetBoundary($this->boundary
[1], "",
867 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
868 $result .= $this->LE
.$this->LE
;
869 $result .= $this->GetBoundary($this->boundary
[1], "",
872 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
873 $result .= $this->LE
.$this->LE
;
875 $result .= $this->EndBoundary($this->boundary
[1]);
878 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
881 $result .= $this->GetBoundary($this->boundary
[1], "", "", "");
882 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
883 $result .= $this->LE
;
885 $result .= $this->AttachAll();
887 case "alt_attachments":
888 $result .= sprintf("--%s%s", $this->boundary
[1], $this->LE
);
889 $result .= sprintf("Content-Type: %s;%s" .
890 "\tboundary=\"%s\"%s",
891 "multipart/alternative", $this->LE
,
892 $this->boundary
[2], $this->LE
.$this->LE
);
895 $result .= $this->GetBoundary($this->boundary
[2], "",
896 "text/plain", "") . $this->LE
;
898 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
899 $result .= $this->LE
.$this->LE
;
901 // Create the HTML body
902 $result .= $this->GetBoundary($this->boundary
[2], "",
903 "text/html", "") . $this->LE
;
905 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
906 $result .= $this->LE
.$this->LE
;
908 $result .= $this->EndBoundary($this->boundary
[2]);
910 $result .= $this->AttachAll();
920 * Returns the start of a message boundary.
923 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
925 if($charSet == "") { $charSet = $this->CharSet
; }
926 if($contentType == "") { $contentType = $this->ContentType
; }
927 if($encoding == "") { $encoding = $this->Encoding
; }
929 $result .= $this->TextLine("--" . $boundary);
930 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
931 $contentType, $charSet);
932 $result .= $this->LE
;
933 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
934 $result .= $this->LE
;
940 * Returns the end of a message boundary.
943 function EndBoundary($boundary) {
944 return $this->LE
. "--" . $boundary . "--" . $this->LE
;
948 * Sets the message type.
952 function SetMessageType() {
953 if(count($this->attachment
) < 1 && strlen($this->AltBody
) < 1)
954 $this->message_type
= "plain";
957 if(count($this->attachment
) > 0)
958 $this->message_type
= "attachments";
959 if(strlen($this->AltBody
) > 0 && count($this->attachment
) < 1)
960 $this->message_type
= "alt";
961 if(strlen($this->AltBody
) > 0 && count($this->attachment
) > 0)
962 $this->message_type
= "alt_attachments";
967 * Returns a formatted header line.
971 function HeaderLine($name, $value) {
972 return $name . ": " . $value . $this->LE
;
976 * Returns a formatted mail line.
980 function TextLine($value) {
981 return $value . $this->LE
;
984 /////////////////////////////////////////////////
985 // ATTACHMENT METHODS
986 /////////////////////////////////////////////////
989 * Adds an attachment from a path on the filesystem.
990 * Returns false if the file could not be found
992 * @param string $path Path to the attachment.
993 * @param string $name Overrides the attachment name.
994 * @param string $encoding File encoding (see $Encoding).
995 * @param string $type File extension (MIME) type.
998 function AddAttachment($path, $name = "", $encoding = "base64",
999 $type = "application/octet-stream") {
1000 if(!@is_file
($path))
1002 $this->SetError($this->Lang("file_access") . $path);
1006 $filename = basename($path);
1010 $cur = count($this->attachment
);
1011 $this->attachment
[$cur][0] = $path;
1012 $this->attachment
[$cur][1] = $filename;
1013 $this->attachment
[$cur][2] = $name;
1014 $this->attachment
[$cur][3] = $encoding;
1015 $this->attachment
[$cur][4] = $type;
1016 $this->attachment
[$cur][5] = false; // isStringAttachment
1017 $this->attachment
[$cur][6] = "attachment";
1018 $this->attachment
[$cur][7] = 0;
1024 * Attaches all fs, string, and binary attachments to the message.
1025 * Returns an empty string on failure.
1029 function AttachAll() {
1030 // Return text of body
1033 // Add all attachments
1034 for($i = 0; $i < count($this->attachment
); $i++
)
1036 // Check for string attachment
1037 $bString = $this->attachment
[$i][5];
1039 $string = $this->attachment
[$i][0];
1041 $path = $this->attachment
[$i][0];
1043 $filename = $this->attachment
[$i][1];
1044 $name = $this->attachment
[$i][2];
1045 $encoding = $this->attachment
[$i][3];
1046 $type = $this->attachment
[$i][4];
1047 $disposition = $this->attachment
[$i][6];
1048 $cid = $this->attachment
[$i][7];
1050 $mime[] = sprintf("--%s%s", $this->boundary
[1], $this->LE
);
1051 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE
);
1052 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE
);
1054 if($disposition == "inline")
1055 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE
);
1057 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1058 $disposition, $name, $this->LE
.$this->LE
);
1060 // Encode as string attachment
1063 $mime[] = $this->EncodeString($string, $encoding);
1064 if($this->IsError()) { return ""; }
1065 $mime[] = $this->LE
.$this->LE
;
1069 $mime[] = $this->EncodeFile($path, $encoding);
1070 if($this->IsError()) { return ""; }
1071 $mime[] = $this->LE
.$this->LE
;
1075 $mime[] = sprintf("--%s--%s", $this->boundary
[1], $this->LE
);
1077 return join("", $mime);
1081 * Encodes attachment in requested format. Returns an
1082 * empty string on failure.
1086 function EncodeFile ($path, $encoding = "base64") {
1087 if(!@$fd = fopen($path, "rb"))
1089 $this->SetError($this->Lang("file_open") . $path);
1092 $magic_quotes = get_magic_quotes_runtime();
1093 set_magic_quotes_runtime(0);
1094 $file_buffer = fread($fd, filesize($path));
1095 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1097 set_magic_quotes_runtime($magic_quotes);
1099 return $file_buffer;
1103 * Encodes string to requested format. Returns an
1104 * empty string on failure.
1108 function EncodeString ($str, $encoding = "base64") {
1110 switch(strtolower($encoding)) {
1112 // chunk_split is found in PHP >= 3.0.6
1113 $encoded = chunk_split(base64_encode($str), 76, $this->LE
);
1117 $encoded = $this->FixEOL($str);
1118 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1119 $encoded .= $this->LE
;
1124 case "quoted-printable":
1125 $encoded = $this->EncodeQP($str);
1128 $this->SetError($this->Lang("encoding") . $encoding);
1135 * Encode a header string to best of Q, B, quoted or none.
1139 function EncodeHeader ($str, $position = 'text') {
1142 switch (strtolower($position)) {
1144 if (!preg_match('/[\200-\377]/', $str)) {
1145 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1146 $encoded = addcslashes($str, "\0..\37\177\\\"");
1148 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1151 return ("\"$encoded\"");
1153 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1156 $x = preg_match_all('/[()"]/', $str, $matches);
1160 $x +
= preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1167 $maxlen = 75 - 7 - strlen($this->CharSet
);
1168 // Try to select the encoding which should produce the shortest output
1169 if (strlen($str)/3 < $x) {
1171 $encoded = base64_encode($str);
1172 $maxlen -= $maxlen %
4;
1173 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1176 $encoded = $this->EncodeQ($str, $position);
1177 $encoded = $this->WrapText($encoded, $maxlen, true);
1178 $encoded = str_replace("=".$this->LE
, "\n", trim($encoded));
1181 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet
."?$encoding?\\1?=", $encoded);
1182 $encoded = trim(str_replace("\n", $this->LE
, $encoded));
1188 * Encode string to quoted-printable.
1192 function EncodeQP ($str) {
1193 $encoded = $this->FixEOL($str);
1194 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1195 $encoded .= $this->LE
;
1197 // Replace every high ascii, control and = characters
1198 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
1199 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1200 // Replace every spaces and tabs when it's the last character on a line
1201 $encoded = preg_replace("/([\011\040])".$this->LE
."/e",
1202 "'='.sprintf('%02X', ord('\\1')).'".$this->LE
."'", $encoded);
1204 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1205 $encoded = $this->WrapText($encoded, 74, true);
1211 * Encode string to q encoding.
1215 function EncodeQ ($str, $position = "text") {
1216 // There should not be any EOL in the string
1217 $encoded = preg_replace("[\r\n]", "", $str);
1219 switch (strtolower($position)) {
1221 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1224 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1227 // Replace every high ascii, control =, ? and _ characters
1228 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1229 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1233 // Replace every spaces to _ (more readable than =20)
1234 $encoded = str_replace(" ", "_", $encoded);
1240 * Adds a string or binary attachment (non-filesystem) to the list.
1241 * This method can be used to attach ascii or binary data,
1242 * such as a BLOB record from a database.
1243 * @param string $string String attachment data.
1244 * @param string $filename Name of the attachment.
1245 * @param string $encoding File encoding (see $Encoding).
1246 * @param string $type File extension (MIME) type.
1249 function AddStringAttachment($string, $filename, $encoding = "base64",
1250 $type = "application/octet-stream") {
1251 // Append to $attachment array
1252 $cur = count($this->attachment
);
1253 $this->attachment
[$cur][0] = $string;
1254 $this->attachment
[$cur][1] = $filename;
1255 $this->attachment
[$cur][2] = $filename;
1256 $this->attachment
[$cur][3] = $encoding;
1257 $this->attachment
[$cur][4] = $type;
1258 $this->attachment
[$cur][5] = true; // isString
1259 $this->attachment
[$cur][6] = "attachment";
1260 $this->attachment
[$cur][7] = 0;
1264 * Adds an embedded attachment. This can include images, sounds, and
1265 * just about any other document. Make sure to set the $type to an
1266 * image type. For JPEG images use "image/jpeg" and for GIF images
1268 * @param string $path Path to the attachment.
1269 * @param string $cid Content ID of the attachment. Use this to identify
1270 * the Id for accessing the image in an HTML form.
1271 * @param string $name Overrides the attachment name.
1272 * @param string $encoding File encoding (see $Encoding).
1273 * @param string $type File extension (MIME) type.
1276 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1277 $type = "application/octet-stream") {
1279 if(!@is_file
($path))
1281 $this->SetError($this->Lang("file_access") . $path);
1285 $filename = basename($path);
1289 // Append to $attachment array
1290 $cur = count($this->attachment
);
1291 $this->attachment
[$cur][0] = $path;
1292 $this->attachment
[$cur][1] = $filename;
1293 $this->attachment
[$cur][2] = $name;
1294 $this->attachment
[$cur][3] = $encoding;
1295 $this->attachment
[$cur][4] = $type;
1296 $this->attachment
[$cur][5] = false; // isStringAttachment
1297 $this->attachment
[$cur][6] = "inline";
1298 $this->attachment
[$cur][7] = $cid;
1304 * Returns true if an inline attachment is present.
1308 function InlineImageExists() {
1310 for($i = 0; $i < count($this->attachment
); $i++
)
1312 if($this->attachment
[$i][6] == "inline")
1322 /////////////////////////////////////////////////
1323 // MESSAGE RESET METHODS
1324 /////////////////////////////////////////////////
1327 * Clears all recipients assigned in the TO array. Returns void.
1330 function ClearAddresses() {
1331 $this->to
= array();
1335 * Clears all recipients assigned in the CC array. Returns void.
1338 function ClearCCs() {
1339 $this->cc
= array();
1343 * Clears all recipients assigned in the BCC array. Returns void.
1346 function ClearBCCs() {
1347 $this->bcc
= array();
1351 * Clears all recipients assigned in the ReplyTo array. Returns void.
1354 function ClearReplyTos() {
1355 $this->ReplyTo
= array();
1359 * Clears all recipients assigned in the TO, CC and BCC
1360 * array. Returns void.
1363 function ClearAllRecipients() {
1364 $this->to
= array();
1365 $this->cc
= array();
1366 $this->bcc
= array();
1370 * Clears all previously set filesystem, string, and binary
1371 * attachments. Returns void.
1374 function ClearAttachments() {
1375 $this->attachment
= array();
1379 * Clears all custom headers. Returns void.
1382 function ClearCustomHeaders() {
1383 $this->CustomHeader
= array();
1387 /////////////////////////////////////////////////
1388 // MISCELLANEOUS METHODS
1389 /////////////////////////////////////////////////
1392 * Adds the error message to the error container.
1397 function SetError($msg) {
1398 $this->error_count++
;
1399 $this->ErrorInfo
= $msg;
1403 * Returns the proper RFC 822 formatted date.
1407 function RFCDate() {
1409 $tzs = ($tz < 0) ?
"-" : "+";
1411 $tz = ($tz/3600)*100 +
($tz%3600
)/60;
1412 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1418 * Returns the appropriate server variable. Should work with both
1419 * PHP 4.1.0+ as well as older versions. Returns an empty string
1420 * if nothing is found.
1424 function ServerVar($varName) {
1425 global $HTTP_SERVER_VARS;
1426 global $HTTP_ENV_VARS;
1428 if(!isset($_SERVER))
1430 $_SERVER = $HTTP_SERVER_VARS;
1431 if(!isset($_SERVER["REMOTE_ADDR"]))
1432 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1435 if(isset($_SERVER[$varName]))
1436 return $_SERVER[$varName];
1442 * Returns the server hostname or 'localhost.localdomain' if unknown.
1446 function ServerHostname() {
1447 if ($this->Hostname
!= "")
1448 $result = $this->Hostname
;
1449 elseif ($this->ServerVar('SERVER_NAME') != "")
1450 $result = $this->ServerVar('SERVER_NAME');
1452 $result = "localhost.localdomain";
1458 * Returns a message in the appropriate language.
1462 function Lang($key) {
1463 if(count($this->language
) < 1)
1464 $this->SetLanguage("en"); // set the default language
1466 if(isset($this->language
[$key]))
1467 return $this->language
[$key];
1469 return "Language string failed to load: " . $key;
1473 * Returns true if an error occurred.
1476 function IsError() {
1477 return ($this->error_count
> 0);
1481 * Changes every end of line from CR or LF to CRLF.
1485 function FixEOL($str) {
1486 $str = str_replace("\r\n", "\n", $str);
1487 $str = str_replace("\r", "\n", $str);
1488 $str = str_replace("\n", $this->LE
, $str);
1493 * Adds a custom header.
1496 function AddCustomHeader($custom_header) {
1497 $this->CustomHeader
[] = explode(":", $custom_header, 2);