I have a rather simple function to send an email. I started to implement translated versions of the email and with this came special characters such as é and ó. Whenever I have those in the subject of the email, the email creates trouble by causing BAD_HEADER errors in my amavis.
Apparently it is not 8bit encoded, which makes sense at first. However, i can't find anywhere on the net any guide or explanation how to encode the subject properly.
Just for fun I tried é instead of é, and of course the problem was handled. but at the same time the email arrived with é in the subject, instead of é.
Here is the script I have currently:
function sendEmail() {
// Build HTML version
ob_start();
include('emailhtml.php');
$msgHTML = ob_get_contents();
ob_end_clean();
// Build TXT version
ob_start();
include('email.php');
$msgTxt = ob_get_contents();
ob_end_clean();
// Subject & headers
$subject = "áéíóú";
$boundary = md5(uniqid(rand()));
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/alternative; boundary = ".$boundary;
$headers[] = "From: ".$from." <".$from_email.">";
$headers[] = "Reply-To: ".$reply2_email;
// Plain text version of message
$body = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgTxt));
// HTML version of message
$body .= "--$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgHTML));
$body .= "--$boundary--\r\n";
// BAM! Shoot it off...
mail($receiver, $subject, $body, implode("\r\n", $headers));
}