remove obsolete frame divider images
[openemr.git] / library / classes / class.phpmailer.php
blobc3e93b879ee3a27a6b5a81d04df4b9a5ae06102b
1 <?php
2 ////////////////////////////////////////////////////
3 // PHPMailer - PHP email class
4 //
5 // Class for sending email using either
6 // sendmail, PHP mail(), or SMTP. Methods are
7 // based upon the standard AspEmail(tm) classes.
8 //
9 // Copyright (C) 2001 - 2003 Brent R. Matzelle
11 // License: LGPL, see LICENSE
12 ////////////////////////////////////////////////////
14 /**
15 * PHPMailer - PHP email transport class
16 * @package PHPMailer
17 * @author Brent R. Matzelle
18 * @copyright 2001 - 2003 Brent R. Matzelle
20 class PHPMailer
22 /////////////////////////////////////////////////
23 // PUBLIC VARIABLES
24 /////////////////////////////////////////////////
26 /**
27 * Email priority (1 = High, 3 = Normal, 5 = low).
28 * @var int
30 var $Priority = 3;
32 /**
33 * Sets the CharSet of the message.
34 * @var string
36 var $CharSet = "iso-8859-1";
38 /**
39 * Sets the Content-type of the message.
40 * @var string
42 var $ContentType = "text/plain";
44 /**
45 * Sets the Encoding of the message. Options for this are "8bit",
46 * "7bit", "binary", "base64", and "quoted-printable".
47 * @var string
49 var $Encoding = "8bit";
51 /**
52 * Holds the most recent mailer error message.
53 * @var string
55 var $ErrorInfo = "";
57 /**
58 * Sets the From email address for the message.
59 * @var string
61 var $From = "root@localhost";
63 /**
64 * Sets the From name of the message.
65 * @var string
67 var $FromName = "Root User";
69 /**
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.
72 * @var string
74 var $Sender = "";
76 /**
77 * Sets the Subject of the message.
78 * @var string
80 var $Subject = "";
82 /**
83 * Sets the Body of the message. This can be either an HTML or text body.
84 * If HTML then run IsHTML(true).
85 * @var string
87 var $Body = "";
89 /**
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.
94 * @var string
96 var $AltBody = "";
98 /**
99 * Sets word wrapping on the body of the message to a given number of
100 * characters.
101 * @var int
103 var $WordWrap = 0;
106 * Method to send mail: ("mail", "sendmail", or "smtp").
107 * @var string
109 var $Mailer = "mail";
112 * Sets the path of the sendmail program.
113 * @var string
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.
120 * @var string
122 var $PluginDir = "";
125 * Holds PHPMailer version.
126 * @var string
128 var $Version = "1.71";
131 * Sets the email address that a reading confirmation will be sent.
132 * @var string
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'.
140 * @var string
142 var $Hostname = "";
145 /////////////////////////////////////////////////
146 // SMTP VARIABLES
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.
155 * @var string
157 var $Host = "localhost";
160 * Sets the default SMTP server port.
161 * @var int
163 var $Port = 25;
166 * Sets the SMTP HELO of the message (Default is $Hostname).
167 * @var string
169 var $Helo = "";
172 * Sets SMTP authentication. Utilizes the Username and Password variables.
173 * @var bool
175 var $SMTPAuth = false;
178 * Sets SMTP username.
179 * @var string
181 var $Username = "";
184 * Sets SMTP password.
185 * @var string
187 var $Password = "";
190 * Sets the SMTP server timeout in seconds. This function will not
191 * work with the win32 version.
192 * @var int
194 var $Timeout = 10;
197 * Sets SMTP class debugging on or off.
198 * @var bool
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().
206 * @var bool
208 var $SMTPKeepAlive = false;
210 /**#@+
211 * @access private
213 var $smtp = NULL;
214 var $to = array();
215 var $cc = array();
216 var $bcc = array();
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;
224 var $LE = "\n";
225 /**#@-*/
227 /////////////////////////////////////////////////
228 // VARIABLE METHODS
229 /////////////////////////////////////////////////
232 * Sets message type to HTML.
233 * @param bool $bool
234 * @return void
236 function IsHTML($bool) {
237 if($bool == true)
238 $this->ContentType = "text/html";
239 else
240 $this->ContentType = "text/plain";
244 * Sets Mailer to send message using SMTP.
245 * @return void
247 function IsSMTP() {
248 $this->Mailer = "smtp";
252 * Sets Mailer to send message using PHP mail() function.
253 * @return void
255 function IsMail() {
256 $this->Mailer = "mail";
260 * Sets Mailer to send message using the $Sendmail program.
261 * @return void
263 function IsSendmail() {
264 $this->Mailer = "sendmail";
268 * Sets Mailer to send message using the qmail MTA.
269 * @return void
271 function IsQmail() {
272 $this->Sendmail = "/var/qmail/bin/sendmail";
273 $this->Mailer = "sendmail";
277 /////////////////////////////////////////////////
278 // RECIPIENT METHODS
279 /////////////////////////////////////////////////
282 * Adds a "To" address.
283 * @param string $address
284 * @param string $name
285 * @return void
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"
296 * mailer.
297 * @param string $address
298 * @param string $name
299 * @return void
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"
310 * mailer.
311 * @param string $address
312 * @param string $name
313 * @return void
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
325 * @return void
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.
342 * @return bool
344 function Send() {
345 $header = "";
346 $body = "";
348 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
350 $this->SetError($this->Lang("provide_address"));
351 return false;
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; }
364 // Choose the mailer
365 if($this->Mailer == "sendmail")
367 if(!$this->SendmailSend($header, $body))
368 return false;
370 elseif($this->Mailer == "mail")
372 if(!$this->MailSend($header, $body))
373 return false;
375 elseif($this->Mailer == "smtp")
377 if(!$this->SmtpSend($header, $body))
378 return false;
380 else
382 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
383 return false;
386 return true;
390 * Sends mail using the $Sendmail program.
391 * @access private
392 * @return bool
394 function SendmailSend($header, $body) {
395 if ($this->Sender != "")
396 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
397 else
398 $sendmail = sprintf("%s -oi -t", $this->Sendmail);
400 if(!@$mail = popen($sendmail, "w"))
402 $this->SetError($this->Lang("execute") . $this->Sendmail);
403 return false;
406 fputs($mail, $header);
407 fputs($mail, $body);
409 $result = pclose($mail) >> 8 & 0xFF;
410 if($result != 0)
412 $this->SetError($this->Lang("execute") . $this->Sendmail);
413 return false;
416 return true;
420 * Sends mail using the PHP mail() function.
421 * @access private
422 * @return bool
424 function MailSend($header, $body) {
425 $to = "";
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,
438 $header, $params);
440 else
441 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
443 if (isset($old_from))
444 ini_set("sendmail_from", $old_from);
446 if(!$rt)
448 $this->SetError($this->Lang("instantiate"));
449 return false;
452 return true;
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.
459 * @access private
460 * @return bool
462 function SmtpSend($header, $body) {
463 require_once($this->PluginDir . "class.smtp.php");
464 $error = "";
465 $bad_rcpt = array();
467 if(!$this->SmtpConnect())
468 return false;
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();
476 return false;
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();
506 return false;
509 if(!$this->smtp->Data($header . $body))
511 $this->SetError($this->Lang("data_not_accepted"));
512 $this->smtp->Reset();
513 return false;
515 if($this->SMTPKeepAlive == true)
516 $this->smtp->Reset();
517 else
518 $this->SmtpClose();
520 return true;
524 * Initiates a connection to an SMTP server. Returns false if the
525 * operation failed.
526 * @access private
527 * @return bool
529 function SmtpConnect() {
530 if($this->smtp == NULL) { $this->smtp = new SMTP(); }
532 $this->smtp->do_debug = $this->SMTPDebug;
533 $hosts = explode(";", $this->Host);
534 $index = 0;
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]);
542 else
544 $host = $hosts[$index];
545 $port = $this->Port;
548 if($this->smtp->Connect($host, $port, $this->Timeout))
550 if ($this->Helo != '')
551 $this->smtp->Hello($this->Helo);
552 else
553 $this->smtp->Hello($this->ServerHostname());
555 if($this->SMTPAuth)
557 if(!$this->smtp->Authenticate($this->Username,
558 $this->Password))
560 $this->SetError($this->Lang("authenticate"));
561 $this->smtp->Reset();
562 $connection = false;
565 $connection = true;
567 $index++;
569 if(!$connection)
570 $this->SetError($this->Lang("connect_host"));
572 return $connection;
576 * Closes the active SMTP session if one exists.
577 * @return void
579 function SmtpClose() {
580 if($this->smtp != NULL)
582 if($this->smtp->Connected())
584 $this->smtp->Quit();
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
593 * is English.
594 * @param string $lang_type Type of language (e.g. Portuguese: "br")
595 * @param string $lang_path Path to the language file directory
596 * @access public
597 * @return bool
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');
604 else
606 $this->SetError("Could not load language file");
607 return false;
609 $this->language = $PHPMAILER_LANG;
611 return true;
614 /////////////////////////////////////////////////
615 // MESSAGE CREATION METHODS
616 /////////////////////////////////////////////////
619 * Creates recipient headers.
620 * @access private
621 * @return string
623 function AddrAppend($type, $addr) {
624 $addr_str = $type . ": ";
625 $addr_str .= $this->AddrFormat($addr[0]);
626 if(count($addr) > 1)
628 for($i = 1; $i < count($addr); $i++)
629 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
631 $addr_str .= $this->LE;
633 return $addr_str;
637 * Formats an address correctly.
638 * @access private
639 * @return string
641 function AddrFormat($addr) {
642 if(empty($addr[1]))
643 $formatted = $addr[0];
644 else
646 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
647 $addr[0] . ">";
650 return $formatted;
654 * Wraps message for use with mailers that do not
655 * automatically perform wrapping and for quoted-printable.
656 * Original written by philippe.
657 * @access private
658 * @return string
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);
668 $message = "";
669 for ($i=0 ;$i < count($line); $i++)
671 $line_part = explode(" ", $line[$i]);
672 $buf = "";
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;
679 if ($e != 0)
681 if ($space_left > 20)
683 $len = $space_left;
684 if (substr($word, $len - 1, 1) == "=")
685 $len--;
686 elseif (substr($word, $len - 2, 1) == "=")
687 $len -= 2;
688 $part = substr($word, 0, $len);
689 $word = substr($word, $len);
690 $buf .= " " . $part;
691 $message .= $buf . sprintf("=%s", $this->LE);
693 else
695 $message .= $buf . $soft_break;
697 $buf = "";
699 while (strlen($word) > 0)
701 $len = $length;
702 if (substr($word, $len - 1, 1) == "=")
703 $len--;
704 elseif (substr($word, $len - 2, 1) == "=")
705 $len -= 2;
706 $part = substr($word, 0, $len);
707 $word = substr($word, $len);
709 if (strlen($word) > 0)
710 $message .= $part . sprintf("=%s", $this->LE);
711 else
712 $buf = $part;
715 else
717 $buf_o = $buf;
718 $buf .= ($e == 0) ? $word : (" " . $word);
720 if (strlen($buf) > $length and $buf_o != "")
722 $message .= $buf_o . $soft_break;
723 $buf = $word;
727 $message .= $buf . $this->LE;
730 return $message;
734 * Set the body wrapping.
735 * @access private
736 * @return void
738 function SetWordWrap() {
739 if($this->WordWrap < 1)
740 return;
742 switch($this->message_type)
744 case "alt":
745 // fall through
746 case "alt_attachment":
747 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
748 break;
749 default:
750 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
751 break;
756 * Assembles message header.
757 * @access private
758 * @return string
760 function CreateHeader() {
761 $result = "";
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));
772 else
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);
786 $from = array();
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)
822 case "plain":
823 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
824 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
825 $this->ContentType, $this->CharSet);
826 break;
827 case "attachments":
828 // fall through
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);
836 else
838 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
839 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
841 break;
842 case "alt":
843 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
844 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
845 break;
848 if($this->Mailer != "mail")
849 $result .= $this->LE.$this->LE;
851 return $result;
855 * Assembles the message body. Returns an empty string on failure.
856 * @access private
857 * @return string
859 function CreateBody() {
860 $result = "";
862 $this->SetWordWrap();
864 switch($this->message_type)
866 case "alt":
867 $result .= $this->GetBoundary($this->boundary[1], "",
868 "text/plain", "");
869 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
870 $result .= $this->LE.$this->LE;
871 $result .= $this->GetBoundary($this->boundary[1], "",
872 "text/html", "");
874 $result .= $this->EncodeString($this->Body, $this->Encoding);
875 $result .= $this->LE.$this->LE;
877 $result .= $this->EndBoundary($this->boundary[1]);
878 break;
879 case "plain":
880 $result .= $this->EncodeString($this->Body, $this->Encoding);
881 break;
882 case "attachments":
883 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
884 $result .= $this->EncodeString($this->Body, $this->Encoding);
885 $result .= $this->LE;
887 $result .= $this->AttachAll();
888 break;
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);
896 // Create text body
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();
913 break;
915 if($this->IsError())
916 $result = "";
918 return $result;
922 * Returns the start of a message boundary.
923 * @access private
925 function GetBoundary($boundary, $charSet, $contentType, $encoding) {
926 $result = "";
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;
938 return $result;
942 * Returns the end of a message boundary.
943 * @access private
945 function EndBoundary($boundary) {
946 return $this->LE . "--" . $boundary . "--" . $this->LE;
950 * Sets the message type.
951 * @access private
952 * @return void
954 function SetMessageType() {
955 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
956 $this->message_type = "plain";
957 else
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.
970 * @access private
971 * @return string
973 function HeaderLine($name, $value) {
974 return $name . ": " . $value . $this->LE;
978 * Returns a formatted mail line.
979 * @access private
980 * @return string
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
993 * or accessed.
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.
998 * @return bool
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);
1005 return false;
1008 $filename = basename($path);
1009 if($name == "")
1010 $name = $filename;
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;
1022 return true;
1026 * Attaches all fs, string, and binary attachments to the message.
1027 * Returns an empty string on failure.
1028 * @access private
1029 * @return string
1031 function AttachAll() {
1032 // Return text of body
1033 $mime = array();
1035 // Add all attachments
1036 for($i = 0; $i < count($this->attachment); $i++)
1038 // Check for string attachment
1039 $bString = $this->attachment[$i][5];
1040 if ($bString)
1041 $string = $this->attachment[$i][0];
1042 else
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
1063 if($bString)
1065 $mime[] = $this->EncodeString($string, $encoding);
1066 if($this->IsError()) { return ""; }
1067 $mime[] = $this->LE.$this->LE;
1069 else
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.
1085 * @access private
1086 * @return string
1088 function EncodeFile ($path, $encoding = "base64") {
1089 if(!@$fd = fopen($path, "rb"))
1091 $this->SetError($this->Lang("file_open") . $path);
1092 return "";
1094 $file_buffer = fread($fd, filesize($path));
1095 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1096 fclose($fd);
1098 return $file_buffer;
1102 * Encodes string to requested format. Returns an
1103 * empty string on failure.
1104 * @access private
1105 * @return string
1107 function EncodeString ($str, $encoding = "base64") {
1108 $encoded = "";
1109 switch(strtolower($encoding)) {
1110 case "base64":
1111 // chunk_split is found in PHP >= 3.0.6
1112 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
1113 break;
1114 case "7bit":
1115 case "8bit":
1116 $encoded = $this->FixEOL($str);
1117 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1118 $encoded .= $this->LE;
1119 break;
1120 case "binary":
1121 $encoded = $str;
1122 break;
1123 case "quoted-printable":
1124 $encoded = $this->EncodeQP($str);
1125 break;
1126 default:
1127 $this->SetError($this->Lang("encoding") . $encoding);
1128 break;
1130 return $encoded;
1134 * Encode a header string to best of Q, B, quoted or none.
1135 * @access private
1136 * @return string
1138 function EncodeHeader ($str, $position = 'text') {
1139 $x = 0;
1141 switch (strtolower($position)) {
1142 case 'phrase':
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))
1148 return ($encoded);
1149 else
1150 return ("\"$encoded\"");
1152 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1153 break;
1154 case 'comment':
1155 $x = preg_match_all('/[()"]/', $str, $matches);
1156 // Fall-through
1157 case 'text':
1158 default:
1159 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1160 break;
1163 if ($x == 0)
1164 return ($str);
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) {
1169 $encoding = 'B';
1170 $encoded = base64_encode($str);
1171 $maxlen -= $maxlen % 4;
1172 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1173 } else {
1174 $encoding = 'Q';
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));
1183 return $encoded;
1187 * Encode string to quoted-printable.
1188 * @access private
1189 * @return string
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);
1206 return $encoded;
1210 * Encode string to q encoding.
1211 * @access private
1212 * @return string
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)) {
1219 case "phrase":
1220 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1221 break;
1222 case "comment":
1223 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1224 case "text":
1225 default:
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);
1229 break;
1232 // Replace every spaces to _ (more readable than =20)
1233 $encoded = str_replace(" ", "_", $encoded);
1235 return $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.
1246 * @return void
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
1266 * use "image/gif".
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.
1273 * @return bool
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);
1281 return false;
1284 $filename = basename($path);
1285 if($name == "")
1286 $name = $filename;
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;
1299 return true;
1303 * Returns true if an inline attachment is present.
1304 * @access private
1305 * @return bool
1307 function InlineImageExists() {
1308 $result = false;
1309 for($i = 0; $i < count($this->attachment); $i++)
1311 if($this->attachment[$i][6] == "inline")
1313 $result = true;
1314 break;
1318 return $result;
1321 /////////////////////////////////////////////////
1322 // MESSAGE RESET METHODS
1323 /////////////////////////////////////////////////
1326 * Clears all recipients assigned in the TO array. Returns void.
1327 * @return void
1329 function ClearAddresses() {
1330 $this->to = array();
1334 * Clears all recipients assigned in the CC array. Returns void.
1335 * @return void
1337 function ClearCCs() {
1338 $this->cc = array();
1342 * Clears all recipients assigned in the BCC array. Returns void.
1343 * @return void
1345 function ClearBCCs() {
1346 $this->bcc = array();
1350 * Clears all recipients assigned in the ReplyTo array. Returns void.
1351 * @return void
1353 function ClearReplyTos() {
1354 $this->ReplyTo = array();
1358 * Clears all recipients assigned in the TO, CC and BCC
1359 * array. Returns void.
1360 * @return 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.
1371 * @return void
1373 function ClearAttachments() {
1374 $this->attachment = array();
1378 * Clears all custom headers. Returns void.
1379 * @return void
1381 function ClearCustomHeaders() {
1382 $this->CustomHeader = array();
1386 /////////////////////////////////////////////////
1387 // MISCELLANEOUS METHODS
1388 /////////////////////////////////////////////////
1391 * Adds the error message to the error container.
1392 * Returns void.
1393 * @access private
1394 * @return void
1396 function SetError($msg) {
1397 $this->error_count++;
1398 $this->ErrorInfo = $msg;
1402 * Returns the proper RFC 822 formatted date.
1403 * @access private
1404 * @return string
1406 function RFCDate() {
1407 $tz = date("Z");
1408 $tzs = ($tz < 0) ? "-" : "+";
1409 $tz = abs($tz);
1410 $tz = ($tz/3600)*100 + ($tz%3600)/60;
1411 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1413 return $result;
1417 * Returns Received header for message tracing.
1418 * @access private
1419 * @return string
1421 function Received() {
1422 if ($this->ServerVar('SERVER_NAME') != '')
1424 $protocol = ($this->ServerVar('HTTPS') == 'on') ? 'HTTPS' : 'HTTP';
1425 $remote = $this->ServerVar('REMOTE_HOST');
1426 if($remote == "")
1427 $remote = 'phpmailer';
1428 $remote .= ' (['.$this->ServerVar('REMOTE_ADDR').'])';
1430 else
1432 $protocol = 'local';
1433 $remote = $this->ServerVar('USER');
1434 if($remote == '')
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);
1443 return $result;
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.
1450 * @access private
1451 * @return mixed
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];
1466 else
1467 return "";
1471 * Returns the server hostname or 'localhost.localdomain' if unknown.
1472 * @access private
1473 * @return string
1475 function ServerHostname() {
1476 if ($this->Hostname != "")
1477 $result = $this->Hostname;
1478 elseif ($this->ServerVar('SERVER_NAME') != "")
1479 $result = $this->ServerVar('SERVER_NAME');
1480 else
1481 $result = "localhost.localdomain";
1483 return $result;
1487 * Returns a message in the appropriate language.
1488 * @access private
1489 * @return string
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];
1497 else
1498 return "Language string failed to load: " . $key;
1502 * Returns true if an error occurred.
1503 * @return bool
1505 function IsError() {
1506 return ($this->error_count > 0);
1510 * Changes every end of line from CR or LF to CRLF.
1511 * @access private
1512 * @return string
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);
1518 return $str;
1522 * Adds a custom header.
1523 * @return void
1525 function AddCustomHeader($custom_header) {
1526 $this->CustomHeader[] = explode(":", $custom_header, 2);