/*---------------------------------------------------------------*/
|
/*
|
Titre : Envoyer un mail type MINE en HTML
|
|
URL : https://phpsources.net/code_s.php?id=427
|
Auteur : Mathias
|
Date édition : 16 Juil 2008
|
*/
|
/*---------------------------------------------------------------*/
|
|
|
// include MIME mailer class
|
|
|
class Mailer{
|
|
var $sender;
|
var $recipient;
|
var $subject;
|
var $headers=array();
|
|
function Mailer($sender,$recipient,$subject,$message){
|
// validate incoming parameters
|
if(!preg_match("/^.+@.+$/",$sender)){
|
trigger_error('Invalid value for email sender.',E_USER_ERROR);
|
}
|
|
if(!preg_match("/^.+@.+$/",$recipient)){
|
trigger_error('Invalid value for email recipient.',E_USER_ERROR);
|
}
|
|
if(!$subject||strlen($subject)>255){
|
trigger_error('Invalid length for email subject.',E_USER_ERROR);
|
}
|
|
if(!$message){
|
trigger_error('Invalid value for email message.',E_USER_ERROR);
|
}
|
|
$this->sender=$sender;
|
$this->recipient=$recipient;
|
$this->subject=$subject;
|
$this->message=$message;
|
|
// define some default MIME headers
|
$this->headers['MIME-Version']='1.0';
|
$this->headers['Content-Type']='multipart/mixed;boundary="MIME_BOUNDRY"';
|
$this->headers['From']='<'.$this->sender.'>';
|
$this->headers['Return-Path']='<'.$this->sender.'>';
|
$this->headers['Reply-To']=$this->sender;
|
$this->headers['X-Mailer']='PHP 4/5';
|
$this->headers['X-Sender']=$this->sender;
|
$this->headers['X-Priority']='3';
|
}
|
|
// create text part of the message
|
|
function buildTextPart(){
|
$mine = "--MIME_BOUNDRYnContent-Type: text/plain;";
|
$mine.= " charset=iso-8859-1nContent-Transfer-Encoding:";
|
$mine.= " quoted-printablennn".$this->message."\n\n";
|
return $mine;
|
}
|
|
// create message MIME headers
|
|
function buildHeaders(){
|
foreach($this->headers as $name=>$value){
|
$headers[]=$name.': '.$value;
|
}
|
return implode("\n",$headers)."\nThis is a multi-part message in MIME format.\n"
|
;
|
}
|
|
// add new MIME header
|
|
function addHeader($name,$value){
|
$this->headers[$name]=$value;
|
}
|
|
// send email
|
|
function send(){
|
$to=$this->recipient;
|
$subject=$this->subject;
|
$headers=$this->buildHeaders();
|
$message=$this->buildTextPart()."--MIME_BOUNDRY--\n";
|
|
if(!mail($to,$subject,$message,$headers)){
|
trigger_error('Error sending email.',E_USER_ERROR);
|
}
|
return true;
|
}
|
}
|
|
|
// create a new instance of the 'Mailer' class
|
|
$mailer=&new Mailer('nom@domain.com','mynom@domain.com','Test','hello');
|
|
// send MIME email message
|
|
if($mailer->send()){
|
|
echo 'Meesage envoyé avec succés.';
|
|
| } ?> |