Fix: Correct bugs in simplemail class that did not handle multi recipients correctly.

This commit is contained in:
Laurent Destailleur 2009-05-24 21:30:37 +00:00
parent c5effe4caf
commit fbf0747e0c
2 changed files with 55 additions and 46 deletions

View File

@ -263,7 +263,9 @@ class simplemail {
$this->headers = ''; $this->headers = '';
if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; } if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; }
// else { $this->AddField2Header("To",$this->recipient); } else { // DOLCHANGE LDR Fix missing recipients in header
$this->AddField2Header("To",$this->recipient);
}
if ( empty($this->subject) ) { if ( empty($this->subject) ) {
$this->error_log("sujet manquant"); $this->error_log("sujet manquant");
@ -322,18 +324,22 @@ class simplemail {
case 'socket': $this->socketmailloop(); break; case 'socket': $this->socketmailloop(); break;
} }
// DOLCHANGE LDR return TRUE;
return (empty($this->error_log)?TRUE:FALSE);
} }
// Mail send by PHPmail // Mail send by PHPmail
function phpmail() { function phpmail() {
while ( list($key, $to) = each($this->recipientlist) ) { // DOLCHANGE LDR Fix the To in header was not filled
$this->recipient = $to['mail']; foreach ($this->recipientlist as $key => $to)
{
$this->recipient = ($this->recipient?$this->recipient.', ':'').$to['mail'];
}
foreach ($this->recipientlist as $key => $to)
{
// $this->recipient = $to['mail']; DOLCHANGE LDR Fix the To in header was not filled
if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) { if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) {
// DOLCHANGE LDR Comment this to have no error when no error ! $this->error_log("envoie vers {$to['nameplusmail']} reussi");
//$this->error_log("envoie vers {$to['nameplusmail']} reussi");
} else { } else {
$this->error_log("envoie vers {$to['nameplusmail']} echoue"); $this->error_log("envoie vers {$to['nameplusmail']} echoue");
} }
@ -358,9 +364,11 @@ class simplemail {
function SocketSend($in,$wait='') { function SocketSend($in,$wait='') {
fputs($this->connect, $in, strlen($in)); fputs($this->connect, $in, strlen($in));
//echo $in; //echo $in;
$this->error_log($in); // DOLCHANGE LDR Add debug
//flush(); //flush();
if(empty($wait)) { if(empty($wait)) {
$rcv = fgets($this->connect, 1024); $rcv = fgets($this->connect, 1024);
$this->error_log('('.$rcv.')'); // DOLCHANGE LDR Add debug
return $rcv; return $rcv;
} }
return TRUE; return TRUE;
@ -373,25 +381,27 @@ class simplemail {
$this->SocketStart(); $this->SocketStart();
if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; } if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; }
else { $serv = $_SERVER['SERVER_NAME']; } else { $serv = $_SERVER['SERVER_NAME']; }
$this->SocketSend("HELO $serv\r\n"); // DOLCHANGE LDR
$serv = ini_get('SMTP');
$this->SocketSend("HELO $serv\r\n",'250');
} }
function socketmailsend($to) { function socketmailsend($to) {
$this->recipient = $to; // $this->recipient = $to; // DOLCHANGE LDR Must not reset this property
// DOLCHANGE LDR To have no error when no error $this->error_log("Socket vers $to");
//$this->error_log("Socket vers $to");
$this->SocketSend( "MAIL FROM:{$this->hfrom}\r\n" ); // DOLCHANGE LDR: From has to be the raw email address, strip the "name" off
$this->SocketSend( "RCPT TO:$to\r\n" ); $fromarray=split(' ',$this->hfrom);
$this->SocketSend( "DATA\r\n" ); $from=(empty($fromarray[1])?$fromarray[0]:$fromarray[1]);
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n", 'NOWAIT' ); $this->SocketSend( "MAIL FROM: ".$from."\r\n", '250');
$this->SocketSend( $this->CleanMailDataString($this->body)."\r\n", 'NOWAIT' ); $this->SocketSend( "RCPT TO: <".$to.">\r\n", '250');
$this->SocketSend( "DATA\r\n", '354');
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n".$this->CleanMailDataString($this->body)."\r\n", '250'); // DOLCHANGE LDR Must wait return 250
$this->SocketSend( ".\r\n" ); $this->SocketSend( ".\r\n" );
$this->SocketSend( "RSET\r\n" ); $this->SocketSend( "RSET\r\n" );
// DOLCHANGE LDR To have no error when no error $this->error_log("Fin de l'envoi vers $to");
//$this->error_log("Fin de l'envoi vers $to");
return TRUE; return TRUE;
} }
@ -404,8 +414,13 @@ class simplemail {
function socketmailloop() { function socketmailloop() {
$this->socketmailstart(); $this->socketmailstart();
while ( list($key, $to) = each($this->recipientlist)) { // DOLCHANGE LDR Fix the To in header was not filled
$this->recipient = $to['mail']; foreach ($this->recipientlist as $key => $to)
{
$this->recipient = ($this->recipient?$this->recipient.', ':'').$to['mail'];
}
foreach ($this->recipientlist as $key => $to)
{
$this->makeheader(); $this->makeheader();
$this->socketmailsend($to['mail']); $this->socketmailsend($to['mail']);
} }
@ -416,7 +431,9 @@ class simplemail {
function error_log($msg='') { function error_log($msg='') {
if(!empty($msg)) { if(!empty($msg)) {
$this->error_log .= $msg . "\r\n--\r\n"; //$this->error_log .= $msg . "\r\n--\r\n";
//DOLCHANGE LDR Better to read log
$this->error_log .= $msg;
return TRUE; return TRUE;
} }
return " --- Error Log --- \r\n\r\n".$this->error_log; return " --- Error Log --- \r\n\r\n".$this->error_log;

View File

@ -412,8 +412,8 @@ class CMailFile
if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_set('SMTP',$conf->global->MAIN_MAIL_SMTP_SERVER); if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_set('SMTP',$conf->global->MAIN_MAIL_SMTP_SERVER);
if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_set('smtp_port',$conf->global->MAIN_MAIL_SMTP_PORT); if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_set('smtp_port',$conf->global->MAIN_MAIL_SMTP_PORT);
if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') $dest=$this->getValidAddress($this->addr_to,2); $dest=$this->getValidAddress($this->addr_to,2);
if (! $dest && $conf->global->MAIN_MAIL_SENDMODE == 'mail') if (! $dest)
{ {
$this->error="Failed to send mail to SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid"; $this->error="Failed to send mail to SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERROR); dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERROR);
@ -476,32 +476,24 @@ class CMailFile
if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_set('SMTP',$conf->global->MAIN_MAIL_SMTP_SERVER); if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_set('SMTP',$conf->global->MAIN_MAIL_SMTP_SERVER);
if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_set('smtp_port',$conf->global->MAIN_MAIL_SMTP_PORT); if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_set('smtp_port',$conf->global->MAIN_MAIL_SMTP_PORT);
if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') $dest=$this->getValidAddress($this->addr_to,2); dol_syslog("CMailFile::sendfile: mail start SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port'), LOG_DEBUG);
if (! $dest && $conf->global->MAIN_MAIL_SENDMODE == 'mail')
$this->message=stripslashes($this->message);
if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
$res = $this->simplemail->sendmail();
if (! $res)
{ {
$this->error="Failed to send mail to SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid"; $this->error="Failed to send mail to SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Check your server logs and your firewalls setup";
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERROR); dol_syslog("CMailFile::sendfile: mail end error ".$this->error, LOG_ERR);
dol_syslog("CMailFile::sendfile: ".$this->simplemail->error_log, LOG_ERR);
} }
else else
{ {
dol_syslog("CMailFile::sendfile: mail start SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port'), LOG_DEBUG); dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
dol_syslog("CMailFile::sendfile: ".$this->simplemail->error_log, LOG_DEBUG);
$this->message=stripslashes($this->message);
if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
$res = $this->simplemail->sendmail();
if (! $res)
{
$this->error="Failed to send mail to SMTP=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Check your server logs and your firewalls setup";
$this->error.="\n".$this->simplemail->error_log;
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
}
else
{
dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
}
} }
if (isset($_SERVER["WINDIR"])) if (isset($_SERVER["WINDIR"]))
@ -535,7 +527,7 @@ class CMailFile
if (! $dest) if (! $dest)
{ {
$this->error="Failed to send mail to SMTP=".$conf->global->MAIN_MAIL_SMTP_SERVER.", PORT=".$conf->global->MAIN_MAIL_SMTP_PORT."<br>Recipient address '$dest' invalid"; $this->error="Failed to send mail to SMTP=".$conf->global->MAIN_MAIL_SMTP_SERVER.", PORT=".$conf->global->MAIN_MAIL_SMTP_PORT."<br>Recipient address '$dest' invalid";
dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_DEBUG); dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
} }
else else
{ {