home *** CD-ROM | disk | FTP | other *** search
/ grafika-wolowiec.cba.pl / grafika-wolowiec.cba.pl.tar / grafika-wolowiec.cba.pl / res / imemail.inc.php < prev    next >
PHP Script  |  2014-01-24  |  6KB  |  184 lines

  1. <?php
  2.       //Incomedia WebSite X5 EMail Class. All rights reserved.
  3.   
  4.     class imEMail {
  5.         var $from;
  6.         var $to;
  7.         var $subject;
  8.         var $charset;
  9.         var $text;
  10.         var $html;
  11.         var $type;
  12.         var $newline = "\r\n";
  13.         var $exposeWsx5 = true;
  14.         
  15.         var $attachments;
  16.         
  17.         function imEMail($from,$to,$subject,$charset) {
  18.             $this->from = $from;
  19.             $this->to = $to;
  20.             $this->charset = $charset;
  21.             $this->subject = strlen($subject) ? "=?" . strtoupper($this->charset) . "?B?". base64_encode($subject) . "?=" : "";
  22.         }
  23.         
  24.         function setExpose($expose) {
  25.             $this->exposeWsx5 = $expose;
  26.         }
  27.  
  28.         /**
  29.          * Set the type of email standard (HTML, HTML-X or Text-only)
  30.          * @param [type] $type [description]
  31.          */
  32.         function setStandardType($type = "html") {
  33.             $this->type = $type;
  34.             $this->newline = (strtolower($type) == "html-x" ? "\n" : "\r\n");
  35.         }
  36.         
  37.         function setFrom($from) {
  38.             $this->from = $from;
  39.         }
  40.         
  41.         function setTo($to) {
  42.             $this->to = $to;
  43.         }
  44.         
  45.         function setSubject($subject) {
  46.             if (strlen($subject))
  47.                 $this->subject = "=?" . strtoupper($this->charset) . "?B?". base64_encode($subject) . "?=";
  48.             else
  49.                 $this->subject = "";
  50.         }
  51.         
  52.         function setCharset($charset) {
  53.             $this->charset = $charset;
  54.         }
  55.         
  56.         function setText($text) {
  57.             $this->text = $text;
  58.         }
  59.         
  60.         function setHTML($html) {
  61.             $this->html = $html;
  62.         }
  63.         
  64.         function attachFile($name,$content,$mime_type) {
  65.             if (strlen($name) === 0)
  66.                 return false;
  67.             $attachment['name'] = "=?" . strtoupper($this->charset) . "?B?". base64_encode($name) . "?=";
  68.             $attachment['content'] = base64_encode($content);
  69.             $attachment['mime_type'] = $mime_type;
  70.             $this->attachments[] = $attachment;
  71.         }
  72.         
  73.         function send() {
  74.             $headers = "";
  75.             $msg = "";
  76.  
  77.             if($this->from == "" || $this->to == "" || ($this->text == "" && $this->html == ""))
  78.                 return false;
  79.             
  80.             if ($this->type != "text") {
  81.  
  82.                 /*
  83.                 |-------------------------------
  84.                 | HTML/HTML-X email
  85.                 |-------------------------------
  86.                  */
  87.                 
  88.                 $boundary_file = md5(time() . "_attachment");
  89.                 $boundary_alt = md5(time() . "_alternative");            
  90.  
  91.                 $headers .= "From: " . $this->from . $this->newline;
  92.                 $headers .= "Message-ID: <" . time() . rand(0,9) . rand(0,9) . "@" . ($this->exposeWsx5 ? "websitex5" : rand(100,200)) . ".users>" . $this->newline;
  93.                 $headers .= "X-Mailer: " . ($this->exposeWsx5 ? "WebSiteX5 Mailer" : "PHP") . $this->newline;
  94.                 
  95.                 $headers .= "MIME-Version: 1.0" . $this->newline;
  96.  
  97.                 if(is_array($this->attachments)) {
  98.                     $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary_file . "\"" . $this->newline . $this->newline;
  99.                     $headers .= "--" . $boundary_file . $this->newline;
  100.                 }
  101.                 
  102.                 if($this->html == "") {
  103.                     $headers .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . $this->newline;
  104.                     if (strtolower($this->charset) != "utf-8")
  105.                         $headers .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  106.                     else
  107.                           $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  108.                     $msg .= $this->text . $this->newline . $this->newline;
  109.                 }
  110.                 else if($this->text == "") {
  111.                     $headers .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . $this->newline;
  112.                     if (strtolower($this->charset) != "utf-8")
  113.                         $headers .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  114.                     else
  115.                           $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  116.                     $msg .= $this->html . $this->newline . $this->newline;
  117.                 }
  118.                 else {
  119.                     $headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary_alt . "\"" . $this->newline;
  120.                     
  121.                     $msg .= "--" .$boundary_alt . $this->newline;
  122.                     $msg .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . $this->newline;
  123.                     if (strtolower($this->charset) != "utf-8")
  124.                         $msg .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  125.                     else
  126.                           $msg .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  127.                     $msg .= $this->newline;
  128.                     $msg .= $this->text . $this->newline . $this->newline;
  129.                     
  130.                     $msg .= "--" . $boundary_alt . $this->newline;
  131.                       $msg .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . $this->newline;
  132.                       if (strtolower($this->charset) != "utf-8")
  133.                         $msg .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  134.                     else
  135.                           $msg .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  136.                     $msg .= $this->newline;
  137.                     $msg .= $this->html . $this->newline . $this->newline;
  138.                     
  139.                     $msg .= "--" . $boundary_alt . "--" . $this->newline . $this->newline;
  140.                 }
  141.                 
  142.                 if(is_array($this->attachments)) {
  143.                     foreach($this->attachments as $attachment) {
  144.                         $msg .= "--" . $boundary_file . $this->newline;
  145.                         $msg .= "Content-Type: " . $attachment["mime_type"] . "; name=\"" . $attachment["name"] . "\"" . $this->newline;
  146.                         $msg .= "Content-Transfer-Encoding: base64" . $this->newline;
  147.                         $msg .= "Content-Disposition: attachment; filename=\"" . $attachment["name"] . "\"" . $this->newline . $this->newline;
  148.                         $msg .= chunk_split($attachment["content"]) . $this->newline . $this->newline;
  149.                     }
  150.                     $msg .= "--" . $boundary_file . "--" . $this->newline . $this->newline;
  151.                 }
  152.                 
  153.                 if (function_exists('ini_set'))
  154.                     @ini_set("sendmail_from", $this->from);
  155.                 
  156.                 // First attempt: -f flag, no more headers
  157.                 if(@mail($this->to, $this->subject, $msg, $headers, "-f" . $this->from))
  158.                     return true;
  159.                 // Second attempt: no -f flag, no more headers
  160.                 if (@mail($this->to, $this->subject, $msg, $headers))
  161.                     return true;
  162.                 // Third attempt: no -f flag, one more To header
  163.                 $headers = "To: " . $this->to . $this->newline . $headers;
  164.                 return @mail($this->to, $this->subject, $msg, $headers);
  165.             } else {
  166.  
  167.                 /*
  168.                 |-------------------------------
  169.                 | Text-only email
  170.                 |-------------------------------
  171.                  */
  172.  
  173.                 $headers .= "From: " . $this->from . $this->newline;
  174.                 $headers .= "Content-Type: text/plain;charset=" . $this->charset . $this->newline;
  175.                 $msg .= $this->text . $this->newline . $this->newline;
  176.  
  177.                 $r = @mail($this->to, $this->subject, $msg, $headers);
  178.                 return $r;
  179.             }
  180.         }
  181.     }
  182.  
  183. // End of file imemail.inc.php
  184.