Soyez le premier à donner votre avis sur cette source.
Vue 4 951 fois - Téléchargée 448 fois
<?php ######################################################################################## ######################################################################################## ### ### Mail Class compatible with everymail client, every support. ### This class allows you to send files (in progress) ### ### @name mail ### @author Edoaurd Kombo <edouard.kombo@live.fr> ### ######################################################################################## ######################################################################################## class intyMailer { protected $_to = ''; protected $_reply_to = ''; protected $_recipient_syntax = ''; protected $_sender_syntax = ''; protected $_from = ''; protected $_subject = 'none'; protected $_message = 'none'; protected $_is_html = false; protected $_confirmation = false; //acknowledgement // accusé de réception protected $_headers; protected $_attachement; protected $_cc = false; protected $_bcc = false; protected $_priority = false; protected $_charset = 'utf-8'; protected $_organization = 'http://www.mywebsite.com'; protected $_boundary; protected $_files = false; protected $_result = false; function __construct(){ $this->_boundary = md5(uniqid(microtime(), TRUE)); } function __destruct(){ } /////////////////////////////////////////// /////////////////////////////////////////// // BACKGROUND METHODS /////////////////////////////////////////// /////////////////////////////////////////// //We check email syntax private function isEmail($email) { return $result = (filter_var($email, FILTER_VALIDATE_EMAIL)) ? true : false ; } private function joinFiles(){ $msg = ''; if(is_array($this->_files)) { foreach($this->_files as $key => $var) { $msg = $this->includeFiles($var); } } else { $msg = $this->includeFiles($this->_files); } return $msg; } private function includeFiles($file_name){ $msg = ''; if(file_exists($file_name)) { $file_type = filetype($file_name); $file_size = filesize($file_name); $handle = fopen($file_name, 'r') or die('File '.$file_name.'can t be open'); $content = fread($handle, $file_size); $content = chunk_split(base64_encode($content)); $f = fclose($handle); $msg .= '--'.$this->_boundary."\n"; $msg .= 'Content-type:'.$file_type.';name='.$file_name."\n"; $msg .= 'Content-transfer-encoding:base64'."\n"; $msg .= $content."\n"; } return $msg; } /////////////////////////////////////////// /////////////////////////////////////////// // SETTERS /////////////////////////////////////////// /////////////////////////////////////////// private function setHeaders(){ //Mail headers $headers = ''; $headers .= 'From: '.$this->_sender_syntax.'' . "\n"; $headers .= 'Reply-To: '.$this->_reply_to.'' . "\n"; $headers .= 'MIME-Version: 1.0' . "\n"; //If we send a file if($this->_files) { $headers .= 'Content-Type: multipart/mixed;boundary='.$this->_boundary."\n"; $headers .= "\n"; } else { //Html or non Html mail if($this->_is_html){ $headers .= 'Content-type: text/html; charset="'.$this->_charset.'"' . "\n"; } else { $headers .= 'Content-type: text/plain; charset="'.$this->_charset.'"' . "\n"; } } $headers .= "\tformat=flowed;\n"; $headers .= "\tcharset=\"".$this->_charset."\";\n"; $headers .= "\treply-type=original\n"; $headers .= 'Content-Transfer-Encoding: 8bit'. "\n"; //Carbone copy if(!empty($this->_cc)){ $headers .= 'Cc: '.$this->_cc.'' . "\n"; } //BCC if(!empty($this->_bcc)){ $headers .= 'Bcc: '.$this->_bcc.'' . "\n"; } $headers .= 'Organization: '.$this->_organization.'' . "\n"; $headers .= 'X-Mailer: PHP/'.phpversion().'' . "\n"; //Mail confirmation if($this->_confirmation){ $headers .= 'X-Confirm-Reading-To: '.$this->_from.'' . "\n"; } //Priority of the mail if($this->_priority){ $headers .= 'X-Priority: '.$this->_priority.'' . "\n"; } return $this->_headers = $headers; } function setFile($file){ //Add attachment try{ if(empty($file)){ $error = 'Attachment can not be empty.'; throw new Exception($error); } else { return $this->_files = $file; } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } function setFormat($state){ //Set mail format (html or not) return $this->_is_html = $state; } //Priority of the mail (level between 1 and 3) private function setPriority($value){ try{ if(is_numeric($value)){ if(($value <= 3) && ($value > 0)){ return $this->_priority = $value; } else { $error = 'Priority degree must be between 1 and 3.'; throw new Exception($error); } } else { $error = 'Priority degree must be a number.'; throw new Exception($error); } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } private function setCharset($charset){ return $this->_charset = $charset; } private function setReceptionAlert($state){ //Acknowledgment return $this->_confirmation = $state; } private function setReplyTo($mail){ try { if($this->isEmail($mail)){ $this->_reply_to = $mail; } else { $error = 'Email to reply to "'.$mail.'" is not a valid email address.'; throw new Exception($error); } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } private function setSending($nature , $user , $to){ try { if($this->isEmail($to)){ if($nature == 'FROM'){ //Sender $this->_from = strip_tags($to); $testUser = (empty($user)) ? $this->_from : utf8_decode(strip_tags($user)) ; $this->_sender_syntax = $testUser.' <'.$this->_from.'>'; } else { //Receiver $this->_to = strip_tags($to); $testUser = (empty($user)) ? $to : $user ; $this->_recipient_syntax= (empty($this->_recipient_syntax)) ? $testUser.' <'.$to.'>' : $testUser.' <'.$to.'>, ' ; } } else { $error = 'Email "'.$to.'" is not a valid email address.'; throw new Exception($error); } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } private function setCopy($nature , $emails){ try{ $mails = (!empty($emails)) ? strip_tags($emails) : false ; if(!$mails){ $error = $nature.' can not be empty.'; throw new Exception($error); } else { if($nature == 'BCC'){ $this->_bcc = $emails; } else { $this->_cc = $emails; } } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } private function setSubject($subject){ return $this->_subject = (empty($subject)) ? $this->_subject : '=?UTF-8?Q?'.strip_tags($subject).'?=' ; //Encoding for mail clients } private function setMessage($message){ $thisMessage = (empty($message)) ? $this->_message : $message ; $thisMessage = (!$this->_is_html) ? wordwrap($thisMessage , 70) : $thisMessage ; //Cut sentences every 70 caracters if non html text return $this->_message = $thisMessage; } /////////////////////////////////////////// /////////////////////////////////////////// // PUBLIC METHODS /////////////////////////////////////////// /////////////////////////////////////////// function set($args){ switch($args[0]){ case 'copy': $result = $this->setCopy($args[1] , $args[2]); break; case 'sending': $result = $this->setSending($args[1] , $args[2] , $args[3]); break; case 'subject': $result = $this->setSubject($args[1]); break; case 'message': $result = $this->setMessage($args[1]); break; case 'charset': $result = $this->setCharset($args[1]); break; case 'priority': $result = $this->setPriority($args[1]); break; case 'replyTo': $result = $this->setReplyTo($args[1]); break; case 'alert': $result = $this->setReceptionAlert($args[1]); break; case 'html': $result = $this->setFormat($args[1]); break; case 'attachment': $result = $this->setFile($args[1]); break; } return $result; } function send(){ try{ //if(!mail($this->_to , $this->_subject , $this->_message.$this->joinFiles() , $this->setHeaders())){ if(!mail($this->_to , $this->_subject , $this->_message , $this->setHeaders())){ $error = 'Unable to send the mail.'; throw new Exception($error); } } catch(Exception $e) { echo 'Exception caught by method "'.__FUNCTION__.'": '.$e->getMessage()."\n"; } } }
3 sept. 2012 à 16:59
Mais pour clore cette discussion (hors sujet de ta source), j'avais trituré les $headers dans tous les sens et simplement le fait de changer un mot basiqu dans le $sujet, faisait passer le mail en indésirable (chez wanodoo et 1&1)...
A l'occasion, je testerai ta source.
Merci et à+
jojo19
3 sept. 2012 à 16:41
3 sept. 2012 à 16:39
En général, un client mail (hotmail, gmail etc...) met un mail dans les courriers indésirables lorsque l'adresse de l'expéditeur provient d'un domaine, ex (sylvain@toto.com), ou de la fréquence d'envoie de mail de la part de cet expéditeur.
Sur mes tests, je n'ai jamais été dans les courriers indésirables sur Hotmail, Gmail, Safari, Thunderbird et Outlook.
A toi de tester.
3 sept. 2012 à 16:14
Je ne suis pas un expert et je ne saurais pas commenter cette source,
mais plus généralement, à ma connaissance, le problème est d'envoyer des mails sans finir dans la boite anti-spam du destinataire !
J'ai eu l'occasion d'utiliser la fonction 'mail' classique (qui fonctionne bien lors de l'envoi) mais 2 fois sur 3, le mail était considéré comme un spam par le FAI du destinataire.
Est-ce que cette source règle ce pb ?
3 sept. 2012 à 09:55
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.