1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| <?php
// classe qui va permettre d'envoyer des mails
class Mailer {
public $nameFile = "../error.log";
// les différents éléments constituant un mail
// from, fromName, to, ReplyTo,cc, bcc, subject, body, attachment
private $from = "****";
// adresse de l'expéditeur, valeur par défaut si non mentionné
private $fromName = "webmaster";
// nom de l'expéditeur, valeur par défaut
private $replyTo = "";
// adresse pour la réponse si différente de l'adresse de l'expéditeur
private $to = array();
// adresse du destinataire; tableau car possibilité d'envoi à plusieurs personnes
private $cc = array();
// carbon Copy
private $bcc = array();
// copie cachée
private $priority = 3;
// valeur entre 1 à 5
private $subject = "";
// sujet du message
private $body = "";
// body
private $attachments = array();
// ensemble de fichiers à envoyer en attachment ...
private $header = "";
// syntaxe pour gérer l'entête du mail ...
private $format = "";
// permettra de récupérer le format du mail
private $charset = "iso-8859-1"; // autre possibilité utf-8
private $frontiere = "";
// à changer si nécessaire
private $messageOk = "message envoyé";
private $messageNo = "message non envoyé";
// constructeur pour récupérer les données afin d'envoyer un mail minimaliste
// l'argument format soit text (body en format text), soit html (body en html), soit mixed (si attachment)
public function __construct($to,$subject,$body,$format=""){
$this->to = $to;
$this->subject = $subject;
$this->format = $format;
$this->body = $body;
}
// argument String
public function setFormat($format){
$this->format = $format;
}
// argument String
public function setReplyTo($replyTo){
$this->replyTo = $replyTo;
}
// argument String
public function setCharset($charset){
$this->charset = $charset;
}
// argument Integer (1 - 3 - 5)
public function setPriority($priority){
$this->priority = $priority;
}
// argument array
public function setCc($cc){
$this->cc = $cc;
}
// argument array
public function setBcc($bcc){
$this->bcc = $bcc;
}
// argument array
public function setAttachments($attachments){
$this->attachments = $attachments;
}
private function buildHeader(){
// chaque ligne de l'entête doit être terminée par un \r\n mis entre guillemets et non des simple-quotes
$this->header = "MIME-Version: 1.0" . "\r\n";
//$this->header .= "Date: " . date("D, j M Y H:i:s") . "\r\n";
$this->header .= "Date: " . date('r') . "\r\n";
$this->header .= "X-Mailer: PHP/" . phpversion() . "\r\n" ;
$this->header .= "FromName: ". $this->fromName . "\r\n";
$this->header .= "From: " . $this->from . "\r\n";
$this->header .= "X-Priority: " . $this->priority . "\r\n";
if ( strlen($this->replyTo) > 0 )
$this->header .= "Reply-to: ". $this->replyTo. "\r\n";
if ( count($this->cc) > 0 )
$this->header .= "Cc: " . $this->arrayToString($this->cc) . "\r\n";
if ( count($this->bcc) > 0 )
$this->header .= "Bcc: " . $this->arrayToString($this->bcc) . "\r\n";
}
private function gererContentType(){
if ( $this->format == "html" ){
$this->header .= "Content-Type: text/html; charset='" . $this->charset . "'\r\n";
$this->header .= "\r\n";
}
if ( $this->format == "text" ){
$this->header .= "Content-Type: text/plain; charset='" . $this->charset . "'\r\n";
$this->header .= "\r\n";
}
if ( $this->format == "mixed" ){
$boundary = md5(uniqid(rand()));
$this->frontiere = $boundary;
$this->header .= "Content-Type: multipart/mixed; boundary='". $this->frontiere ."'\r\n";
$this->header .= "\r\n";
}
// 8 bits pour les caractères accentués ou 7 bits pour les anglo-saxons
}
private function arrayToString($tab){
$chaine = "";
for ( $i = 0; $i < count($tab); $i++ ){
$chaine .= $tab[$i];
if ( $i < count($tab)-1 )
$chaine .= ","; // les adresses sont séparées par une virgule
}
return $chaine;
}
private function gererBody(){
//-----------------------------------------------
//MESSAGE TEXTE
//-----------------------------------------------
$message = "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--". $this->frontiere ."\r\n";
$message .= "Content-Type: text/plain; charset='" . $this->charset."'\r\n";
$message .= "Content-Transfer-Encoding: 8bit \r\n";
$message .= "\r\n";
$message .= strip_tags($this->body)."\r\n\r\n";
//-----------------------------------------------
//MESSAGE HTML
//-----------------------------------------------
$message .= "--" . $this->frontiere ."\r\n";
$message .= "Content-Type: text/html; charset='" . $this->charset."'\r\n";
$message .= "Content-Transfer-Encoding: 8bit \r\n";
$message .= "\r\n";
$message .= $this->body."\r\n";
$message .= "\r\n";
if (count($this->attachments) > 0){
$message .= "--" . $this->frontiere ."\r\n";
//-----------------------------------------------
//PIECE JOINTE
//-----------------------------------------------
// gérer les différents formats ...
$message .= "Content-Type: image/jpeg; name=" . $this->attachments[0] ."\r\n";
$message .= "Content-Transfer-Encoding: base64 \r\n";
$message .= "Content-Disposition:attachment; filename=" . $this->attachments[0] . "\r\n";
$message .= "\r\n";
$message .= chunk_split(base64_encode(file_get_contents($this->attachments[0])));
$message .= "--" . $this->frontiere . "\r\n";
}
return $message;
}
public function send() {
// récupérer tous les adresses destinataire
if ( count($this->to) == 0 ){
throw new MyException("pas d'adresses destinataire pour envoyer le mail");
}
else if ( strlen($this->format) == 0 ) {
throw new MyException("pas de format spécifié (text ou html ou mixed) pour envoyer le mail");
}
else {
$recipient = $this->arrayToString($this->to);
}
$this->buildHeader();// construire l'entête du mail ...
$this->gererContentType(); // spécifier le content-type
$mess = "";
switch($this->format){
case "html":
$mess = $this->body;
break;
case "text":
$mess = strip_tags($this->body);
break;
case "mixed":
$mess = $this->gererBody();
break;
default:
throw new MyException("format non spécifié ou pas compatible pour envoyer le mail");
}
$success = mail($recipient,$this->subject,$mess,$this->header);
if ( $success )
return $this->messageOk;
else
return $this->messageNo;
}
}
?> |
Partager