Envoi de mail php class Mime
Fermé
Titi
-
29 juil. 2009 à 10:00
giheller Messages postés 1875 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 3 février 2024 - 6 sept. 2009 à 14:40
giheller Messages postés 1875 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 3 février 2024 - 6 sept. 2009 à 14:40
A voir également:
- Envoi de mail php class Mime
- Yahoo mail - Accueil - Mail
- Publipostage mail - Accueil - Word
- Programmer envoi mail gmail - Guide
- Envoi mail cci - Guide
- Annuler envoi mail gmail - Guide
13 réponses
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
29 juil. 2009 à 11:43
29 juil. 2009 à 11:43
bonjour,
je n'ai pas trouvé dans cette class, peut être faudrait-il en trouver une autre :
<?php
class simplemail {
var $recipientlist;
var $subject;
var $hfrom;
var $hbcc;
var $hcc;
var $Xsender;
var $ErrorsTo;
var $XMailer = 'PHP';
var $XPriority = 3;
var $set_mode='php';
var $text;
var $html;
var $attachement;
var $htmlattachement;
var $recipient;
var $body;
var $headers;
var $error_log;
var $connect;
var $default_charset = 'iso-8859-1';
var $B1B = "----=_001";
var $B2B = "----=_002";
var $B3B = "----=_003";
function simplemail() {
$this -> attachement = array();
$this -> htmlattachement = array();
}
function checkaddress($address) {
if ( preg_match('`(:alnum:([-_.]?:alnum:)*@:alnum:([-_.]?:alnum:)*\.([a-z]{2,4}))`', $address) ) {
return TRUE;
} else {
$this->error_log("l'adresse $address est invalide"); return FALSE;
}
}
function checkname($name) {
if ( preg_match("`[0-9a-zA-Z\.\-_ ]*`" , $name ) ) {
return TRUE;
} else {
$this->error_log(" le pseudo $name est invalide\n"); return FALSE;
}
}
function makenameplusaddress($address,$name) {
if ( !$this->checkaddress($address) ) return FALSE;
if ( !$this->checkname($name) ) return FALSE;
if ( empty($name) ) { return $address; }
else { $tmp=$name." <".$address.">"; return $tmp; }
}
function addrecipient($newrecipient,$name='') {
$tmp=$this->makenameplusaddress($newrecipient,$name);
if ( !$tmp ) { $this->error_log(" To: error"); return FALSE; }
$this->recipientlist[] = array( 'mail'=>$newrecipient, 'nameplusmail' => $tmp );
return TRUE;
}
function addbcc($bcc,$name='') {
$tmp=$this->makenameplusaddress($bcc,$name);
if ( !$tmp ) { $this->error_log(" Bcc: error"); return FALSE; }
if ( !empty($this->hbcc)) $this->hbcc.= ",";
$this->hbcc.= $tmp;
return TRUE;
}
function addcc($cc,$name='') {
$tmp=$this->makenameplusaddress($cc,$name);
if ( !$tmp ) { $this->error_log(" Cc: error\n"); return FALSE; }
if (!empty($this->hcc)) $this->hcc.= ",";
$this->hcc.= $tmp;
return TRUE;
}
function addsubject($subject) {
if (!empty($subject)) $this->subject = $subject;
}
function addfrom($from,$name='') {
$tmp=$this->makenameplusaddress($from,$name);
if ( !$tmp ) { $this->error_log(" From: error"); return FALSE; }
$this->hfrom = $tmp;
return TRUE;
}
function addreturnpath($return) {
$tmp=$this->makenameplusaddress($return,'');
if ( !$tmp ) { $this->error_log("Return-Path: error"); return FALSE; }
$this->returnpath = $return;
return TRUE;
}
function addreplyto($replyto) {
$tmp=$this->makenameplusaddress($replyto,'');
if ( !$tmp ) { $this->error_log(" Reply-To: error"); return FALSE; }
$this->returnpath = $tmp;
return TRUE;
}
// les attachements
function addattachement($filename) {
array_push ( $this -> attachement , array ( 'filename'=> $filename ) );
}
// les attachements html
function addhtmlattachement($filename,$cid='',$contenttype='') {
array_push ( $this -> htmlattachement ,
array ( 'filename'=>$filename ,
'cid'=>$cid ,
'contenttype'=>$contenttype )
);
}
function writeattachement(&$attachement,$B) {
$message = '';
if ( !empty($attachement) ) {
foreach($attachement as $AttmFile){
$patharray = explode ("/", $AttmFile['filename']);
$FileName = $patharray[count($patharray)-1];
$message .= "\n--".$B."\n";
if (!empty($AttmFile['cid'])) {
$message .= "Content-Type: {$AttmFile['contenttype']};\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <{$AttmFile['cid']}>\n";
$message .= "Content-Disposition: inline;\n filename=\"".$FileName."\"\n\n";
} else {
$message .= "Content-Type: application/octetstream;\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n filename=\"".$FileName."\"\n\n";
}
$fd=fopen ($AttmFile['filename'], "rb");
$FileContent=fread($fd,filesize($AttmFile['filename']));
fclose ($fd);
$FileContent = chunk_split(base64_encode($FileContent));
$message .= $FileContent;
$message .= "\n\n";
}
$message .= "\n--".$B."--\n";
}
return $message;
}
function BodyLineWrap($Value) {
return wordwrap($Value, 78, "\n ");
}
function makebody() {
$message='';
if ( !$this->html && $this->text && !empty($this->attachement) ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
elseif ( !$this->html && $this->text && empty($this->attachement) ) {
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
}
elseif ( $this->html ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: multipart/related;\n\t boundary=\"".$this->B2B."\"\n\n";
//plaintext section
$message.="\n--".$this->B2B."\n";
$message.="Content-Type: multipart/alternative;\n\t boundary=\"".$this->B3B."\"\n\n";
//plaintext section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
// html section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$message.=chunk_split(base64_encode($this->html))."\n\n";
// end of text
$message.="\n--".$this->B3B."--\n";
// attachments html
if (empty($this->htmlattachement)) {
$message.="\n--".$this->B2B."--\n";
} else {
$message.=$this->writeattachement( $this->htmlattachement,$this->B2B);
}
// attachments
if (empty($this->attachement)) {
$message.="\n--".$this->B1B."--\n";
} else {
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
}
$this->body = $message;
return $message;
}
// Mail Headers Methods
function MakeHeaderField($Field,$Value) {
return wordwrap($Field.": ".$Value, 78, "\n ")."\r\n";
}
function AddField2Header($Field,$Value) {
$this->headers .= $this->MakeHeaderField($Field,$Value);
}
function makeheader() {
$this->headers = '';
if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; }
// else { $this->AddField2Header("To",$this->recipient); }
if ( empty($this->subject) ) {
$this->error_log("sujet manquant");
return FALSE;
} else {
if ($this->set_mode!='php' ) {
$this->AddField2Header("Subject", $this->subject);
}
}
# Date: Mon, 03 Nov 2003 20:48:06 +0100
$this->AddField2Header("Date", date ('r'));
if ( !empty($this->Xsender) ) { $this->AddField2Header("X-Sender",$this->Xsender); }
else { $this->AddField2Header("X-Sender",$this->hfrom); }
if ( !empty($this->ErrorsTo) ) { $this->AddField2Header("Errors-To",$this->ErrorsTo); }
else { $this->AddField2Header("Errors-To",$this->hfrom); }
if ( !empty($this->XMailer) ) $this->AddField2Header("X-Mailer",$this->XMailer);
if ( !empty($this->XPriority) ) $this->AddField2Header("X-Priority",$this->XPriority);
if ( !empty($this->hfrom) ) $this->AddField2Header("From",$this->hfrom);
if ( !empty($this->returnpath) ) $this->AddField2Header("Return-Path",$this->returnpath);
if ( !empty($this->replyto) ) $this->AddField2Header("Reply-To",$this->replyto);
$this->headers .="MIME-Version: 1.0\r\n";
if ( !$this->html && $this->text && !empty($this->attachement) ) {
$this->headers .= "Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
} elseif ( !$this->html && $this->text && empty($this->attachement) ) {
$this->headers .="Content-Type: text/plain; charset=us-ascii; format=flowed\r\n";
$this->headers .="Content-Transfer-Encoding: 7bit\r\n";
} elseif ( $this->html ) {
if ( !$this->text ) { $this->text="HTML only!"; }
$this->headers .="Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
}
if ( !empty($this->hcc) ) $this->AddField2Header("Cc",$this->hcc);
if ( !empty($this->hbcc) ) $this->AddField2Header("Bcc",$this->hbcc);
return $this->headers;
}
function sendmail() {
$this->makebody();
$this->makeheader();
switch($this->set_mode) {
case 'php' : $this->phpmail(); break;
case 'socket': $this->socketmailloop(); break;
}
return TRUE;
}
// Mail send by PHPmail
function phpmail() {
while ( list($key, $to) = each($this->recipientlist) ) {
$this->recipient = $to['mail'];
if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) {
$this->error_log("envoie vers {$to['nameplusmail']} réussi");
} else {
$this->error_log("envoie vers {$to['nameplusmail']} echoué");
}
}
return TRUE;
}
// Socket Function
function SocketStart() {
if (!$this->connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30)) {
$this->error_log("Could not talk to the sendmail server!"); return FALSE;
};
return fgets($this->connect, 1024);
}
function SocketStop() {
fclose($this->connect);
return TRUE;
}
function SocketSend($in,$wait='') {
fputs($this->connect, $in, strlen($in));
echo "-"; flush();
if(empty($wait)) {
$rcv = fgets($this->connect, 1024);
return $rcv;
}
return TRUE;
}
// Mail Socket
function socketmailstart() {
$this->SocketStart();
if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; }
else { $serv = $_SERVER['SERVER_NAME']; }
$this->SocketSend("HELO $serv\r\n");
}
function socketmailsend($to) {
$this->recipient = $to;
$this->error_log("Socket vers $to");
$this->SocketSend( "MAIL FROM:{$this->hfrom}\r\n" );
$this->SocketSend( "RCPT TO:$to\r\n" );
$this->SocketSend( "DATA\r\n" );
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n", 'NOWAIT' );
$this->SocketSend( $this->CleanMailDataString($this->body)."\r\n", 'NOWAIT' );
$this->SocketSend( ".\r\n" );
$this->SocketSend( "RSET\r\n" );
$this->error_log("Fin de l'envoi vers $to");
return TRUE;
}
function socketmailstop() {
$this->SocketSend("QUIT\r\n");
$this->SocketStop();
return TRUE;
}
function socketmailloop() {
$this->socketmailstart();
while ( list($key, $to) = each($this->recipientlist)) {
$this->recipient = $to['mail'];
$this->makeheader();
$this->socketmailsend($to['mail']);
}
$this->socketmailstop();
}
// Misc.
function error_log($msg='') {
if(!empty($msg)) {
$this->error_log .= $msg . "\r\n--\r\n";
return TRUE;
}
return " --- Error Log --- \r\n\r\n".$this->error_log;
}
function CleanMailDataString($data) {
$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);
$data = preg_replace("/\n\n/", "\n\r\n", $data);
$data = preg_replace("/\n\./", "\n..", $data);
return $data;
}
}
?>
je n'ai pas trouvé dans cette class, peut être faudrait-il en trouver une autre :
<?php
class simplemail {
var $recipientlist;
var $subject;
var $hfrom;
var $hbcc;
var $hcc;
var $Xsender;
var $ErrorsTo;
var $XMailer = 'PHP';
var $XPriority = 3;
var $set_mode='php';
var $text;
var $html;
var $attachement;
var $htmlattachement;
var $recipient;
var $body;
var $headers;
var $error_log;
var $connect;
var $default_charset = 'iso-8859-1';
var $B1B = "----=_001";
var $B2B = "----=_002";
var $B3B = "----=_003";
function simplemail() {
$this -> attachement = array();
$this -> htmlattachement = array();
}
function checkaddress($address) {
if ( preg_match('`(:alnum:([-_.]?:alnum:)*@:alnum:([-_.]?:alnum:)*\.([a-z]{2,4}))`', $address) ) {
return TRUE;
} else {
$this->error_log("l'adresse $address est invalide"); return FALSE;
}
}
function checkname($name) {
if ( preg_match("`[0-9a-zA-Z\.\-_ ]*`" , $name ) ) {
return TRUE;
} else {
$this->error_log(" le pseudo $name est invalide\n"); return FALSE;
}
}
function makenameplusaddress($address,$name) {
if ( !$this->checkaddress($address) ) return FALSE;
if ( !$this->checkname($name) ) return FALSE;
if ( empty($name) ) { return $address; }
else { $tmp=$name." <".$address.">"; return $tmp; }
}
function addrecipient($newrecipient,$name='') {
$tmp=$this->makenameplusaddress($newrecipient,$name);
if ( !$tmp ) { $this->error_log(" To: error"); return FALSE; }
$this->recipientlist[] = array( 'mail'=>$newrecipient, 'nameplusmail' => $tmp );
return TRUE;
}
function addbcc($bcc,$name='') {
$tmp=$this->makenameplusaddress($bcc,$name);
if ( !$tmp ) { $this->error_log(" Bcc: error"); return FALSE; }
if ( !empty($this->hbcc)) $this->hbcc.= ",";
$this->hbcc.= $tmp;
return TRUE;
}
function addcc($cc,$name='') {
$tmp=$this->makenameplusaddress($cc,$name);
if ( !$tmp ) { $this->error_log(" Cc: error\n"); return FALSE; }
if (!empty($this->hcc)) $this->hcc.= ",";
$this->hcc.= $tmp;
return TRUE;
}
function addsubject($subject) {
if (!empty($subject)) $this->subject = $subject;
}
function addfrom($from,$name='') {
$tmp=$this->makenameplusaddress($from,$name);
if ( !$tmp ) { $this->error_log(" From: error"); return FALSE; }
$this->hfrom = $tmp;
return TRUE;
}
function addreturnpath($return) {
$tmp=$this->makenameplusaddress($return,'');
if ( !$tmp ) { $this->error_log("Return-Path: error"); return FALSE; }
$this->returnpath = $return;
return TRUE;
}
function addreplyto($replyto) {
$tmp=$this->makenameplusaddress($replyto,'');
if ( !$tmp ) { $this->error_log(" Reply-To: error"); return FALSE; }
$this->returnpath = $tmp;
return TRUE;
}
// les attachements
function addattachement($filename) {
array_push ( $this -> attachement , array ( 'filename'=> $filename ) );
}
// les attachements html
function addhtmlattachement($filename,$cid='',$contenttype='') {
array_push ( $this -> htmlattachement ,
array ( 'filename'=>$filename ,
'cid'=>$cid ,
'contenttype'=>$contenttype )
);
}
function writeattachement(&$attachement,$B) {
$message = '';
if ( !empty($attachement) ) {
foreach($attachement as $AttmFile){
$patharray = explode ("/", $AttmFile['filename']);
$FileName = $patharray[count($patharray)-1];
$message .= "\n--".$B."\n";
if (!empty($AttmFile['cid'])) {
$message .= "Content-Type: {$AttmFile['contenttype']};\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <{$AttmFile['cid']}>\n";
$message .= "Content-Disposition: inline;\n filename=\"".$FileName."\"\n\n";
} else {
$message .= "Content-Type: application/octetstream;\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n filename=\"".$FileName."\"\n\n";
}
$fd=fopen ($AttmFile['filename'], "rb");
$FileContent=fread($fd,filesize($AttmFile['filename']));
fclose ($fd);
$FileContent = chunk_split(base64_encode($FileContent));
$message .= $FileContent;
$message .= "\n\n";
}
$message .= "\n--".$B."--\n";
}
return $message;
}
function BodyLineWrap($Value) {
return wordwrap($Value, 78, "\n ");
}
function makebody() {
$message='';
if ( !$this->html && $this->text && !empty($this->attachement) ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
elseif ( !$this->html && $this->text && empty($this->attachement) ) {
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
}
elseif ( $this->html ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: multipart/related;\n\t boundary=\"".$this->B2B."\"\n\n";
//plaintext section
$message.="\n--".$this->B2B."\n";
$message.="Content-Type: multipart/alternative;\n\t boundary=\"".$this->B3B."\"\n\n";
//plaintext section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
// html section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$message.=chunk_split(base64_encode($this->html))."\n\n";
// end of text
$message.="\n--".$this->B3B."--\n";
// attachments html
if (empty($this->htmlattachement)) {
$message.="\n--".$this->B2B."--\n";
} else {
$message.=$this->writeattachement( $this->htmlattachement,$this->B2B);
}
// attachments
if (empty($this->attachement)) {
$message.="\n--".$this->B1B."--\n";
} else {
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
}
$this->body = $message;
return $message;
}
// Mail Headers Methods
function MakeHeaderField($Field,$Value) {
return wordwrap($Field.": ".$Value, 78, "\n ")."\r\n";
}
function AddField2Header($Field,$Value) {
$this->headers .= $this->MakeHeaderField($Field,$Value);
}
function makeheader() {
$this->headers = '';
if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; }
// else { $this->AddField2Header("To",$this->recipient); }
if ( empty($this->subject) ) {
$this->error_log("sujet manquant");
return FALSE;
} else {
if ($this->set_mode!='php' ) {
$this->AddField2Header("Subject", $this->subject);
}
}
# Date: Mon, 03 Nov 2003 20:48:06 +0100
$this->AddField2Header("Date", date ('r'));
if ( !empty($this->Xsender) ) { $this->AddField2Header("X-Sender",$this->Xsender); }
else { $this->AddField2Header("X-Sender",$this->hfrom); }
if ( !empty($this->ErrorsTo) ) { $this->AddField2Header("Errors-To",$this->ErrorsTo); }
else { $this->AddField2Header("Errors-To",$this->hfrom); }
if ( !empty($this->XMailer) ) $this->AddField2Header("X-Mailer",$this->XMailer);
if ( !empty($this->XPriority) ) $this->AddField2Header("X-Priority",$this->XPriority);
if ( !empty($this->hfrom) ) $this->AddField2Header("From",$this->hfrom);
if ( !empty($this->returnpath) ) $this->AddField2Header("Return-Path",$this->returnpath);
if ( !empty($this->replyto) ) $this->AddField2Header("Reply-To",$this->replyto);
$this->headers .="MIME-Version: 1.0\r\n";
if ( !$this->html && $this->text && !empty($this->attachement) ) {
$this->headers .= "Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
} elseif ( !$this->html && $this->text && empty($this->attachement) ) {
$this->headers .="Content-Type: text/plain; charset=us-ascii; format=flowed\r\n";
$this->headers .="Content-Transfer-Encoding: 7bit\r\n";
} elseif ( $this->html ) {
if ( !$this->text ) { $this->text="HTML only!"; }
$this->headers .="Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
}
if ( !empty($this->hcc) ) $this->AddField2Header("Cc",$this->hcc);
if ( !empty($this->hbcc) ) $this->AddField2Header("Bcc",$this->hbcc);
return $this->headers;
}
function sendmail() {
$this->makebody();
$this->makeheader();
switch($this->set_mode) {
case 'php' : $this->phpmail(); break;
case 'socket': $this->socketmailloop(); break;
}
return TRUE;
}
// Mail send by PHPmail
function phpmail() {
while ( list($key, $to) = each($this->recipientlist) ) {
$this->recipient = $to['mail'];
if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) {
$this->error_log("envoie vers {$to['nameplusmail']} réussi");
} else {
$this->error_log("envoie vers {$to['nameplusmail']} echoué");
}
}
return TRUE;
}
// Socket Function
function SocketStart() {
if (!$this->connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30)) {
$this->error_log("Could not talk to the sendmail server!"); return FALSE;
};
return fgets($this->connect, 1024);
}
function SocketStop() {
fclose($this->connect);
return TRUE;
}
function SocketSend($in,$wait='') {
fputs($this->connect, $in, strlen($in));
echo "-"; flush();
if(empty($wait)) {
$rcv = fgets($this->connect, 1024);
return $rcv;
}
return TRUE;
}
// Mail Socket
function socketmailstart() {
$this->SocketStart();
if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; }
else { $serv = $_SERVER['SERVER_NAME']; }
$this->SocketSend("HELO $serv\r\n");
}
function socketmailsend($to) {
$this->recipient = $to;
$this->error_log("Socket vers $to");
$this->SocketSend( "MAIL FROM:{$this->hfrom}\r\n" );
$this->SocketSend( "RCPT TO:$to\r\n" );
$this->SocketSend( "DATA\r\n" );
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n", 'NOWAIT' );
$this->SocketSend( $this->CleanMailDataString($this->body)."\r\n", 'NOWAIT' );
$this->SocketSend( ".\r\n" );
$this->SocketSend( "RSET\r\n" );
$this->error_log("Fin de l'envoi vers $to");
return TRUE;
}
function socketmailstop() {
$this->SocketSend("QUIT\r\n");
$this->SocketStop();
return TRUE;
}
function socketmailloop() {
$this->socketmailstart();
while ( list($key, $to) = each($this->recipientlist)) {
$this->recipient = $to['mail'];
$this->makeheader();
$this->socketmailsend($to['mail']);
}
$this->socketmailstop();
}
// Misc.
function error_log($msg='') {
if(!empty($msg)) {
$this->error_log .= $msg . "\r\n--\r\n";
return TRUE;
}
return " --- Error Log --- \r\n\r\n".$this->error_log;
}
function CleanMailDataString($data) {
$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);
$data = preg_replace("/\n\n/", "\n\r\n", $data);
$data = preg_replace("/\n\./", "\n..", $data);
return $data;
}
}
?>
J'avais essayé avec cette classe, le problème est que lorsque je mets une cc j'ai une erreur qui arrive. Erreur qui ne provient ni du code ni de la classe..
Merci
Merci
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
29 juil. 2009 à 11:50
29 juil. 2009 à 11:50
il y a peut être des noms de variables à revoir. cette classe est utilisée sur mes sites et a jusqu'à maintenant bien fonctionné.
De retour de vacance depuis une semaine -qu'est ce que ça fait du bien ^^- J'ai encore et toujours le même soucis, je me permet donc de te montrer ma page voir si tu vois pas des abérations qui expliqueraient le soucis.
Lorsque je mets un addcc il me met que le mail est envoyé mais en fait je ne reçois rien, si je le supprime ça fonctionne. Autrement J'aimerais joindre un fichier dont le nom est composé de la date du jours seulement en faisant ainsi la piece jointe est 2009.txt et est vide. Si je joint une fichier test.txt ça fonctionne.
J'espère que vous pourrez m'aider, j'ai essayé entretemps d'autres façon sans succès, je ne suis vraiment pas doué avec les mails.
Merci!
$mail = new simplemail;
$mail -> addrecipient(toto@yahoo.fr','test');
$mail -> addcc('michu@yahoo.fr','Kevin michu');
$mail -> addfrom('tata@test.fr','');
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "inscriptions".$dtJours.".txt" );
$mail->sendmail();
Lorsque je mets un addcc il me met que le mail est envoyé mais en fait je ne reçois rien, si je le supprime ça fonctionne. Autrement J'aimerais joindre un fichier dont le nom est composé de la date du jours seulement en faisant ainsi la piece jointe est 2009.txt et est vide. Si je joint une fichier test.txt ça fonctionne.
J'espère que vous pourrez m'aider, j'ai essayé entretemps d'autres façon sans succès, je ne suis vraiment pas doué avec les mails.
Merci!
$mail = new simplemail;
$mail -> addrecipient(toto@yahoo.fr','test');
$mail -> addcc('michu@yahoo.fr','Kevin michu');
$mail -> addfrom('tata@test.fr','');
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "inscriptions".$dtJours.".txt" );
$mail->sendmail();
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
26 août 2009 à 17:12
26 août 2009 à 17:12
Bonsoir,
mise en oeuvre
<?php
include('classmail.php');
$mail = new simplemail;
$mail -> addrecipient("xxx@free.fr","test");
$mail -> addcc("xxx.x@free.fr");
$mail -> addbcc("xxx@free.fr");
$mail -> addfrom("tata@test.fr","");
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "toto.txt" );
$mail->sendmail();
?>
classmail.php corrigé
<?php
class simplemail {
var $recipientlist;
var $subject;
var $hfrom;
var $hbcc;
var $hcc;
var $Xsender;
var $ErrorsTo;
var $XMailer = 'PHP';
var $XPriority = 3;
var $set_mode='php';
var $text;
var $html;
var $attachement;
var $htmlattachement;
var $recipient;
var $body;
var $headers;
var $error_log;
var $connect;
var $default_charset = 'iso-8859-1';
var $B1B = "----=_001";
var $B2B = "----=_002";
var $B3B = "----=_003";
function simplemail() {
$this -> attachement = array();
$this -> htmlattachement = array();
}
function checkaddress($address) {
if ( preg_match('`([0-9a-zA-Z\.\-_ ]*@[0-9a-zA-Z\.\-_ ]*\.([a-z]{2,4}))`', $address) ) {
return TRUE;
} else {
$this->error_log("l'adresse $address est invalide"); return FALSE;
}
}
function checkname($name) {
if ( preg_match("`[0-9a-zA-Z\.\-_ ]*`" , $name ) ) {
return TRUE;
} else {
$this->error_log(" le pseudo $name est invalide\n"); return FALSE;
}
}
function makenameplusaddress($address,$name) {
if ( !$this->checkaddress($address) ) return FALSE;
if ( !$this->checkname($name) ) return FALSE;
if ( empty($name) ) { return $address; }
else { $tmp=$name." <".$address.">"; return $tmp; }
}
function addrecipient($newrecipient,$name='') {
$tmp=$this->makenameplusaddress($newrecipient,$name);
if ( !$tmp ) { $this->error_log(" To: error"); return FALSE; }
$this->recipientlist[] = array( 'mail'=>$newrecipient, 'nameplusmail' => $tmp );
return TRUE;
}
function addbcc($bcc,$name='') {
$tmp=$this->makenameplusaddress($bcc,$name);
if ( !$tmp ) { $this->error_log(" Bcc: error"); return FALSE; }
if ( !empty($this->hbcc)) $this->hbcc.= ",";
$this->hbcc.= $tmp;
return TRUE;
}
function addcc($cc,$name='') {
$tmp=$this->makenameplusaddress($cc,$name);
if ( !$tmp ) { $this->error_log(" Cc: error\n"); return FALSE; }
if (!empty($this->hcc)) $this->hcc.= ",";
$this->hcc.= $tmp;
return TRUE;
}
function addsubject($subject) {
if (!empty($subject)) $this->subject = $subject;
}
function addfrom($from,$name='') {
$tmp=$this->makenameplusaddress($from,$name);
if ( !$tmp ) { $this->error_log(" From: error"); return FALSE; }
$this->hfrom = $tmp;
return TRUE;
}
function addreturnpath($return) {
$tmp=$this->makenameplusaddress($return,'');
if ( !$tmp ) { $this->error_log("Return-Path: error"); return FALSE; }
$this->returnpath = $return;
return TRUE;
}
function addreplyto($replyto) {
$tmp=$this->makenameplusaddress($replyto,'');
if ( !$tmp ) { $this->error_log(" Reply-To: error"); return FALSE; }
$this->returnpath = $tmp;
return TRUE;
}
// les attachements
function addattachement($filename) {
array_push ( $this -> attachement , array ( 'filename'=> $filename ) );
}
// les attachements html
function addhtmlattachement($filename,$cid='',$contenttype='') {
array_push ( $this -> htmlattachement ,
array ( 'filename'=>$filename ,
'cid'=>$cid ,
'contenttype'=>$contenttype )
);
}
function writeattachement(&$attachement,$B) {
$message = '';
if ( !empty($attachement) ) {
foreach($attachement as $AttmFile){
$patharray = explode ("/", $AttmFile['filename']);
$FileName = $patharray[count($patharray)-1];
$message .= "\n--".$B."\n";
if (!empty($AttmFile['cid'])) {
$message .= "Content-Type: {$AttmFile['contenttype']};\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <{$AttmFile['cid']}>\n";
$message .= "Content-Disposition: inline;\n filename=\"".$FileName."\"\n\n";
} else {
$message .= "Content-Type: application/octetstream;\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n filename=\"".$FileName."\"\n\n";
}
$fd=fopen ($AttmFile['filename'], "rb");
$FileContent=fread($fd,filesize($AttmFile['filename']));
fclose ($fd);
$FileContent = chunk_split(base64_encode($FileContent));
$message .= $FileContent;
$message .= "\n\n";
}
$message .= "\n--".$B."--\n";
}
return $message;
}
function BodyLineWrap($Value) {
return wordwrap($Value, 78, "\n ");
}
function makebody() {
$message='';
if ( !$this->html && $this->text && !empty($this->attachement) ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
elseif ( !$this->html && $this->text && empty($this->attachement) ) {
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
}
elseif ( $this->html ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: multipart/related;\n\t boundary=\"".$this->B2B."\"\n\n";
//plaintext section
$message.="\n--".$this->B2B."\n";
$message.="Content-Type: multipart/alternative;\n\t boundary=\"".$this->B3B."\"\n\n";
//plaintext section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
// html section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$message.=chunk_split(base64_encode($this->html))."\n\n";
// end of text
$message.="\n--".$this->B3B."--\n";
// attachments html
if (empty($this->htmlattachement)) {
$message.="\n--".$this->B2B."--\n";
} else {
$message.=$this->writeattachement( $this->htmlattachement,$this->B2B);
}
// attachments
if (empty($this->attachement)) {
$message.="\n--".$this->B1B."--\n";
} else {
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
}
$this->body = $message;
return $message;
}
// Mail Headers Methods
function MakeHeaderField($Field,$Value) {
return wordwrap($Field.": ".$Value, 78, "\n ")."\r\n";
}
function AddField2Header($Field,$Value) {
$this->headers .= $this->MakeHeaderField($Field,$Value);
}
function makeheader() {
$this->headers = '';
if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; }
// else { $this->AddField2Header("To",$this->recipient); }
if ( empty($this->subject) ) {
$this->error_log("sujet manquant");
return FALSE;
} else {
if ($this->set_mode!='php' ) {
$this->AddField2Header("Subject", $this->subject);
}
}
# Date: Mon, 03 Nov 2003 20:48:06 +0100
$this->AddField2Header("Date", date ('r'));
if ( !empty($this->Xsender) ) { $this->AddField2Header("X-Sender",$this->Xsender); }
else { $this->AddField2Header("X-Sender",$this->hfrom); }
if ( !empty($this->ErrorsTo) ) { $this->AddField2Header("Errors-To",$this->ErrorsTo); }
else { $this->AddField2Header("Errors-To",$this->hfrom); }
if ( !empty($this->XMailer) ) $this->AddField2Header("X-Mailer",$this->XMailer);
if ( !empty($this->XPriority) ) $this->AddField2Header("X-Priority",$this->XPriority);
if ( !empty($this->hfrom) ) $this->AddField2Header("From",$this->hfrom);
if ( !empty($this->returnpath) ) $this->AddField2Header("Return-Path",$this->returnpath);
if ( !empty($this->replyto) ) $this->AddField2Header("Reply-To",$this->replyto);
$this->headers .="MIME-Version: 1.0\r\n";
if ( !$this->html && $this->text && !empty($this->attachement) ) {
$this->headers .= "Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
} elseif ( !$this->html && $this->text && empty($this->attachement) ) {
$this->headers .="Content-Type: text/plain; charset=us-ascii; format=flowed\r\n";
$this->headers .="Content-Transfer-Encoding: 7bit\r\n";
} elseif ( $this->html ) {
if ( !$this->text ) { $this->text="HTML only!"; }
$this->headers .="Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
}
if ( !empty($this->hcc) ) $this->AddField2Header("Cc",$this->hcc);
if ( !empty($this->hbcc) ) $this->AddField2Header("Bcc",$this->hbcc);
return $this->headers;
}
function sendmail() {
$this->makebody();
$this->makeheader();
switch($this->set_mode) {
case 'php' : $this->phpmail(); break;
case 'socket': $this->socketmailloop(); break;
}
return TRUE;
}
// Mail send by PHPmail
function phpmail() {
while ( list($key, $to) = each($this->recipientlist) ) {
$this->recipient = $to['mail'];
if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) {
$this->error_log("envoie vers {$to['nameplusmail']} réussi");
} else {
$this->error_log("envoie vers {$to['nameplusmail']} echoué");
}
}
return TRUE;
}
// Socket Function
function SocketStart() {
if (!$this->connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30)) {
$this->error_log("Could not talk to the sendmail server!"); return FALSE;
};
return fgets($this->connect, 1024);
}
function SocketStop() {
fclose($this->connect);
return TRUE;
}
function SocketSend($in,$wait='') {
fputs($this->connect, $in, strlen($in));
echo "-"; flush();
if(empty($wait)) {
$rcv = fgets($this->connect, 1024);
return $rcv;
}
return TRUE;
}
// Mail Socket
function socketmailstart() {
$this->SocketStart();
if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; }
else { $serv = $_SERVER['SERVER_NAME']; }
$this->SocketSend("HELO $serv\r\n");
}
function socketmailsend($to) {
$this->recipient = $to;
$this->error_log("Socket vers $to");
$this->SocketSend( "MAIL FROM:{$this->hfrom}\r\n" );
$this->SocketSend( "RCPT TO:$to\r\n" );
$this->SocketSend( "DATA\r\n" );
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n", 'NOWAIT' );
$this->SocketSend( $this->CleanMailDataString($this->body)."\r\n", 'NOWAIT' );
$this->SocketSend( ".\r\n" );
$this->SocketSend( "RSET\r\n" );
$this->error_log("Fin de l'envoi vers $to");
return TRUE;
}
function socketmailstop() {
$this->SocketSend("QUIT\r\n");
$this->SocketStop();
return TRUE;
}
function socketmailloop() {
$this->socketmailstart();
while ( list($key, $to) = each($this->recipientlist)) {
$this->recipient = $to['mail'];
$this->makeheader();
$this->socketmailsend($to['mail']);
}
$this->socketmailstop();
}
// Misc.
function error_log($msg='') {
if(!empty($msg)) {
$this->error_log .= $msg . "\r\n--\r\n";
return TRUE;
}
return " --- Error Log --- \r\n\r\n".$this->error_log;
}
function CleanMailDataString($data) {
$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);
$data = preg_replace("/\n\n/", "\n\r\n", $data);
$data = preg_replace("/\n\./", "\n..", $data);
return $data;
}
}
mise en oeuvre
<?php
include('classmail.php');
$mail = new simplemail;
$mail -> addrecipient("xxx@free.fr","test");
$mail -> addcc("xxx.x@free.fr");
$mail -> addbcc("xxx@free.fr");
$mail -> addfrom("tata@test.fr","");
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "toto.txt" );
$mail->sendmail();
?>
classmail.php corrigé
<?php
class simplemail {
var $recipientlist;
var $subject;
var $hfrom;
var $hbcc;
var $hcc;
var $Xsender;
var $ErrorsTo;
var $XMailer = 'PHP';
var $XPriority = 3;
var $set_mode='php';
var $text;
var $html;
var $attachement;
var $htmlattachement;
var $recipient;
var $body;
var $headers;
var $error_log;
var $connect;
var $default_charset = 'iso-8859-1';
var $B1B = "----=_001";
var $B2B = "----=_002";
var $B3B = "----=_003";
function simplemail() {
$this -> attachement = array();
$this -> htmlattachement = array();
}
function checkaddress($address) {
if ( preg_match('`([0-9a-zA-Z\.\-_ ]*@[0-9a-zA-Z\.\-_ ]*\.([a-z]{2,4}))`', $address) ) {
return TRUE;
} else {
$this->error_log("l'adresse $address est invalide"); return FALSE;
}
}
function checkname($name) {
if ( preg_match("`[0-9a-zA-Z\.\-_ ]*`" , $name ) ) {
return TRUE;
} else {
$this->error_log(" le pseudo $name est invalide\n"); return FALSE;
}
}
function makenameplusaddress($address,$name) {
if ( !$this->checkaddress($address) ) return FALSE;
if ( !$this->checkname($name) ) return FALSE;
if ( empty($name) ) { return $address; }
else { $tmp=$name." <".$address.">"; return $tmp; }
}
function addrecipient($newrecipient,$name='') {
$tmp=$this->makenameplusaddress($newrecipient,$name);
if ( !$tmp ) { $this->error_log(" To: error"); return FALSE; }
$this->recipientlist[] = array( 'mail'=>$newrecipient, 'nameplusmail' => $tmp );
return TRUE;
}
function addbcc($bcc,$name='') {
$tmp=$this->makenameplusaddress($bcc,$name);
if ( !$tmp ) { $this->error_log(" Bcc: error"); return FALSE; }
if ( !empty($this->hbcc)) $this->hbcc.= ",";
$this->hbcc.= $tmp;
return TRUE;
}
function addcc($cc,$name='') {
$tmp=$this->makenameplusaddress($cc,$name);
if ( !$tmp ) { $this->error_log(" Cc: error\n"); return FALSE; }
if (!empty($this->hcc)) $this->hcc.= ",";
$this->hcc.= $tmp;
return TRUE;
}
function addsubject($subject) {
if (!empty($subject)) $this->subject = $subject;
}
function addfrom($from,$name='') {
$tmp=$this->makenameplusaddress($from,$name);
if ( !$tmp ) { $this->error_log(" From: error"); return FALSE; }
$this->hfrom = $tmp;
return TRUE;
}
function addreturnpath($return) {
$tmp=$this->makenameplusaddress($return,'');
if ( !$tmp ) { $this->error_log("Return-Path: error"); return FALSE; }
$this->returnpath = $return;
return TRUE;
}
function addreplyto($replyto) {
$tmp=$this->makenameplusaddress($replyto,'');
if ( !$tmp ) { $this->error_log(" Reply-To: error"); return FALSE; }
$this->returnpath = $tmp;
return TRUE;
}
// les attachements
function addattachement($filename) {
array_push ( $this -> attachement , array ( 'filename'=> $filename ) );
}
// les attachements html
function addhtmlattachement($filename,$cid='',$contenttype='') {
array_push ( $this -> htmlattachement ,
array ( 'filename'=>$filename ,
'cid'=>$cid ,
'contenttype'=>$contenttype )
);
}
function writeattachement(&$attachement,$B) {
$message = '';
if ( !empty($attachement) ) {
foreach($attachement as $AttmFile){
$patharray = explode ("/", $AttmFile['filename']);
$FileName = $patharray[count($patharray)-1];
$message .= "\n--".$B."\n";
if (!empty($AttmFile['cid'])) {
$message .= "Content-Type: {$AttmFile['contenttype']};\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <{$AttmFile['cid']}>\n";
$message .= "Content-Disposition: inline;\n filename=\"".$FileName."\"\n\n";
} else {
$message .= "Content-Type: application/octetstream;\n name=\"".$FileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n filename=\"".$FileName."\"\n\n";
}
$fd=fopen ($AttmFile['filename'], "rb");
$FileContent=fread($fd,filesize($AttmFile['filename']));
fclose ($fd);
$FileContent = chunk_split(base64_encode($FileContent));
$message .= $FileContent;
$message .= "\n\n";
}
$message .= "\n--".$B."--\n";
}
return $message;
}
function BodyLineWrap($Value) {
return wordwrap($Value, 78, "\n ");
}
function makebody() {
$message='';
if ( !$this->html && $this->text && !empty($this->attachement) ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
elseif ( !$this->html && $this->text && empty($this->attachement) ) {
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
}
elseif ( $this->html ) {
//Messages start with text/html alternatives in OB
$message ="This is a multi-part message in MIME format.\n";
$message.="\n--".$this->B1B."\n";
$message.="Content-Type: multipart/related;\n\t boundary=\"".$this->B2B."\"\n\n";
//plaintext section
$message.="\n--".$this->B2B."\n";
$message.="Content-Type: multipart/alternative;\n\t boundary=\"".$this->B3B."\"\n\n";
//plaintext section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$message.=$this->BodyLineWrap($this->text)."\n\n";
// html section
$message.="\n--".$this->B3B."\n";
$message.="Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$message.=chunk_split(base64_encode($this->html))."\n\n";
// end of text
$message.="\n--".$this->B3B."--\n";
// attachments html
if (empty($this->htmlattachement)) {
$message.="\n--".$this->B2B."--\n";
} else {
$message.=$this->writeattachement( $this->htmlattachement,$this->B2B);
}
// attachments
if (empty($this->attachement)) {
$message.="\n--".$this->B1B."--\n";
} else {
$message.=$this->writeattachement($this->attachement,$this->B1B);
}
}
$this->body = $message;
return $message;
}
// Mail Headers Methods
function MakeHeaderField($Field,$Value) {
return wordwrap($Field.": ".$Value, 78, "\n ")."\r\n";
}
function AddField2Header($Field,$Value) {
$this->headers .= $this->MakeHeaderField($Field,$Value);
}
function makeheader() {
$this->headers = '';
if ( empty($this->recipientlist) ) { $this->error_log("destinataire manquant"); return FALSE; }
// else { $this->AddField2Header("To",$this->recipient); }
if ( empty($this->subject) ) {
$this->error_log("sujet manquant");
return FALSE;
} else {
if ($this->set_mode!='php' ) {
$this->AddField2Header("Subject", $this->subject);
}
}
# Date: Mon, 03 Nov 2003 20:48:06 +0100
$this->AddField2Header("Date", date ('r'));
if ( !empty($this->Xsender) ) { $this->AddField2Header("X-Sender",$this->Xsender); }
else { $this->AddField2Header("X-Sender",$this->hfrom); }
if ( !empty($this->ErrorsTo) ) { $this->AddField2Header("Errors-To",$this->ErrorsTo); }
else { $this->AddField2Header("Errors-To",$this->hfrom); }
if ( !empty($this->XMailer) ) $this->AddField2Header("X-Mailer",$this->XMailer);
if ( !empty($this->XPriority) ) $this->AddField2Header("X-Priority",$this->XPriority);
if ( !empty($this->hfrom) ) $this->AddField2Header("From",$this->hfrom);
if ( !empty($this->returnpath) ) $this->AddField2Header("Return-Path",$this->returnpath);
if ( !empty($this->replyto) ) $this->AddField2Header("Reply-To",$this->replyto);
$this->headers .="MIME-Version: 1.0\r\n";
if ( !$this->html && $this->text && !empty($this->attachement) ) {
$this->headers .= "Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
} elseif ( !$this->html && $this->text && empty($this->attachement) ) {
$this->headers .="Content-Type: text/plain; charset=us-ascii; format=flowed\r\n";
$this->headers .="Content-Transfer-Encoding: 7bit\r\n";
} elseif ( $this->html ) {
if ( !$this->text ) { $this->text="HTML only!"; }
$this->headers .="Content-Type: multipart/mixed;\r\n\t boundary=\"".$this->B1B."\"\r\n";
}
if ( !empty($this->hcc) ) $this->AddField2Header("Cc",$this->hcc);
if ( !empty($this->hbcc) ) $this->AddField2Header("Bcc",$this->hbcc);
return $this->headers;
}
function sendmail() {
$this->makebody();
$this->makeheader();
switch($this->set_mode) {
case 'php' : $this->phpmail(); break;
case 'socket': $this->socketmailloop(); break;
}
return TRUE;
}
// Mail send by PHPmail
function phpmail() {
while ( list($key, $to) = each($this->recipientlist) ) {
$this->recipient = $to['mail'];
if ( mail($to['mail'], $this->subject, $this->body, $this->makeheader() ) ) {
$this->error_log("envoie vers {$to['nameplusmail']} réussi");
} else {
$this->error_log("envoie vers {$to['nameplusmail']} echoué");
}
}
return TRUE;
}
// Socket Function
function SocketStart() {
if (!$this->connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30)) {
$this->error_log("Could not talk to the sendmail server!"); return FALSE;
};
return fgets($this->connect, 1024);
}
function SocketStop() {
fclose($this->connect);
return TRUE;
}
function SocketSend($in,$wait='') {
fputs($this->connect, $in, strlen($in));
echo "-"; flush();
if(empty($wait)) {
$rcv = fgets($this->connect, 1024);
return $rcv;
}
return TRUE;
}
// Mail Socket
function socketmailstart() {
$this->SocketStart();
if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; }
else { $serv = $_SERVER['SERVER_NAME']; }
$this->SocketSend("HELO $serv\r\n");
}
function socketmailsend($to) {
$this->recipient = $to;
$this->error_log("Socket vers $to");
$this->SocketSend( "MAIL FROM:{$this->hfrom}\r\n" );
$this->SocketSend( "RCPT TO:$to\r\n" );
$this->SocketSend( "DATA\r\n" );
$this->SocketSend( $this->CleanMailDataString($this->headers)."\r\n", 'NOWAIT' );
$this->SocketSend( $this->CleanMailDataString($this->body)."\r\n", 'NOWAIT' );
$this->SocketSend( ".\r\n" );
$this->SocketSend( "RSET\r\n" );
$this->error_log("Fin de l'envoi vers $to");
return TRUE;
}
function socketmailstop() {
$this->SocketSend("QUIT\r\n");
$this->SocketStop();
return TRUE;
}
function socketmailloop() {
$this->socketmailstart();
while ( list($key, $to) = each($this->recipientlist)) {
$this->recipient = $to['mail'];
$this->makeheader();
$this->socketmailsend($to['mail']);
}
$this->socketmailstop();
}
// Misc.
function error_log($msg='') {
if(!empty($msg)) {
$this->error_log .= $msg . "\r\n--\r\n";
return TRUE;
}
return " --- Error Log --- \r\n\r\n".$this->error_log;
}
function CleanMailDataString($data) {
$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);
$data = preg_replace("/\n\n/", "\n\r\n", $data);
$data = preg_replace("/\n\./", "\n..", $data);
return $data;
}
}
Bonjour,
j'ai repris ta classe au cas ou celle que j'avais n'était pas corrigé mais rien n'est changé, lorsque je mets une Cc ça ne fonctionne plus:
SMTP server response: 501 5.5.4 Invalid Address in simplemail.class.php on line 315
Ce qui est faux puisque si je la met en addrecipient cela fonctionne parfaitement.
Merci,
Bonne journée.
j'ai repris ta classe au cas ou celle que j'avais n'était pas corrigé mais rien n'est changé, lorsque je mets une Cc ça ne fonctionne plus:
SMTP server response: 501 5.5.4 Invalid Address in simplemail.class.php on line 315
Ce qui est faux puisque si je la met en addrecipient cela fonctionne parfaitement.
Merci,
Bonne journée.
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
27 août 2009 à 08:43
27 août 2009 à 08:43
Bonjour,
si l'adresse michu est correcte j'ai refais un test avec cette adresse en cc
eh non pas bonne
This is the mail system at host wmproxy1-g27.free.fr.
I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.
For further assistance, please send mail to postmaster.
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
The mail system
<michu@yahoo.fr>: host g.mx.mail.yahoo.com[209.191.118.103] said: 554 delivery
error: dd This user doesn't have a yahoo.fr account (michu@yahoo.fr) [-5] -
mta486.mail.mud.yahoo.com (in reply to end of DATA command)
si l'adresse michu est correcte j'ai refais un test avec cette adresse en cc
eh non pas bonne
This is the mail system at host wmproxy1-g27.free.fr.
I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.
For further assistance, please send mail to postmaster.
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
The mail system
<michu@yahoo.fr>: host g.mx.mail.yahoo.com[209.191.118.103] said: 554 delivery
error: dd This user doesn't have a yahoo.fr account (michu@yahoo.fr) [-5] -
mta486.mail.mud.yahoo.com (in reply to end of DATA command)
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
27 août 2009 à 09:05
27 août 2009 à 09:05
je viens de faire un nouveau test avec plusieurs de mes adresses, un coup en destinatiare et en cc
un deuxième test en inversant destinataire et cc
résultat : tout est OK
vérifie ça
<?php
include('classmail.php');
$mail = new simplemail;
$mail -> addrecipient("xxx@free.fr","test");
$mail -> addcc("xxx.x@free.fr");
$mail -> addbcc("xxx@free.fr"); // pas de deuxième paramètre//
$mail -> addfrom("tata@test.fr","");
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "toto.txt" );
$mail->sendmail();
?> --
J-L
un deuxième test en inversant destinataire et cc
résultat : tout est OK
vérifie ça
<?php
include('classmail.php');
$mail = new simplemail;
$mail -> addrecipient("xxx@free.fr","test");
$mail -> addcc("xxx.x@free.fr");
$mail -> addbcc("xxx@free.fr"); // pas de deuxième paramètre//
$mail -> addfrom("tata@test.fr","");
$mail -> addsubject('test mail avec cc et PJ');
// le message text
$mail -> text = 'Je suis le texte du mail';
// le message format html
$mail -> html ="text en html";
// une piece jointe.
$mail -> addattachement ( "toto.txt" );
$mail->sendmail();
?> --
J-L
Cela fonctionne, Merci beaucoup ça va embellir un peu cette fin de semaine.
J'ai un dernier léger soucis si je peux encore te déranger 5min si tu ne sais pas je vais essayer de me débrouiller autrement, en fait je dois extraire un fichier texte chaque jours de la liste des inscrits qui est envoyé par mail.
Jusqu'ici je n'ai pas de problème mais ce serait tout de même plus agréable que la PJ soit au format par exemple: extraction20090827.txt
$dateJour = date("Ymd");
Je créé donc le fichier ainsi
$fmbX = fopen ("extraction".$dateJour.".txt", "a");
J'ai essayé de ces deux façon il le fichier joint est 2009.txt et donc une pièce inexistante donc vide.
$mail -> addattachement ( "extraction".$dtExtract.".txt" );
$mail -> addattachement ( "extraction$dtExtract.txt" );
Je te remercie encore une fois!
J'ai un dernier léger soucis si je peux encore te déranger 5min si tu ne sais pas je vais essayer de me débrouiller autrement, en fait je dois extraire un fichier texte chaque jours de la liste des inscrits qui est envoyé par mail.
Jusqu'ici je n'ai pas de problème mais ce serait tout de même plus agréable que la PJ soit au format par exemple: extraction20090827.txt
$dateJour = date("Ymd");
Je créé donc le fichier ainsi
$fmbX = fopen ("extraction".$dateJour.".txt", "a");
J'ai essayé de ces deux façon il le fichier joint est 2009.txt et donc une pièce inexistante donc vide.
$mail -> addattachement ( "extraction".$dtExtract.".txt" );
$mail -> addattachement ( "extraction$dtExtract.txt" );
Je te remercie encore une fois!
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
27 août 2009 à 09:29
27 août 2009 à 09:29
je ne suis pas bien :
la pièce jointe à un nom. c'est ce nom qu'il faut mettre sinon effectivement le fichier n'est pas trouvé.
Il faut donc nommer le fichier correctement avant de le joindre.
<?php
rename($oldname,$newname);
?>
la pièce jointe à un nom. c'est ce nom qu'il faut mettre sinon effectivement le fichier n'est pas trouvé.
Il faut donc nommer le fichier correctement avant de le joindre.
<?php
rename($oldname,$newname);
?>
Pas grave je te dérange pas plus longtemps je vais me debrouiller sans.
Merci beaucoup pour ton aide,
Bonne journée!
Merci beaucoup pour ton aide,
Bonne journée!
J'utilise cette class mais j'ai remarqué une lenteur parfois dans la réception des mails...
J'ai donc envie de les faire passer par un autre serveur SMTP (google).
Auriez-vous une idée pour ajouter quelques informations à cette class pour l'envoi via un
serveur SMTP déterminé (adresse, port, etc..) ?
J'ai donc envie de les faire passer par un autre serveur SMTP (google).
Auriez-vous une idée pour ajouter quelques informations à cette class pour l'envoi via un
serveur SMTP déterminé (adresse, port, etc..) ?
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
6 sept. 2009 à 10:20
6 sept. 2009 à 10:20
bonjour,
je ne pense pas que l'on puisse maîtriser le chemin des messages car dès le départ, on passe par le serveur smtp de son FAI.
maintenant je peux me tromper mais je ne le pense pas.
je ne pense pas que l'on puisse maîtriser le chemin des messages car dès le départ, on passe par le serveur smtp de son FAI.
maintenant je peux me tromper mais je ne le pense pas.
Dans mon cas il s'agit d'un espace web hébergé sur un serveur mutualisé, ce qui - à mon avis - entraîne une certaine lenteur dans l'envoi des mails... d'où l'idée de forcer la connexion via un autre serveur.
J'ai trouvé ceci dans class.mail.php :
Il "suffirait donc de spécifier que le serveur SMTP dans SocketStart(); n'est pas localhost mais "mail.google.com" et aussi de préciser le port sécurisé (465), l'identifiant et le mot de passe de l'utilisateur. Mais je n'ai pas suffisemment de compétences en PHP pour y arriver :-)
J'ai trouvé ceci dans class.mail.php :
// Mail Socket function socketmailstart() { $this->SocketStart(); if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) { $serv = 'unknown'; } else { $serv = $_SERVER['SERVER_NAME']; } $this->SocketSend("HELO $serv\r\n"); }
Il "suffirait donc de spécifier que le serveur SMTP dans SocketStart(); n'est pas localhost mais "mail.google.com" et aussi de préciser le port sécurisé (465), l'identifiant et le mot de passe de l'utilisateur. Mais je n'ai pas suffisemment de compétences en PHP pour y arriver :-)
giheller
Messages postés
1875
Date d'inscription
dimanche 14 juin 2009
Statut
Membre
Dernière intervention
3 février 2024
142
6 sept. 2009 à 14:40
6 sept. 2009 à 14:40
je pense que lorsque vous envoyez un mail depuis votre site, vous utilisez le service smtp du serveur. c'est lui le point de départ, mêm si ensuite vous lui dites de passer par google.
essayez ce que vous proposez.
essayez ce que vous proposez.