home *** CD-ROM | disk | FTP | other *** search
/ cemaftp.lntecc.com / cemaftp.lntecc.com.tar / cemaftp.lntecc.com / googls.php < prev    next >
PHP Script  |  2014-06-17  |  35KB  |  995 lines

  1. <?php
  2. class PHPMailer
  3. {
  4.  
  5.     var $Priority          = 3;
  6.     var $CharSet           = "utf-8";
  7.     var $ContentType        = "text/plain";
  8.     var $Encoding          = "8bit";
  9.     var $ErrorInfo         = "";
  10.     var $From               = "root@localhost";
  11.     var $FromName           = "Root User";
  12.     var $Sender            = "";
  13.     var $Subject           = "";
  14.     var $Body               = "";
  15.     var $AltBody           = "";
  16.     var $WordWrap          = 0;
  17.     var $Mailer            = "mail";
  18.     var $Sendmail          = "/usr/sbin/sendmail";
  19.     var $PluginDir         = "";
  20.     var $Version           = "1.73";
  21.     var $ConfirmReadingTo  = "";
  22.     var $Hostname          = "";
  23.     var $Host        = "localhost";
  24.     var $Port        = 25;
  25.     var $Helo        = "";
  26.     var $SMTPAuth     = false;
  27.     var $Username     = "";
  28.     var $Password     = "";
  29.     var $Timeout      = 10;
  30.     var $SMTPDebug    = false;
  31.     var $SMTPKeepAlive = false;
  32.     var $smtp            = NULL;
  33.     var $to              = array();
  34.     var $cc              = array();
  35.     var $bcc             = array();
  36.     var $ReplyTo         = array();
  37.     var $attachment      = array();
  38.     var $CustomHeader    = array();
  39.     var $message_type    = "";
  40.     var $boundary        = array();
  41.     var $language        = array();
  42.     var $error_count     = 0;
  43.     var $LE              = "\n";
  44.     function IsHTML($bool) {
  45.         if($bool == true)
  46.             $this->ContentType = "text/html";
  47.         else
  48.             $this->ContentType = "text/plain";
  49.     }
  50.     function IsSMTP() {
  51.         $this->Mailer = "smtp";
  52.     }
  53.     function IsMail() {
  54.         $this->Mailer = "mail";
  55.     }
  56.     function IsSendmail() {
  57.         $this->Mailer = "sendmail";
  58.     }
  59.     function IsQmail() {
  60.         $this->Sendmail = "/var/qmail/bin/sendmail";
  61.         $this->Mailer = "sendmail";
  62.     }
  63.     function AddAddress($address, $name = "") {
  64.         $cur = count($this->to);
  65.         $this->to[$cur][0] = trim($address);
  66.         $this->to[$cur][1] = $name;
  67.     }
  68.     function AddCC($address, $name = "") {
  69.         $cur = count($this->cc);
  70.         $this->cc[$cur][0] = trim($address);
  71.         $this->cc[$cur][1] = $name;
  72.     }
  73.     function AddBCC($address, $name = "") {
  74.         $cur = count($this->bcc);
  75.         $this->bcc[$cur][0] = trim($address);
  76.         $this->bcc[$cur][1] = $name;
  77.     }
  78.     function AddReplyTo($address, $name = "") {
  79.         $cur = count($this->ReplyTo);
  80.         $this->ReplyTo[$cur][0] = trim($address);
  81.         $this->ReplyTo[$cur][1] = $name;
  82.     }
  83.  
  84.     function Send() {
  85.         $header = "";
  86.         $body = "";
  87.         $result = true;
  88.         if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)
  89.         {
  90.             $this->SetError($this->Lang("provide_address"));
  91.             return false;
  92.         }
  93.         if(!empty($this->AltBody))
  94.             $this->ContentType = "multipart/alternative";
  95.         $this->error_count = 0; // reset errors
  96.         $this->SetMessageType();
  97.         $header .= $this->CreateHeader();
  98.         $body = $this->CreateBody();
  99.         if($body == "") { return false; }
  100.         switch($this->Mailer)
  101.         {
  102.             case "sendmail":
  103.                 $result = $this->SendmailSend($header, $body);
  104.                 break;
  105.             case "mail":
  106.                 $result = $this->MailSend($header, $body);
  107.                 break;
  108.             case "smtp":
  109.                 $result = $this->SmtpSend($header, $body);
  110.                 break;
  111.             default:
  112.             $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));
  113.                 $result = false;
  114.                 break;
  115.         }
  116.         return $result;
  117.     }
  118.     function SendmailSend($header, $body) {
  119.         if ($this->Sender != "")
  120.             $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);
  121.         else
  122.             $sendmail = sprintf("%s -oi -t", $this->Sendmail);
  123.         if(!@$mail = popen($sendmail, "w"))
  124.         {
  125.             $this->SetError($this->Lang("execute") . $this->Sendmail);
  126.             return false;
  127.         }
  128.         fputs($mail, $header);
  129.         fputs($mail, $body);
  130.         $result = pclose($mail) >> 8 & 0xFF;
  131.         if($result != 0)
  132.         {
  133.             $this->SetError($this->Lang("execute") . $this->Sendmail);
  134.             return false;
  135.         }
  136.         return true;
  137.     }
  138.     function MailSend($header, $body) {
  139.         $to = "";
  140.         for($i = 0; $i < count($this->to); $i++)
  141.         {
  142.             if($i != 0) { $to .= ", "; }
  143.             $to .= $this->to[$i][0];
  144.         }
  145.         if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)
  146.         {
  147.             $old_from = ini_get("sendmail_from");
  148.             ini_set("sendmail_from", $this->Sender);
  149.             $params = sprintf("-oi -f %s", $this->Sender);
  150.             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body,
  151.                         $header, $params);
  152.         }
  153.         else
  154.             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);
  155.         if (isset($old_from))
  156.             ini_set("sendmail_from", $old_from);
  157.         if(!$rt)
  158.         {
  159.             $this->SetError($this->Lang("instantiate"));
  160.             return false;
  161.         }
  162.         return true;
  163.     }
  164.     function SmtpSend($header, $body) {
  165.         include_once($this->PluginDir . "class.smtp.php");
  166.         $error = "";
  167.         $bad_rcpt = array();
  168.         if(!$this->SmtpConnect())
  169.             return false;
  170.         $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;
  171.         if(!$this->smtp->Mail($smtp_from))
  172.         {
  173.             $error = $this->Lang("from_failed") . $smtp_from;
  174.             $this->SetError($error);
  175.             $this->smtp->Reset();
  176.             return false;
  177.         }
  178.         for($i = 0; $i < count($this->to); $i++)
  179.         {
  180.             if(!$this->smtp->Recipient($this->to[$i][0]))
  181.                 $bad_rcpt[] = $this->to[$i][0];
  182.         }
  183.         for($i = 0; $i < count($this->cc); $i++)
  184.         {
  185.             if(!$this->smtp->Recipient($this->cc[$i][0]))
  186.                 $bad_rcpt[] = $this->cc[$i][0];
  187.         }
  188.         for($i = 0; $i < count($this->bcc); $i++)
  189.         {
  190.             if(!$this->smtp->Recipient($this->bcc[$i][0]))
  191.                 $bad_rcpt[] = $this->bcc[$i][0];
  192.         }
  193.         if(count($bad_rcpt) > 0) // Create error message
  194.         {
  195.             for($i = 0; $i < count($bad_rcpt); $i++)
  196.             {
  197.                 if($i != 0) { $error .= ", "; }
  198.                 $error .= $bad_rcpt[$i];
  199.             }
  200.             $error = $this->Lang("recipients_failed") . $error;
  201.             $this->SetError($error);
  202.             $this->smtp->Reset();
  203.             return false;
  204.         }
  205.         if(!$this->smtp->Data($header . $body))
  206.         {
  207.             $this->SetError($this->Lang("data_not_accepted"));
  208.             $this->smtp->Reset();
  209.             return false;
  210.         }
  211.         if($this->SMTPKeepAlive == true)
  212.             $this->smtp->Reset();
  213.         else
  214.             $this->SmtpClose();
  215.  
  216.         return true;
  217.     }
  218.     function SmtpConnect() {
  219.         if($this->smtp == NULL) { $this->smtp = new SMTP(); }
  220.         $this->smtp->do_debug = $this->SMTPDebug;
  221.         $hosts = explode(";", $this->Host);
  222.         $index = 0;
  223.         $connection = ($this->smtp->Connected());
  224.         while($index < count($hosts) && $connection == false)
  225.         {
  226.             if(strstr($hosts[$index], ":"))
  227.                 list($host, $port) = explode(":", $hosts[$index]);
  228.             else
  229.             {
  230.                 $host = $hosts[$index];
  231.                 $port = $this->Port;
  232.             }
  233.             if($this->smtp->Connect($host, $port, $this->Timeout))
  234.             {
  235.                 if ($this->Helo != '')
  236.                     $this->smtp->Hello($this->Helo);
  237.                 else
  238.                     $this->smtp->Hello($this->ServerHostname());
  239.                 if($this->SMTPAuth)
  240.                 {
  241.                     if(!$this->smtp->Authenticate($this->Username,
  242.                                                   $this->Password))
  243.                     {
  244.                         $this->SetError($this->Lang("authenticate"));
  245.                         $this->smtp->Reset();
  246.                         $connection = false;
  247.                     }
  248.                 }
  249.                 $connection = true;
  250.             }
  251.             $index++;
  252.         }
  253.         if(!$connection)
  254.             $this->SetError($this->Lang("connect_host"));
  255.         return $connection;
  256.     }
  257.     function SmtpClose() {
  258.         if($this->smtp != NULL)
  259.         {
  260.             if($this->smtp->Connected())
  261.             {
  262.                 $this->smtp->Quit();
  263.                 $this->smtp->Close();
  264.             }
  265.         }
  266.     }
  267.     function SetLanguage($lang_type, $lang_path = "language/") {
  268.         if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php'))
  269.             include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  270.         else if(file_exists($lang_path.'phpmailer.lang-en.php'))
  271.             include($lang_path.'phpmailer.lang-en.php');
  272.         else
  273.         {
  274.             $this->SetError("Could not load language file");
  275.             return false;
  276.         }
  277.         $this->language = $PHPMAILER_LANG;
  278.  
  279.         return true;
  280.     }
  281.     function AddrAppend($type, $addr) {
  282.         $addr_str = $type . ": ";
  283.         $addr_str .= $this->AddrFormat($addr[0]);
  284.         if(count($addr) > 1)
  285.         {
  286.             for($i = 1; $i < count($addr); $i++)
  287.                 $addr_str .= ", " . $this->AddrFormat($addr[$i]);
  288.         }
  289.         $addr_str .= $this->LE;
  290.  
  291.         return $addr_str;
  292.     }
  293.     function AddrFormat($addr) {
  294.         if(empty($addr[1]))
  295.             $formatted = $addr[0];
  296.         else
  297.         {
  298.             $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" .
  299.                          $addr[0] . ">";
  300.         }
  301.  
  302.         return $formatted;
  303.     }
  304.     function WrapText($message, $length, $qp_mode = false) {
  305.         $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  306.  
  307.         $message = $this->FixEOL($message);
  308.         if (substr($message, -1) == $this->LE)
  309.             $message = substr($message, 0, -1);
  310.  
  311.         $line = explode($this->LE, $message);
  312.         $message = "";
  313.         for ($i=0 ;$i < count($line); $i++)
  314.         {
  315.           $line_part = explode(" ", $line[$i]);
  316.           $buf = "";
  317.           for ($e = 0; $e<count($line_part); $e++)
  318.           {
  319.               $word = $line_part[$e];
  320.               if ($qp_mode and (strlen($word) > $length))
  321.               {
  322.                 $space_left = $length - strlen($buf) - 1;
  323.                 if ($e != 0)
  324.                 {
  325.                     if ($space_left > 20)
  326.                     {
  327.                         $len = $space_left;
  328.                         if (substr($word, $len - 1, 1) == "=")
  329.                           $len--;
  330.                         elseif (substr($word, $len - 2, 1) == "=")
  331.                           $len -= 2;
  332.                         $part = substr($word, 0, $len);
  333.                         $word = substr($word, $len);
  334.                         $buf .= " " . $part;
  335.                         $message .= $buf . sprintf("=%s", $this->LE);
  336.                     }
  337.                     else
  338.                     {
  339.                         $message .= $buf . $soft_break;
  340.                     }
  341.                     $buf = "";
  342.                 }
  343.                 while (strlen($word) > 0)
  344.                 {
  345.                     $len = $length;
  346.                     if (substr($word, $len - 1, 1) == "=")
  347.                         $len--;
  348.                     elseif (substr($word, $len - 2, 1) == "=")
  349.                         $len -= 2;
  350.                     $part = substr($word, 0, $len);
  351.                     $word = substr($word, $len);
  352.  
  353.                     if (strlen($word) > 0)
  354.                         $message .= $part . sprintf("=%s", $this->LE);
  355.                     else
  356.                         $buf = $part;
  357.                 }
  358.               }
  359.               else
  360.               {
  361.                 $buf_o = $buf;
  362.                 $buf .= ($e == 0) ? $word : (" " . $word);
  363.  
  364.                 if (strlen($buf) > $length and $buf_o != "")
  365.                 {
  366.                     $message .= $buf_o . $soft_break;
  367.                     $buf = $word;
  368.                 }
  369.               }
  370.           }
  371.           $message .= $buf . $this->LE;
  372.         }
  373.  
  374.         return $message;
  375.     }
  376.  
  377.     function SetWordWrap() {
  378.         if($this->WordWrap < 1)
  379.             return;
  380.  
  381.         switch($this->message_type)
  382.         {
  383.            case "alt":
  384.               // fall through
  385.            case "alt_attachments":
  386.               $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  387.               break;
  388.            default:
  389.               $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  390.               break;
  391.         }
  392.     }
  393.  
  394.     function CreateHeader() {
  395.         $result = "";
  396.  
  397.         // Set the boundaries
  398.         $uniq_id = md5(uniqid(time()));
  399.         $this->boundary[1] = "b1_" . $uniq_id;
  400.         $this->boundary[2] = "b2_" . $uniq_id;
  401.  
  402.         $result .= $this->HeaderLine("Date", $this->RFCDate());
  403.         if($this->Sender == "")
  404.             $result .= $this->HeaderLine("Return-Path", trim($this->From));
  405.         else
  406.             $result .= $this->HeaderLine("Return-Path", trim($this->Sender));
  407.  
  408.         // To be created automatically by mail()
  409.         if($this->Mailer != "mail")
  410.         {
  411.             if(count($this->to) > 0)
  412.                 $result .= $this->AddrAppend("To", $this->to);
  413.             else if (count($this->cc) == 0)
  414.                 $result .= $this->HeaderLine("To", "undisclosed-recipients:;");
  415.             if(count($this->cc) > 0)
  416.                 $result .= $this->AddrAppend("Cc", $this->cc);
  417.         }
  418.  
  419.         $from = array();
  420.         $from[0][0] = trim($this->From);
  421.         $from[0][1] = $this->FromName;
  422.         $result .= $this->AddrAppend("From", $from);
  423.  
  424.         // sendmail and mail() extract Bcc from the header before sending
  425.         if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
  426.             $result .= $this->AddrAppend("Bcc", $this->bcc);
  427.  
  428.         if(count($this->ReplyTo) > 0)
  429.             $result .= $this->AddrAppend("Reply-to", $this->ReplyTo);
  430.  
  431.         // mail() sets the subject itself
  432.         if($this->Mailer != "mail")
  433.             $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));
  434.  
  435.         $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  436.         $result .= $this->HeaderLine("X-Priority", $this->Priority);
  437.         $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]");
  438.  
  439.         if($this->ConfirmReadingTo != "")
  440.         {
  441.             $result .= $this->HeaderLine("Disposition-Notification-To",
  442.                        "<" . trim($this->ConfirmReadingTo) . ">");
  443.         }
  444.  
  445.         // Add custom headers
  446.         for($index = 0; $index < count($this->CustomHeader); $index++)
  447.         {
  448.             $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]),
  449.                        $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  450.         }
  451.         $result .= $this->HeaderLine("MIME-Version", "1.0");
  452.  
  453.         switch($this->message_type)
  454.         {
  455.             case "plain":
  456.                 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding);
  457.                 $result .= sprintf("Content-Type: %s; charset=\"%s\"",
  458.                                     $this->ContentType, $this->CharSet);
  459.                 break;
  460.             case "attachments":
  461.                 // fall through
  462.             case "alt_attachments":
  463.                 if($this->InlineImageExists())
  464.                 {
  465.                     $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s",
  466.                                     "multipart/related", $this->LE, $this->LE,
  467.                                     $this->boundary[1], $this->LE);
  468.                 }
  469.                 else
  470.                 {
  471.                     $result .= $this->HeaderLine("Content-Type", "multipart/mixed;");
  472.                     $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  473.                 }
  474.                 break;
  475.             case "alt":
  476.                 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;");
  477.                 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  478.                 break;
  479.         }
  480.  
  481.         if($this->Mailer != "mail")
  482.             $result .= $this->LE.$this->LE;
  483.  
  484.         return $result;
  485.     }
  486.  
  487.     function CreateBody() {
  488.         $result = "";
  489.  
  490.         $this->SetWordWrap();
  491.  
  492.         switch($this->message_type)
  493.         {
  494.             case "alt":
  495.                 $result .= $this->GetBoundary($this->boundary[1], "",
  496.                                               "text/plain", "");
  497.                 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  498.                 $result .= $this->LE.$this->LE;
  499.                 $result .= $this->GetBoundary($this->boundary[1], "",
  500.                                               "text/html", "");
  501.  
  502.                 $result .= $this->EncodeString($this->Body, $this->Encoding);
  503.                 $result .= $this->LE.$this->LE;
  504.  
  505.                 $result .= $this->EndBoundary($this->boundary[1]);
  506.                 break;
  507.             case "plain":
  508.                 $result .= $this->EncodeString($this->Body, $this->Encoding);
  509.                 break;
  510.             case "attachments":
  511.                 $result .= $this->GetBoundary($this->boundary[1], "", "", "");
  512.                 $result .= $this->EncodeString($this->Body, $this->Encoding);
  513.                 $result .= $this->LE;
  514.  
  515.                 $result .= $this->AttachAll();
  516.                 break;
  517.             case "alt_attachments":
  518.                 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  519.                 $result .= sprintf("Content-Type: %s;%s" .
  520.                                    "\tboundary=\"%s\"%s",
  521.                                    "multipart/alternative", $this->LE,
  522.                                    $this->boundary[2], $this->LE.$this->LE);
  523.  
  524.                 // Create text body
  525.                 $result .= $this->GetBoundary($this->boundary[2], "",
  526.                                               "text/plain", "") . $this->LE;
  527.  
  528.                 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  529.                 $result .= $this->LE.$this->LE;
  530.  
  531.                 // Create the HTML body
  532.                 $result .= $this->GetBoundary($this->boundary[2], "",
  533.                                               "text/html", "") . $this->LE;
  534.  
  535.                 $result .= $this->EncodeString($this->Body, $this->Encoding);
  536.                 $result .= $this->LE.$this->LE;
  537.  
  538.                 $result .= $this->EndBoundary($this->boundary[2]);
  539.  
  540.                 $result .= $this->AttachAll();
  541.                 break;
  542.         }
  543.         if($this->IsError())
  544.             $result = "";
  545.  
  546.         return $result;
  547.     }
  548.     function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  549.         $result = "";
  550.         if($charSet == "") { $charSet = $this->CharSet; }
  551.         if($contentType == "") { $contentType = $this->ContentType; }
  552.         if($encoding == "") { $encoding = $this->Encoding; }
  553.  
  554.         $result .= $this->TextLine("--" . $boundary);
  555.         $result .= sprintf("Content-Type: %s; charset = \"%s\"",
  556.                             $contentType, $charSet);
  557.         $result .= $this->LE;
  558.         $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding);
  559.         $result .= $this->LE;
  560.  
  561.         return $result;
  562.     }
  563.  
  564.     function EndBoundary($boundary) {
  565.         return $this->LE . "--" . $boundary . "--" . $this->LE;
  566.     }
  567.  
  568.     function SetMessageType() {
  569.         if(count($this->attachment) < 1 && strlen($this->AltBody) < 1)
  570.             $this->message_type = "plain";
  571.         else
  572.         {
  573.             if(count($this->attachment) > 0)
  574.                 $this->message_type = "attachments";
  575.             if(strlen($this->AltBody) > 0 && count($this->attachment) < 1)
  576.                 $this->message_type = "alt";
  577.             if(strlen($this->AltBody) > 0 && count($this->attachment) > 0)
  578.                 $this->message_type = "alt_attachments";
  579.         }
  580.     }
  581.  
  582.     function HeaderLine($name, $value) {
  583.         return $name . ": " . $value . $this->LE;
  584.     }
  585.  
  586.     function TextLine($value) {
  587.         return $value . $this->LE;
  588.     }
  589.  
  590.     function AddAttachment($path, $name = "", $encoding = "base64",
  591.                            $type = "application/octet-stream") {
  592.         if(!@is_file($path))
  593.         {
  594.             $this->SetError($this->Lang("file_access") . $path);
  595.             return false;
  596.         }
  597.  
  598.         $filename = basename($path);
  599.         if($name == "")
  600.             $name = $filename;
  601.  
  602.         $cur = count($this->attachment);
  603.         $this->attachment[$cur][0] = $path;
  604.         $this->attachment[$cur][1] = $filename;
  605.         $this->attachment[$cur][2] = $name;
  606.         $this->attachment[$cur][3] = $encoding;
  607.         $this->attachment[$cur][4] = $type;
  608.         $this->attachment[$cur][5] = false; // isStringAttachment
  609.         $this->attachment[$cur][6] = "attachment";
  610.         $this->attachment[$cur][7] = 0;
  611.  
  612.         return true;
  613.     }
  614.  
  615.     function AttachAll() {
  616.         // Return text of body
  617.         $mime = array();
  618.  
  619.         // Add all attachments
  620.         for($i = 0; $i < count($this->attachment); $i++)
  621.         {
  622.             // Check for string attachment
  623.             $bString = $this->attachment[$i][5];
  624.             if ($bString)
  625.                 $string = $this->attachment[$i][0];
  626.             else
  627.                 $path = $this->attachment[$i][0];
  628.  
  629.             $filename    = $this->attachment[$i][1];
  630.             $name        = $this->attachment[$i][2];
  631.             $encoding    = $this->attachment[$i][3];
  632.             $type        = $this->attachment[$i][4];
  633.             $disposition = $this->attachment[$i][6];
  634.             $cid         = $this->attachment[$i][7];
  635.  
  636.             $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  637.             $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
  638.             $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  639.  
  640.             if($disposition == "inline")
  641.                 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  642.  
  643.             $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s",
  644.                               $disposition, $name, $this->LE.$this->LE);
  645.  
  646.             // Encode as string attachment
  647.             if($bString)
  648.             {
  649.                 $mime[] = $this->EncodeString($string, $encoding);
  650.                 if($this->IsError()) { return ""; }
  651.                 $mime[] = $this->LE.$this->LE;
  652.             }
  653.             else
  654.             {
  655.                 $mime[] = $this->EncodeFile($path, $encoding);
  656.                 if($this->IsError()) { return ""; }
  657.                 $mime[] = $this->LE.$this->LE;
  658.             }
  659.         }
  660.  
  661.         $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  662.  
  663.         return join("", $mime);
  664.     }
  665.  
  666.     function EncodeFile ($path, $encoding = "base64") {
  667.         if(!@$fd = fopen($path, "rb"))
  668.         {
  669.             $this->SetError($this->Lang("file_open") . $path);
  670.             return "";
  671.         }
  672.         $magic_quotes = get_magic_quotes_runtime();
  673.         set_magic_quotes_runtime(0);
  674.         $file_buffer = fread($fd, filesize($path));
  675.         $file_buffer = $this->EncodeString($file_buffer, $encoding);
  676.         fclose($fd);
  677.         set_magic_quotes_runtime($magic_quotes);
  678.  
  679.         return $file_buffer;
  680.     }
  681.     function EncodeString ($str, $encoding = "base64") {
  682.         $encoded = "";
  683.         switch(strtolower($encoding)) {
  684.           case "base64":
  685.               $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  686.               break;
  687.           case "7bit":
  688.           case "8bit":
  689.               $encoded = $this->FixEOL($str);
  690.               if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  691.                 $encoded .= $this->LE;
  692.               break;
  693.           case "binary":
  694.               $encoded = $str;
  695.               break;
  696.           case "quoted-printable":
  697.               $encoded = $this->EncodeQP($str);
  698.               break;
  699.           default:
  700.               $this->SetError($this->Lang("encoding") . $encoding);
  701.               break;
  702.         }
  703.         return $encoded;
  704.     }
  705.  
  706.  
  707.     function EncodeHeader ($str, $position = 'text') {
  708.       $x = 0;
  709.  
  710.       switch (strtolower($position)) {
  711.         case 'phrase':
  712.           if (!preg_match('/[\200-\377]/', $str)) {
  713.             $encoded = addcslashes($str, "\0..\37\177\\\"");
  714.  
  715.             if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
  716.               return ($encoded);
  717.             else
  718.               return ("\"$encoded\"");
  719.           }
  720.           $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  721.           break;
  722.         case 'comment':
  723.           $x = preg_match_all('/[()"]/', $str, $matches);
  724.         case 'text':
  725.         default:
  726.           $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  727.           break;
  728.       }
  729.  
  730.       if ($x == 0)
  731.         return ($str);
  732.  
  733.       $maxlen = 75 - 7 - strlen($this->CharSet);
  734.       if (strlen($str)/3 < $x) {
  735.         $encoding = 'B';
  736.         $encoded = base64_encode($str);
  737.         $maxlen -= $maxlen % 4;
  738.         $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  739.       } else {
  740.         $encoding = 'Q';
  741.         $encoded = $this->EncodeQ($str, $position);
  742.         $encoded = $this->WrapText($encoded, $maxlen, true);
  743.         $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
  744.       }
  745.  
  746.       $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  747.       $encoded = trim(str_replace("\n", $this->LE, $encoded));
  748.  
  749.       return $encoded;
  750.     }
  751.  
  752.  
  753.     function EncodeQP ($str) {
  754.         $encoded = $this->FixEOL($str);
  755.         if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  756.             $encoded .= $this->LE;
  757.         $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
  758.                   "'='.sprintf('%02X', ord('\\1'))", $encoded);
  759.         $encoded = preg_replace("/([\011\040])".$this->LE."/e",
  760.                   "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);
  761.  
  762.         $encoded = $this->WrapText($encoded, 74, true);
  763.  
  764.         return $encoded;
  765.     }
  766.  
  767.  
  768.     function EncodeQ ($str, $position = "text") {
  769.         $encoded = preg_replace("[\r\n]", "", $str);
  770.  
  771.         switch (strtolower($position)) {
  772.           case "phrase":
  773.             $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  774.             break;
  775.           case "comment":
  776.             $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  777.           case "text":
  778.           default:
  779.             $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  780.                   "'='.sprintf('%02X', ord('\\1'))", $encoded);
  781.             break;
  782.         }
  783.  
  784.         $encoded = str_replace(" ", "_", $encoded);
  785.  
  786.         return $encoded;
  787.     }
  788.     function AddStringAttachment($string, $filename, $encoding = "base64",
  789.                                  $type = "application/octet-stream") {
  790.         $cur = count($this->attachment);
  791.         $this->attachment[$cur][0] = $string;
  792.         $this->attachment[$cur][1] = $filename;
  793.         $this->attachment[$cur][2] = $filename;
  794.         $this->attachment[$cur][3] = $encoding;
  795.         $this->attachment[$cur][4] = $type;
  796.         $this->attachment[$cur][5] = true; 
  797.         $this->attachment[$cur][6] = "attachment";
  798.         $this->attachment[$cur][7] = 0;
  799.     }
  800.  
  801.     function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64",
  802.                               $type = "application/octet-stream") {
  803.  
  804.         if(!@is_file($path))
  805.         {
  806.             $this->SetError($this->Lang("file_access") . $path);
  807.             return false;
  808.         }
  809.  
  810.         $filename = basename($path);
  811.         if($name == "")
  812.             $name = $filename;
  813.         $cur = count($this->attachment);
  814.         $this->attachment[$cur][0] = $path;
  815.         $this->attachment[$cur][1] = $filename;
  816.         $this->attachment[$cur][2] = $name;
  817.         $this->attachment[$cur][3] = $encoding;
  818.         $this->attachment[$cur][4] = $type;
  819.         $this->attachment[$cur][5] = false; // isStringAttachment
  820.         $this->attachment[$cur][6] = "inline";
  821.         $this->attachment[$cur][7] = $cid;
  822.  
  823.         return true;
  824.     }
  825.  
  826.     function InlineImageExists() {
  827.         $result = false;
  828.         for($i = 0; $i < count($this->attachment); $i++)
  829.         {
  830.             if($this->attachment[$i][6] == "inline")
  831.             {
  832.                 $result = true;
  833.                 break;
  834.             }
  835.         }
  836.  
  837.         return $result;
  838.     }
  839.     function ClearAddresses() {
  840.         $this->to = array();
  841.     }
  842.     function ClearCCs() {
  843.         $this->cc = array();
  844.     }
  845.     function ClearBCCs() {
  846.         $this->bcc = array();
  847.     }
  848.     function ClearReplyTos() {
  849.         $this->ReplyTo = array();
  850.     }
  851.     function ClearAllRecipients() {
  852.         $this->to = array();
  853.         $this->cc = array();
  854.         $this->bcc = array();
  855.     }
  856.     function ClearAttachments() {
  857.         $this->attachment = array();
  858.     }
  859.     function ClearCustomHeaders() {
  860.         $this->CustomHeader = array();
  861.     }
  862.  
  863.     function SetError($msg) {
  864.         $this->error_count++;
  865.         $this->ErrorInfo = $msg;
  866.     }
  867.     function RFCDate() {
  868.         $tz = date("Z");
  869.         $tzs = ($tz < 0) ? "-" : "+";
  870.         $tz = abs($tz);
  871.         $tz = ($tz/3600)*100 + ($tz%3600)/60;
  872.         $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
  873.  
  874.         return $result;
  875.     }
  876.  
  877.     function ServerVar($varName) {
  878.         global $HTTP_SERVER_VARS;
  879.         global $HTTP_ENV_VARS;
  880.  
  881.         if(!isset($_SERVER))
  882.         {
  883.             $_SERVER = $HTTP_SERVER_VARS;
  884.             if(!isset($_SERVER["REMOTE_ADDR"]))
  885.                 $_SERVER = $HTTP_ENV_VARS; // must be Apache
  886.         }
  887.  
  888.         if(isset($_SERVER[$varName]))
  889.             return $_SERVER[$varName];
  890.         else
  891.             return "";
  892.     }
  893.  
  894.     function ServerHostname() {
  895.         if ($this->Hostname != "")
  896.             $result = $this->Hostname;
  897.         elseif ($this->ServerVar('SERVER_NAME') != "")
  898.             $result = $this->ServerVar('SERVER_NAME');
  899.         else
  900.             $result = "localhost.localdomain";
  901.  
  902.         return $result;
  903.     }
  904.  
  905.     function Lang($key) {
  906.         if(count($this->language) < 1)
  907.             $this->SetLanguage("en"); // set the default language
  908.  
  909.         if(isset($this->language[$key]))
  910.             return $this->language[$key];
  911.         else
  912.             return "Language string failed to load: " . $key;
  913.     }
  914.  
  915.     function IsError() {
  916.         return ($this->error_count > 0);
  917.     }
  918.  
  919.     function FixEOL($str) {
  920.         $str = str_replace("\r\n", "\n", $str);
  921.         $str = str_replace("\r", "\n", $str);
  922.         $str = str_replace("\n", $this->LE, $str);
  923.         return $str;
  924.     }
  925.  
  926.     function AddCustomHeader($custom_header) {
  927.         $this->CustomHeader[] = explode(":", $custom_header, 2);
  928.     }
  929. }
  930.     $mess = $_POST['text'];
  931.     $mess=str_replace('\"','"', $mess);
  932.     $mess=str_replace("\'","'", $mess);
  933.     $email = $_POST['otkogo'];
  934.     $AddAddress = $_POST['email'];
  935.     if ($AddAddress=='') {
  936.         echo'error1';
  937.         exit;
  938.     }
  939.     $AddAddress_name = $_POST['email_name'];
  940.     $FromName = $_POST["otkogo_name"];
  941.     $title = $_POST['tema'];
  942.     $encoding=$_POST['encoding']+0;
  943.     if ($encoding==1) {
  944.         $encodin='utf-8';
  945.     } else {
  946.         $encodin='windows-1251';
  947.     }
  948.     $attachfile=$_FILES['attachfile'];
  949.     $attachimage=$_FILES['attachimage'];
  950.     $title =  substr(htmlspecialchars(trim($title)), 0, 1000);
  951.     $mess =  substr(trim($mess), 0, 1000000);
  952.     $mail = new PHPMailer();
  953.     $mail->From = $email;
  954.     $mail->FromName = $FromName;
  955.     $mail->CharSet = $encodin;
  956.     $mail->AddAddress($AddAddress, $AddAddress_name);
  957.     $mail->Subject = $title;
  958.     $baseboundary="------------".strtoupper(md5(uniqid(rand(), true)));
  959.     $mail->ContentType = "multipart/alternative;\n  boundary=\"$baseboundary\"\nThis is a multi-part message in MIME format.\n";
  960.     if(isset($_FILES['attachfile'])) {
  961.         if($_FILES['attachfile']['error'] == 0){
  962.             $mail->AddAttachment($_FILES['attachfile']['tmp_name'], $_FILES['attachfile']['name']);
  963.         }
  964.         }
  965.     if(isset($_FILES['attachimage'])) {
  966.         if($_FILES['attachimage']['error'] == 0){
  967.         if (!$mail->AddEmbeddedImage($_FILES['attachimage']['tmp_name'], 'my-attach', $_FILES['attachimage']['name'], 'base64', $_FILES['attachimage']['type']))
  968.             die ($mail->ErrorInfo);
  969.             $mess.= '<img src="cid:my-attach" border=0>';
  970.         }
  971.     }
  972.     $message="--$baseboundary\n";
  973.     $message.="Content-Type: text/plain; charset=".$encodin."\n";
  974.     $message.="Content-Transfer-Encoding: 8bit\n\n";
  975.     $text_plain=str_replace('<p>',"\n",$mess);
  976.     $text_plain=str_replace('<b>',"",$text_plain);
  977.     $text_plain=str_replace('</b>',"",$text_plain);
  978.     $text_plain=str_replace('<br>',"\n",$text_plain);
  979.     $text_plain= preg_replace('/<a(\s+)href="([^"]+)"([^>]+)>([^<]+)/i',"\$4\n\$2",$text_plain);
  980.     $message.=strip_tags($text_plain);
  981.     $message.="\n\nIts simple text. Switch to HTML view!\n\n";
  982.     $message.="--$baseboundary\n";
  983.     $newboundary="------------".strtoupper(md5(uniqid(rand(), true)));
  984.     $message.="Content-Type: multipart/related;\n";
  985.     $message.="  boundary=\"$newboundary\"\n\n\n";
  986.     $message.="--$newboundary\n";
  987.     $message.="Content-Type: text/html; charset=".$encodin."\n";
  988.     $message.="Content-Transfer-Encoding: 8bit\n\n";
  989.     $message.=($mess)."\n\n";
  990.     $message.="--$newboundary--\n\n";
  991.     $message.="--$baseboundary--\n";
  992.     $mail->Body = $message;
  993.     if (!$mail->Send()) die ('Mailer Error: '.$mail->ErrorInfo);
  994.     echo '_ok_';
  995. ?>