Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00041
00042
00043 function set_from($p_from)
00044 {
00045 $this->from = $p_from;
00046 }
00047
00048
00049
00050
00051
00052 function set_subject($p_subject)
00053 {
00054 $this->subject = $p_subject;
00055 }
00056
00057
00058
00059
00060
00061 function mailto($p_mailto)
00062 {
00063 $this->mailto = $p_mailto;
00064 }
00065
00066
00067
00068
00069
00070 function set_message($p_message)
00071 {
00072 $this->message = $p_message;
00073 }
00074
00075
00076
00077
00078
00079 function add_file(FileToSend $file)
00080 {
00081 $this->afile[] = $file;
00082 }
00083
00084
00085
00086
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
00102
00103 function compose()
00104 {
00105 $this->verify();
00106 $uid = md5(uniqid(time()));
00107
00108
00109 $separator = md5(time());
00110
00111
00112 $eol = PHP_EOL;
00113
00114
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
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
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
00149
00150
00151 function send()
00152 {
00153
00154 if (!mail($this->mailto, $this->subject, "", $this->content))
00155 {
00156 throw new Exception('send failed');
00157 }
00158 }
00159
00160 }