Php > mail

Titremail
Postée le27-05-2011
Affichée279
Mini-lien
Description

mail

EtatNe contient pas d'erreurs. Ne contient pas d'erreurs.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format php
Plein ecran
<?php
/************ Class Mail V2.5 by Benjy ************/

class mail {
        protected $destinataire;
        protected $sujet;
        protected $message;
        protected $attachement;
        protected $attachementhtml;
        protected $expediteur;
        protected $copieconforme;
        protected $copiecachee;
        protected $boundary;
        protected $priority;
        protected $sendmail_path;
        protected $errolog;

/************ Methodes propre a la class Mail ************/

        function __construct() {
                $this->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";
                        if($type == 'moi' && empty($this->attachement))
                                $entete .= "Content-Type: text/html; charset=\"iso-8859-1\"; ";
                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 == 'moi' || $type == 'tout') {
                                if(empty($this->attachement) && $type == 'moi')
                                        $body .= $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 == '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.]+)" [\</\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 . '"( [\</\w]+\>)`';
                                        $file[2][$name] = '$1src="cid:'.$ID.'"$2';
                                }
                        }
                        else {
                                $erreur .= 'Erreur, fichier '. $name .' pour l\'attachement inexistant<br />';
                        }
                }
                //$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<br />';
                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<br />';
                                }
                                else
                                        $erreur .= 'Erreur, destinataire "'. $dest .'" deja presente<br />';
                        }
                }
                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<br />';
        }

        function addAttach() {
                $erreur = '';
                if(func_num_args() == 0)
                        $erreur .= 'Erreur, vous devez specifiez un fichier<br />';
                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<br />';
                        }
                }
                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<br />';
                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<br />';
                                }
                                else
                                        $erreur .=  'Erreur, adresse "'. $cc .'" pour la copie conforme deja presente<br />';
                        }
                }
                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<br />';
                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<br />';
                                }
                                else
                                        $erreur .= 'Erreur, adresse "'. $bcc .'" pour la copie cachee deja presente<br />';
                        }
                }
                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<br />';
                        return FALSE;
                }
        }

        function defExpediteur($exp) {
                if($exp = $this->verifMail($exp, TRUE)) {
                        $this->expediteur = $exp;
                        return TRUE;
                }
                else {
                        $this->errolog .= 'Erreur, expediteur "'. $exp .'" incorrect<br />';
                        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<br />';
                        return FALSE;
                }
        }

        function defSendmailpath($sendpath) {
                if(!empty($sendpath) && is_string($sendpath)) {
                        $this->sendmail_path = $sendpath;
                        return TRUE;
                }
                else {
                        $this->errolog .= 'Erreur, parametre '. $sendpath .' incorrect<br />';
                        return FALSE;
                }
        }

        function getInfo() {
                echo '<u>Destinataire(s):</u> ' . htmlspecialchars(implode(',', $this->destinataire)) . '<br />';
                echo '<u>Sujet:</u> ' . $this->sujet . '<br />';
                echo '<u>Message:</u><br />' . $this->message . '<br />';
                echo '<u>Expediteur:</u> ' . htmlspecialchars($this->expediteur) . '<br />';
                if(!empty($this->copieconforme))
                        echo '<u>Copie(s) conforme(s):</u> ' . htmlspecialchars(implode(',', $this->copieconforme)) . '<br />';
                if(!empty($this->copiecachee))
                        echo '<u>Copie(s) cachee(s):</u> ' . htmlspecialchars(implode(',', $this->copiecachee)) . '<br />';
                if(!empty($this->attachement))
                        echo '<u>Piece(s) jointe(s):</u> ' . implode(',', $this->attachement) . '<br />';
                if(!empty($this->sendmail_path))
                        echo '<u>Commande:</u> ' . $this->sendmail_path . '<br />';
        }

        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<br />';
                                                $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<br />';
                                                $this->errolog .= $erreur;
                                                return FALSE;
                                        }
                        }
                        else {
                                $this->errolog .= $erreur;
                                return FALSE;
                        }
                }
        }

}
?>