php utf-8 emails with national characters

I'm facing an interesting issue and am not sure where to look for the solution.

Here are the details:

(1) php code is used to send an html message.

(2) the message contains Hebrew characters.

(3) when the message is received at the other end (a gmail account) by an auto parser, all the Hebrew characters appear as question marks and the parser fails.

(4) when the message is sent to any other destination (also gmail), being opened and then forwarded to the parser, the Hebrew chars are correctly received and parsed.

(5) when the message is auto-forwarded from other email accounts to the parser, the Hebrew chars appear as question marks again.

Relevant part of the PHP code:

$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

// $message is an html code with Hebrew content.
mail($to, $subject, $message, $headers);

as per RFC 1555: http://www.ietf.org/rfc/rfc1555.txt

Have tried also the following headers:

$headers  = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-8\r\n";
$headers .= "Content-transfer-encoding: BASE64 | Quoted-Printable\r\n";

and also with the following conversion:

mail($to, $subject, iconv("UTF-8", "ISO-8859-8//TRANSLIT", $message), $headers);

any idea would be great !! cheers !!

2 Replies

Assuming you don't need to deal with Hebrew characters in the source/destination addresses or the Subject: line, you should be able to handle everything within the HTML document ($message). It's not a surprise that some mailservers don't handle your first example well; it's missing some MIME headers. Try including all the relevant ones:

$headers  = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";

Note that the 8bit transfer encoding is specified. This will keep the non-ASCII characters from being munged. If the path the mail traverses over the Internet is not 8-bit clean, the mailservers should be able to automatically convert the body to quoted-printable or base64. If this doesn't work, you could wrap a call to quotedprintableencode() around $message and use Content-Transfer-Encoding: quoted-printable\r\n instead.

In the HTML $message, it would be a good idea to include a line

in the head section. This tells the parser what character set to expect, since the file may already be separated from the mail headers.

@Vance - Thank you so much. Your fixed headers did the trick! Cheers!!!

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct