Kyberia v2.3 - 1st revision from SVN (Without patches of kyberia.sk team)
[mirrors/Kyberia-bloodline.git] / inc / phpmailer / class.phpmailer.php
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
10 //
11 // License: LGPL, see LICENSE
12 ////////////////////////////////////////////////////
13
14 /**
15 * PHPMailer - PHP email transport class
16 * @package PHPMailer
17 * @author Brent R. Matzelle
18 * @copyright 2001 - 2003 Brent R. Matzelle
19 */
20 class PHPMailer
21 {
22 /////////////////////////////////////////////////
23 // PUBLIC VARIABLES
24 /////////////////////////////////////////////////
25
26 /**
27 * Email priority (1 = High, 3 = Normal, 5 = low).
28 * @var int
29 */
30 var $Priority = 3;
31
32 /**
33 * Sets the CharSet of the message.
34 * @var string
35 */
36 var $CharSet = "iso-8859-1";
37
38 /**
39 * Sets the Content-type of the message.
40 * @var string
41 */
42 var $ContentType = "text/plain";
43
44 /**
45 * Sets the Encoding of the message. Options for this are "8bit",
46 * "7bit", "binary", "base64", and "quoted-printable".
47 * @var string
48 */
49 var $Encoding = "8bit";
50
51 /**
52 * Holds the most recent mailer error message.
53 * @var string
54 */
55 var $ErrorInfo = "";
56
57 /**
58 * Sets the From email address for the message.
59 * @var string
60 */
61 var $From = "root@localhost";
62
63 /**
64 * Sets the From name of the message.
65 * @var string
66 */
67 var $FromName = "Root User";
68
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
73 */
74 var $Sender = "";
75
76 /**
77 * Sets the Subject of the message.
78 * @var string
79 */
80 var $Subject = "";
81
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
86 */
87 var $Body = "";
88
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
95 */
96 var $AltBody = "";
97
98 /**
99 * Sets word wrapping on the body of the message to a given number of
100 * characters.
101 * @var int
102 */
103 var $WordWrap = 0;
104
105 /**
106 * Method to send mail: ("mail", "sendmail", or "smtp").
107 * @var string
108 */
109 var $Mailer = "mail";
110
111 /**
112 * Sets the path of the sendmail program.
113 * @var string
114 */
115 var $Sendmail = "/usr/sbin/sendmail";
116
117 /**
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
121 */
122 var $PluginDir = "";
123
124 /**
125 * Holds PHPMailer version.
126 * @var string
127 */
128 var $Version = "1.71";
129
130 /**
131 * Sets the email address that a reading confirmation will be sent.
132 * @var string
133 */
134 var $ConfirmReadingTo = "";
135
136 /**
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
141 */
142 var $Hostname = "";
143
144
145 /////////////////////////////////////////////////
146 // SMTP VARIABLES
147 /////////////////////////////////////////////////
148
149 /**
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
156 */
157 var $Host = "localhost";
158
159 /**
160 * Sets the default SMTP server port.
161 * @var int
162 */
163 var $Port = 25;
164
165 /**
166 * Sets the SMTP HELO of the message (Default is $Hostname).
167 * @var string
168 */
169 var $Helo = "";
170
171 /**
172 * Sets SMTP authentication. Utilizes the Username and Password variables.
173 * @var bool
174 */
175 var $SMTPAuth = false;
176
177 /**
178 * Sets SMTP username.
179 * @var string
180 */
181 var $Username = "";
182
183 /**
184 * Sets SMTP password.
185 * @var string
186 */
187 var $Password = "";
188
189 /**
190 * Sets the SMTP server timeout in seconds. This function will not
191 * work with the win32 version.
192 * @var int
193 */
194 var $Timeout = 10;
195
196 /**
197 * Sets SMTP class debugging on or off.
198 * @var bool
199 */
200 var $SMTPDebug = false;
201
202 /**
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
207 */
208 var $SMTPKeepAlive = false;
209
210 /**#@+
211 * @access private
212 */
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 /**#@-*/
226
227 /////////////////////////////////////////////////
228 // VARIABLE METHODS
229 /////////////////////////////////////////////////
230
231 /**
232 * Sets message type to HTML.
233 * @param bool $bool
234 * @return void
235 */
236 function IsHTML($bool) {
237 if($bool == true)
238 $this->ContentType = "text/html";
239 else
240 $this->ContentType = "text/plain";
241 }
242
243 /**
244 * Sets Mailer to send message using SMTP.
245 * @return void
246 */
247 function IsSMTP() {
248 $this->Mailer = "smtp";
249 }
250
251 /**
252 * Sets Mailer to send message using PHP mail() function.
253 * @return void
254 */
255 function IsMail() {
256 $this->Mailer = "mail";
257 }
258
259 /**
260 * Sets Mailer to send message using the $Sendmail program.
261 * @return void
262 */
263 function IsSendmail() {
264 $this->Mailer = "sendmail";
265 }
266
267 /**
268 * Sets Mailer to send message using the qmail MTA.
269 * @return void
270 */
271 function IsQmail() {
272 $this->Sendmail = "/var/qmail/bin/sendmail";
273 $this->Mailer = "sendmail";
274 }
275
276
277 /////////////////////////////////////////////////
278 // RECIPIENT METHODS
279 /////////////////////////////////////////////////
280
281 /**
282 * Adds a "To" address.
283 * @param string $address
284 * @param string $name
285 * @return void
286 */
287 function AddAddress($address, $name = "") {
288 $cur = count($this->to);
289 $this->to[$cur][0] = trim($address);
290 $this->to[$cur][1] = $name;
291 }
292
293 /**
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
300 */
301 function AddCC($address, $name = "") {
302 $cur = count($this->cc);
303 $this->cc[$cur][0] = trim($address);
304 $this->cc[$cur][1] = $name;
305 }
306
307 /**
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
314 */
315 function AddBCC($address, $name = "") {
316 $cur = count($this->bcc);
317 $this->bcc[$cur][0] = trim($address);
318 $this->bcc[$cur][1] = $name;
319 }
320
321 /**
322 * Adds a "Reply-to" address.
323 * @param string $address
324 * @param string $name
325 * @return void
326 */
327 function AddReplyTo($address, $name = "") {
328 $cur = count($this->ReplyTo);
329 $this->ReplyTo[$cur][0] = trim($address);
330 $this->ReplyTo[$cur][1] = $name;
331 }
332
333
334 /////////////////////////////////////////////////
335 // MAIL SENDING METHODS
336 /////////////////////////////////////////////////
337
338 /**
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
343 */
344 function Send() {
345 $header = "";
346 $body = "";
347
348 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
349 {
350 $this->SetError($this->Lang("provide_address"));
351 return false;
352 }
353
354 // Set whether the message is multipart/alternative
355 if(!empty($this->AltBody))
356 $this->ContentType = "multipart/alternative";
357
358 $this->SetMessageType();
359 $header .= $this->CreateHeader();
360 $body = $this->CreateBody();
361
362 if($body == "") { return false; }
363
364 // Choose the mailer
365 if($this->Mailer == "sendmail")
366 {
367 if(!$this->SendmailSend($header, $body))
368 return false;
369 }
370 elseif($this->Mailer == "mail")
371 {
372 if(!$this->MailSend($header, $body))
373 return false;
374 }
375 elseif($this->Mailer == "smtp")
376 {
377 if(!$this->SmtpSend($header, $body))
378 return false;
379 }
380 else
381 {
382 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
383 return false;
384 }
385
386 return true;
387 }
388
389 /**
390 * Sends mail using the $Sendmail program.
391 * @access private
392 * @return bool
393 */
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);
399
400 if(!@$mail = popen($sendmail, "w"))
401 {
402 $this->SetError($this->Lang("execute") . $this->Sendmail);
403 return false;
404 }
405
406 fputs($mail, $header);
407 fputs($mail, $body);
408
409 $result = pclose($mail) >> 8 & 0xFF;
410 if($result != 0)
411 {
412 $this->SetError($this->Lang("execute") . $this->Sendmail);
413 return false;
414 }
415
416 return true;
417 }
418
419 /**
420 * Sends mail using the PHP mail() function.
421 * @access private
422 * @return bool
423 */
424 function MailSend($header, $body) {
425 $to = "";
426 for($i = 0; $i < count($this->to); $i++)
427 {
428 if($i != 0) { $to .= ", "; }
429 $to .= $this->to[$i][0];
430 }
431
432 if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
433 {
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);
439 }
440 else
441 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
442
443 if (isset($old_from))
444 ini_set("sendmail_from", $old_from);
445
446 if(!$rt)
447 {
448 $this->SetError($this->Lang("instantiate"));
449 return false;
450 }
451
452 return true;
453 }
454
455 /**
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
461 */
462 function SmtpSend($header, $body) {
463 include_once($this->PluginDir . "class.smtp.php");
464 $error = "";
465 $bad_rcpt = array();
466
467 if(!$this->SmtpConnect())
468 return false;
469
470 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
471 if(!$this->smtp->Mail($smtp_from))
472 {
473 $error = $this->Lang("from_failed") . $smtp_from;
474 $this->SetError($error);
475 $this->smtp->Reset();
476 return false;
477 }
478
479 // Attempt to send attach all recipients
480 for($i = 0; $i < count($this->to); $i++)
481 {
482 if(!$this->smtp->Recipient($this->to[$i][0]))
483 $bad_rcpt[] = $this->to[$i][0];
484 }
485 for($i = 0; $i < count($this->cc); $i++)
486 {
487 if(!$this->smtp->Recipient($this->cc[$i][0]))
488 $bad_rcpt[] = $this->cc[$i][0];
489 }
490 for($i = 0; $i < count($this->bcc); $i++)
491 {
492 if(!$this->smtp->Recipient($this->bcc[$i][0]))
493 $bad_rcpt[] = $this->bcc[$i][0];
494 }
495
496 if(count($bad_rcpt) > 0) // Create error message
497 {
498 for($i = 0; $i < count($bad_rcpt); $i++)
499 {
500 if($i != 0) { $error .= ", "; }
501 $error .= $bad_rcpt[$i];
502 }
503 $error = $this->Lang("recipients_failed") . $error;
504 $this->SetError($error);
505 $this->smtp->Reset();
506 return false;
507 }
508
509 if(!$this->smtp->Data($header . $body))
510 {
511 $this->SetError($this->Lang("data_not_accepted"));
512 $this->smtp->Reset();
513 return false;
514 }
515 if($this->SMTPKeepAlive == true)
516 $this->smtp->Reset();
517 else
518 $this->SmtpClose();
519
520 return true;
521 }
522
523 /**
524 * Initiates a connection to an SMTP server. Returns false if the
525 * operation failed.
526 * @access private
527 * @return bool
528 */
529 function SmtpConnect() {
530 if($this->smtp == NULL) { $this->smtp = new SMTP(); }
531
532 $this->smtp->do_debug = $this->SMTPDebug;
533 $hosts = explode(";", $this->Host);
534 $index = 0;
535 $connection = ($this->smtp->Connected());
536
537 // Retry while there is no connection
538 while($index < count($hosts) && $connection == false)
539 {
540 if(strstr($hosts[$index], ":"))
541 list($host, $port) = explode(":", $hosts[$index]);
542 else
543 {
544 $host = $hosts[$index];
545 $port = $this->Port;
546 }
547
548 if($this->smtp->Connect($host, $port, $this->Timeout))
549 {
550 if ($this->Helo != '')
551 $this->smtp->Hello($this->Helo);
552 else
553 $this->smtp->Hello($this->ServerHostname());
554
555 if($this->SMTPAuth)
556 {
557 if(!$this->smtp->Authenticate($this->Username,
558 $this->Password))
559 {
560 $this->SetError($this->Lang("authenticate"));
561 $this->smtp->Reset();
562 $connection = false;
563 }
564 }
565 $connection = true;
566 }
567 $index++;
568 }
569 if(!$connection)
570 $this->SetError($this->Lang("connect_host"));
571
572 return $connection;
573 }
574
575 /**
576 * Closes the active SMTP session if one exists.
577 * @return void
578 */
579 function SmtpClose() {
580 if($this->smtp != NULL)
581 {
582 if($this->smtp->Connected())
583 {
584 $this->smtp->Quit();
585 $this->smtp->Close();
586 }
587 }
588 }
589
590 /**
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
598 */
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
605 {
606 $this->SetError("Could not load language file");
607 return false;
608 }
609 $this->language = $PHPMAILER_LANG;
610
611 return true;
612 }
613
614 /////////////////////////////////////////////////
615 // MESSAGE CREATION METHODS
616 /////////////////////////////////////////////////
617
618 /**
619 * Creates recipient headers.
620 * @access private
621 * @return string
622 */
623 function AddrAppend($type, $addr) {
624 $addr_str = $type . ": ";
625 $addr_str .= $this->AddrFormat($addr[0]);
626 if(count($addr) > 1)
627 {
628 for($i = 1; $i < count($addr); $i++)
629 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
630 }
631 $addr_str .= $this->LE;
632
633 return $addr_str;
634 }
635
636 /**
637 * Formats an address correctly.
638 * @access private
639 * @return string
640 */
641 function AddrFormat($addr) {
642 if(empty($addr[1]))
643 $formatted = $addr[0];
644 else
645 {
646 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
647 $addr[0] . ">";
648 }
649
650 return $formatted;
651 }
652
653 /**
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
659 */
660 function WrapText($message, $length, $qp_mode = false) {
661 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
662
663 $message = $this->FixEOL($message);
664 if (substr($message, -1) == $this->LE)
665 $message = substr($message, 0, -1);
666
667 $line = explode($this->LE, $message);
668 $message = "";
669 for ($i=0 ;$i < count($line); $i++)
670 {
671 $line_part = explode(" ", $line[$i]);
672 $buf = "";
673 for ($e = 0; $e<count($line_part); $e++)
674 {
675 $word = $line_part[$e];
676 if ($qp_mode and (strlen($word) > $length))
677 {
678 $space_left = $length - strlen($buf) - 1;
679 if ($e != 0)
680 {
681 if ($space_left > 20)
682 {
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);
692 }
693 else
694 {
695 $message .= $buf . $soft_break;
696 }
697 $buf = "";
698 }
699 while (strlen($word) > 0)
700 {
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);
708
709 if (strlen($word) > 0)
710 $message .= $part . sprintf("=%s", $this->LE);
711 else
712 $buf = $part;
713 }
714 }
715 else
716 {
717 $buf_o = $buf;
718 $buf .= ($e == 0) ? $word : (" " . $word);
719
720 if (strlen($buf) > $length and $buf_o != "")
721 {
722 $message .= $buf_o . $soft_break;
723 $buf = $word;
724 }
725 }
726 }
727 $message .= $buf . $this->LE;
728 }
729
730 return $message;
731 }
732
733 /**
734 * Set the body wrapping.
735 * @access private
736 * @return void
737 */
738 function SetWordWrap() {
739 if($this->WordWrap < 1)
740 return;
741
742 switch($this->message_type)
743 {
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;
752 }
753 }
754
755 /**
756 * Assembles message header.
757 * @access private
758 * @return string
759 */
760 function CreateHeader() {
761 $result = "";
762
763 // Set the boundaries
764 $uniq_id = md5(uniqid(time()));
765 $this->boundary[1] = "b1_" . $uniq_id;
766 $this->boundary[2] = "b2_" . $uniq_id;
767
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));
774
775 // To be created automatically by mail()
776 if($this->Mailer != "mail")
777 {
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);
784 }
785
786 $from = array();
787 $from[0][0] = trim($this->From);
788 $from[0][1] = $this->FromName;
789 $result .= $this->AddrAppend("From", $from);
790
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);
794
795 if(count($this->ReplyTo) > 0)
796 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
797
798 // mail() sets the subject itself
799 if($this->Mailer != "mail")
800 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
801
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 . "]");
805
806 if($this->ConfirmReadingTo != "")
807 {
808 $result .= $this->HeaderLine("Disposition-Notification-To",
809 "<" . trim($this->ConfirmReadingTo) . ">");
810 }
811
812 // Add custom headers
813 for($index = 0; $index < count($this->CustomHeader); $index++)
814 {
815 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
816 $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
817 }
818 $result .= $this->HeaderLine("MIME-Version", "1.0");
819
820 switch($this->message_type)
821 {
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())
831 {
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);
835 }
836 else
837 {
838 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
839 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
840 }
841 break;
842 case "alt":
843 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
844 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
845 break;
846 }
847
848 if($this->Mailer != "mail")
849 $result .= $this->LE.$this->LE;
850
851 return $result;
852 }
853
854 /**
855 * Assembles the message body. Returns an empty string on failure.
856 * @access private
857 * @return string
858 */
859 function CreateBody() {
860 $result = "";
861
862 $this->SetWordWrap();
863
864 switch($this->message_type)
865 {
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", "");
873
874 $result .= $this->EncodeString($this->Body, $this->Encoding);
875 $result .= $this->LE.$this->LE;
876
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;
886
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);
895
896 // Create text body
897 $result .= $this->GetBoundary($this->boundary[2], "",
898 "text/plain", "") . $this->LE;
899
900 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
901 $result .= $this->LE.$this->LE;
902
903 // Create the HTML body
904 $result .= $this->GetBoundary($this->boundary[2], "",
905 "text/html", "") . $this->LE;
906
907 $result .= $this->EncodeString($this->Body, $this->Encoding);
908 $result .= $this->LE.$this->LE;
909
910 $result .= $this->EndBoundary($this->boundary[2]);
911
912 $result .= $this->AttachAll();
913 break;
914 }
915 if($this->IsError())
916 $result = "";
917
918 return $result;
919 }
920
921 /**
922 * Returns the start of a message boundary.
923 * @access private
924 */
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; }
930
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;
937
938 return $result;
939 }
940
941 /**
942 * Returns the end of a message boundary.
943 * @access private
944 */
945 function EndBoundary($boundary) {
946 return $this->LE . "--" . $boundary . "--" . $this->LE;
947 }
948
949 /**
950 * Sets the message type.
951 * @access private
952 * @return void
953 */
954 function SetMessageType() {
955 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
956 $this->message_type = "plain";
957 else
958 {
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";
965 }
966 }
967
968 /**
969 * Returns a formatted header line.
970 * @access private
971 * @return string
972 */
973 function HeaderLine($name, $value) {
974 return $name . ": " . $value . $this->LE;
975 }
976
977 /**
978 * Returns a formatted mail line.
979 * @access private
980 * @return string
981 */
982 function TextLine($value) {
983 return $value . $this->LE;
984 }
985
986 /////////////////////////////////////////////////
987 // ATTACHMENT METHODS
988 /////////////////////////////////////////////////
989
990 /**
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
999 */
1000 function AddAttachment($path, $name = "", $encoding = "base64",
1001 $type = "application/octet-stream") {
1002 if(!@is_file($path))
1003 {
1004 $this->SetError($this->Lang("file_access") . $path);
1005 return false;
1006 }
1007
1008 $filename = basename($path);
1009 if($name == "")
1010 $name = $filename;
1011
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;
1021
1022 return true;
1023 }
1024
1025 /**
1026 * Attaches all fs, string, and binary attachments to the message.
1027 * Returns an empty string on failure.
1028 * @access private
1029 * @return string
1030 */
1031 function AttachAll() {
1032 // Return text of body
1033 $mime = array();
1034
1035 // Add all attachments
1036 for($i = 0; $i < count($this->attachment); $i++)
1037 {
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];
1044
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];
1051
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);
1055
1056 if($disposition == "inline")
1057 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
1058
1059 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
1060 $disposition, $name, $this->LE.$this->LE);
1061
1062 // Encode as string attachment
1063 if($bString)
1064 {
1065 $mime[] = $this->EncodeString($string, $encoding);
1066 if($this->IsError()) { return ""; }
1067 $mime[] = $this->LE.$this->LE;
1068 }
1069 else
1070 {
1071 $mime[] = $this->EncodeFile($path, $encoding);
1072 if($this->IsError()) { return ""; }
1073 $mime[] = $this->LE.$this->LE;
1074 }
1075 }
1076
1077 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
1078
1079 return join("", $mime);
1080 }
1081
1082 /**
1083 * Encodes attachment in requested format. Returns an
1084 * empty string on failure.
1085 * @access private
1086 * @return string
1087 */
1088 function EncodeFile ($path, $encoding = "base64") {
1089 if(!@$fd = fopen($path, "rb"))
1090 {
1091 $this->SetError($this->Lang("file_open") . $path);
1092 return "";
1093 }
1094 $file_buffer = fread($fd, filesize($path));
1095 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1096 fclose($fd);
1097
1098 return $file_buffer;
1099 }
1100
1101 /**
1102 * Encodes string to requested format. Returns an
1103 * empty string on failure.
1104 * @access private
1105 * @return string
1106 */
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;
1129 }
1130 return $encoded;
1131 }
1132
1133 /**
1134 * Encode a header string to best of Q, B, quoted or none.
1135 * @access private
1136 * @return string
1137 */
1138 function EncodeHeader ($str, $position = 'text') {
1139 $x = 0;
1140
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\\\"");
1146
1147 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
1148 return ($encoded);
1149 else
1150 return ("\"$encoded\"");
1151 }
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;
1161 }
1162
1163 if ($x == 0)
1164 return ($str);
1165
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));
1178 }
1179
1180 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
1181 $encoded = trim(str_replace("\n", $this->LE, $encoded));
1182
1183 return $encoded;
1184 }
1185
1186 /**
1187 * Encode string to quoted-printable.
1188 * @access private
1189 * @return string
1190 */
1191 function EncodeQP ($str) {
1192 $encoded = $this->FixEOL($str);
1193 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1194 $encoded .= $this->LE;
1195
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);
1202
1203 // Maximum line length of 76 characters before CRLF (74 + space + '=')
1204 $encoded = $this->WrapText($encoded, 74, true);
1205
1206 return $encoded;
1207 }
1208
1209 /**
1210 * Encode string to q encoding.
1211 * @access private
1212 * @return string
1213 */
1214 function EncodeQ ($str, $position = "text") {
1215 // There should not be any EOL in the string
1216 $encoded = preg_replace("[\r\n]", "", $str);
1217
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;
1230 }
1231
1232 // Replace every spaces to _ (more readable than =20)
1233 $encoded = str_replace(" ", "_", $encoded);
1234
1235 return $encoded;
1236 }
1237
1238 /**
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
1247 */
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;
1260 }
1261
1262 /**
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
1274 */
1275 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
1276 $type = "application/octet-stream") {
1277
1278 if(!@is_file($path))
1279 {
1280 $this->SetError($this->Lang("file_access") . $path);
1281 return false;
1282 }
1283
1284 $filename = basename($path);
1285 if($name == "")
1286 $name = $filename;
1287
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;
1298
1299 return true;
1300 }
1301
1302 /**
1303 * Returns true if an inline attachment is present.
1304 * @access private
1305 * @return bool
1306 */
1307 function InlineImageExists() {
1308 $result = false;
1309 for($i = 0; $i < count($this->attachment); $i++)
1310 {
1311 if($this->attachment[$i][6] == "inline")
1312 {
1313 $result = true;
1314 break;
1315 }
1316 }
1317
1318 return $result;
1319 }
1320
1321 /////////////////////////////////////////////////
1322 // MESSAGE RESET METHODS
1323 /////////////////////////////////////////////////
1324
1325 /**
1326 * Clears all recipients assigned in the TO array. Returns void.
1327 * @return void
1328 */
1329 function ClearAddresses() {
1330 $this->to = array();
1331 }
1332
1333 /**
1334 * Clears all recipients assigned in the CC array. Returns void.
1335 * @return void
1336 */
1337 function ClearCCs() {
1338 $this->cc = array();
1339 }
1340
1341 /**
1342 * Clears all recipients assigned in the BCC array. Returns void.
1343 * @return void
1344 */
1345 function ClearBCCs() {
1346 $this->bcc = array();
1347 }
1348
1349 /**
1350 * Clears all recipients assigned in the ReplyTo array. Returns void.
1351 * @return void
1352 */
1353 function ClearReplyTos() {
1354 $this->ReplyTo = array();
1355 }
1356
1357 /**
1358 * Clears all recipients assigned in the TO, CC and BCC
1359 * array. Returns void.
1360 * @return void
1361 */
1362 function ClearAllRecipients() {
1363 $this->to = array();
1364 $this->cc = array();
1365 $this->bcc = array();
1366 }
1367
1368 /**
1369 * Clears all previously set filesystem, string, and binary
1370 * attachments. Returns void.
1371 * @return void
1372 */
1373 function ClearAttachments() {
1374 $this->attachment = array();
1375 }
1376
1377 /**
1378 * Clears all custom headers. Returns void.
1379 * @return void
1380 */
1381 function ClearCustomHeaders() {
1382 $this->CustomHeader = array();
1383 }
1384
1385
1386 /////////////////////////////////////////////////
1387 // MISCELLANEOUS METHODS
1388 /////////////////////////////////////////////////
1389
1390 /**
1391 * Adds the error message to the error container.
1392 * Returns void.
1393 * @access private
1394 * @return void
1395 */
1396 function SetError($msg) {
1397 $this->error_count++;
1398 $this->ErrorInfo = $msg;
1399 }
1400
1401 /**
1402 * Returns the proper RFC 822 formatted date.
1403 * @access private
1404 * @return string
1405 */
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);
1412
1413 return $result;
1414 }
1415
1416 /**
1417 * Returns Received header for message tracing.
1418 * @access private
1419 * @return string
1420 */
1421 function Received() {
1422 if ($this->ServerVar('SERVER_NAME') != '')
1423 {
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').'])';
1429 }
1430 else
1431 {
1432 $protocol = 'local';
1433 $remote = $this->ServerVar('USER');
1434 if($remote == '')
1435 $remote = 'phpmailer';
1436 }
1437
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);
1442
1443 return $result;
1444 }
1445
1446 /**
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
1452 */
1453 function ServerVar($varName) {
1454 global $HTTP_SERVER_VARS;
1455 global $HTTP_ENV_VARS;
1456
1457 if(!isset($_SERVER))
1458 {
1459 $_SERVER = $HTTP_SERVER_VARS;
1460 if(!isset($_SERVER["REMOTE_ADDR"]))
1461 $_SERVER = $HTTP_ENV_VARS; // must be Apache
1462 }
1463
1464 if(isset($_SERVER[$varName]))
1465 return $_SERVER[$varName];
1466 else
1467 return "";
1468 }
1469
1470 /**
1471 * Returns the server hostname or 'localhost.localdomain' if unknown.
1472 * @access private
1473 * @return string
1474 */
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";
1482
1483 return $result;
1484 }
1485
1486 /**
1487 * Returns a message in the appropriate language.
1488 * @access private
1489 * @return string
1490 */
1491 function Lang($key) {
1492 if(count($this->language) < 1)
1493 $this->SetLanguage("en"); // set the default language
1494
1495 if(isset($this->language[$key]))
1496 return $this->language[$key];
1497 else
1498 return "Language string failed to load: " . $key;
1499 }
1500
1501 /**
1502 * Returns true if an error occurred.
1503 * @return bool
1504 */
1505 function IsError() {
1506 return ($this->error_count > 0);
1507 }
1508
1509 /**
1510 * Changes every end of line from CR or LF to CRLF.
1511 * @access private
1512 * @return string
1513 */
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;
1519 }
1520
1521 /**
1522 * Adds a custom header.
1523 * @return void
1524 */
1525 function AddCustomHeader($custom_header) {
1526 $this->CustomHeader[] = explode(":", $custom_header, 2);
1527 }
1528 }
1529
1530 ?>
This page took 0.981364 seconds and 4 git commands to generate.