Fork me on GitHub

Rapport de message :*
 

Re: L'envoi de newsletters / mails

Titre du sujet : Re: L'envoi de newsletters / mails
par overmysoul sur 02/10/2007 10:55:50

ca avec la classe en php5 que j ai c pas un souci
je vous la file d ailleurs car elle vaut le coup
define("BOUNDARY""--".md5(rand())); 

class 
CMail 
  public 
$from;        // votre email 
  
public $fromName;    // votre nom 
  
public $to;          // destinataire 
  
public $cc;          // copie à 
  
public $bcc;         // copie cachée à 
  
public $subject;     // sujet du mail 
  
public $priority;    // priorité 1-5 
  
public $returnPath;  // email utilisé pour la réponse 
  
public $notify;      // email pour notification 
  
public $message;     // texte du mail 
  
public $charset;     // tjeu de caractères, iso-8859-1 par défaut 
  
public $mime;        // type mime, text/plain par défaut 
  
public $debug;       // affichage ou non des erreurs 
  
public $debug_txt;   // messages d'erreurs 
   
  
protected $body
  protected 
$header
  protected 
$attachments = Array(); 

  
// Email priorities   
  
protected $priorities = Array( 
    
'1 (Highest)'
    
'2 (High)'
    
'3 (Normal)'
    
'4 (Low)'
    
'5 (Lowest)'); 
   
   
  
// --- constructeur 
  
public function __construct() { 
    
$this->clear(); 
  } 

  
// --- valeurs par défaut 
  
public function clear() { 
    
$this->mime        "text/plain"
    
$this->message     ""
    
$this->charset     "iso-8859-1"
    
$this->from        ""
    
$this->fromName    ""
    
$this->to          ""
    
$this->cc          ""
    
$this->bcc         ""
    
$this->subject     ""
    
$this->returnPath  ""
    
$this->notify      ""
    
$this->priority    0
    
$this->debug       FALSE
    
$this->clearAttachments(); 
  } 

  
// --- vérifie si la syntaxe d'une adresse email est valide 
  
public static function email_ok($email) { 
    return 
eregi("^([-!#$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+\.)+[a-zA-Z]{2,6}$"$email) != 0
    echo 
"ok";
  } 

  
// --- retourne le type MIME d'un fichier 
  
public static function getMimeType($file) { 
    static 
$mimeTypes = Array( 
      
'.gif'  => 'image/gif'
      
'.jpg'  => 'image/jpeg'
      
'.jpeg' => 'image/jpeg'
      
'.jpe'  => 'image/jpeg'
      
'.bmp'  => 'image/bmp'
      
'.png'  => 'image/png'
      
'.tif'  => 'image/tiff'
      
'.tiff' => 'image/tiff'
      
'.swf'  => 'application/x-shockwave-flash'
      
'.doc'  => 'application/msword'
      
'.xls'  => 'application/vnd.ms-excel'
      
'.ppt'  => 'application/vnd.ms-powerpoint'
      
'.pdf'  => 'application/pdf'
      
'.ps'   => 'application/postscript'
      
'.eps'  => 'application/postscript'
      
'.rtf'  => 'application/rtf'
      
'.bz2'  => 'application/x-bzip2'
      
'.gz'   => 'application/x-gzip'
      
'.tgz'  => 'application/x-gzip'
      
'.tar'  => 'application/x-tar'
      
'.zip'  => 'application/zip'
      
'.rar'  => 'application/rar'
      
'.js'   => 'text/javascript'
      
'.html' => 'text/html'
      
'.htm'  => 'text/html'
      
'.txt'  => 'text/plain'
      
'.css'  => 'text/css' 
    
); 

    
$att StrRChr(StrToLower($file), "."); 
    if(!IsSet(
$mimeTypes[$att])) 
      return 
"application/octet-stream"
    else 
      return 
$mimeTypes[$att]; 
  } 

  
// --- supprime les pièces jointes 
  
public function clearAttachments() { 
    
$this->attachments = Array(); 
  } 

  
// --- ajout d'une pièce jointe encodée en base64 
  // @param $filename     nom du fichier sur le serveur 
  // @param $inner_name   nom du fichier affiché dans l'email 
  // @param $mime         type mime 
  
public function addAttachment($filename$inner_name=""$mime="") { 
    if(!
file_exists($filename)) 
      
$this->debug_txt .= "Fichier filename non trouvé"

    if(!
is_readable($filename)) 
      
$this->debug_txt .= "Fichier filename inaccessible"

    
$fp = @fopen($filename"r"); 
    if(!
$fp
      
$this->debug_txt .= "Impossible d'ouvrir le fichier $filename"

    
// --- nom de fichier à afficher non précisé, on 
    // prend le nom du fichier ou est stockée la pièce jointe 
    
if($inner_name == ""
      
$inner_name basename($filename); 

    
// --- type mime non précisé, on le détermine à 
    // partir du nom du fichier 
    
if($mime == ""
      
$mime $this->getMimeType($inner_name); 

    
$attachment ""

    
$attachment .= "nn--".BOUNDARY."n"
    
$attachment .= "Content-Transfer-Encoding: base64n"
    
$attachment .= "Content-Type: $mime; name="".$inner_name
    ""; charset="
us-ascii"n"
    
$attachment .= "Content-Disposition: attachment;  
    filename="".
$inner_name.""nn"
    
$attachment .= chunk_split(base64_encode(@fread($fp, @filesize($filename)))); 

    
array_push($this->attachments$attachment); 

    @
fclose($fp); 
  } 

  
// --- envoi du mail 
  /*  
  @param $emailfile si spécifié,  
  le mail sera également envoyé en pièce jointe 
  */ 
  
public function send($emailfile "") { 
    
$this->body        ""
    
$this->header      ""

    if(
strlen($this->from)) 
      if(!
$this->email_ok($this->from)) 
        
$this->debug_txt .= "From: ".$this->from." n'est pas un email valide"

    if(
strlen($this->returnPath)) { 
      if(!
$this->email_ok($this->returnPath)) 
        
$this->debug_txt .= "Return Path ".$this->returnPath."  
        n'est pas un email valide"

      
$this->header .= "Return-path: <".$this->returnPath.">n"
    } 

    if(
strlen($this->from)) 
      
$this->header .= "From: ".$this->fromName." <".$this->from.">n"

    
$ok $this->email_ok($this->to); 
    if(!
$ok$this->debug_txt .= "Email To: $$this->to n'est pas un email valide"

    if(!Empty(
$this->cc)) { 
      
$ok $this->email_ok($this->cc); 
      if(!
$ok$this->debug_txt .= "Email Cc: $invalidEmail n'est pas valide"
      
$this->header .= "Cc: "
      
$this->header .= is_array($this->cc) ? implode(", "$this->cc) : $this->cc
      
$this->header .= "n"
    } 
       
    if(!Empty(
$this->bcc)) { 
      
$ok $this->email_ok($this->bcc); 
      if(!
ok$this->debug_txt .= "Email Bcc: $invalidEmail  n'est pas valide"
       
$this->header .= "Bcc: "
      
$this->header .= is_array($this->bcc) ? implode(", "$this->bcc) : $this->bcc
      
$this->header .= "n"
    } 

    
$this->header .= "Mime-Version: 1.0n"
     
    if(
IntVal($this->notify) == 1
      
$this->header .= "Disposition-Notification-To: <".$this->from.">n"
    else if(
strlen($this->notify)) 
      
$this->header .= "Disposition-Notification-To: <".$this->notify.">n"
       
    if(!Empty(
$this->attachments)) { 
      
// header with attachments 
     
$this->header .= "Content-Type: multipart/mixed; boundary="".BOUNDARY.""n"
     
$this->header .= "Content-Transfer-Encoding: 7bitn"
     
$this->body   .= "This is a multi-part message in MIME format.nn"
    } 
    else { 
     
// header with no attachments 
     
$this->header .= "Content-Transfer-Encoding: 8bitn"
     
$this->header .= "Content-Type: ".$this->mime."; charset="".$this->charset.""".(Empty($emailfile) ? "" " name="$emailfile"")."n"
     
$this->body   .= $this->message
    } 

    if(
$this->priority
     
$this->header .= "X-Priority: ".$this->priorities[$this->priority]."n"
     
    if(!Empty(
$this->attachments)) { 
     
$this->body .= "nn--".BOUNDARY."n"
     
$this->body .= "Content-Transfer-Encoding: 8bitn"
     
$this->body .= "Content-Type: ".$this->mime."; charset="".$this->charset.""".(Empty($emailfile) ? "" " name="$emailfile"")."n"
     
$this->body .= "Mime-Version: 1.0nn"
     
$this->body .= $this->message."nn"

     
reset($this->attachments); 
     while(list(
$key$attachment) = each($this->attachments)) { 
       
$this->body .= $attachment
     } 
       
     
// --- fin du mail 
     
$this->body .= "nn--".BOUNDARY."--"
   } 
     
   
// --- texte pour deboguage 
   
if($this->debug) { 
     echo 
"<pre>"
     echo 
"nTOn".HTMLSpecialChars($this->to); 
     echo 
"nSUBJECTn".HTMLSpecialChars($this->subject); 
     echo 
"nBODYn".HTMLSpecialChars($this->body); 
     echo 
"nHEADERn".HTMLSpecialChars($this->header); 
     echo 
"</pre>"
   } 

   
// --- envoi à plusieurs personnes si le 
   // paramètre est un tableau 
   
if(is_array($this->to)) { 
     
reset($this->to); 
     while(list(
$key$val) = each($this->to)) { 
       
$this->sendTo($val); 
     } 
   } 
   else { 
     
$this->sendTo($this->to); 
   } 
 } 
   
 protected function 
sendTo($to) { 
   if(!@
mail($to$this->subject$this->body$this->header)) { 
     
$this->debug_txt .= "PHP::Mail() Erreur d'envoi du mail $to"
     }
     else
     {return 
TRUE;}
 }  


class 
verification_email 
  var 
$system_OS "linux";//système d'exploitation (win/linux) 
  
var $debug 0;// textes de mise au point 
  
var $check_result;  // résultats 

  
function teste_email($email) { 
    
$email strtolower($email); 
    
// --- vérification de la syntaxe 
    
$ok $this->check_email($email); 
    if (!
$ok) return FALSE
    
// --- decomposition de l'adresse 
    
list($userName$hostName) = split("@"$email); 
    
// --- on teste l'enregistrement MX 
    
if (!$this->checkDNS($hostName)) { 
      
$this->check_result .= "Pas d'enregistrement MX"
      return 
FALSE
    } 
    else { 
      
$this->check_result .= "Enregistrement MX OK"
      return 
TRUE
    } 
  } 

  
// --- vérification de la syntaxe de l'adresse email 
  
function check_email($email) { 
    if (
preg_match('/^[-!#$%&'*+\./0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+.)+([0-9A-Z]){2,4}$/i', $email)) { 
      
$this->check_result .= "Syntaxe OK"; 
      return TRUE; 
    } 
    else { 
      
$this->check_result .= "Erreur de syntaxe"; 
      return FALSE; 
    } 
  } 

  // vérifie si l'enregistrement MX existe pour le domaine 
  function checkDNS(
$hostName) { 
    // --- linux 
    if (
$this->system_OS == "linux") { 
      if (checkdnsrr(
$hostName,"MX")) return TRUE; 
      else return FALSE; 
    } 
    // --- windows 
    else { 
      if(!empty(
$hostName)) { 
        exec("nslookup -type=MX ".
$hostName$result); 
        // vérifie chaque ligne  pour voir si l'une d'entre elles 
        // commence par le nom de domaine 
        // si c'est le cas, la vérification est positive 
        // sinon, c'est qu'il n'y a pas d'enregistrement 
        // MX pour le domaine 
        foreach (
$result as $line) { 
          if(eregi("^".
$hostName,$line)) return true; 
        } 
        return false; 
      } 
      return false; 
    } 
  } 
}


Après pour envoyer le mail rien de plus simple :
$ok $cl_email->teste_email($email_t); 
    if (
$ok)
    {
    include(
"./inc/header_mail.php");
    include(
"./inc/footer_mail.php");
    
$Mail = new CMail
    
$Mail->from     $general_mail
    
$Mail->fromName $lang_from_mail_doc
    
$Mail->to       $email_t
    
$Mail->subject  $lang_titre_mail_doc
    
$Mail->message  $header_mail;
    
$Mail->message  .= $lang_texte_mail_doc;
    
$Mail->message  .= $footer_mail;
    
$Mail->charset  "iso-8859-2"
    
$Mail->mime     "text/html"
    
$Mail->AddAttachment("./Dossier/fichier.pdf"); 
    
$Mail->Send(); 
}


Alors pour expliquer vite fait :

$email_t c a qui on expédit
$general_mail : c est votre mail
$lang_from_mail_doc : votre nom
$Mail->subject => votre sujet
$Mail->message => le corps de texte. Perso pour les mail en html je creer un template que je mais dans deux fichiers (le header et le footer) apres y a plus qu a insérer votre message au centre avec l encapsulation.

Bon courage a celui qui veut se lancer !!!!!!
Propulsé avec XOOPS | Graphisme adapté par Tatane, Grosdunord, Montuy337513

11 Personne(s) en ligne (3 Personne(s) connectée(s) sur Forum) | Utilisateur(s): 0 | Invité(s): 11 | Plus ...