Certains mails ne partent pas avec un envoi PHP et SMTP
Posté : 12 août 2014, 22:57
Bonsoir,
J'ai un script d'envoi de mails via SMTP depuis un fonction PHP.
Mon site génère des envois plus ou moins important de mails, afin de garantir une bonne délivrabilité j'utilise un envoi via SMTP.
Jusqu'à récemment tout fonctionnait parfaitement. j'ai du changer d'hébergeur et malgré des contacts réguliers avec l'assistance de mon
hébergeur, il est impossible de trouver le "problème", aussi je vous demande votre aide.
J'ai une page appelé par une tâche CRON toutes les 10 minutes :
envoidemails.php
dans laquelle je met dès le départ : include('mail_class.php');
Plus loin dans cette page (envoidemails.php) j'appelle :
if (new Mail($arr[2],$subject,$message,$format,$webmasteremail,$nom_autorep,$idmembre)) { }
Voici le code de "envoidemails.php" :
Mon problème est qu'en faisant partir un mail simple :
Et également un mail plus complexe avec un titre et un corps de message contenant des accents circonflexes, des apostrophes etc :
Le premier mail part bien, le second lui ne s'envoi pas (en tout cas n'arrive pas).
J'ai évidemment testé sur la même boîte mail et sur une boîte différente.
J'ai du mal à faire du debug sur la fonction pour savoir là où cela pourrait gêner, je ne suis pas à l'origine du code de départ.
Merci d'avance pour toute l'aide que vous pourriez m'apporter étant donné que je butte depuis des jours sur ce problème.
J'ai un script d'envoi de mails via SMTP depuis un fonction PHP.
Mon site génère des envois plus ou moins important de mails, afin de garantir une bonne délivrabilité j'utilise un envoi via SMTP.
Jusqu'à récemment tout fonctionnait parfaitement. j'ai du changer d'hébergeur et malgré des contacts réguliers avec l'assistance de mon
hébergeur, il est impossible de trouver le "problème", aussi je vous demande votre aide.
J'ai une page appelé par une tâche CRON toutes les 10 minutes :
envoidemails.php
dans laquelle je met dès le départ : include('mail_class.php');
Plus loin dans cette page (envoidemails.php) j'appelle :
if (new Mail($arr[2],$subject,$message,$format,$webmasteremail,$nom_autorep,$idmembre)) { }
Voici le code de "envoidemails.php" :
class Mail{
private $smtpServer = 'xxxx'; // YOURSMTPHOST Enter the smtp server
private $port = '26';
private $timeout = '45';
private $uname = '[email protected]'; // YourSiteName enter uname
private $username = '[email protected]'; // YourEmail enter username (smtp server)
private $password = 'xxxx'; // YourPassword enter password (smtp server)
private $newline = "\r\n";
private $localdomain = 'xxxx'; //enter localdomain.
private $charset = 'utf-8'; //charser code
private $contentTransferEncoding = false;
// Do not change anything below
private $smtpConnect = false;
private $to = false;
private $subject = false;
private $message = false;
private $format = 1;
private $headers = false;
private $logArray = array(); // Array response message for debug
private $Error = '';
//ajour de $webmasteremail, $nom_autorep
public function __construct($to, $subject, $message, $format, $webmasteremail, $nom_autorep, $idmembre) {
$this->to = &$to;
$this->subject = &$subject;
$this->message = &$message;
echo "message : ".$this->message;
$this->format = &$format;
$this->webmasteremail = &$webmasteremail;
$this->nom_autorep = &$nom_autorep;
$this->idmembre = &$idmembre;
$this->newmailmembre = $this->idmembre.'@xxxx';
// Connect to server
if(!$this->Connect2Server()) {
// Display error message
echo $this->Error.$this->newline.' '.$this->newline;
print_r($this->logArray);
echo $this->newline.' '.$this->newline;
return false;
}
return true;
}
private function Connect2Server() {
// Connect to server
$this->smtpConnect = fsockopen($this->smtpServer,$this->port,$errno,$error,$this->timeout);
$this->logArray['CONNECT_RESPONSE'] = $this->readResponse();
if (!is_resource($this->smtpConnect)) {
return false;
}
$this->logArray['connection'] = "Connection accepted: $smtpResponse";
// Hi, server!
$this->sendCommand("EHLO $this->localdomain");
$this->logArray['EHLO'] = $this->readResponse();
// Let's know each other
$this->sendCommand('AUTH LOGIN');
$this->logArray['AUTH_REQUEST'] = $this->readResponse();
// My name...
$this->sendCommand(base64_encode($this->username));
$this->logArray['REQUEST_USER'] = $this->readResponse();
// My password..
$this->sendCommand(base64_encode($this->password));
$this->logArray['REQUEST_PASSWD'] = $this->readResponse();
// If error in response auth...
if (substr($this->logArray['REQUEST_PASSWD'],0,3)!='235') {
$this->Error .= 'Authorization error! '.$this->logArray['REQUEST_PASSWD'].$this->newline;
return false;
}
// "From" mail...
$this->sendCommand("MAIL FROM: $this->username");
$this->logArray['MAIL_FROM_RESPONSE'] = $this->readResponse();
if (substr($this->logArray['MAIL_FROM_RESPONSE'],0,3)!='250') {
$this->Error .= 'Mistake in sender\'s address! '.$this->logArray['MAIL_FROM_RESPONSE'].$this->newline;
return false;
}
// "To" address
$this->sendCommand("RCPT TO: $this->to");
$this->logArray['RCPT_TO_RESPONCE'] = $this->readResponse();
if (substr($this->logArray['RCPT_TO_RESPONCE'],0,3)!='250') {
$this->Error .= 'Mistake in reciepent address! '.$this->logArray['RCPT_TO_RESPONCE'].$this->newline;
}
// Send data to server
$this->sendCommand('DATA');
$this->logArray['DATA_RESPONSE'] = $this->readResponse();
// Send mail message
if (!$this->sendMail()) return false;
// Good bye server! =)
$this->sendCommand('QUIT');
$this->logArray['QUIT_RESPONSE'] = $this->readResponse();
// Close smtp connect
fclose($this->smtpConnect);
return true;
}
// Function send mail
private function sendMail() {
$this->sendHeaders();
$this->sendCommand($this->message);
$this->sendCommand('.');
$this->logArray['SEND_DATA_RESPONSE'] = $this->readResponse();
if(substr($this->logArray['SEND_DATA_RESPONSE'],0,3)!='250') {
$this->Error .= 'Mistake in sending data! '.$this->logArray['SEND_DATA_RESPONSE'].$this->newline;
return false;
}
return true;
}
// Function read response
private function readResponse() {
$data="";
while($str = fgets($this->smtpConnect,4096))
{
$data .= $str;
if(substr($str,3,1) == " ") { break; }
}
return $data;
}
// function send command to server
private function sendCommand($string) {
fputs($this->smtpConnect,$string.$this->newline);
return ;
}
// function send headers
private function sendHeaders() {
$this->sendCommand("Date: ".date("D, j M Y H:i:s")." +0700");
//changement $this->sendCommand("From: $this->uname <$this->username>"); en $this->sendCommand("From: $this->nom_autorep <$this->webmasteremail>");
$this->sendCommand("From: $this->nom_autorep <$this->webmasteremail>");
//changement $this->sendCommand("Reply-To: <$this->username>"); en $this->sendCommand("Reply-To: <$this->webmasteremail>");
$this->sendCommand("Reply-To: <$this->webmasteremail>");
//ajout $this->sendCommand("Sender: <$this->newmailmembre");
$this->sendCommand("Sender: <$this->newmailmembre>");
$this->sendCommand("To: <$this->to>");
$this->sendCommand("Subject: $this->subject");
$this->sendCommand("MIME-Version: 1.0");
if(($this->format)==1)
$this->sendCommand("Content-Type: text/plain; charset=$this->charset");
else
$this->sendCommand("Content-Type: text/html; charset=$this->charset");
if ($this->contentTransferEncoding) $this->sendCommand("Content-Transfer-Encoding: $this->contentTransferEncoding");
$this->sendCommand($this->newline);
// print_r($this->to);
return ;
}
public function __destruct() {
if (is_resource($this->smtpConnect)) fclose($this->smtpConnect);
}
}
Mon problème est qu'en faisant partir un mail simple :
sujet :
Test d un second message
message :
You are receiving this message because you/someone else had subscribed to our list with this email address. For removal check the bottom instructions.
=======================================
Voici un second message!
=======================================
Si vous souhaitez ne plus recevoir de messages, cliquez sur le lien suivant : http://*****remove.php?id=***&email=****
=======================================
Et également un mail plus complexe avec un titre et un corps de message contenant des accents circonflexes, des apostrophes etc :
Vous recevez ce message parce que vous vous êtes inscrit à notre liste avec cette adresse e-mail. Vous pouvez vous désabonner en fin de mail. ======================================= Bonjour Damien, Avez-vous bien lu le livre concernant les "****"? Alors qu'en avez-vous pensé?
[ etc...] ======================================= Si vous souhaitez ne plus recevoir de messages, cliquez sur le lien suivant : http://*****remove.php?id=***&email=**** =======================================
Le premier mail part bien, le second lui ne s'envoi pas (en tout cas n'arrive pas).
J'ai évidemment testé sur la même boîte mail et sur une boîte différente.
J'ai du mal à faire du debug sur la fonction pour savoir là où cela pourrait gêner, je ne suis pas à l'origine du code de départ.
Merci d'avance pour toute l'aide que vous pourriez m'apporter étant donné que je butte depuis des jours sur ce problème.