Envoyer un mail type MINE en HTML

L'une des taches les plus communes pour un programmeur PHP est qu'il doit faire face au développement d'applications qui envoient des mails MIME. En termes simples, le MIME consiste en une extension de la technologie traditionnelle et cette classe vous sera très pratique pour envoyer des mails au format HTML avec la possibilité d'envoyer des fichiers en pièces jointes dans une grande variété de formats.

9 842  vues
Compatibilité du code
PHP 7
  code classé dans   Classes
  code source classé dans   Classes
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                    
<?php
/*------------------------------*/
/*
Titre : Envoyer un mail type MINE en HTML

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.';

} ?>
<?php
/*------------------------------*/
/*
Titre : Envoyer un mail type MINE en HTML

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.';

} ?>

      Fonctions du code - Doc officielle PHP

   php.net  
Description
Versions PHP
    array
Crée un tableau
PHP 4, 5, 7 et 8
    echo
Affiche une chaîne de caractères
PHP 4, 5, 7 et 8
    implode
Rassemble les éléments d'un tableau en une chaîne
PHP 4, 5, 7 et 8
    mail
Envoi de mail
PHP 4, 5, 7 et 8
    preg_match
Effectue une recherche de correspondance avec une expression rationnelle standard
PHP 4, 5, 7 et 8
    strlen
Calcule la taille d'une chaîne
PHP 4, 5, 7 et 8
    trigger_error
Déclenche une erreur utilisateur
PHP 4, 5, 7 et 8
Minimum 10 mots. Votre commentaire sera visible après validation.


 Autres snippets qui pourraient vous intéresser

Connaitre avec PHP le type de navigateur

Compatibilité : PHP 5, PHP 7, PHP 8

Connaitre le type de navigateur du client avec la fonction getenv () qui retourne la valeur d'une variable d'environnement.

Validation d'une adresse mail

Compatibilité : PHP 5, PHP 7, PHP 8

Voici 2 manières de filtrer une adresse email afin de savoir si la syntaxe de l'adresse email est valide ou invalide. Avec la fonction filter_var () ou un regex.

* Requêtes exécutées avec Recherche Contextuelle
avatar

Mathias

  16 Juil 2008

  SOURCE   Télécharger
9 842 Vues
Compatibilité du code
PHP 7