Source for file mail.class.php

Documentation is available at mail.class.php

  1. <?php
  2. /**
  3.  * TPLN mail Plugin
  4.  * @package Template Engine
  5.  */
  6. class Mail extends Rss
  7. {
  8.     protected $from = '';
  9.     protected $fromLabel = '';
  10.     protected $replyTo = '';
  11.     protected $to = '';
  12.     protected $cc = '';
  13.     protected $bcc = '';
  14.     protected $subject = '';
  15.     protected $body = '';
  16.     protected $format = '';
  17.     protected $files = array();
  18.     protected $charset = 'iso-8859-1';
  19.     protected $urgent = false;
  20.     protected $confirm  = false;
  21.     protected $mailErr = '';
  22.  
  23.     /**
  24.      * this method verifies if the email adress is correct
  25.      *
  26.      * @param string $address 
  27.      *
  28.      * @return boolean 
  29.      * @author H2LSOFT */
  30.     public function isMail($address)
  31.     {
  32.         if (preg_match('`([[:alnum:]]([-_.]?[[:alnum:]])*@[[:alnum:]]([-_.]?[[:alnum:]])*\.([a-z]{2,4}))`'$address))
  33.             return true;
  34.         else
  35.             return false;
  36.     }
  37.  
  38.     /**
  39.      * This method allows to define a mail with a urgent priority.
  40.      *
  41.      * @param boolean $bool 
  42.      * @author H2LSOFT */
  43.     public function mailUrgent($bool)
  44.     {
  45.         $this->urgent = $bool;
  46.     }
  47.  
  48.     /**
  49.      * This method allows to change encode by default ISO-8859-15 or UTF8 of the mail.
  50.      *
  51.      * @param string $str 
  52.      * @author H2LSOFT */
  53.     public function mailCharset($str)
  54.     {
  55.         $this->charset = $str;
  56.     }
  57.  
  58.     /**
  59.      * This method allows to add a confirmation of answer to the email.
  60.      *
  61.      * @param boolean $bool 
  62.      *
  63.      * @since 2.7
  64.      * @author H2LSOFT */
  65.     public function mailConfirm($bool)
  66.     {
  67.         $this->confirm = $bool;
  68.     }
  69.  
  70.     /**
  71.      * This method allows to define the sender of the mail.
  72.      *
  73.      * @param string $str 
  74.      * @param string $label 
  75.      * @author H2LSOFT */
  76.     public function mailFrom($str$label='')
  77.     {
  78.         $this->from = $str;
  79.         $this->fromLabel = $label;
  80.     }
  81.  
  82.     /**
  83.      *This method allows to define a email address for response.
  84.      *
  85.      * @param string $str 
  86.      * @author H2LSOFT */
  87.     public function mailReplyTo($str)
  88.     {
  89.         $this->replyTo = $str;
  90.     }
  91.  
  92.     /**
  93.      * This method allows to define one of more email address of recipient.
  94.      *
  95.      * @param string $str 
  96.      * @author H2LSOFT */
  97.     public function mailTo($str)
  98.     {
  99.         $this->to = $str;
  100.     }
  101.  
  102.     /**
  103.      * This method allows to send a conform copy of the mail.
  104.      *
  105.      * @param string $str 
  106.      * @author H2LSOFT */
  107.     public function mailCC($str)
  108.     {
  109.         $this->cc = $str;
  110.     }
  111.  
  112.     /**
  113.      * This method allows to send a conform copy of the mail.
  114.      *
  115.      * @param string $str 
  116.      * @author H2LSOFT */
  117.     public function mailBCC($str)
  118.     {
  119.         $this->bcc = $str;
  120.     }
  121.  
  122.     /**
  123.      * This methode allows to define the object of the email.
  124.      *
  125.      * @param string $str 
  126.      * @author H2LSOFT */
  127.     public function mailSubject($str)
  128.     {
  129.         $this->subject = $str;
  130.     }
  131.  
  132.     /**
  133.      * This method allows to define the body of the message.
  134.      *
  135.      * @param string $str 
  136.      * @param string $format HTML or TXT
  137.      * @author H2LSOFT */
  138.     public function mailBody($str$format)
  139.     {
  140.         $this->body = $str;
  141.         $this->format = $format;
  142.     }
  143.  
  144.     /**
  145.      * This method allows to attach files to the email.
  146.      *
  147.      * if $type parameter is not filled, it will affect a type regarding file extension
  148.      *
  149.      * @param string $src 
  150.      * @param string $name 
  151.      * @param string $type 
  152.      * @author H2LSOFT */
  153.     public function mailAttachFile($src$name$type='')
  154.     {
  155.         // type of file is not filled
  156.         if(empty($type))
  157.         {
  158.             // try to recognize the extention
  159.             switch(strrchr(basename($name)"."))
  160.             {
  161.                 case ".gz"$type "application/x-gzip";
  162.                     break;
  163.                 case ".tgz"$type "application/x-gzip";
  164.                     break;
  165.                 case ".zip"$type "application/zip";
  166.                     break;
  167.                 case ".pdf"$type "application/pdf";
  168.                     break;
  169.                 case ".png"$type "image/png";
  170.                     break;
  171.                 case ".gif"$type "image/gif";
  172.                     break;
  173.                 case ".jpg"$type "image/jpeg";
  174.                     break;
  175.                 case ".txt"$type "text/plain";
  176.                     break;
  177.                 case ".htm"$type "text/html";
  178.                     break;
  179.                 case ".html"$type "text/html";
  180.                     break;
  181.                 default$type "application/octet-stream";
  182.                     break;
  183.             }
  184.         }
  185.         $this->files[array(
  186.             'src' => $src,
  187.             'name' => $name,
  188.             'type' => $type
  189.         );
  190.     }
  191.  
  192.     /**
  193.      * This method allows to return the last error message
  194.      *
  195.      * @return string 
  196.      * @author H2LSOFT */
  197.     public function mailError()
  198.     {
  199.         return 'Error: '.$this->mailErr;
  200.     }
  201.  
  202.     /**
  203.      * This method allows to send an email.
  204.      *
  205.      * @return boolean 
  206.      * @author H2LSOFT */
  207.     public function mailSend()
  208.     {
  209.         // obligatory FROM
  210.         if(!$this->isMail($this->from))
  211.         {
  212.             $this->mailErr = "$this->from is not a valid email address";
  213.             return false;
  214.         }
  215.  
  216.         // obligatory TO
  217.         $arr explode(','$this->to);
  218.         foreach($arr as $to)
  219.         {
  220.             $to trim($to);
  221.             if(!$this->isMail($to))
  222.             {
  223.                 $this->mailErr = "$to is not a valid email address in To";
  224.                 return false;
  225.             }
  226.         }
  227.  
  228.         // check REPLYTO
  229.         if(!empty($this->replyTo))
  230.         {
  231.             if(!$this->isMail($this->replyTo))
  232.             {
  233.                 $this->mailErr = "$this->replyTo is not a valid email address";
  234.                 return false;
  235.             }
  236.         }
  237.  
  238.  
  239.  
  240.         // headers
  241.         $headers '';
  242.  
  243.         if(!empty($this->fromLabel))$this->from = "$this->fromLabel <$this->from>";
  244.         $headers .= "From: $this->from"."\n";
  245.         if(count($this->files0)$headers .= "MIME-Version: 1.0\n";
  246.  
  247.         //$headers .= "To: $this->to"."\n";
  248.         $headers .= "Return-Path: $this->from"."\n";
  249.         if(!empty($this->replyTo))$headers .= "Reply-To: $this->replyTo"."\n";
  250.         if(!empty($this->cc))$headers .= "Cc: $this->cc"."\n";
  251.         if(!empty($this->bcc))$headers .= "Bcc: $this->bcc"."\n";
  252.         if(!empty($this->confirm))$headers .= "Disposition-Notification-To: $this->from"."\n";
  253.         $headers .= 'X-Mailer: PHP/'.phpversion()."\n";
  254.  
  255.         if($this->urgent)$headers .= "X-Priority: 1\n";
  256.  
  257.         // there are any files ?
  258.         if(count($this->files0)
  259.         {
  260.             // add the message
  261.             if($this->format == 'HTML')
  262.                 $this->mailAttachFile($this->body'''text/html');
  263.             else
  264.                 $this->mailAttachFile($this->body'''text/plain');
  265.  
  266.             $boundary "b".md5(uniqid(time()));
  267.             $headers .= "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
  268.  
  269.             // construct the mime with towards
  270.             for($i=count($this->files)-1$i >=  0$i--)
  271.             {
  272.                 // construct the message
  273.                 $headers .= "\n";
  274.                 $message $this->files[$i]['src'];
  275.                 $message chunk_split(base64_encode($message));
  276.                 $headers .= "Content-Type: ".$this->files[$i]['type'];
  277.                 if(!empty($this->files[$i]['name']))
  278.                     $headers .= "; name = \"".$this->files[$i]['name']"\"";
  279.                 $headers .= "\nContent-Transfer-Encoding: base64\n\n";
  280.                 if(!empty($this->files[$i]['name']))
  281.                     $headers .= "Content-Disposition: attachment;\n\n";
  282.                 $headers .= "$message\n";
  283.                 $headers .= "--$boundary";
  284.             }
  285.             $headers .= "--";
  286.  
  287.  
  288.             // send
  289.             if(!@mail($this->to$this->subject''$headers))
  290.             {
  291.                 $this->mailErr = "Email has not been sent";
  292.                 return false;
  293.             }
  294.         }
  295.         else
  296.         {
  297.             // send with HTML ?
  298.             $headers .= "MIME-Version: 1.0\n";
  299.             if($this->format == 'HTML')
  300.                 $headers .= "content-type: text/html; charset=$this->charset\n";
  301.  
  302.             if(!@mail($this->to$this->subject$this->body$headers))
  303.             {
  304.                 $this->mailErr = "Email has not been sent";
  305.                 return false;
  306.             }
  307.         }
  308.  
  309.         return true;
  310.         
  311.     }
  312. }
  313.  
  314. ?>

Documentation generated on Sat, 06 Mar 2010 21:33:59 +0100 by phpDocumentor 1.4.3