noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_sendmail.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 
00021 
00022 /**
00023  * Description of class_sendmail
00024  *
00025  * @author dany
00026  */
00027 require_once 'class_filetosend.php';
00028 
00029 class Sendmail
00030 {
00031 
00032     private $mailto;
00033     private $afile;
00034     private $subject;
00035     private $message;
00036     private $from;
00037     private $content;
00038 
00039     /**
00040      * set the from
00041      * @parameter $p_from has the form name <info@phpcompta.eu>
00042      */
00043     function set_from($p_from)
00044     {
00045         $this->from = $p_from;
00046     }
00047 
00048     /**
00049      * 
00050      * @param $p_subject set the subject
00051      */
00052     function set_subject($p_subject)
00053     {
00054         $this->subject = $p_subject;
00055     }
00056 
00057     /**
00058      * set the recipient
00059      * @param type $p_mailto has the form name <email@email.com>
00060      */
00061     function mailto($p_mailto)
00062     {
00063         $this->mailto = $p_mailto;
00064     }
00065 
00066     /**
00067      * body of the message (utf8)
00068      * @param type $p_message
00069      */
00070     function set_message($p_message)
00071     {
00072         $this->message = $p_message;
00073     }
00074 
00075     /**
00076      * Add file to the message
00077      * @param FileToSend $file file to add to the message
00078      */
00079     function add_file(FileToSend $file)
00080     {
00081         $this->afile[] = $file;
00082     }
00083 
00084     /**
00085      *  verify that the message is ready to go
00086      * @throws Exception
00087      */
00088     function verify()
00089     {
00090         $array = explode(",", "from,subject,mailto,message");
00091         for ($i = 0; $i < count($array); $i++)
00092         {
00093             $name = $array[$i];
00094             if (trim($this->$name) == "")
00095             {
00096                 throw new Exception($name ._(" est vide"));
00097             }
00098         }
00099     }
00100     /**
00101     * create the message before sending
00102     */
00103     function compose()
00104     {
00105         $this->verify();
00106         $uid = md5(uniqid(time()));
00107 
00108         // a random hash will be necessary to send mixed content
00109         $separator = md5(time());
00110 
00111         // carriage return type (we use a PHP end of line constant)
00112         $eol = PHP_EOL;
00113 
00114         // main header (multipart mandatory)
00115         $headers = "From: " . $this->from . $eol;
00116         $headers .= "MIME-Version: 1.0" . $eol;
00117         $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
00118         $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
00119         $headers .= "This is a MIME encoded message." . $eol . $eol;
00120         $headers .= $eol . $eol;
00121 
00122         // message
00123         $headers .= "--" . $separator . $eol;
00124         $headers .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol;
00125         $headers .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
00126         $headers .= $this->message . $eol . $eol;
00127 
00128         // attachment
00129         for ($i = 0; $i < count($this->afile); $i++)
00130         {
00131             $file = $this->afile[$i];
00132             $file_size = filesize($file->full_name);
00133             $handle = fopen($file->full_name, "r");
00134             $content = fread($handle, $file_size);
00135             fclose($handle);
00136             $content = chunk_split(base64_encode($content));
00137             $headers .= "--" . $separator . $eol;
00138             $headers .= "Content-Type: " . $file->type . "; name=\"" . $file->filename . "\"" . $eol;
00139             $headers .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol;
00140             $headers .= "Content-Transfer-Encoding: base64" . $eol;
00141             $headers.=$eol;
00142             $headers .= $content . $eol . $eol;
00143         }
00144         $headers .= "--" . $separator . "--";
00145         $this->content = $headers;
00146     }
00147     /**
00148      * Send the message 
00149      * @throws Exception
00150      */
00151     function send()
00152     {
00153         //SEND Mail
00154         if (!mail($this->mailto, $this->subject, "", $this->content))
00155         {
00156             throw new Exception('send failed');
00157         }
00158     }
00159 
00160 }
 All Data Structures Namespaces Files Functions Variables Enumerations