Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

class.phpmailer.php

Go to the documentation of this file.
00001 <?php
00003 // PHPMailer - PHP email class
00004 //
00005 // Class for sending email using either
00006 // sendmail, PHP mail(), or SMTP.  Methods are
00007 // based upon the standard AspEmail(tm) classes.
00008 //
00009 // Copyright (C) 2001 - 2003  Brent R. Matzelle
00010 //
00011 // License: LGPL, see LICENSE
00013 
00020 class PHPMailer
00021 {
00023     // PUBLIC VARIABLES
00025 
00030     var $Priority          = 3;
00031 
00036     var $CharSet           = "iso-8859-1";
00037 
00042     var $ContentType        = "text/plain";
00043 
00049     var $Encoding          = "8bit";
00050 
00055     var $ErrorInfo         = "";
00056 
00061     var $From               = "root@localhost";
00062 
00067     var $FromName           = "Root User";
00068 
00074     var $Sender            = "";
00075 
00080     var $Subject           = "";
00081 
00087     var $Body               = "";
00088 
00096     var $AltBody           = "";
00097 
00103     var $WordWrap          = 0;
00104 
00109     var $Mailer            = "mail";
00110 
00115     var $Sendmail          = "/usr/sbin/sendmail";
00116     
00122     var $PluginDir         = "";
00123 
00128     var $Version           = "1.73";
00129 
00134     var $ConfirmReadingTo  = "";
00135 
00142     var $Hostname          = "";
00143 
00145     // SMTP VARIABLES
00147 
00156     var $Host        = "localhost";
00157 
00162     var $Port        = 25;
00163 
00168     var $Helo        = "";
00169 
00174     var $SMTPAuth     = false;
00175 
00180     var $Username     = "";
00181 
00186     var $Password     = "";
00187 
00193     var $Timeout      = 10;
00194 
00199     var $SMTPDebug    = false;
00200 
00207     var $SMTPKeepAlive = false;
00208 
00212     var $smtp            = NULL;
00213     var $to              = array();
00214     var $cc              = array();
00215     var $bcc             = array();
00216     var $ReplyTo         = array();
00217     var $attachment      = array();
00218     var $CustomHeader    = array();
00219     var $message_type    = "";
00220     var $boundary        = array();
00221     var $language        = array();
00222     var $error_count     = 0;
00223     var $LE              = "\n";
00226 
00227     // VARIABLE METHODS
00229 
00235     function IsHTML($bool) {
00236         if($bool == true)
00237             $this->ContentType = "text/html";
00238         else
00239             $this->ContentType = "text/plain";
00240     }
00241 
00246     function IsSMTP() {
00247         $this->Mailer = "smtp";
00248     }
00249 
00254     function IsMail() {
00255         $this->Mailer = "mail";
00256     }
00257 
00262     function IsSendmail() {
00263         $this->Mailer = "sendmail";
00264     }
00265 
00270     function IsQmail() {
00271         $this->Sendmail = "/var/qmail/bin/sendmail";
00272         $this->Mailer = "sendmail";
00273     }
00274 
00275 
00277     // RECIPIENT METHODS
00279 
00286     function AddAddress($address, $name = "") {
00287         $cur = count($this->to);
00288         $this->to[$cur][0] = trim($address);
00289         $this->to[$cur][1] = $name;
00290     }
00291 
00300     function AddCC($address, $name = "") {
00301         $cur = count($this->cc);
00302         $this->cc[$cur][0] = trim($address);
00303         $this->cc[$cur][1] = $name;
00304     }
00305 
00314     function AddBCC($address, $name = "") {
00315         $cur = count($this->bcc);
00316         $this->bcc[$cur][0] = trim($address);
00317         $this->bcc[$cur][1] = $name;
00318     }
00319 
00326     function AddReplyTo($address, $name = "") {
00327         $cur = count($this->ReplyTo);
00328         $this->ReplyTo[$cur][0] = trim($address);
00329         $this->ReplyTo[$cur][1] = $name;
00330     }
00331 
00332 
00334     // MAIL SENDING METHODS
00336 
00343     function Send() {
00344         $header = "";
00345         $body = "";
00346         $result = true;
00347 
00348         if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
00349         {
00350             $this->SetError($this->Lang("provide_address"));
00351             return false;
00352         }
00353 
00354         // Set whether the message is multipart/alternative
00355         if(!empty($this->AltBody))
00356             $this->ContentType = "multipart/alternative";
00357 
00358         $this->error_count = 0; // reset errors
00359         $this->SetMessageType();
00360         $header .= $this->CreateHeader();
00361         $body = $this->CreateBody();
00362 
00363         if($body == "") { return false; }
00364 
00365         // Choose the mailer
00366         switch($this->Mailer)
00367         {
00368             case "sendmail":
00369                 $result = $this->SendmailSend($header, $body);
00370                 break;
00371             case "mail":
00372                 $result = $this->MailSend($header, $body);
00373                 break;
00374             case "smtp":
00375                 $result = $this->SmtpSend($header, $body);
00376                 break;
00377             default:
00378             $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
00379                 $result = false;
00380                 break;
00381         }
00382 
00383         return $result;
00384     }
00385     
00391     function SendmailSend($header, $body) {
00392         if ($this->Sender != "")
00393             $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
00394         else
00395             $sendmail = sprintf("%s -oi -t", $this->Sendmail);
00396 
00397         if(!@$mail = popen($sendmail, "w"))
00398         {
00399             $this->SetError($this->Lang("execute") . $this->Sendmail);
00400             return false;
00401         }
00402 
00403         fputs($mail, $header);
00404         fputs($mail, $body);
00405         
00406         $result = pclose($mail) >> 8 & 0xFF;
00407         if($result != 0)
00408         {
00409             $this->SetError($this->Lang("execute") . $this->Sendmail);
00410             return false;
00411         }
00412 
00413         return true;
00414     }
00415 
00421     function MailSend($header, $body) {
00422         $to = "";
00423         for($i = 0; $i < count($this->to); $i++)
00424         {
00425             if($i != 0) { $to .= ", "; }
00426             $to .= $this->to[$i][0];
00427         }
00428 
00429         if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
00430         {
00431             $old_from = ini_get("sendmail_from");
00432             ini_set("sendmail_from", $this->Sender);
00433             $params = sprintf("-oi -f %s", $this->Sender);
00434             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, 
00435                         $header, $params);
00436         }
00437         else
00438             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
00439 
00440         if (isset($old_from))
00441             ini_set("sendmail_from", $old_from);
00442 
00443         if(!$rt)
00444         {
00445             $this->SetError($this->Lang("instantiate"));
00446             return false;
00447         }
00448 
00449         return true;
00450     }
00451 
00459     function SmtpSend($header, $body) {
00460         include_once($this->PluginDir . "class.smtp.php");
00461         $error = "";
00462         $bad_rcpt = array();
00463 
00464         if(!$this->SmtpConnect())
00465             return false;
00466 
00467         $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
00468         if(!$this->smtp->Mail($smtp_from))
00469         {
00470             $error = $this->Lang("from_failed") . $smtp_from;
00471             $this->SetError($error);
00472             $this->smtp->Reset();
00473             return false;
00474         }
00475 
00476         // Attempt to send attach all recipients
00477         for($i = 0; $i < count($this->to); $i++)
00478         {
00479             if(!$this->smtp->Recipient($this->to[$i][0]))
00480                 $bad_rcpt[] = $this->to[$i][0];
00481         }
00482         for($i = 0; $i < count($this->cc); $i++)
00483         {
00484             if(!$this->smtp->Recipient($this->cc[$i][0]))
00485                 $bad_rcpt[] = $this->cc[$i][0];
00486         }
00487         for($i = 0; $i < count($this->bcc); $i++)
00488         {
00489             if(!$this->smtp->Recipient($this->bcc[$i][0]))
00490                 $bad_rcpt[] = $this->bcc[$i][0];
00491         }
00492 
00493         if(count($bad_rcpt) > 0) // Create error message
00494         {
00495             for($i = 0; $i < count($bad_rcpt); $i++)
00496             {
00497                 if($i != 0) { $error .= ", "; }
00498                 $error .= $bad_rcpt[$i];
00499             }
00500             $error = $this->Lang("recipients_failed") . $error;
00501             $this->SetError($error);
00502             $this->smtp->Reset();
00503             return false;
00504         }
00505 
00506         if(!$this->smtp->Data($header . $body))
00507         {
00508             $this->SetError($this->Lang("data_not_accepted"));
00509             $this->smtp->Reset();
00510             return false;
00511         }
00512         if($this->SMTPKeepAlive == true)
00513             $this->smtp->Reset();
00514         else
00515             $this->SmtpClose();
00516 
00517         return true;
00518     }
00519 
00526     function SmtpConnect() {
00527         if($this->smtp == NULL) { $this->smtp = new SMTP(); }
00528 
00529         $this->smtp->do_debug = $this->SMTPDebug;
00530         $hosts = explode(";", $this->Host);
00531         $index = 0;
00532         $connection = ($this->smtp->Connected()); 
00533 
00534         // Retry while there is no connection
00535         while($index < count($hosts) && $connection == false)
00536         {
00537             if(strstr($hosts[$index], ":"))
00538                 list($host, $port) = explode(":", $hosts[$index]);
00539             else
00540             {
00541                 $host = $hosts[$index];
00542                 $port = $this->Port;
00543             }
00544 
00545             if($this->smtp->Connect($host, $port, $this->Timeout))
00546             {
00547                 if ($this->Helo != '')
00548                     $this->smtp->Hello($this->Helo);
00549                 else
00550                     $this->smtp->Hello($this->ServerHostname());
00551         
00552                 if($this->SMTPAuth)
00553                 {
00554                     if(!$this->smtp->Authenticate($this->Username, 
00555                                                   $this->Password))
00556                     {
00557                         $this->SetError($this->Lang("authenticate"));
00558                         $this->smtp->Reset();
00559                         $connection = false;
00560                     }
00561                 }
00562                 $connection = true;
00563             }
00564             $index++;
00565         }
00566         if(!$connection)
00567             $this->SetError($this->Lang("connect_host"));
00568 
00569         return $connection;
00570     }
00571 
00576     function SmtpClose() {
00577         if($this->smtp != NULL)
00578         {
00579             if($this->smtp->Connected())
00580             {
00581                 $this->smtp->Quit();
00582                 $this->smtp->Close();
00583             }
00584         }
00585     }
00586 
00596     function SetLanguage($lang_type, $lang_path = "language/") {
00597         if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
00598             include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
00599         else if(file_exists($lang_path.'phpmailer.lang-en.php'))
00600             include($lang_path.'phpmailer.lang-en.php');
00601         else
00602         {
00603             $this->SetError("Could not load language file");
00604             return false;
00605         }
00606         $this->language = $PHPMAILER_LANG;
00607     
00608         return true;
00609     }
00610 
00612     // MESSAGE CREATION METHODS
00614 
00620     function AddrAppend($type, $addr) {
00621         $addr_str = $type . ": ";
00622         $addr_str .= $this->AddrFormat($addr[0]);
00623         if(count($addr) > 1)
00624         {
00625             for($i = 1; $i < count($addr); $i++)
00626                 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
00627         }
00628         $addr_str .= $this->LE;
00629 
00630         return $addr_str;
00631     }
00632     
00638     function AddrFormat($addr) {
00639         if(empty($addr[1]))
00640             $formatted = $addr[0];
00641         else
00642         {
00643             $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" . 
00644                          $addr[0] . ">";
00645         }
00646 
00647         return $formatted;
00648     }
00649 
00657     function WrapText($message, $length, $qp_mode = false) {
00658         $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
00659 
00660         $message = $this->FixEOL($message);
00661         if (substr($message, -1) == $this->LE)
00662             $message = substr($message, 0, -1);
00663 
00664         $line = explode($this->LE, $message);
00665         $message = "";
00666         for ($i=0 ;$i < count($line); $i++)
00667         {
00668           $line_part = explode(" ", $line[$i]);
00669           $buf = "";
00670           for ($e = 0; $e<count($line_part); $e++)
00671           {
00672               $word = $line_part[$e];
00673               if ($qp_mode and (strlen($word) > $length))
00674               {
00675                 $space_left = $length - strlen($buf) - 1;
00676                 if ($e != 0)
00677                 {
00678                     if ($space_left > 20)
00679                     {
00680                         $len = $space_left;
00681                         if (substr($word, $len - 1, 1) == "=")
00682                           $len--;
00683                         elseif (substr($word, $len - 2, 1) == "=")
00684                           $len -= 2;
00685                         $part = substr($word, 0, $len);
00686                         $word = substr($word, $len);
00687                         $buf .= " " . $part;
00688                         $message .= $buf . sprintf("=%s", $this->LE);
00689                     }
00690                     else
00691                     {
00692                         $message .= $buf . $soft_break;
00693                     }
00694                     $buf = "";
00695                 }
00696                 while (strlen($word) > 0)
00697                 {
00698                     $len = $length;
00699                     if (substr($word, $len - 1, 1) == "=")
00700                         $len--;
00701                     elseif (substr($word, $len - 2, 1) == "=")
00702                         $len -= 2;
00703                     $part = substr($word, 0, $len);
00704                     $word = substr($word, $len);
00705 
00706                     if (strlen($word) > 0)
00707                         $message .= $part . sprintf("=%s", $this->LE);
00708                     else
00709                         $buf = $part;
00710                 }
00711               }
00712               else
00713               {
00714                 $buf_o = $buf;
00715                 $buf .= ($e == 0) ? $word : (" " . $word); 
00716 
00717                 if (strlen($buf) > $length and $buf_o != "")
00718                 {
00719                     $message .= $buf_o . $soft_break;
00720                     $buf = $word;
00721                 }
00722               }
00723           }
00724           $message .= $buf . $this->LE;
00725         }
00726 
00727         return $message;
00728     }
00729     
00735     function SetWordWrap() {
00736         if($this->WordWrap < 1)
00737             return;
00738             
00739         switch($this->message_type)
00740         {
00741            case "alt":
00742               // fall through
00743            case "alt_attachments":
00744               $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
00745               break;
00746            default:
00747               $this->Body = $this->WrapText($this->Body, $this->WordWrap);
00748               break;
00749         }
00750     }
00751 
00757     function CreateHeader() {
00758         $result = "";
00759         
00760         // Set the boundaries
00761         $uniq_id = md5(uniqid(time()));
00762         $this->boundary[1] = "b1_" . $uniq_id;
00763         $this->boundary[2] = "b2_" . $uniq_id;
00764 
00765         $result .= $this->HeaderLine("Date", $this->RFCDate());
00766         if($this->Sender == "")
00767             $result .= $this->HeaderLine("Return-Path", trim($this->From));
00768         else
00769             $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
00770         
00771         // To be created automatically by mail()
00772         if($this->Mailer != "mail")
00773         {
00774             if(count($this->to) > 0)
00775                 $result .= $this->AddrAppend("To", $this->to);
00776             else if (count($this->cc) == 0)
00777                 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
00778             if(count($this->cc) > 0)
00779                 $result .= $this->AddrAppend("Cc", $this->cc);
00780         }
00781 
00782         $from = array();
00783         $from[0][0] = trim($this->From);
00784         $from[0][1] = $this->FromName;
00785         $result .= $this->AddrAppend("From", $from); 
00786 
00787         // sendmail and mail() extract Bcc from the header before sending
00788         if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
00789             $result .= $this->AddrAppend("Bcc", $this->bcc);
00790 
00791         if(count($this->ReplyTo) > 0)
00792             $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
00793 
00794         // mail() sets the subject itself
00795         if($this->Mailer != "mail")
00796             $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
00797 
00798         $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
00799         $result .= $this->HeaderLine("X-Priority", $this->Priority);
00800         $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]");
00801         
00802         if($this->ConfirmReadingTo != "")
00803         {
00804             $result .= $this->HeaderLine("Disposition-Notification-To", 
00805                        "<" . trim($this->ConfirmReadingTo) . ">");
00806         }
00807 
00808         // Add custom headers
00809         for($index = 0; $index < count($this->CustomHeader); $index++)
00810         {
00811             $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), 
00812                        $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
00813         }
00814         $result .= $this->HeaderLine("MIME-Version", "1.0");
00815 
00816         switch($this->message_type)
00817         {
00818             case "plain":
00819                 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
00820                 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
00821                                     $this->ContentType, $this->CharSet);
00822                 break;
00823             case "attachments":
00824                 // fall through
00825             case "alt_attachments":
00826                 if($this->InlineImageExists())
00827                 {
00828                     $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 
00829                                     "multipart/related", $this->LE, $this->LE, 
00830                                     $this->boundary[1], $this->LE);
00831                 }
00832                 else
00833                 {
00834                     $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
00835                     $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
00836                 }
00837                 break;
00838             case "alt":
00839                 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
00840                 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
00841                 break;
00842         }
00843 
00844         if($this->Mailer != "mail")
00845             $result .= $this->LE.$this->LE;
00846 
00847         return $result;
00848     }
00849 
00855     function CreateBody() {
00856         $result = "";
00857 
00858         $this->SetWordWrap();
00859 
00860         switch($this->message_type)
00861         {
00862             case "alt":
00863                 $result .= $this->GetBoundary($this->boundary[1], "", 
00864                                               "text/plain", "");
00865                 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
00866                 $result .= $this->LE.$this->LE;
00867                 $result .= $this->GetBoundary($this->boundary[1], "", 
00868                                               "text/html", "");
00869                 
00870                 $result .= $this->EncodeString($this->Body, $this->Encoding);
00871                 $result .= $this->LE.$this->LE;
00872     
00873                 $result .= $this->EndBoundary($this->boundary[1]);
00874                 break;
00875             case "plain":
00876                 $result .= $this->EncodeString($this->Body, $this->Encoding);
00877                 break;
00878             case "attachments":
00879                 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
00880                 $result .= $this->EncodeString($this->Body, $this->Encoding);
00881                 $result .= $this->LE;
00882      
00883                 $result .= $this->AttachAll();
00884                 break;
00885             case "alt_attachments":
00886                 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
00887                 $result .= sprintf("Content-Type: %s;%s" .
00888                                    "\tboundary=\"%s\"%s",
00889                                    "multipart/alternative", $this->LE, 
00890                                    $this->boundary[2], $this->LE.$this->LE);
00891     
00892                 // Create text body
00893                 $result .= $this->GetBoundary($this->boundary[2], "", 
00894                                               "text/plain", "") . $this->LE;
00895 
00896                 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
00897                 $result .= $this->LE.$this->LE;
00898     
00899                 // Create the HTML body
00900                 $result .= $this->GetBoundary($this->boundary[2], "", 
00901                                               "text/html", "") . $this->LE;
00902     
00903                 $result .= $this->EncodeString($this->Body, $this->Encoding);
00904                 $result .= $this->LE.$this->LE;
00905 
00906                 $result .= $this->EndBoundary($this->boundary[2]);
00907                 
00908                 $result .= $this->AttachAll();
00909                 break;
00910         }
00911         if($this->IsError())
00912             $result = "";
00913 
00914         return $result;
00915     }
00916 
00921     function GetBoundary($boundary, $charSet, $contentType, $encoding) {
00922         $result = "";
00923         if($charSet == "") { $charSet = $this->CharSet; }
00924         if($contentType == "") { $contentType = $this->ContentType; }
00925         if($encoding == "") { $encoding = $this->Encoding; }
00926 
00927         $result .= $this->TextLine("--" . $boundary);
00928         $result .= sprintf("Content-Type: %s; charset = \"%s\"", 
00929                             $contentType, $charSet);
00930         $result .= $this->LE;
00931         $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
00932         $result .= $this->LE;
00933        
00934         return $result;
00935     }
00936     
00941     function EndBoundary($boundary) {
00942         return $this->LE . "--" . $boundary . "--" . $this->LE; 
00943     }
00944     
00950     function SetMessageType() {
00951         if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
00952             $this->message_type = "plain";
00953         else
00954         {
00955             if(count($this->attachment) > 0)
00956                 $this->message_type = "attachments";
00957             if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
00958                 $this->message_type = "alt";
00959             if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
00960                 $this->message_type = "alt_attachments";
00961         }
00962     }
00963 
00969     function HeaderLine($name, $value) {
00970         return $name . ": " . $value . $this->LE;
00971     }
00972 
00978     function TextLine($value) {
00979         return $value . $this->LE;
00980     }
00981 
00983     // ATTACHMENT METHODS
00985 
00996     function AddAttachment($path, $name = "", $encoding = "base64", 
00997                            $type = "application/octet-stream") {
00998         if(!@is_file($path))
00999         {
01000             $this->SetError($this->Lang("file_access") . $path);
01001             return false;
01002         }
01003 
01004         $filename = basename($path);
01005         if($name == "")
01006             $name = $filename;
01007 
01008         $cur = count($this->attachment);
01009         $this->attachment[$cur][0] = $path;
01010         $this->attachment[$cur][1] = $filename;
01011         $this->attachment[$cur][2] = $name;
01012         $this->attachment[$cur][3] = $encoding;
01013         $this->attachment[$cur][4] = $type;
01014         $this->attachment[$cur][5] = false; // isStringAttachment
01015         $this->attachment[$cur][6] = "attachment";
01016         $this->attachment[$cur][7] = 0;
01017 
01018         return true;
01019     }
01020 
01027     function AttachAll() {
01028         // Return text of body
01029         $mime = array();
01030 
01031         // Add all attachments
01032         for($i = 0; $i < count($this->attachment); $i++)
01033         {
01034             // Check for string attachment
01035             $bString = $this->attachment[$i][5];
01036             if ($bString)
01037                 $string = $this->attachment[$i][0];
01038             else
01039                 $path = $this->attachment[$i][0];
01040 
01041             $filename    = $this->attachment[$i][1];
01042             $name        = $this->attachment[$i][2];
01043             $encoding    = $this->attachment[$i][3];
01044             $type        = $this->attachment[$i][4];
01045             $disposition = $this->attachment[$i][6];
01046             $cid         = $this->attachment[$i][7];
01047             
01048             $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
01049             $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
01050             $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
01051 
01052             if($disposition == "inline")
01053                 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
01054 
01055             $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", 
01056                               $disposition, $name, $this->LE.$this->LE);
01057 
01058             // Encode as string attachment
01059             if($bString)
01060             {
01061                 $mime[] = $this->EncodeString($string, $encoding);
01062                 if($this->IsError()) { return ""; }
01063                 $mime[] = $this->LE.$this->LE;
01064             }
01065             else
01066             {
01067                 $mime[] = $this->EncodeFile($path, $encoding);                
01068                 if($this->IsError()) { return ""; }
01069                 $mime[] = $this->LE.$this->LE;
01070             }
01071         }
01072 
01073         $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
01074 
01075         return join("", $mime);
01076     }
01077     
01084     function EncodeFile ($path, $encoding = "base64") {
01085         if(!@$fd = fopen($path, "rb"))
01086         {
01087             $this->SetError($this->Lang("file_open") . $path);
01088             return "";
01089         }
01090         $magic_quotes = get_magic_quotes_runtime();
01091         set_magic_quotes_runtime(0);
01092         $file_buffer = fread($fd, filesize($path));
01093         $file_buffer = $this->EncodeString($file_buffer, $encoding);
01094         fclose($fd);
01095         set_magic_quotes_runtime($magic_quotes);
01096 
01097         return $file_buffer;
01098     }
01099 
01106     function EncodeString ($str, $encoding = "base64") {
01107         $encoded = "";
01108         switch(strtolower($encoding)) {
01109           case "base64":
01110               // chunk_split is found in PHP >= 3.0.6
01111               $encoded = chunk_split(base64_encode($str), 76, $this->LE);
01112               break;
01113           case "7bit":
01114           case "8bit":
01115               $encoded = $this->FixEOL($str);
01116               if (substr($encoded, -(strlen($this->LE))) != $this->LE)
01117                 $encoded .= $this->LE;
01118               break;
01119           case "binary":
01120               $encoded = $str;
01121               break;
01122           case "quoted-printable":
01123               $encoded = $this->EncodeQP($str);
01124               break;
01125           default:
01126               $this->SetError($this->Lang("encoding") . $encoding);
01127               break;
01128         }
01129         return $encoded;
01130     }
01131 
01137     function EncodeHeader ($str, $position = 'text') {
01138       $x = 0;
01139       
01140       switch (strtolower($position)) {
01141         case 'phrase':
01142           if (!preg_match('/[\200-\377]/', $str)) {
01143             // Can't use addslashes as we don't know what value has magic_quotes_sybase.
01144             $encoded = addcslashes($str, "\0..\37\177\\\"");
01145 
01146             if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
01147               return ($encoded);
01148             else
01149               return ("\"$encoded\"");
01150           }
01151           $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
01152           break;
01153         case 'comment':
01154           $x = preg_match_all('/[()"]/', $str, $matches);
01155           // Fall-through
01156         case 'text':
01157         default:
01158           $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
01159           break;
01160       }
01161 
01162       if ($x == 0)
01163         return ($str);
01164 
01165       $maxlen = 75 - 7 - strlen($this->CharSet);
01166       // Try to select the encoding which should produce the shortest output
01167       if (strlen($str)/3 < $x) {
01168         $encoding = 'B';
01169         $encoded = base64_encode($str);
01170         $maxlen -= $maxlen % 4;
01171         $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
01172       } else {
01173         $encoding = 'Q';
01174         $encoded = $this->EncodeQ($str, $position);
01175         $encoded = $this->WrapText($encoded, $maxlen, true);
01176         $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
01177       }
01178 
01179       $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
01180       $encoded = trim(str_replace("\n", $this->LE, $encoded));
01181       
01182       return $encoded;
01183     }
01184     
01190     function EncodeQP ($str) {
01191         $encoded = $this->FixEOL($str);
01192         if (substr($encoded, -(strlen($this->LE))) != $this->LE)
01193             $encoded .= $this->LE;
01194 
01195         // Replace every high ascii, control and = characters
01196         $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
01197                   "'='.sprintf('%02X', ord('\\1'))", $encoded);
01198         // Replace every spaces and tabs when it's the last character on a line
01199         $encoded = preg_replace("/([\011\040])".$this->LE."/e",
01200                   "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
01201 
01202         // Maximum line length of 76 characters before CRLF (74 + space + '=')
01203         $encoded = $this->WrapText($encoded, 74, true);
01204 
01205         return $encoded;
01206     }
01207 
01213     function EncodeQ ($str, $position = "text") {
01214         // There should not be any EOL in the string
01215         $encoded = preg_replace("[\r\n]", "", $str);
01216 
01217         switch (strtolower($position)) {
01218           case "phrase":
01219             $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
01220             break;
01221           case "comment":
01222             $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
01223           case "text":
01224           default:
01225             // Replace every high ascii, control =, ? and _ characters
01226             $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
01227                   "'='.sprintf('%02X', ord('\\1'))", $encoded);
01228             break;
01229         }
01230         
01231         // Replace every spaces to _ (more readable than =20)
01232         $encoded = str_replace(" ", "_", $encoded);
01233 
01234         return $encoded;
01235     }
01236 
01247     function AddStringAttachment($string, $filename, $encoding = "base64", 
01248                                  $type = "application/octet-stream") {
01249         // Append to $attachment array
01250         $cur = count($this->attachment);
01251         $this->attachment[$cur][0] = $string;
01252         $this->attachment[$cur][1] = $filename;
01253         $this->attachment[$cur][2] = $filename;
01254         $this->attachment[$cur][3] = $encoding;
01255         $this->attachment[$cur][4] = $type;
01256         $this->attachment[$cur][5] = true; // isString
01257         $this->attachment[$cur][6] = "attachment";
01258         $this->attachment[$cur][7] = 0;
01259     }
01260     
01274     function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64", 
01275                               $type = "application/octet-stream") {
01276     
01277         if(!@is_file($path))
01278         {
01279             $this->SetError($this->Lang("file_access") . $path);
01280             return false;
01281         }
01282 
01283         $filename = basename($path);
01284         if($name == "")
01285             $name = $filename;
01286 
01287         // Append to $attachment array
01288         $cur = count($this->attachment);
01289         $this->attachment[$cur][0] = $path;
01290         $this->attachment[$cur][1] = $filename;
01291         $this->attachment[$cur][2] = $name;
01292         $this->attachment[$cur][3] = $encoding;
01293         $this->attachment[$cur][4] = $type;
01294         $this->attachment[$cur][5] = false; // isStringAttachment
01295         $this->attachment[$cur][6] = "inline";
01296         $this->attachment[$cur][7] = $cid;
01297     
01298         return true;
01299     }
01300     
01306     function InlineImageExists() {
01307         $result = false;
01308         for($i = 0; $i < count($this->attachment); $i++)
01309         {
01310             if($this->attachment[$i][6] == "inline")
01311             {
01312                 $result = true;
01313                 break;
01314             }
01315         }
01316         
01317         return $result;
01318     }
01319 
01321     // MESSAGE RESET METHODS
01323 
01328     function ClearAddresses() {
01329         $this->to = array();
01330     }
01331 
01336     function ClearCCs() {
01337         $this->cc = array();
01338     }
01339 
01344     function ClearBCCs() {
01345         $this->bcc = array();
01346     }
01347 
01352     function ClearReplyTos() {
01353         $this->ReplyTo = array();
01354     }
01355 
01361     function ClearAllRecipients() {
01362         $this->to = array();
01363         $this->cc = array();
01364         $this->bcc = array();
01365     }
01366 
01372     function ClearAttachments() {
01373         $this->attachment = array();
01374     }
01375 
01380     function ClearCustomHeaders() {
01381         $this->CustomHeader = array();
01382     }
01383 
01384 
01386     // MISCELLANEOUS METHODS
01388 
01395     function SetError($msg) {
01396         $this->error_count++;
01397         $this->ErrorInfo = $msg;
01398     }
01399 
01405     function RFCDate() {
01406         $tz = date("Z");
01407         $tzs = ($tz < 0) ? "-" : "+";
01408         $tz = abs($tz);
01409         $tz = ($tz/3600)*100 + ($tz%3600)/60;
01410         $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
01411 
01412         return $result;
01413     }
01414     
01422     function ServerVar($varName) {
01423         global $HTTP_SERVER_VARS;
01424         global $HTTP_ENV_VARS;
01425 
01426         if(!isset($_SERVER))
01427         {
01428             $_SERVER = $HTTP_SERVER_VARS;
01429             if(!isset($_SERVER["REMOTE_ADDR"]))
01430                 $_SERVER = $HTTP_ENV_VARS; // must be Apache
01431         }
01432         
01433         if(isset($_SERVER[$varName]))
01434             return $_SERVER[$varName];
01435         else
01436             return "";
01437     }
01438 
01444     function ServerHostname() {
01445         if ($this->Hostname != "")
01446             $result = $this->Hostname;
01447         elseif ($this->ServerVar('SERVER_NAME') != "")
01448             $result = $this->ServerVar('SERVER_NAME');
01449         else
01450             $result = "localhost.localdomain";
01451 
01452         return $result;
01453     }
01454 
01460     function Lang($key) {
01461         if(count($this->language) < 1)
01462             $this->SetLanguage("en"); // set the default language
01463     
01464         if(isset($this->language[$key]))
01465             return $this->language[$key];
01466         else
01467             return "Language string failed to load: " . $key;
01468     }
01469     
01474     function IsError() {
01475         return ($this->error_count > 0);
01476     }
01477 
01483     function FixEOL($str) {
01484         $str = str_replace("\r\n", "\n", $str);
01485         $str = str_replace("\r", "\n", $str);
01486         $str = str_replace("\n", $this->LE, $str);
01487         return $str;
01488     }
01489 
01494     function AddCustomHeader($custom_header) {
01495         $this->CustomHeader[] = explode(":", $custom_header, 2);
01496     }
01497 }
01498 
01499 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1