destinataire = array(); $this->sujet = ''; $this->message = ''; $this->boundary = "_" . md5(uniqid(rand())); $this->attachement = array(); $this->attachementhtml = ''; $this->expediteur = ''; $this->copieconforme = array(); $this->copiecachee = array(); $this->priority = 3; $this->sendmail_path = ''; $this->errolog = ''; } function __destruct() { } protected function BodyLineWrap($Value) { return wordwrap($Value, 78, "\n "); } protected function constructheader($type) { $entete = "Date: ". date('r') ."\n"; $entete .= "MIME-Version: 1.0\n"; $entete .= "X-Sender: $this->expediteur\n"; $entete .= "X-Mailer: PHP\n"; $entete .= "X-auth-smtp-user: webmaster@votresite.com \n"; $entete .= "X-abuse-contact: abuse@votresite.com \n"; $entete .= "X-Priority: $this->priority \n"; $entete .= "Disposition-Notification-To: $this->expediteur\n"; $entete .= "X-Confirm-Reading-To: $this->expediteur\n"; $entete .= "Return-receipt-to: $this->expediteur\n"; $entete .= "Errors-To: $this->expediteur\n"; $entete .= "From: $this->expediteur\n"; $entete .= "Reply-to: $this->expediteur\n"; $entete .= "Return-Path: $this->expediteur\n"; if(!empty($this->copieconforme)) $entete .= "Cc: " . implode(',', $this->copieconforme) . "\n"; if(!empty($this->copiecachee)) $entete .= "Bcc: " . implode(',', $this->copiecachee) . "\n"; if($type == 'texte' && empty($this->attachement)) $entete .= "Content-Type: text/plain;\nContent-Transfer-Encoding: 8bit"; elseif($type == 'html' && empty($this->attachement) && empty($this->attachementhtml)) { $entete .= "Content-Type: text/html; charset=\"iso-8859-1\"; format=flowed\n"; $entete .= "Content-Transfer-Encoding: quoted-printable"; } else { if(empty($this->attachementhtml)) $entete .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n"; else $entete .= "Content-Type: multipart/related;\n boundary=\"$this->boundary\"\n"; } return $entete; } protected function constructbody($type) { if(!empty($this->message)) { $body = ''; $message = $this->message; if($type == 'texte' || $type == 'tout') { if(empty($this->attachement) && $type == 'texte') $body .= strip_tags($message); else { $body .= "This is a multi-part message in MIME format.\n"; $body .= "--$this->boundary\n"; $body .= "Content-Type: text/plain;\nContent-Transfer-Encoding: 8bit\n\n"; $body .= $this->BodyLineWrap(strip_tags($message)); $body .= "\n--$this->boundary\n"; } } if($type == 'html' || $type == 'tout') { if(empty($this->attachement) && empty($this->attachementhtml) && $type == 'html') { $message = nl2br(html_entity_decode(stripslashes($message))); $body .= $message; } else { $body .= "This is a multi-part message in MIME format.\n"; $body .= "--$this->boundary\n"; $body .= "Content-Type: text/html; charset=\"iso-8859-1\"; format=flowed\n"; $body .= "Content-Transfer-Encoding: 8bit\n\n"; $message = nl2br(html_entity_decode(stripslashes($message))); $body .= $message; $body .= "\n--$this->boundary\n"; if(!empty($this->attachementhtml)) $body .= $this->attachementhtml; } } foreach($this->attachement as $attach) { $body .= "Content-Type: application/octetstream;\n name=\"".$attach."\"\n"; $body .= "Content-Transfer-Encoding: base64\n"; $body .= "Content-Disposition: attachment;\n filename=\"".$attach."\"\n\n"; $body .= chunk_split(base64_encode(file_get_contents($attach)))."\n\n"; $body .= "\n--$this->boundary\n"; } return $body; } } protected function htmlattachement() { $erreur = ''; preg_match_all('`\<[\w]+ src="([\w.]+)" [\`im', $this->message, $out); $file[0] = array(); foreach($out[1] as $name) { if(file_exists($name)) { if(!in_array($name,$file[0])) { $ID = md5( uniqid ( rand() ) ).$_SERVER['SERVER_NAME']; $file[0][$name] = $ID; $file[1][$name] = '`(\<[\w]+ )src="' . $name . '"( [\)`'; $file[2][$name] = '$1src="cid:'.$ID.'"$2'; } } else { $erreur .= 'Erreur, fichier '. $name .' pour l\'attachement inexistant
'; } } $this->message = preg_replace($file[1],$file[2], $this->message); foreach($file[0] as $name => $ID) { $this->attachementhtml .= "Content-Type: ".mime_content_type($name)."; name=\"$name\"\n"; $this->attachementhtml .= "Content-Transfer-Encoding: base64\n"; $this->attachementhtml .= "Content-ID: <$ID>\n\n"; $this->attachementhtml .= chunk_split(base64_encode(file_get_contents($name)))."\n\n"; $this->attachementhtml .= "\n--$this->boundary\n"; } if(empty($erreur)) return TRUE; else { $this->errolog .= $erreur; return FALSE; } } /************ Methodes utilisateur ************/ function addDestinataire() { $erreur = ''; if(func_num_args() == 0) $erreur .= 'Erreur, vous devez specifiez un destinataire
'; else { $arg = func_get_args(); foreach($arg as $dest) { if(!in_array($dest, $this->destinataire) && !in_array($dest, $this->copieconforme) && !in_array($dest, $this->copiecachee)) { if($res = $this->verifMail($dest, TRUE)) { array_push($this->destinataire, $res); } else $erreur .= 'Erreur, destinataire "'. $dest .'" incorrect
'; } else $erreur .= 'Erreur, destinataire "'. $dest .'" deja presente
'; } } if(empty($erreur)) return TRUE; else { $this->errolog .= $erreur; return FALSE; } } function defSujet($subject) { if(!empty($subject) && is_string($subject)) { $this->sujet = nl2br(html_entity_decode(stripslashes($subject))); return TRUE; } else $this->errolog .= 'Erreur, sujet '. $subject .' incorrect
'; } function addAttach() { $erreur = ''; if(func_num_args() == 0) $erreur .= 'Erreur, vous devez specifiez un fichier
'; else { $arg = func_get_args(); foreach($arg as $attch) { if(!in_array($dest, $this->attachement) && is_string($attch) && file_exists($attch)) array_push($this->attachement, $attch); else $erreur .= 'Erreur, fichier '. $attch .' pour l\'attachement incorrect ou inexistant
'; } } if(empty($erreur)) return TRUE; else { $this->errolog .= $erreur; return FALSE; } } function addCc() { $erreur = ''; if(func_num_args() == 0) $erreur .= 'Erreur, vous devez specifiez une adresse pour la copie conforme
'; else { $arg = func_get_args(); foreach($arg as $cc) { if(!in_array($dest, $this->destinataire) && !in_array($dest, $this->copieconforme) && !in_array($dest, $this->copiecachee)) { if($res = $this->verifMail($cc, TRUE)) { array_push($this->copieconforme, $res); } else $erreur .= 'Erreur, adresse "'. $cc .'" pour la copie conforme incorrect
'; } else $erreur .= 'Erreur, adresse "'. $cc .'" pour la copie conforme deja presente
'; } } if(empty($erreur)) return TRUE; else { $this->errolog .= $erreur; return FALSE; } } function addBcc() { $erreur = ''; if(func_num_args() == 0) $erreur .= 'Erreur, vous devez specifiez une adresse pour la copie cachee
'; else { $arg = func_get_args(); foreach($arg as $bcc) { if(!in_array($dest, $this->destinataire) && !in_array($dest, $this->copieconforme) && !in_array($dest, $this->copiecachee)) { if($res = $this->verifMail($bcc, TRUE)) { array_push($this->copiecachee, $res); } else $erreur .= 'Erreur, adresse "'. $bcc .'" pour la copie cachee incorrect
'; } else $erreur .= 'Erreur, adresse "'. $bcc .'" pour la copie cachee deja presente
'; } } if(empty($erreur)) return TRUE; else { $this->errolog .= $erreur; return FALSE; } } function defMessage($mess) { if(!empty($mess) && is_string($mess)) { $this->message = $mess; return TRUE; } else { $this->errolog .= 'Erreur, message '. $mess .' incorrect
'; return FALSE; } } function defExpediteur($exp) { if($exp = $this->verifMail($exp, TRUE)) { $this->expediteur = $exp; return TRUE; } else { $this->errolog .= 'Erreur, expediteur "'. $exp .'" incorrect
'; return FALSE; } } function defPriority($prio) { if(!empty($prio) && is_string($prio)) { switch($prio) { case 'urgent': $this->priority = 1; break; case 'normal': $this->priority = 3; break; case 'bas': $this->priority = 5; break; } return TRUE; } else { $this->errolog .= 'Erreur, priorite "'. $prio .'" incorrect
'; return FALSE; } } function defSendmailpath($sendpath) { if(!empty($sendpath) && is_string($sendpath)) { $this->sendmail_path = $sendpath; return TRUE; } else { $this->errolog .= 'Erreur, parametre '. $sendpath .' incorrect
'; return FALSE; } } function getInfo() { echo 'Destinataire(s): ' . htmlspecialchars(implode(',', $this->destinataire)) . '
'; echo 'Sujet: ' . $this->sujet . '
'; echo 'Message:
' . $this->message . '
'; echo 'Expediteur: ' . htmlspecialchars($this->expediteur) . '
'; if(!empty($this->copieconforme)) echo 'Copie(s) conforme(s): ' . htmlspecialchars(implode(',', $this->copieconforme)) . '
'; if(!empty($this->copiecachee)) echo 'Copie(s) cachee(s): ' . htmlspecialchars(implode(',', $this->copiecachee)) . '
'; if(!empty($this->attachement)) echo 'Piece(s) jointe(s): ' . implode(',', $this->attachement) . '
'; if(!empty($this->sendmail_path)) echo 'Commande: ' . $this->sendmail_path . '
'; } function getErrorlog() { return $this->errolog; } static function verifMail($mail, $fct = NULL) { if(preg_match('`^(\w(?:[-_. ]?\w)* )?((\w(?:[-_.]?\w)*)@\w(?:[-_.]?\w)*\.(?:[a-z]{2,4}))$`', $mail, $out)) { if($fct) { $nom = empty($out[1]) ? $out[3] : $out[1]; return ($nom.'<'.$out[2].'>'); } else return TRUE; } else return FALSE; } function envoi($type = 'texte') { $erreur = ''; if(empty($this->destinataire)) $erreur .= 'Erreur, vous devez definir au moins un destinataire!'; elseif(empty($this->expediteur)) $erreur .= 'Erreur, vous devez definir un expediteur!'; elseif(empty($this->sujet)) $erreur .= 'Erreur, vous devez definir un sujet!'; elseif(empty($this->message)) $erreur .= 'Erreur, vous devez definir un message!'; elseif(!$this->htmlattachement()) $erreur .= 'Erreur lors de l\'attachement des fichiers present dans le message html!'; else { if(empty($erreur)) { if(empty($this->sendmail_path)) if(mail(implode(',', $this->destinataire), $this->sujet, $this->constructbody($type), $this->constructheader($type))) return TRUE; else { $erreur .= 'Erreur, mail non envoye
'; $this->errolog .= $erreur; return FALSE; } else if(mail(implode(',', $this->destinataire), $this->sujet, $this->constructbody($type), $this->constructheader($type), $this->sendmail_path)) return TRUE; else { $erreur .= 'Erreur, mail non envoye
'; $this->errolog .= $erreur; return FALSE; } } else { $this->errolog .= $erreur; return FALSE; } } } } ?>