MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
class.phpmailer.php
Ir para a documentação deste ficheiro.
1<?php
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
10//
11// License: LGPL, see LICENSE
13
21{
23 // PUBLIC VARIABLES
25
30 var $Priority = 3;
31
36 var $CharSet = "iso-8859-1";
37
42 var $ContentType = "text/plain";
43
49 var $Encoding = "8bit";
50
55 var $ErrorInfo = "";
56
61 var $From = "root@localhost";
62
67 var $FromName = "Root User";
68
74 var $Sender = "";
75
80 var $Subject = "";
81
87 var $Body = "";
88
96 var $AltBody = "";
97
103 var $WordWrap = 0;
104
109 var $Mailer = "mail";
110
115 var $Sendmail = "/usr/sbin/sendmail";
116
122 var $PluginDir = "";
123
128 var $Version = "1.71";
129
135
142 var $Hostname = "";
143
145 // SMTP VARIABLES
147
156 var $Host = "localhost";
157
162 var $Port = 25;
163
168 var $Helo = "";
169
174 var $SMTPAuth = false;
175
180 var $Username = "";
181
186 var $Password = "";
187
193 var $Timeout = 10;
194
199 var $SMTPDebug = false;
200
207 var $SMTPKeepAlive = false;
208
212 var $smtp = NULL;
213 var $to = array(
214 );
215
216 var $cc = array(
217 );
218
219 var $bcc = array(
220 );
221
222 var $ReplyTo = array(
223 );
224
225 var $attachment = array(
226 );
227
228 var $CustomHeader = array(
229 );
230
232 var $boundary = array(
233 );
234
235 var $language = array(
236 );
237
239 var $LE = "\n";
243 // VARIABLE METHODS
245
251 function IsHTML($bool)
252 {
253 if ($bool == true)
254 $this->ContentType = "text/html";
255 else
256 $this->ContentType = "text/plain";
257 }
258
263 function IsSMTP()
264 {
265 $this->Mailer = "smtp";
266 }
267
272 function IsMail()
273 {
274 $this->Mailer = "mail";
275 }
276
281 function IsSendmail()
282 {
283 $this->Mailer = "sendmail";
284 }
285
290 function IsQmail()
291 {
292 $this->Sendmail = "/var/qmail/bin/sendmail";
293 $this->Mailer = "sendmail";
294 }
295
297 // RECIPIENT METHODS
299
306 function AddAddress($address, $name = "")
307 {
308 $cur = count($this->to);
309 $this->to[$cur][0] = trim($address);
310 $this->to[$cur][1] = $name;
311 }
312
321 function AddCC($address, $name = "")
322 {
323 $cur = count($this->cc);
324 $this->cc[$cur][0] = trim($address);
325 $this->cc[$cur][1] = $name;
326 }
327
336 function AddBCC($address, $name = "")
337 {
338 $cur = count($this->bcc);
339 $this->bcc[$cur][0] = trim($address);
340 $this->bcc[$cur][1] = $name;
341 }
342
349 function AddReplyTo($address, $name = "")
350 {
351 $cur = count($this->ReplyTo);
352 $this->ReplyTo[$cur][0] = trim($address);
353 $this->ReplyTo[$cur][1] = $name;
354 }
355
357 // MAIL SENDING METHODS
359
366 function Send()
367 {
368 $header = "";
369 $body = "";
370
371 if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
372 {
373 $this->SetError($this->Lang("provide_address"));
374 return false;
375 }
376
377 // Set whether the message is multipart/alternative
378 if (!empty($this->AltBody))
379 $this->ContentType = "multipart/alternative";
380
381 $this->SetMessageType();
382 $header .= $this->CreateHeader();
383 $body = $this->CreateBody();
384
385 if ($body == "")
386 {
387 return false;
388 }
389
390 // Choose the mailer
391 if ($this->Mailer == "sendmail")
392 {
393 if (!$this->SendmailSend($header, $body))
394 return false;
395 }
396 elseif ($this->Mailer == "mail")
397 {
398 if (!$this->MailSend($header, $body))
399 return false;
400 }
401 elseif ($this->Mailer == "smtp")
402 {
403 if (!$this->SmtpSend($header, $body))
404 return false;
405 }
406 else
407 {
408 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
409 return false;
410 }
411
412 return true;
413 }
414
420 function SendmailSend($header, $body)
421 {
422 if ($this->Sender != "")
423 $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
424 else
425 $sendmail = sprintf("%s -oi -t", $this->Sendmail);
426
427 if (!@$mail = popen($sendmail, "w"))
428 {
429 $this->SetError($this->Lang("execute") . $this->Sendmail);
430 return false;
431 }
432
433 fputs($mail, $header);
434 fputs($mail, $body);
435
436 $result = pclose($mail) >> 8 & 0xFF;
437
438 if ($result != 0)
439 {
440 $this->SetError($this->Lang("execute") . $this->Sendmail);
441 return false;
442 }
443
444 return true;
445 }
446
452 function MailSend($header, $body)
453 {
454 $to = "";
455
456 for ($i = 0; $i < count($this->to); $i++)
457 {
458 if ($i != 0)
459 {
460 $to .= ", ";
461 }
462
463 $to .= $this->to[$i][0];
464 }
465
466 if ($this->Sender != "" && strlen(ini_get("safe_mode")) < 1)
467 {
468 $old_from = ini_get("sendmail_from");
469 ini_set("sendmail_from", $this->Sender);
470 $params = sprintf("-oi -f %s", $this->Sender);
471 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header, $params);
472 }
473 else
474 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
475
476 if (isset($old_from))
477 ini_set("sendmail_from", $old_from);
478
479 if (!$rt)
480 {
481 $this->SetError($this->Lang("instantiate"));
482 return false;
483 }
484
485 return true;
486 }
487
495 function SmtpSend($header, $body)
496 {
497 include_once ($this->PluginDir . "class.smtp.php");
498 $error = "";
499 $bad_rcpt = array(
500 );
501
502 if (!$this->SmtpConnect())
503 return false;
504
505 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
506
507 if (!$this->smtp->Mail($smtp_from))
508 {
509 $error = $this->Lang("from_failed") . $smtp_from;
510 $this->SetError($error);
511 $this->smtp->Reset();
512 return false;
513 }
514
515 // Attempt to send attach all recipients
516 for ($i = 0; $i < count($this->to); $i++)
517 {
518 if (!$this->smtp->Recipient($this->to[$i][0]))
519 $bad_rcpt[] = $this->to[$i][0];
520 }
521
522 for ($i = 0; $i < count($this->cc); $i++)
523 {
524 if (!$this->smtp->Recipient($this->cc[$i][0]))
525 $bad_rcpt[] = $this->cc[$i][0];
526 }
527
528 for ($i = 0; $i < count($this->bcc); $i++)
529 {
530 if (!$this->smtp->Recipient($this->bcc[$i][0]))
531 $bad_rcpt[] = $this->bcc[$i][0];
532 }
533
534 if (count($bad_rcpt) > 0) // Create error message
535 {
536 for ($i = 0; $i < count($bad_rcpt); $i++)
537 {
538 if ($i != 0)
539 {
540 $error .= ", ";
541 }
542
543 $error .= $bad_rcpt[$i];
544 }
545
546 $error = $this->Lang("recipients_failed") . $error;
547 $this->SetError($error);
548 $this->smtp->Reset();
549 return false;
550 }
551
552 if (!$this->smtp->Data($header . $body))
553 {
554 $this->SetError($this->Lang("data_not_accepted"));
555 $this->smtp->Reset();
556 return false;
557 }
558
559 if ($this->SMTPKeepAlive == true)
560 $this->smtp->Reset();
561 else
562 $this->SmtpClose();
563
564 return true;
565 }
566
573 function SmtpConnect()
574 {
575 if ($this->smtp == NULL)
576 {
577 $this->smtp = new SMTP();
578 }
579
580 $this->smtp->do_debug = $this->SMTPDebug;
581 $hosts = explode(";", $this->Host);
582 $index = 0;
583 $connection = ($this->smtp->Connected());
584
585 // Retry while there is no connection
586 while ($index < count($hosts) && $connection == false)
587 {
588 if (strstr($hosts[$index], ":"))
589 list($host, $port) = explode(":", $hosts[$index]);
590 else
591 {
592 $host = $hosts[$index];
593 $port = $this->Port;
594 }
595
596 if ($this->smtp->Connect($host, $port, $this->Timeout))
597 {
598 if ($this->Helo != '')
599 $this->smtp->Hello($this->Helo);
600 else
601 $this->smtp->Hello($this->ServerHostname());
602
603 if ($this->SMTPAuth)
604 {
605 if (!$this->smtp->Authenticate($this->Username, $this->Password))
606 {
607 $this->SetError($this->Lang("authenticate"));
608 $this->smtp->Reset();
609 $connection = false;
610 }
611 }
612
613 $connection = true;
614 }
615
616 $index++;
617 }
618
619 if (!$connection)
620 $this->SetError($this->Lang("connect_host"));
621
622 return $connection;
623 }
624
629 function SmtpClose()
630 {
631 if ($this->smtp != NULL)
632 {
633 if ($this->smtp->Connected())
634 {
635 $this->smtp->Quit();
636 $this->smtp->Close();
637 }
638 }
639 }
640
650 function SetLanguage($lang_type, $lang_path = "")
651 {
652 if (file_exists($lang_path . 'phpmailer.lang-' . $lang_type . '.php'))
653 include ($lang_path . 'phpmailer.lang-' . $lang_type . '.php');
654
655 else if (file_exists($lang_path . 'phpmailer.lang-en.php'))
656 include ($lang_path . 'phpmailer.lang-en.php');
657
658 else
659 {
660 $this->SetError("Could not load language file");
661 return false;
662 }
663
664 $this->language = $PHPMAILER_LANG;
665
666 return true;
667 }
668
670 // MESSAGE CREATION METHODS
672
678 function AddrAppend($type, $addr)
679 {
680 $addr_str = $type . ": ";
681 $addr_str .= $this->AddrFormat($addr[0]);
682
683 if (count($addr) > 1)
684 {
685 for ($i = 1; $i < count($addr); $i++)
686 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
687 }
688
689 $addr_str .= $this->LE;
690
691 return $addr_str;
692 }
693
699 function AddrFormat($addr)
700 {
701 if (empty($addr[1]))
702 $formatted = $addr[0];
703 else
704 {
705 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" . $addr[0] . ">";
706 }
707
708 return $formatted;
709 }
710
718 function WrapText($message, $length, $qp_mode = false)
719 {
720 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
721
722 $message = $this->FixEOL($message);
723
724 if (substr($message, -1) == $this->LE)
725 $message = substr($message, 0, -1);
726
727 $line = explode($this->LE, $message);
728 $message = "";
729
730 for ($i = 0; $i < count($line); $i++)
731 {
732 $line_part = explode(" ", $line[$i]);
733 $buf = "";
734
735 for ($e = 0; $e < count($line_part); $e++)
736 {
737 $word = $line_part[$e];
738
739 if ($qp_mode and (strlen($word) > $length))
740 {
741 $space_left = $length - strlen($buf) - 1;
742
743 if ($e != 0)
744 {
745 if ($space_left > 20)
746 {
747 $len = $space_left;
748
749 if (substr($word, $len - 1, 1) == "=")
750 $len--;
751
752 elseif (substr($word, $len - 2, 1) == "=")
753 $len -= 2;
754
755 $part = substr($word, 0, $len);
756 $word = substr($word, $len);
757 $buf .= " " . $part;
758 $message .= $buf . sprintf("=%s", $this->LE);
759 }
760 else
761 {
762 $message .= $buf . $soft_break;
763 }
764
765 $buf = "";
766 }
767
768 while (strlen($word) > 0)
769 {
770 $len = $length;
771
772 if (substr($word, $len - 1, 1) == "=")
773 $len--;
774
775 elseif (substr($word, $len - 2, 1) == "=")
776 $len -= 2;
777
778 $part = substr($word, 0, $len);
779 $word = substr($word, $len);
780
781 if (strlen($word) > 0)
782 $message .= $part . sprintf("=%s", $this->LE);
783 else
784 $buf = $part;
785 }
786 }
787 else
788 {
789 $buf_o = $buf;
790 $buf .= ($e == 0) ? $word : (" " . $word);
791
792 if (strlen($buf) > $length and $buf_o != "")
793 {
794 $message .= $buf_o . $soft_break;
795 $buf = $word;
796 }
797 }
798 }
799
800 $message .= $buf . $this->LE;
801 }
802
803 return $message;
804 }
805
811 function SetWordWrap()
812 {
813 if ($this->WordWrap < 1)
814 return;
815
816 switch ($this->message_type)
817 {
818 case "alt":
819 // fall through
820 case "alt_attachment":
821 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
822
823 break;
824
825 default:
826 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
827
828 break;
829 }
830 }
831
837 function CreateHeader()
838 {
839 $result = "";
840
841 // Set the boundaries
842 $uniq_id = md5(uniqid(time()));
843 $this->boundary[1] = "b1_" . $uniq_id;
844 $this->boundary[2] = "b2_" . $uniq_id;
845
846 $result .= $this->Received();
847 $result .= $this->HeaderLine("Date", $this->RFCDate());
848
849 if ($this->Sender == "")
850 $result .= $this->HeaderLine("Return-Path", trim($this->From));
851 else
852 $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
853
854 // To be created automatically by mail()
855 if ($this->Mailer != "mail")
856 {
857 if (count($this->to) > 0)
858 $result .= $this->AddrAppend("To", $this->to);
859
860 else if (count($this->cc) == 0)
861 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
862
863 if (count($this->cc) > 0)
864 $result .= $this->AddrAppend("Cc", $this->cc);
865 }
866
867 $from = array(
868 );
869
870 $from[0][0] = trim($this->From);
871 $from[0][1] = $this->FromName;
872 $result .= $this->AddrAppend("From", $from);
873
874 // sendmail and mail() extract Bcc from the header before sending
875 if ((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
876 $result .= $this->AddrAppend("Bcc", $this->bcc);
877
878 if (count($this->ReplyTo) > 0)
879 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
880
881 // mail() sets the subject itself
882 if ($this->Mailer != "mail")
883 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
884
885 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
886 $result .= $this->HeaderLine("X-Priority", $this->Priority);
887 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]");
888
889 if ($this->ConfirmReadingTo != "")
890 {
891 $result .= $this->HeaderLine("Disposition-Notification-To", "<" . trim($this->ConfirmReadingTo) . ">");
892 }
893
894 // Add custom headers
895 for ($index = 0; $index < count($this->CustomHeader); $index++)
896 {
897 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
898 $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
899 }
900
901 $result .= $this->HeaderLine("MIME-Version", "1.0");
902
903 switch ($this->message_type)
904 {
905 case "plain":
906 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
907
908 $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
909 break;
910
911 case "attachments":
912 // fall through
913 case "alt_attachments":
914 if ($this->InlineImageExists())
915 {
916 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
917 "multipart/related",
918 $this->LE,
919 $this->LE,
920 $this->boundary[1],
921 $this->LE);
922 }
923 else
924 {
925 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
926 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
927 }
928
929 break;
930
931 case "alt":
932 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
933
934 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
935 break;
936 }
937
938 if ($this->Mailer != "mail")
939 $result .= $this->LE . $this->LE;
940
941 return $result;
942 }
943
949 function CreateBody()
950 {
951 $result = "";
952
953 $this->SetWordWrap();
954
955 switch ($this->message_type)
956 {
957 case "alt":
958 $result .= $this->GetBoundary($this->boundary[1], "", "text/plain", "");
959
960 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
961 $result .= $this->LE . $this->LE;
962 $result .= $this->GetBoundary($this->boundary[1], "", "text/html", "");
963
964 $result .= $this->EncodeString($this->Body, $this->Encoding);
965 $result .= $this->LE . $this->LE;
966
967 $result .= $this->EndBoundary($this->boundary[1]);
968 break;
969
970 case "plain":
971 $result .= $this->EncodeString($this->Body, $this->Encoding);
972
973 break;
974
975 case "attachments":
976 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
977
978 $result .= $this->EncodeString($this->Body, $this->Encoding);
979 $result .= $this->LE;
980
981 $result .= $this->AttachAll();
982 break;
983
984 case "alt_attachments":
985 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
986
987 $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", "multipart/alternative", $this->LE,
988 $this->boundary[2], $this->LE . $this->LE);
989
990 // Create text body
991 $result .= $this->GetBoundary($this->boundary[2], "", "text/plain", "") . $this->LE;
992
993 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
994 $result .= $this->LE . $this->LE;
995
996 // Create the HTML body
997 $result .= $this->GetBoundary($this->boundary[2], "", "text/html", "") . $this->LE;
998
999 $result .= $this->EncodeString($this->Body, $this->Encoding);
1000 $result .= $this->LE . $this->LE;
1001
1002 $result .= $this->EndBoundary($this->boundary[2]);
1003
1004 $result .= $this->AttachAll();
1005 break;
1006 }
1007
1008 if ($this->IsError())
1009 $result = "";
1010
1011 return $result;
1012 }
1013
1018 function GetBoundary($boundary, $charSet, $contentType, $encoding)
1019 {
1020 $result = "";
1021
1022 if ($charSet == "")
1023 {
1024 $charSet = $this->CharSet;
1025 }
1026
1027 if ($contentType == "")
1028 {
1029 $contentType = $this->ContentType;
1030 }
1031
1032 if ($encoding == "")
1033 {
1034 $encoding = $this->Encoding;
1035 }
1036
1037 $result .= $this->TextLine("--" . $boundary);
1038 $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
1039 $result .= $this->LE;
1040 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
1041 $result .= $this->LE;
1042
1043 return $result;
1044 }
1045
1050 function EndBoundary($boundary)
1051 {
1052 return $this->LE . "--" . $boundary . "--" . $this->LE;
1053 }
1054
1061 {
1062 if (count($this->attachment) < 1 && strlen($this->AltBody) < 1)
1063 $this->message_type = "plain";
1064 else
1065 {
1066 if (count($this->attachment) > 0)
1067 $this->message_type = "attachments";
1068
1069 if (strlen($this->AltBody) > 0 && count($this->attachment) < 1)
1070 $this->message_type = "alt";
1071
1072 if (strlen($this->AltBody) > 0 && count($this->attachment) > 0)
1073 $this->message_type = "alt_attachments";
1074 }
1075 }
1076
1082 function HeaderLine($name, $value)
1083 {
1084 return $name . ": " . $value . $this->LE;
1085 }
1086
1092 function TextLine($value)
1093 {
1094 return $value . $this->LE;
1095 }
1096
1098 // ATTACHMENT METHODS
1100
1111 function AddAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream")
1112 {
1113 if (!@is_file($path))
1114 {
1115 $this->SetError($this->Lang("file_access") . $path);
1116 return false;
1117 }
1118
1119 $filename = basename($path);
1120
1121 if ($name == "")
1122 $name = $filename;
1123
1124 $cur = count($this->attachment);
1125 $this->attachment[$cur][0] = $path;
1126 $this->attachment[$cur][1] = $filename;
1127 $this->attachment[$cur][2] = $name;
1128 $this->attachment[$cur][3] = $encoding;
1129 $this->attachment[$cur][4] = $type;
1130 $this->attachment[$cur][5] = false; // isStringAttachment
1131 $this->attachment[$cur][6] = "attachment";
1132 $this->attachment[$cur][7] = 0;
1133
1134 return true;
1135 }
1136
1143 function AttachAll()
1144 {
1145 // Return text of body
1146 $mime = array(
1147 );
1148
1149 // Add all attachments
1150 for ($i = 0; $i < count($this->attachment); $i++)
1151 {
1152 // Check for string attachment
1153 $bString = $this->attachment[$i][5];
1154
1155 if ($bString)
1156 $string = $this->attachment[$i][0];
1157 else
1158 $path = $this->attachment[$i][0];
1159
1160 $filename = $this->attachment[$i][1];
1161 $name = $this->attachment[$i][2];
1162 $encoding = $this->attachment[$i][3];
1163 $type = $this->attachment[$i][4];
1164 $disposition = $this->attachment[$i][6];
1165 $cid = $this->attachment[$i][7];
1166
1167 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
1168 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
1169 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
1170
1171 if ($disposition == "inline")
1172 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
1173
1174 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE . $this->LE);
1175
1176 // Encode as string attachment
1177 if ($bString)
1178 {
1179 $mime[] = $this->EncodeString($string, $encoding);
1180
1181 if ($this->IsError())
1182 {
1183 return "";
1184 }
1185
1186 $mime[] = $this->LE . $this->LE;
1187 }
1188 else
1189 {
1190 $mime[] = $this->EncodeFile($path, $encoding);
1191
1192 if ($this->IsError())
1193 {
1194 return "";
1195 }
1196
1197 $mime[] = $this->LE . $this->LE;
1198 }
1199 }
1200
1201 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
1202
1203 return join("", $mime);
1204 }
1205
1212 function EncodeFile($path, $encoding = "base64")
1213 {
1214 if (!@$fd = fopen($path, "rb"))
1215 {
1216 $this->SetError($this->Lang("file_open") . $path);
1217 return "";
1218 }
1219
1220 $file_buffer = fread($fd, filesize($path));
1221 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1222 fclose ($fd);
1223
1224 return $file_buffer;
1225 }
1226
1233 function EncodeString($str, $encoding = "base64")
1234 {
1235 $encoded = "";
1236
1237 switch (strtolower($encoding))
1238 {
1239 case "base64":
1240 // chunk_split is found in PHP >= 3.0.6
1241 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
1242
1243 break;
1244
1245 case "7bit":
1246 case "8bit":
1247 $encoded = $this->FixEOL($str);
1248
1249 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1250 $encoded .= $this->LE;
1251
1252 break;
1253
1254 case "binary":
1255 $encoded = $str;
1256
1257 break;
1258
1259 case "quoted-printable":
1260 $encoded = $this->EncodeQP($str);
1261
1262 break;
1263
1264 default:
1265 $this->SetError($this->Lang("encoding") . $encoding);
1266
1267 break;
1268 }
1269
1270 return $encoded;
1271 }
1272
1278 function EncodeHeader($str, $position = 'text')
1279 {
1280 $x = 0;
1281
1282 switch (strtolower($position))
1283 {
1284 case 'phrase':
1285 if (!preg_match('/[\200-\377]/', $str))
1286 {
1287 // Can't use addslashes as we don't know what value has magic_quotes_sybase.
1288 $encoded = addcslashes($str, "\0..\37\177\\\"");
1289
1290 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1291 return ($encoded);
1292 else
1293 return ("\"$encoded\"");
1294 }
1295
1296 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1297 break;
1298
1299 case 'comment': $x = preg_match_all('/[()"]/', $str, $matches);
1300
1301 // Fall-through
1302 case 'text':
1303 default:
1304 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1305
1306 break;
1307 }
1308
1309 if ($x == 0)
1310 return ($str);
1311
1312 $maxlen = 75 - 7 - strlen($this->CharSet);
1313
1314 // Try to select the encoding which should produce the shortest output
1315 if (strlen($str) / 3 < $x)
1316 {
1317 $encoding = 'B';
1318 $encoded = base64_encode($str);
1319 $maxlen -= $maxlen % 4;
1320 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1321 }
1322 else
1323 {
1324 $encoding = 'Q';
1325 $encoded = $this->EncodeQ($str, $position);
1326 $encoded = $this->WrapText($encoded, $maxlen, true);
1327 $encoded = str_replace("=" . $this->LE, "\n", trim($encoded));
1328 }
1329
1330 $encoded = preg_replace('/^(.*)$/m', " =?" . $this->CharSet . "?$encoding?\\1?=", $encoded);
1331 $encoded = trim(str_replace("\n", $this->LE, $encoded));
1332
1333 return $encoded;
1334 }
1335
1341 function EncodeQP($str)
1342 {
1343 $encoded = $this->FixEOL($str);
1344
1345 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1346 $encoded .= $this->LE;
1347
1348 // Replace every high ascii, control and = characters
1349 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/', "'='.sprintf('%02X', ord('\\1'))",
1350 $encoded);
1351 // Replace every spaces and tabs when it's the last character on a line
1352 $encoded = preg_replace("/([\011\040])" . $this->LE . "/",
1353 "'='.sprintf('%02X', ord('\\1')).'" . $this->LE . "'",
1354 $encoded);
1355
1356 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1357 $encoded = $this->WrapText($encoded, 74, true);
1358
1359 return $encoded;
1360 }
1361
1367 function EncodeQ($str, $position = "text")
1368 {
1369 // There should not be any EOL in the string
1370 $encoded = preg_replace("[\r\n]", "", $str);
1371
1372 switch (strtolower($position))
1373 {
1374 case "phrase":
1375 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1376
1377 break;
1378
1379 case "comment": $encoded = preg_replace("/([\‍(\‍)\"])/", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1380
1381 case "text":
1382 default:
1383 // Replace every high ascii, control =, ? and _ characters
1384 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/',
1385 "'='.sprintf('%02X', ord('\\1'))",
1386 $encoded);
1387
1388 break;
1389 }
1390
1391 // Replace every spaces to _ (more readable than =20)
1392 $encoded = str_replace(" ", "_", $encoded);
1393
1394 return $encoded;
1395 }
1396
1407 function AddStringAttachment($string, $filename, $encoding = "base64", $type = "application/octet-stream")
1408 {
1409 // Append to $attachment array
1410 $cur = count($this->attachment);
1411 $this->attachment[$cur][0] = $string;
1412 $this->attachment[$cur][1] = $filename;
1413 $this->attachment[$cur][2] = $filename;
1414 $this->attachment[$cur][3] = $encoding;
1415 $this->attachment[$cur][4] = $type;
1416 $this->attachment[$cur][5] = true; // isString
1417 $this->attachment[$cur][6] = "attachment";
1418 $this->attachment[$cur][7] = 0;
1419 }
1420
1434 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64", $type = "application/octet-stream")
1435 {
1436 if (!@is_file($path))
1437 {
1438 $this->SetError($this->Lang("file_access") . $path);
1439 return false;
1440 }
1441
1442 $filename = basename($path);
1443
1444 if ($name == "")
1445 $name = $filename;
1446
1447 // Append to $attachment array
1448 $cur = count($this->attachment);
1449 $this->attachment[$cur][0] = $path;
1450 $this->attachment[$cur][1] = $filename;
1451 $this->attachment[$cur][2] = $name;
1452 $this->attachment[$cur][3] = $encoding;
1453 $this->attachment[$cur][4] = $type;
1454 $this->attachment[$cur][5] = false; // isStringAttachment
1455 $this->attachment[$cur][6] = "inline";
1456 $this->attachment[$cur][7] = $cid;
1457
1458 return true;
1459 }
1460
1467 {
1468 $result = false;
1469
1470 for ($i = 0; $i < count($this->attachment); $i++)
1471 {
1472 if ($this->attachment[$i][6] == "inline")
1473 {
1474 $result = true;
1475 break;
1476 }
1477 }
1478
1479 return $result;
1480 }
1481
1483 // MESSAGE RESET METHODS
1485
1491 {
1492 $this->to = array(
1493 );
1494 }
1495
1500 function ClearCCs()
1501 {
1502 $this->cc = array(
1503 );
1504 }
1505
1510 function ClearBCCs()
1511 {
1512 $this->bcc = array(
1513 );
1514 }
1515
1520 function ClearReplyTos()
1521 {
1522 $this->ReplyTo = array(
1523 );
1524 }
1525
1532 {
1533 $this->to = array(
1534 );
1535
1536 $this->cc = array(
1537 );
1538
1539 $this->bcc = array(
1540 );
1541 }
1542
1549 {
1550 $this->attachment = array(
1551 );
1552 }
1553
1559 {
1560 $this->CustomHeader = array(
1561 );
1562 }
1563
1565 // MISCELLANEOUS METHODS
1567
1574 function SetError($msg)
1575 {
1576 $this->error_count++;
1577 $this->ErrorInfo = $msg;
1578 }
1579
1585 function RFCDate()
1586 {
1587 $tz = date("Z");
1588 $tzs = ($tz < 0) ? "-" : "+";
1589 $tz = abs($tz);
1590 $tz = ($tz / 3600) * 100 + ($tz % 3600) / 60;
1591 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
1592
1593 return $result;
1594 }
1595
1601 function Received()
1602 {
1603 if ($this->ServerVar('SERVER_NAME') != '')
1604 {
1605 $protocol = ($this->ServerVar('HTTPS') == 'on') ? 'HTTPS' : 'HTTP';
1606 $remote = $this->ServerVar('REMOTE_HOST');
1607
1608 if ($remote == "")
1609 $remote = 'phpmailer';
1610
1611 $remote .= ' ([' . $this->ServerVar('REMOTE_ADDR') . '])';
1612 }
1613 else
1614 {
1615 $protocol = 'local';
1616 $remote = $this->ServerVar('USER');
1617
1618 if ($remote == '')
1619 $remote = 'phpmailer';
1620 }
1621
1622 $result = sprintf("Received: from %s %s\tby %s " . "with %s (PHPMailer);%s\t%s%s", $remote, $this->LE,
1623 $this->ServerHostname(), $protocol, $this->LE,
1624 $this->RFCDate(), $this->LE);
1625
1626 return $result;
1627 }
1628
1636 function ServerVar($varName)
1637 {
1638 global $HTTP_SERVER_VARS;
1639 global $HTTP_ENV_VARS;
1640
1641 if (!isset($_SERVER))
1642 {
1643 $_SERVER = $HTTP_SERVER_VARS;
1644
1645 if (!isset($_SERVER["REMOTE_ADDR"]))
1646 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1647 }
1648
1649 if (isset($_SERVER[$varName]))
1650 return $_SERVER[$varName];
1651 else
1652 return "";
1653 }
1654
1661 {
1662 if ($this->Hostname != "")
1663 $result = $this->Hostname;
1664
1665 elseif ($this->ServerVar('SERVER_NAME') != "")
1666 $result = $this->ServerVar('SERVER_NAME');
1667
1668 else
1669 $result = "localhost.localdomain";
1670
1671 return $result;
1672 }
1673
1679 function Lang($key)
1680 {
1681 if (count($this->language) < 1)
1682 $this->SetLanguage("en"); // set the default language
1683
1684 if (isset($this->language[$key]))
1685 return $this->language[$key];
1686 else
1687 return "Language string failed to load: " . $key;
1688 }
1689
1694 function IsError()
1695 {
1696 return ($this->error_count > 0);
1697 }
1698
1704 function FixEOL($str)
1705 {
1706 $str = str_replace("\r\n", "\n", $str);
1707 $str = str_replace("\r", "\n", $str);
1708 $str = str_replace("\n", $this->LE, $str);
1709 return $str;
1710 }
1711
1716 function AddCustomHeader($custom_header)
1717 {
1718 $this->CustomHeader[] = explode(":", $custom_header, 2);
1719 }
1720}
1721?>
EncodeFile($path, $encoding="base64")
SetLanguage($lang_type, $lang_path="")
SendmailSend($header, $body)
EncodeHeader($str, $position='text')
AddrFormat($addr)
SmtpSend($header, $body)
HeaderLine($name, $value)
AddBCC($address, $name="")
AddAddress($address, $name="")
EncodeString($str, $encoding="base64")
GetBoundary($boundary, $charSet, $contentType, $encoding)
AddStringAttachment($string, $filename, $encoding="base64", $type="application/octet-stream")
AddAttachment($path, $name="", $encoding="base64", $type="application/octet-stream")
EndBoundary($boundary)
AddReplyTo($address, $name="")
AddrAppend($type, $addr)
TextLine($value)
EncodeQ($str, $position="text")
MailSend($header, $body)
AddEmbeddedImage($path, $cid, $name="", $encoding="base64", $type="application/octet-stream")
AddCustomHeader($custom_header)
AddCC($address, $name="")
ServerVar($varName)
WrapText($message, $length, $qp_mode=false)