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 /////////////////////////////////////////////////
232 * Hack for Moodle as class may be included from various locations
237 function PHPMailer () {
239 $this->PluginDir
= $CFG->libdir
.'/phpmailer/';
245 * Sets message type to HTML.
249 function IsHTML($bool) {
251 $this->ContentType
= "text/html";
253 $this->ContentType
= "text/plain";
257 * Sets Mailer to send message using SMTP.
261 $this->Mailer
= "smtp";
265 * Sets Mailer to send message using PHP mail() function.
269 $this->Mailer
= "mail";
273 * Sets Mailer to send message using the $Sendmail program.
276 function IsSendmail() {
277 $this->Mailer
= "sendmail";
281 * Sets Mailer to send message using the qmail MTA.
285 $this->Sendmail
= "/var/qmail/bin/sendmail";
286 $this->Mailer
= "sendmail";
290 /////////////////////////////////////////////////
292 /////////////////////////////////////////////////
295 * Adds a "To" address.
296 * @param string $address
297 * @param string $name
300 function AddAddress($address, $name = "") {
301 $cur = count($this->to
);
302 $this->to
[$cur][0] = trim($address);
303 $this->to
[$cur][1] = $name;
307 * Adds a "Cc" 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 AddCC($address, $name = "") {
315 $cur = count($this->cc
);
316 $this->cc
[$cur][0] = trim($address);
317 $this->cc
[$cur][1] = $name;
321 * Adds a "Bcc" address. Note: this function works
322 * with the SMTP mailer on win32, not with the "mail"
324 * @param string $address
325 * @param string $name
328 function AddBCC($address, $name = "") {
329 $cur = count($this->bcc
);
330 $this->bcc
[$cur][0] = trim($address);
331 $this->bcc
[$cur][1] = $name;
335 * Adds a "Reply-to" address.
336 * @param string $address
337 * @param string $name
340 function AddReplyTo($address, $name = "") {
341 $cur = count($this->ReplyTo
);
342 $this->ReplyTo
[$cur][0] = trim($address);
343 $this->ReplyTo
[$cur][1] = $name;
347 /////////////////////////////////////////////////
348 // MAIL SENDING METHODS
349 /////////////////////////////////////////////////
352 * Creates message and assigns Mailer. If the message is
353 * not sent successfully then it returns false. Use the ErrorInfo
354 * variable to view description of the error.
362 if((count($this->to
) +
count($this->cc
) +
count($this->bcc
)) < 1)
364 $this->SetError($this->Lang("provide_address"));
368 // Set whether the message is multipart/alternative
369 if(!empty($this->AltBody
))
370 $this->ContentType
= "multipart/alternative";
372 $this->error_count
= 0; // reset errors
373 $this->SetMessageType();
374 $header .= $this->CreateHeader();
375 $body = $this->CreateBody();
377 if($body == "") { return false; }
380 switch($this->Mailer
)
383 $result = $this->SendmailSend($header, $body);
386 $result = $this->MailSend($header, $body);
389 $result = $this->SmtpSend($header, $body);
392 $this->SetError($this->Mailer
. $this->Lang("mailer_not_supported"));
401 * Sends mail using the $Sendmail program.
405 function SendmailSend($header, $body) {
406 if ($this->Sender
!= "")
407 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail
, $this->Sender
);
409 $sendmail = sprintf("%s -oi -t", $this->Sendmail
);
411 if(!@$mail = popen($sendmail, "w"))
413 $this->SetError($this->Lang("execute") . $this->Sendmail
);
417 fputs($mail, $header);
420 $result = pclose($mail) >> 8 & 0xFF;
423 $this->SetError($this->Lang("execute") . $this->Sendmail
);
431 * Sends mail using the PHP mail() function.
435 function MailSend($header, $body) {
437 for($i = 0; $i < count($this->to
); $i++
)
439 if($i != 0) { $to .= ", "; }
440 $to .= $this->to
[$i][0];
443 $safe = ini_get("safe_mode");
444 if ($this->Sender
!= "" && ($safe == "" ||
$safe == "0"))
446 $old_from = ini_get("sendmail_from");
447 ini_set("sendmail_from", $this->Sender
);
448 $params = sprintf("-oi -f %s", $this->Sender
);
449 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body,
453 $rt = @mail
($to, $this->EncodeHeader($this->Subject
), $body, $header);
455 if (isset($old_from))
456 ini_set("sendmail_from", $old_from);
460 $this->SetError($this->Lang("instantiate"));
468 * Sends mail via SMTP using PhpSMTP (Author:
469 * Chris Ryan). Returns bool. Returns false if there is a
470 * bad MAIL FROM, RCPT, or DATA input.
474 function SmtpSend($header, $body) {
475 include_once($this->PluginDir
. "class.smtp.php");
479 if(!$this->SmtpConnect())
482 $smtp_from = ($this->Sender
== "") ?
$this->From
: $this->Sender
;
483 if(!$this->smtp
->Mail($smtp_from))
485 $error = $this->Lang("from_failed") . $smtp_from;
486 $this->SetError($error);
487 $this->smtp
->Reset();
491 // Attempt to send attach all recipients
492 for($i = 0; $i < count($this->to
); $i++
)
494 if(!$this->smtp
->Recipient($this->to
[$i][0]))
495 $bad_rcpt[] = $this->to
[$i][0];
497 for($i = 0; $i < count($this->cc
); $i++
)
499 if(!$this->smtp
->Recipient($this->cc
[$i][0]))
500 $bad_rcpt[] = $this->cc
[$i][0];
502 for($i = 0; $i < count($this->bcc
); $i++
)
504 if(!$this->smtp
->Recipient($this->bcc
[$i][0]))
505 $bad_rcpt[] = $this->bcc
[$i][0];
508 if(count($bad_rcpt) > 0) // Create error message
510 for($i = 0; $i < count($bad_rcpt); $i++
)
512 if($i != 0) { $error .= ", "; }
513 $error .= $bad_rcpt[$i];
515 $error = $this->Lang("recipients_failed") . $error;
516 $this->SetError($error);
517 $this->smtp
->Reset();
521 if(!$this->smtp
->Data($header . $body))
523 $this->SetError($this->Lang("data_not_accepted"));
524 $this->smtp
->Reset();
527 if($this->SMTPKeepAlive
== true)
528 $this->smtp
->Reset();
536 * Initiates a connection to an SMTP server. Returns false if the
541 function SmtpConnect() {
542 if($this->smtp
== NULL) { $this->smtp
= new SMTP(); }
544 $this->smtp
->do_debug
= $this->SMTPDebug
;
545 $hosts = explode(";", $this->Host
);
547 $connection = ($this->smtp
->Connected());
549 // Retry while there is no connection
550 while($index < count($hosts) && $connection == false)
552 if(strstr($hosts[$index], ":"))
553 list($host, $port) = explode(":", $hosts[$index]);
556 $host = $hosts[$index];
560 if($this->smtp
->Connect($host, $port, $this->Timeout
))
562 if ($this->Helo
!= '')
563 $this->smtp
->Hello($this->Helo
);
565 $this->smtp
->Hello($this->ServerHostname());
569 if(!$this->smtp
->Authenticate($this->Username
,
572 $this->SetError($this->Lang("authenticate"));
573 $this->smtp
->Reset();
582 $this->SetError($this->Lang("connect_host"));
588 * Closes the active SMTP session if one exists.
591 function SmtpClose() {
592 if($this->smtp
!= NULL)
594 if($this->smtp
->Connected())
597 $this->smtp
->Close();
603 * Sets the language for all class error messages. Returns false
604 * if it cannot load the language file. The default language type
606 * SE 20041001: Added '$this->PluginDir' for Moodle compatibility
608 * @param string $lang_type Type of language (e.g. Portuguese: "br")
609 * @param string $lang_path Path to the language file directory
613 function SetLanguage($lang_type, $lang_path = "language/") {
614 if(file_exists($this->PluginDir
.$lang_path.'phpmailer.lang-'.$lang_type.'.php'))
615 include($this->PluginDir
.$lang_path.'phpmailer.lang-'.$lang_type.'.php');
616 else if(file_exists($lang_path.'phpmailer.lang-en.php'))
617 include($this->PluginDir
.$lang_path.'phpmailer.lang-en.php');
620 $this->SetError("Could not load language file");
623 $this->language
= $PHPMAILER_LANG;
628 /////////////////////////////////////////////////
629 // MESSAGE CREATION METHODS
630 /////////////////////////////////////////////////
633 * Creates recipient headers.
637 function AddrAppend($type, $addr) {
638 $addr_str = $type . ": ";
639 $addr_str .= $this->AddrFormat($addr[0]);
642 for($i = 1; $i < count($addr); $i++
)
643 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
645 $addr_str .= $this->LE
;
651 * Formats an address correctly.
655 function AddrFormat($addr) {
657 $formatted = $addr[0];
660 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
668 * Wraps message for use with mailers that do not
669 * automatically perform wrapping and for quoted-printable.
670 * Original written by philippe.
674 function WrapText($message, $length, $qp_mode = false) {
675 $soft_break = ($qp_mode) ?
sprintf(" =%s", $this->LE
) : $this->LE
;
677 $message = $this->FixEOL($message);
678 if (substr($message, -1) == $this->LE
)
679 $message = substr($message, 0, -1);
681 $line = explode($this->LE
, $message);
683 for ($i=0 ;$i < count($line); $i++
)
685 $line_part = explode(" ", $line[$i]);
687 for ($e = 0; $e<count($line_part); $e++
)
689 $word = $line_part[$e];
690 if ($qp_mode and (strlen($word) > $length))
692 $space_left = $length - strlen($buf) - 1;
695 if ($space_left > 20)
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 $message .= $buf . sprintf("=%s", $this->LE
);
709 $message .= $buf . $soft_break;
713 while (strlen($word) > 0)
716 if (substr($word, $len - 1, 1) == "=")
718 elseif (substr($word, $len - 2, 1) == "=")
720 $part = substr($word, 0, $len);
721 $word = substr($word, $len);
723 if (strlen($word) > 0)
724 $message .= $part . sprintf("=%s", $this->LE
);
732 $buf .= ($e == 0) ?
$word : (" " . $word);
734 if (strlen($buf) > $length and $buf_o != "")
736 $message .= $buf_o . $soft_break;
741 $message .= $buf . $this->LE
;
748 * Set the body wrapping.
752 function SetWordWrap() {
753 if($this->WordWrap
< 1)
756 switch($this->message_type
)
760 case "alt_attachments":
761 $this->AltBody
= $this->WrapText($this->AltBody
, $this->WordWrap
);
764 $this->Body
= $this->WrapText($this->Body
, $this->WordWrap
);
770 * Assembles message header.
774 function CreateHeader() {
777 // Set the boundaries
778 $uniq_id = md5(uniqid(time()));
779 $this->boundary
[1] = "b1_" . $uniq_id;
780 $this->boundary
[2] = "b2_" . $uniq_id;
782 $result .= $this->HeaderLine("Date", $this->RFCDate());
783 if($this->Sender
== "")
784 $result .= $this->HeaderLine("Return-Path", trim($this->From
));
786 $result .= $this->HeaderLine("Return-Path", trim($this->Sender
));
788 // To be created automatically by mail()
789 if($this->Mailer
!= "mail")
791 if(count($this->to
) > 0)
792 $result .= $this->AddrAppend("To", $this->to
);
793 else if (count($this->cc
) == 0)
794 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
795 if(count($this->cc
) > 0)
796 $result .= $this->AddrAppend("Cc", $this->cc
);
800 $from[0][0] = trim($this->From
);
801 $from[0][1] = $this->FromName
;
802 $result .= $this->AddrAppend("From", $from);
804 // sendmail and mail() extract Bcc from the header before sending
805 if((($this->Mailer
== "sendmail") ||
($this->Mailer
== "mail")) && (count($this->bcc
) > 0))
806 $result .= $this->AddrAppend("Bcc", $this->bcc
);
808 if(count($this->ReplyTo
) > 0)
809 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo
);
811 // mail() sets the subject itself
812 if($this->Mailer
!= "mail")
813 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject
)));
815 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE
);
816 $result .= $this->HeaderLine("X-Priority", $this->Priority
);
817 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version
. "]");
819 if($this->ConfirmReadingTo
!= "")
821 $result .= $this->HeaderLine("Disposition-Notification-To",
822 "<" . trim($this->ConfirmReadingTo
) . ">");
825 // Add custom headers
826 for($index = 0; $index < count($this->CustomHeader
); $index++
)
828 $result .= $this->HeaderLine(trim($this->CustomHeader
[$index][0]),
829 $this->EncodeHeader(trim($this->CustomHeader
[$index][1])));
831 $result .= $this->HeaderLine("MIME-Version", "1.0");
833 switch($this->message_type
)
836 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding
);
837 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
838 $this->ContentType
, $this->CharSet
);
842 case "alt_attachments":
843 if($this->InlineImageExists())
845 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
846 "multipart/related", $this->LE
, $this->LE
,
847 $this->boundary
[1], $this->LE
);
851 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
852 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
856 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
857 $result .= $this->TextLine("\tboundary=\"" . $this->boundary
[1] . '"');
861 if($this->Mailer
!= "mail")
862 $result .= $this->LE
.$this->LE
;
868 * Assembles the message body. Returns an empty string on failure.
872 function CreateBody() {
875 $this->SetWordWrap();
877 switch($this->message_type
)
880 $result .= $this->GetBoundary($this->boundary
[1], "",
882 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
883 $result .= $this->LE
.$this->LE
;
884 $result .= $this->GetBoundary($this->boundary
[1], "",
887 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
888 $result .= $this->LE
.$this->LE
;
890 $result .= $this->EndBoundary($this->boundary
[1]);
893 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
896 $result .= $this->GetBoundary($this->boundary
[1], "", "", "");
897 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
898 $result .= $this->LE
;
900 $result .= $this->AttachAll();
902 case "alt_attachments":
903 $result .= sprintf("--%s%s", $this->boundary
[1], $this->LE
);
904 $result .= sprintf("Content-Type: %s;%s" .
905 "\tboundary=\"%s\"%s",
906 "multipart/alternative", $this->LE
,
907 $this->boundary
[2], $this->LE
.$this->LE
);
910 $result .= $this->GetBoundary($this->boundary
[2], "",
911 "text/plain", "") . $this->LE
;
913 $result .= $this->EncodeString($this->AltBody
, $this->Encoding
);
914 $result .= $this->LE
.$this->LE
;
916 // Create the HTML body
917 $result .= $this->GetBoundary($this->boundary
[2], "",
918 "text/html", "") . $this->LE
;
920 $result .= $this->EncodeString($this->Body
, $this->Encoding
);
921 $result .= $this->LE
.$this->LE
;
923 $result .= $this->EndBoundary($this->boundary
[2]);
925 $result .= $this->AttachAll();
935 * Returns the start of a message boundary.
938 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
940 if($charSet == "") { $charSet = $this->CharSet
; }
941 if($contentType == "") { $contentType = $this->ContentType
; }
942 if($encoding == "") { $encoding = $this->Encoding
; }
944 $result .= $this->TextLine("--" . $boundary);
945 $result .= sprintf("Content-Type: %s; charset = \"%s\"",
946 $contentType, $charSet);
947 $result .= $this->LE
;
948 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
949 $result .= $this->LE
;
955 * Returns the end of a message boundary.
958 function EndBoundary($boundary) {
959 return $this->LE
. "--" . $boundary . "--" . $this->LE
;
963 * Sets the message type.
967 function SetMessageType() {
968 if(count($this->attachment
) < 1 && strlen($this->AltBody
) < 1)
969 $this->message_type
= "plain";
972 if(count($this->attachment
) > 0)
973 $this->message_type
= "attachments";
974 if(strlen($this->AltBody
) > 0 && count($this->attachment
) < 1)
975 $this->message_type
= "alt";
976 if(strlen($this->AltBody
) > 0 && count($this->attachment
) > 0)
977 $this->message_type
= "alt_attachments";
982 * Returns a formatted header line.
986 function HeaderLine($name, $value) {
987 return $name . ": " . $value . $this->LE
;
991 * Returns a formatted mail line.
995 function TextLine($value) {
996 return $value . $this->LE
;
999 /////////////////////////////////////////////////
1000 // ATTACHMENT METHODS
1001 /////////////////////////////////////////////////
1004 * Adds an attachment from a path on the filesystem.
1005 * Returns false if the file could not be found
1007 * @param string $path Path to the attachment.
1008 * @param string $name Overrides the attachment name.
1009 * @param string $encoding File encoding (see $Encoding).
1010 * @param string $type File extension (MIME) type.
1013 function AddAttachment($path, $name = "", $encoding = "base64",
1014 $type = "application/octet-stream") {
1015 if(!@is_file
($path))
1017 $this->SetError($this->Lang("file_access") . $path);
1021 $filename = basename($path);
1025 $cur = count($this->attachment
);
1026 $this->attachment
[$cur][0] = $path;
1027 $this->attachment
[$cur][1] = $filename;
1028 $this->attachment
[$cur][2] = $name;
1029 $this->attachment
[$cur][3] = $encoding;
1030 $this->attachment
[$cur][4] = $type;
1031 $this->attachment
[$cur][5] = false; // isStringAttachment
1032 $this->attachment
[$cur][6] = "attachment";
1033 $this->attachment
[$cur][7] = 0;
1039 * Attaches all fs, string, and binary attachments to the message.
1040 * Returns an empty string on failure.
1044 function AttachAll() {
1045 // Return text of body
1048 // Add all attachments
1049 for($i = 0; $i < count($this->attachment
); $i++
)
1051 // Check for string attachment
1052 $bString = $this->attachment
[$i][5];
1054 $string = $this->attachment
[$i][0];
1056 $path = $this->attachment
[$i][0];
1058 $filename = $this->attachment
[$i][1];
1059 $name = $this->attachment
[$i][2];
1060 $encoding = $this->attachment
[$i][3];
1061 $type = $this->attachment
[$i][4];
1062 $disposition = $this->attachment
[$i][6];
1063 $cid = $this->attachment
[$i][7];
1065 $mime[] = sprintf("--%s%s", $this->boundary
[1], $this->LE
);
1066 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE
);
1067 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE
);
1069 if($disposition == "inline")
1070 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE
);
1072 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1073 $disposition, $name, $this->LE
.$this->LE
);
1075 // Encode as string attachment
1078 $mime[] = $this->EncodeString($string, $encoding);
1079 if($this->IsError()) { return ""; }
1080 $mime[] = $this->LE
.$this->LE
;
1084 $mime[] = $this->EncodeFile($path, $encoding);
1085 if($this->IsError()) { return ""; }
1086 $mime[] = $this->LE
.$this->LE
;
1090 $mime[] = sprintf("--%s--%s", $this->boundary
[1], $this->LE
);
1092 return join("", $mime);
1096 * Encodes attachment in requested format. Returns an
1097 * empty string on failure.
1101 function EncodeFile ($path, $encoding = "base64") {
1102 if(!@$fd = fopen($path, "rb"))
1104 $this->SetError($this->Lang("file_open") . $path);
1107 $magic_quotes = get_magic_quotes_runtime();
1108 set_magic_quotes_runtime(0);
1109 $file_buffer = fread($fd, filesize($path));
1110 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1112 set_magic_quotes_runtime($magic_quotes);
1114 return $file_buffer;
1118 * Encodes string to requested format. Returns an
1119 * empty string on failure.
1123 function EncodeString ($str, $encoding = "base64") {
1125 switch(strtolower($encoding)) {
1127 // chunk_split is found in PHP >= 3.0.6
1128 $encoded = chunk_split(base64_encode($str), 76, $this->LE
);
1132 $encoded = $this->FixEOL($str);
1133 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1134 $encoded .= $this->LE
;
1139 case "quoted-printable":
1140 $encoded = $this->EncodeQP($str);
1143 $this->SetError($this->Lang("encoding") . $encoding);
1150 * Encode a header string to best of Q, B, quoted or none.
1154 function EncodeHeader ($str, $position = 'text') {
1157 switch (strtolower($position)) {
1159 if (!preg_match('/[\200-\377]/', $str)) {
1160 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1161 $encoded = addcslashes($str, "\0..\37\177\\\"");
1163 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1166 return ("\"$encoded\"");
1168 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1171 $x = preg_match_all('/[()"]/', $str, $matches);
1175 $x +
= preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1182 $maxlen = 75 - 7 - strlen($this->CharSet
);
1183 // Try to select the encoding which should produce the shortest output
1184 if (strlen($str)/3 < $x) {
1186 $encoded = base64_encode($str);
1187 $maxlen -= $maxlen %
4;
1188 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1191 $encoded = $this->EncodeQ($str, $position);
1192 $encoded = $this->WrapText($encoded, $maxlen, true);
1193 $encoded = str_replace("=".$this->LE
, "\n", trim($encoded));
1196 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet
."?$encoding?\\1?=", $encoded);
1197 $encoded = trim(str_replace("\n", $this->LE
, $encoded));
1203 * Encode string to quoted-printable.
1207 function EncodeQP ($str) {
1208 $encoded = $this->FixEOL($str);
1209 if (substr($encoded, -(strlen($this->LE
))) != $this->LE
)
1210 $encoded .= $this->LE
;
1212 // Replace every high ascii, control and = characters
1213 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
1214 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1215 // Replace every spaces and tabs when it's the last character on a line
1216 $encoded = preg_replace("/([\011\040])".$this->LE
."/e",
1217 "'='.sprintf('%02X', ord('\\1')).'".$this->LE
."'", $encoded);
1219 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1220 $encoded = $this->WrapText($encoded, 74, true);
1226 * Encode string to q encoding.
1230 function EncodeQ ($str, $position = "text") {
1231 // There should not be any EOL in the string
1232 $encoded = preg_replace("[\r\n]", "", $str);
1234 switch (strtolower($position)) {
1236 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1239 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1242 // Replace every high ascii, control =, ? and _ characters
1243 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1244 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1248 // Replace every spaces to _ (more readable than =20)
1249 $encoded = str_replace(" ", "_", $encoded);
1255 * Adds a string or binary attachment (non-filesystem) to the list.
1256 * This method can be used to attach ascii or binary data,
1257 * such as a BLOB record from a database.
1258 * @param string $string String attachment data.
1259 * @param string $filename Name of the attachment.
1260 * @param string $encoding File encoding (see $Encoding).
1261 * @param string $type File extension (MIME) type.
1264 function AddStringAttachment($string, $filename, $encoding = "base64",
1265 $type = "application/octet-stream") {
1266 // Append to $attachment array
1267 $cur = count($this->attachment
);
1268 $this->attachment
[$cur][0] = $string;
1269 $this->attachment
[$cur][1] = $filename;
1270 $this->attachment
[$cur][2] = $filename;
1271 $this->attachment
[$cur][3] = $encoding;
1272 $this->attachment
[$cur][4] = $type;
1273 $this->attachment
[$cur][5] = true; // isString
1274 $this->attachment
[$cur][6] = "attachment";
1275 $this->attachment
[$cur][7] = 0;
1279 * Adds an embedded attachment. This can include images, sounds, and
1280 * just about any other document. Make sure to set the $type to an
1281 * image type. For JPEG images use "image/jpeg" and for GIF images
1283 * @param string $path Path to the attachment.
1284 * @param string $cid Content ID of the attachment. Use this to identify
1285 * the Id for accessing the image in an HTML form.
1286 * @param string $name Overrides the attachment name.
1287 * @param string $encoding File encoding (see $Encoding).
1288 * @param string $type File extension (MIME) type.
1291 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1292 $type = "application/octet-stream") {
1294 if(!@is_file
($path))
1296 $this->SetError($this->Lang("file_access") . $path);
1300 $filename = basename($path);
1304 // Append to $attachment array
1305 $cur = count($this->attachment
);
1306 $this->attachment
[$cur][0] = $path;
1307 $this->attachment
[$cur][1] = $filename;
1308 $this->attachment
[$cur][2] = $name;
1309 $this->attachment
[$cur][3] = $encoding;
1310 $this->attachment
[$cur][4] = $type;
1311 $this->attachment
[$cur][5] = false; // isStringAttachment
1312 $this->attachment
[$cur][6] = "inline";
1313 $this->attachment
[$cur][7] = $cid;
1319 * Returns true if an inline attachment is present.
1323 function InlineImageExists() {
1325 for($i = 0; $i < count($this->attachment
); $i++
)
1327 if($this->attachment
[$i][6] == "inline")
1337 /////////////////////////////////////////////////
1338 // MESSAGE RESET METHODS
1339 /////////////////////////////////////////////////
1342 * Clears all recipients assigned in the TO array. Returns void.
1345 function ClearAddresses() {
1346 $this->to
= array();
1350 * Clears all recipients assigned in the CC array. Returns void.
1353 function ClearCCs() {
1354 $this->cc
= array();
1358 * Clears all recipients assigned in the BCC array. Returns void.
1361 function ClearBCCs() {
1362 $this->bcc
= array();
1366 * Clears all recipients assigned in the ReplyTo array. Returns void.
1369 function ClearReplyTos() {
1370 $this->ReplyTo
= array();
1374 * Clears all recipients assigned in the TO, CC and BCC
1375 * array. Returns void.
1378 function ClearAllRecipients() {
1379 $this->to
= array();
1380 $this->cc
= array();
1381 $this->bcc
= array();
1385 * Clears all previously set filesystem, string, and binary
1386 * attachments. Returns void.
1389 function ClearAttachments() {
1390 $this->attachment
= array();
1394 * Clears all custom headers. Returns void.
1397 function ClearCustomHeaders() {
1398 $this->CustomHeader
= array();
1402 /////////////////////////////////////////////////
1403 // MISCELLANEOUS METHODS
1404 /////////////////////////////////////////////////
1407 * Adds the error message to the error container.
1412 function SetError($msg) {
1413 $this->error_count++
;
1414 $this->ErrorInfo
= $msg;
1418 * Returns the proper RFC 822 formatted date.
1422 function RFCDate() {
1424 $tzs = ($tz < 0) ?
"-" : "+";
1426 $tz = ($tz/3600)*100 +
($tz%3600
)/60;
1427 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1433 * Returns the appropriate server variable. Should work with both
1434 * PHP 4.1.0+ as well as older versions. Returns an empty string
1435 * if nothing is found.
1439 function ServerVar($varName) {
1440 global $HTTP_SERVER_VARS;
1441 global $HTTP_ENV_VARS;
1443 if(!isset($_SERVER))
1445 $_SERVER = $HTTP_SERVER_VARS;
1446 if(!isset($_SERVER["REMOTE_ADDR"]))
1447 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1450 if(isset($_SERVER[$varName]))
1451 return $_SERVER[$varName];
1457 * Returns the server hostname or 'localhost.localdomain' if unknown.
1461 function ServerHostname() {
1462 if ($this->Hostname
!= "")
1463 $result = $this->Hostname
;
1464 elseif ($this->ServerVar('SERVER_NAME') != "")
1465 $result = $this->ServerVar('SERVER_NAME');
1467 $result = "localhost.localdomain";
1473 * Returns a message in the appropriate language.
1477 function Lang($key) {
1478 if(count($this->language
) < 1)
1479 $this->SetLanguage("en"); // set the default language
1481 if(isset($this->language
[$key]))
1482 return $this->language
[$key];
1484 return "Language string failed to load: " . $key;
1488 * Returns true if an error occurred.
1491 function IsError() {
1492 return ($this->error_count
> 0);
1496 * Changes every end of line from CR or LF to CRLF.
1500 function FixEOL($str) {
1501 $str = str_replace("\r\n", "\n", $str);
1502 $str = str_replace("\r", "\n", $str);
1503 $str = str_replace("\n", $this->LE
, $str);
1508 * Adds a custom header.
1511 function AddCustomHeader($custom_header) {
1512 $this->CustomHeader
[] = explode(":", $custom_header, 2);