Vous êtes ici Codes PHP et MySQL > Classes > Envoyer un mail type MINE en HTML

 

Envoyer un mail type MINE en HTML

L'une des tâches 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.
 
Mathias
Code vu 3840 fois
Enregistré le 16 Juil 2008
  • Digg ce code sur digg.com
  • Bookmark ce code sur del.icio.us
  • Bookmark ce code sur Google
  • Bookmark ce code sur Yahoo
  • Ajoute Envoyer un mail type MINE en HTML
  • Partage ce code sur Facebook
 
 
 
 

Code Source


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.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
 
<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Envoyer un mail type MINE en HTML                         */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts427-PHP.htm              */
/* 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

: Crée un tableau - (PHP 4, PHP 5)
: Expression rationnelle standard - (PHP 4, PHP 5)
: Déclenche une erreur utilisateur - (PHP 4 >= 4.0.1, PHP 5)
: Calcule la taille d'une chaîne - (PHP 4, PHP 5)
: Rassemble les éléments d'un tableau en une chaîne - (PHP 4, PHP 5)
: Envoi de mail - (PHP 4, PHP 5)
: Affiche une chaîne de caractères - (PHP 4, PHP 5)
Ajouter un commentaire
Code de sécurité

Attention: Les champs marqués d'une étoile * sont obligatoires
 
Librairie PHP

Connexion

 
 

Classes

 
 

PHP

 
 
 
PHP 5 ; MySQL 4 Livre PHP

PHP 5 ; MySQL 4
 
    Offres d'emploi

Plus de 500 offres d'emploi PHP/MySQL

Offres d'emploi
 
        Publicité