Skip to content

Instantly share code, notes, and snippets.

@tech-nova
Last active March 10, 2016 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tech-nova/b443ca057c48d983494a to your computer and use it in GitHub Desktop.
Save tech-nova/b443ca057c48d983494a to your computer and use it in GitHub Desktop.
Pour envoyer un mail en utf-8 (code forgé à la main)
<?php
// Code utilisé pour envoyer un message utf8
// Sous deux formats (html + texte)
// Sans problème d'accent
// Les données injectées dans le mail proviennent d'une page encodée en utf_8
// via <meta charset>
$message_txt = sprintf(utf8_encode("Vous avez reçu un message depuis le formulaire de contact :
Expéditeur : %s %s
email : %s
-----------------------------
%s
")
, $_POST["nom"]
, $_POST["prenom"]
, $_POST["email"]
, $_POST["message"]
);
$message_html = sprintf(utf8_encode("<html>
<head>
</head>
<body>
<p>Vous avez reçu un message depuis le formulaire de contact : </p>
<br>
<table style=\"width : 600px\" border=\"0\">
<tbody>
<tr>
<th style=\"width : 100px; font-weight:bold;\" align=\"right\">Expéditeur : </th>
<td style=\"width : 100px\">%s %s </td>
</tr><tr>
<th style=\"width : 100px; font-weight:bold;\" align=\"right\">email : </th>
<td>%s </td>
</tr><tr>
<th style=\"width : 100px; font-weight:bold;\" align=\"right\" valign=\"top\">Message : </th>
<td>%s</td>
</tbody>
</table>
</body>
</html>")
, $_POST["nom"]
, $_POST["prenom"]
, $_POST["email"]
, str_replace("\r\n", "<br>", $_POST["message"])
);
$boundary = "-----=".md5(rand());
$boundary_alt = "-----=".md5(rand());
$subject = "Message depuis le site ASTRHA.org (envoyé à " . date('H\hi') . ")";
$from = '"Expéditeur du site Internet" <contact@nom_du_site.org>';
if (function_exists('iconv_mime_encode')) {
// Encoder proprement les éléments de type MIME
$preferences = array(
"input-charset" => "ISO-8859-1",
"output-charset" => "UTF-8",
"line-length" => 76,
"line-break-chars" => "\r\n"
);
$preferences["scheme"] = "Q";
$subject = substr(iconv_mime_encode("", $subject, $preferences), 2);
$from = substr(iconv_mime_encode("", $from, $preferences), 2);
}
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; charset="UTF-8"; boundary="' . $boundary . '"',
'Content-Transfer-Encoding: 8bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
$message = array(
'',
'This is a multi-part message in MIME format.',
'--' . $boundary,
'Content-Type: multipart/alternative;',
' boundary="' . $boundary_alt . '"',
'',
'--' . $boundary_alt,
'Content-Type: text/plain; charset="utf-8"',
'Content-Transfer-Encoding: 8bit',
'',
$message_txt,
'',
'--' . $boundary_alt,
'Content-Type: text/html; charset="utf-8"',
'Content-Transfer-Encoding: 8bit',
'',
$message_html,
'',
'--' . $boundary_alt . '--',
'--' . $boundary . '--'
);
// Envoi du message
if(!mail('contact@nom_du_site.org',
$subject,
implode("\r\n", $message),
implode("\r\n", $headers))) {
echo "ERREUR : l'e-mail n'a pas été envoyé";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment