Source for file email.classe.php

Documentation is available at email.classe.php

  1. <?
  2.     /**
  3.      * email.classe.php
  4.      * 
  5.      * Envia email da forma convencional ou com html e attach de imagens.
  6.      * 
  7.      * @filesource
  8.      * @package Soomp
  9.      * @author Paulo F Nhaia <paulo.nhaia@gmail.com>
  10.      * @version 1.0
  11.      * @since 18/06/2005
  12.      * @copyright 2008 Soomp
  13.      */
  14.     /* $Id: email.classe.php,v 1.6 2007/03/16 15:00:51 mmerlone Exp $ */
  15.  
  16.     if(!defined('INDEX'&& !defined('AJAX'&& !defined('SOAP')){
  17.         die('Este script nao pode ser acessado diretamente!');
  18.     }
  19.  
  20.     /**
  21.      * Classe para envio de E-mail
  22.      */
  23.     class mime_mail{
  24.         public $parts;
  25.          public $to;
  26.         public $from;
  27.         public $headers;
  28.         public $subject;
  29.         public $body;
  30.         public $charset;
  31.     
  32.         /**
  33.          * 
  34.          * Construtor da Classe.
  35.          * <code>
  36.          *    $mail = new mime_mail();
  37.           *    $mail->from = "foo@bar.com";
  38.           *    $mail->headers = "Errors-To: foo@bar.com";
  39.           *    $mail->to = "bar@foo.com";
  40.           *    $mail->subject = "Testing...";
  41.          *    $mail->body = "This is just a test.";
  42.          *    $mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
  43.           *    $mail->send();
  44.          * </code>
  45.          * 
  46.          * @param string     $this->parts;     =  partes da mensagem, imagens CID, anexos e body
  47.           * @param string    $this->to;        =  para quem a mensagem se destina formato de email �nico
  48.           * @param string    $this->from;      =  de quem
  49.           * @param string    $this->headers;   =  todos os cabecalhos de tipo mime... setado automaticamente.
  50.           * @param string     $this->subject;   =  assunto
  51.           * @param string    $this->body;      =  conteudo da mensagem
  52.           * @param string    $this->charset    =  tipo de formata��o (Valor Default iso-8859-1)
  53.          */
  54.         function mime_mail(){
  55.               $this->parts = array();
  56.               $this->to =  "";
  57.               $this->from =  "";
  58.               $this->subject =  "";
  59.               $this->body =  "";
  60.               $this->headers =  "";
  61.               $this->charset = "iso-8859-1";
  62.           }
  63.           
  64.         /**
  65.          * Seta a propriedade Charset
  66.          * <code>
  67.          * $this->setCharset("utf-8");
  68.          * </code>
  69.          * 
  70.          * @param string    $pCharset = tipo de codificação da mensagem
  71.          */
  72.           function setCharset($pCharset{
  73.               $this->charset = $pCharset;
  74.           }
  75.     
  76.         /**
  77.          * Attach Arquivos.
  78.          * <code>
  79.          *     $mail = new mime_mail();
  80.           *    $mail->from = "foo@bar.com";
  81.           *    $mail->headers = "Errors-To: foo@bar.com";
  82.           *    $mail->to = "bar@foo.com";
  83.           *    $mail->subject = "Testing...";
  84.          *  $mail->body = "This is just a test.";
  85.          *    $mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
  86.           *    $mail->send();
  87.          * </code>
  88.          * 
  89.          * @param string     $message;     =  partes da mensagem, imagens CID, anexos e body
  90.           * @param string    $name;        =  para quem a mensagem se destina formato de email �nico
  91.           * @param string    $ctype;       =  de quem
  92.           * @return void add_attachment(string message, [string name], [string ctype])
  93.          */
  94.          function add_attachment($message$name =  ""$ctype =  "application/octet-stream"){
  95.               $this->parts[array (
  96.                                "ctype" => $ctype,
  97.                                "message" => $message,
  98.                                "encode" => isset($encode)?$encode:"",
  99.                                "name" => $name
  100.                               );
  101.         }
  102.     
  103.         /**
  104.          * 
  105.          * Build message parts of an multipart mail.
  106.          * <code>
  107.          * $this->build_message();
  108.          * </code>
  109.          * 
  110.          * @param string    $part;   =   Mensagem
  111.           * @return void build_message(array part)
  112.          */
  113.         function build_message($part){
  114.             $message $part"message"];
  115.              $encoding =  "base64";
  116.          
  117.              if (strpos($part["ctype"],"magex")!=false{
  118.                  // esta parte � usada apenas para anexar imagens que estejam no texto html
  119.                   // deve ser passado como paremetro imagex/tipo para que ele acione esta funcao.
  120.                   $part["ctype"]=str_replace("magex","mage",$part["ctype"]);
  121.                  $message chunk_split(base64_encode($message));
  122.                  $partemessage"Content-Type: ".$part"ctype"].
  123.                                     ($part"name"]"; name = \"".$part"name"]"\"" :  "").
  124.                                      "\nContent-Transfer-Encoding: $encoding.
  125.                                      "\nContent-ID: <".$part"name"].">".
  126.                                      "\n\n$message \n";
  127.          
  128.             else if ($part["ctype"]=="text/bodyhtml"){
  129.                  //pega o que for tipomime text/bodyhtml e acrecenta como conteudo do documento no formato hrml
  130.                  $partemessage"Content-Type: text/html; charset=\"".$this->charset."\" \n".
  131.                               "\n \n $message \n";
  132.              else if ($part["ctype"]=="text/plain"){
  133.                  //pega o que for tipomime text/plain e acrecenta como conteudo do documento no formato text
  134.                  $partemessage"Content-Type: text/plain; charset=\"".$this->charset."\" \n".
  135.                                 "\n\n$message\n";                          
  136.              }else{
  137.                  //adiciona qualquer arquivo na mensagem como anexo
  138.                  $message chunk_split(base64_encode($message));
  139.                  $partemessage"Content-Type: ".$part"ctype"].
  140.                                     ($part"name"]"; name = \"".$part"name"]"\"" :  "").
  141.                                     "\nContent-ID: <".$part"name"].">".
  142.                                     "\nContent-Transfer-Encoding: $encoding\n\n$message\n";         
  143.              }
  144.             return $partemessage;
  145.         }
  146.     
  147.         /**
  148.          * 
  149.          * Build a multipart mail.
  150.          * <code>
  151.          * $this->build_multipart();
  152.          * </code>
  153.          * 
  154.          * @return void build_multipart()
  155.          */
  156.         function build_multipart(){
  157.             $boundary =  "barea".md5(uniqid(time()));
  158.              $multipart =  "Content-Type: multipart/mixed; \n".
  159.                             "              boundary = $boundary .
  160.                             "\n\n" .
  161.                             "This is a multi-part message in MIME format." .
  162.                             "\n\n" .
  163.                             "--$boundary";
  164.         
  165.             for($i sizeof($this->parts)-1$i >= 0$i--){
  166.                 $multipart .=  "\n".$this->build_message($this->parts[$i])."--$boundary";
  167.              }
  168.              return $multipart.=  "--\n";
  169.         }
  170.         
  171.         /**
  172.          * 
  173.          * Send the mail (last class-function to be called).
  174.          * <code>
  175.          *     $mail = new mime_mail();
  176.           *    $mail->from = "foo@bar.com";
  177.           *    $mail->headers = "Errors-To: foo@bar.com";
  178.           *    $mail->to = "bar@foo.com";
  179.           *    $mail->subject = "Testing...";
  180.          *  $mail->body = "This is just a test.";
  181.          *    $mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
  182.           *    $mail->send();
  183.          * </code>
  184.          * 
  185.          * @return void send()
  186.          */
  187.         function send(){
  188.              $mime =  "";
  189.              if (!empty($this->from))
  190.                 $mime .=  "From: ".$this->from"\n";
  191.              if (!empty($this->headers))
  192.                 $mime .= $this->headers"\n";
  193.                 
  194.              if (!empty($this->body)) 
  195.                 $this->add_attachment($this->body,  "",  "text/bodyhtml")
  196.         
  197.              $mime .=  "MIME-Version: 1.0\n".$this->build_multipart();
  198.              return mail($this->to$this->subject,  ""$mime"-f".$this->from);
  199.         }
  200.     }
  201. ?>

Documentation generated on Sun, 09 Mar 2008 23:51:40 -0300 by phpDocumentor 1.4.0

SourceForge.net Logo Support This Project