Astuces php
Accueil
  News 
 Scripts 
 Bouquins 
 Newsletter 
  Emplois 
 Annuaire 
 Forum 
 
Offres d'emplois
Formateur confirmé PHP5/MySQL
Ip-formation, école d'informatique depuis 13 ans, recherche un formateur confirmé en PHP5 / MySQL. Missions principales : - Assurer les cours...
 
 C'est ici
 
Avez vous lu ?
PHP 5 Astuces d'experts
Avez vous lu ? -> PHP 5 Astuces d'experts : Cet ouvrage, utile et pratique, délivre aux utilisateurs initiés des astuces toutes plus utiles les unes que les autres sur PHP 5. Il permet entre autre d’apprendre à envoyer un e-mail aux formats texte et HTML, de récupérer la date de la dernière visite d’un utilisateur, de générer un cookie, de...
 
 Commander
 
 Chapitres - e-mail
PHP 5 Astuces d'experts -> e-mail -> Envoyez un mail à plusieurs destinataires Envoyez un mail à plusieurs destinataires
PHP 5 Astuces d'experts -> e-mail -> Envoyez un mail avec une pièce jointe Envoyez un mail avec une pièce jointe
PHP 5 Astuces d'experts -> e-mail -> Envoyer un mail au format HTML Envoyer un mail au format HTML
PHP 5 Astuces d'experts -> e-mail -> Un fichier externe pour déterminer la liste des destinataires Un fichier externe pour déterminer la liste des destinataires
PHP 5 Astuces d'experts -> e-mail -> Une classe pour contrôler les emails Une classe pour contrôler les emails
PHP 5 Astuces d'experts -> e-mail -> Une classe pour envoyer des emails Une classe pour envoyer des emails
PHP 5 Astuces d'experts -> e-mail -> Gérer les comptes Imap Gérer les comptes Imap
 Une classe pour envoyer des emails
<?
require_once("classe_envoi_mail.php");
$Mail = new CMail;
$Mail->from     "test@test.com";
$Mail->fromName "Mon nom";
$Mail->to       = Array("email1@test.com""email2@test.com");
$Mail->subject  "Sujet du mail";
$Mail->message  "<HTML><BODY>ceci est un test</BODY></HTML>";
$Mail->charset  "iso-8859-2";
$Mail->mime     "text/html";

$Mail->AddAttachment("mail.php""fichier.txt");
$Mail->Send();
?>

<?php

// --- classe pour envoyer des emails
// avec gestion des pièces jointes

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;
  }

  
// --- 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 .= "\n\n--".BOUNDARY."\n";
    
$attachment .= "Content-Transfer-Encoding: base64\n";
    
$attachment .= "Content-Type: $mime; name=\"".$inner_name.
    
"\"; charset=\"us-ascii\"\n";
    
$attachment .= "Content-Disposition: attachment; 
    filename=\""
.$inner_name."\"\n\n";
    
$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.0\n";
    
    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: 7bit\n";
     
$this->body   .= "This is a multi-part message in MIME format.\n\n";
    }
    else {
     
// header with no attachments
     
$this->header .= "Content-Transfer-Encoding: 8bit\n";
     
$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 .= "\n\n--".BOUNDARY."\n";
     
$this->body .= "Content-Transfer-Encoding: 8bit\n";
     
$this->body .= "Content-Type: ".$this->mime."; charset=\"".$this->charset."\"".(Empty($emailfile) ? "" " name=\"$emailfile\"")."\n";
     
$this->body .= "Mime-Version: 1.0\n\n";
     
$this->body .= $this->message."\n\n";

     
reset($this->attachments);
     while(list(
$key$attachment) = each($this->attachments)) {
       
$this->body .= $attachment;
     }
      
     
// --- fin du mail
     
$this->body .= "\n\n--".BOUNDARY."--";
   }
    
   
// --- texte pour deboguage
   
if($this->debug) {
     echo 
"<pre>";
     echo 
"\nTO\n".HTMLSpecialChars($this->to);
     echo 
"\nSUBJECT\n".HTMLSpecialChars($this->subject);
     echo 
"\nBODY\n".HTMLSpecialChars($this->body);
     echo 
"\nHEADER\n".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";
 } 
}
?>
 Rechercher
Tapez un mot ou une phrase clé
 

 Téléchargement
Téléchargez les sources de l'ouvrage
PHP 5 solutions et composants open-source
Téléchargez les sources de l'ouvrage  -> PHP 5 solutions et composants open-source : Boostez et améliorez vos sites PHP avec des composants et extensions !

Voici enfin l'ouvrage pour améliorer votre site sans tout réécrire. Pourquoi réinventer la roue puisqu'il existe une multitude de composants indépendants à intégrer à votre site ou même des extensions au langage PHP qui peu...
 


copyright astuces-php.info - Plan du site - Mylinea.org : l'annuaire des annuaires - échange de liens - Faites du fric