Envoyer un mail HTML avec pièces jointes
Il n'est pas rare d'avoir besoin d'une fonctionnalité d'envoi de mail sur un site, mais la fonction mail() du php est assez compliquée à utiliser lorsqu'on veut faire un mail élaboré.Donc, j'ai créé une classe qui permet d'envoyer soit un mail simple (du texte) soit un mail HTML avec (ou sans) pièces jointes.
Bien entendu, j'y ai intégré la possibilité de mettre un ou plusieurs destinataires en copie (cachée ou pas).
Voici la classe:
<?
class phpmail {
/*** Classe phpmail ***/
var $sender;
var $receiver;
var $subject;
var $body;
function phpmail() {
// Constructeur de l'objet, on définit les champs par défaut
$sender = "Admin <admin@c-p-f.org>"; // Expéditeur
$receiver = "Admin <test@c-p-f.org>"; // Destinataire
$subject = "(No subject)"; // Sujet
$body = "Empty mail"; // Corps du texte
// Constructeur de la classe
$this->SetFrom($sender);
$this->SetReplyTo($sender);
$this->SetTo($receiver);
$this->SetSubject($subject);
$this->SetPlainText($body);
$this->error = ""; // Initialisation des erreurs
}
function SetFrom($sender) {
// Modification de l'expéditeur
$this->from = "From: $sender\r\n";
}
function SetReplyTo($replyto) {
// Modification de l'adresse de réponse
$this->replyto = "Reply-To: $replyto\r\n";
}
function SetTo($receiver) {
// Modification du destinataire
$this->to = $receiver;
}
function AddCc($carbon) {
// Ajout de Cc (Copie Carbon)
if (!isset($this->cc) || ($this->cc == "")) {
// Le premier Cc, il faut définir le champ
$this->cc = "Cc: $carbon\r\n";
} else {
$this->cc .= ", $carbon\r\n";
}
}
function AddBcc($bcarbon) {
// Ajout de Bcc (Blind Copie Carbon)
if (!isset($this->bcc) || ($this->bcc == "")) {
// Le premier Bcc, il faut définir le champ
$this->bcc = "Bcc: $bcarbon\r\n";
} else {
$this->bcc .= ", $bcarbon\r\n";
}
}
function SetSubject($subject) {
// Modifier le sujet est une bonne idée
$this->subject = $subject;
}
function SetPlainText($body) {
// Bien sur, on peut envoyer un texte normal
$this->text = "Content-Type: text/plain; charset=\"iso-8859-15\"\n";
$this->text .= "Content-Transfer-Encoding: 8bit\n\n";
$this->text = htmlentities($body)."\n\n";
}
function SetHtmlText($body) {
// Mais aussi un texte HTML
$this->html = "Content-Type: text/html; charset=\"iso-8859-15\"\n";
$this->html .= "Content-Transfer-Encoding: 8bit\n\n";
$this->html .= $body."\n\n";
}
function AddAttachment($filename) {
// Le plus important, l'ajout des pièces jointes
if (file_exists($filename)) {
$this->attfile[] = $filename;
} else {
$this->error .= "$filename est introuvable\n";
}
}
function SendMail() {
// La fonction d'envoi
$body = "";
// Création des entêtes: emetteur et destinataires
$headers = $this->from;
$headers .= $this->replyto;
$headers .= $this->cc;
$headers .= $this->bcc;
if (isset($this->html) || isset($this->attfile)) {
// Nous avons du HTML ou des pièces jointes, il faut les entêtes adéquats
$boundary = md5(uniqid(rand()));
$headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=$boundary;\n\n";
if (isset($this->html)) {
// HTML: on ajoute la partie HTML
$body .= "--$boundary\n".$this->html;
}
if (isset($this->attfile)) {
foreach ($this->attfile as $file) {
$this->att_type = mime_content_type($file);
$name = substr($file,strrpos($file, "/") 1);
$body .= "--$boundary\n";
$body .= "Content-Type: $this->att_type; name=\"$name\"\n";
$body .= "Content-Transfer-Encoding: base64\n\n";
$Inf = fopen($file, "r");
$data = fread($Inf, filesize($file));
fclose($Inf);
$body .= chunk_split(base64_encode($data))."\n\n";
}
}
$body .= "--$boundary--";
} else {
$body = $this->text;
}
if ($this->error == "") {
if (!mail($this->to, $this->subject, $body, $headers)) die("Erreur");
}
}
}
/*** EXEMPLE D'UTILISATION ***/
$mon_mail = new phpmail();
$mon_mail->SetTo("CrazyCat <crazycat@c-p-f.org>");
$mon_mail->AddCc("info <info@domain.tld>");
$mon_mail->SetHtmlText("<html>\n<body><h1>Test</h1><h2>Réussi</h2></body>\n</html>\n");
$mon_mail->AddAttachment("./image.jpg");
$mon_mail->AddAttachment("./documents/mon_pdf.pdf");
$mon_mail->SendMail();
?>
class phpmail {
/*** Classe phpmail ***/
var $sender;
var $receiver;
var $subject;
var $body;
function phpmail() {
// Constructeur de l'objet, on définit les champs par défaut
$sender = "Admin <admin@c-p-f.org>"; // Expéditeur
$receiver = "Admin <test@c-p-f.org>"; // Destinataire
$subject = "(No subject)"; // Sujet
$body = "Empty mail"; // Corps du texte
// Constructeur de la classe
$this->SetFrom($sender);
$this->SetReplyTo($sender);
$this->SetTo($receiver);
$this->SetSubject($subject);
$this->SetPlainText($body);
$this->error = ""; // Initialisation des erreurs
}
function SetFrom($sender) {
// Modification de l'expéditeur
$this->from = "From: $sender\r\n";
}
function SetReplyTo($replyto) {
// Modification de l'adresse de réponse
$this->replyto = "Reply-To: $replyto\r\n";
}
function SetTo($receiver) {
// Modification du destinataire
$this->to = $receiver;
}
function AddCc($carbon) {
// Ajout de Cc (Copie Carbon)
if (!isset($this->cc) || ($this->cc == "")) {
// Le premier Cc, il faut définir le champ
$this->cc = "Cc: $carbon\r\n";
} else {
$this->cc .= ", $carbon\r\n";
}
}
function AddBcc($bcarbon) {
// Ajout de Bcc (Blind Copie Carbon)
if (!isset($this->bcc) || ($this->bcc == "")) {
// Le premier Bcc, il faut définir le champ
$this->bcc = "Bcc: $bcarbon\r\n";
} else {
$this->bcc .= ", $bcarbon\r\n";
}
}
function SetSubject($subject) {
// Modifier le sujet est une bonne idée
$this->subject = $subject;
}
function SetPlainText($body) {
// Bien sur, on peut envoyer un texte normal
$this->text = "Content-Type: text/plain; charset=\"iso-8859-15\"\n";
$this->text .= "Content-Transfer-Encoding: 8bit\n\n";
$this->text = htmlentities($body)."\n\n";
}
function SetHtmlText($body) {
// Mais aussi un texte HTML
$this->html = "Content-Type: text/html; charset=\"iso-8859-15\"\n";
$this->html .= "Content-Transfer-Encoding: 8bit\n\n";
$this->html .= $body."\n\n";
}
function AddAttachment($filename) {
// Le plus important, l'ajout des pièces jointes
if (file_exists($filename)) {
$this->attfile[] = $filename;
} else {
$this->error .= "$filename est introuvable\n";
}
}
function SendMail() {
// La fonction d'envoi
$body = "";
// Création des entêtes: emetteur et destinataires
$headers = $this->from;
$headers .= $this->replyto;
$headers .= $this->cc;
$headers .= $this->bcc;
if (isset($this->html) || isset($this->attfile)) {
// Nous avons du HTML ou des pièces jointes, il faut les entêtes adéquats
$boundary = md5(uniqid(rand()));
$headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=$boundary;\n\n";
if (isset($this->html)) {
// HTML: on ajoute la partie HTML
$body .= "--$boundary\n".$this->html;
}
if (isset($this->attfile)) {
foreach ($this->attfile as $file) {
$this->att_type = mime_content_type($file);
$name = substr($file,strrpos($file, "/") 1);
$body .= "--$boundary\n";
$body .= "Content-Type: $this->att_type; name=\"$name\"\n";
$body .= "Content-Transfer-Encoding: base64\n\n";
$Inf = fopen($file, "r");
$data = fread($Inf, filesize($file));
fclose($Inf);
$body .= chunk_split(base64_encode($data))."\n\n";
}
}
$body .= "--$boundary--";
} else {
$body = $this->text;
}
if ($this->error == "") {
if (!mail($this->to, $this->subject, $body, $headers)) die("Erreur");
}
}
}
/*** EXEMPLE D'UTILISATION ***/
$mon_mail = new phpmail();
$mon_mail->SetTo("CrazyCat <crazycat@c-p-f.org>");
$mon_mail->AddCc("info <info@domain.tld>");
$mon_mail->SetHtmlText("<html>\n<body><h1>Test</h1><h2>Réussi</h2></body>\n</html>\n");
$mon_mail->AddAttachment("./image.jpg");
$mon_mail->AddAttachment("./documents/mon_pdf.pdf");
$mon_mail->SendMail();
?>