Question

Currently when sending an email from PHP which includes a spanish accent, the email is being rendered as follow:

Ω₯ζλZΫiz«’Ό*'΅ινO*^rνz{

I'm setting the following headers:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";

A sample body message is:

Estudio bíblico en Web Church Connect

I'm also setting the charset of the html:

$message = '
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>WebChurchConnect</title>
        </head>
        <body>
';

Any thoughts is appreciated.

Thanks.

Était-ce utile?

La solution

E-Mail only accepts ASCII characters as many MTAs are not equipped to correctly relay other messages. You should think of MIME-E-Mails like onions: (they smell, they make you cry) and they have layers. The HTML message (inner layer) only gets decoded after the plain text of your message is handled (outer layer).

You need to explicitly encode any non-ASCII characters in the “outer” layer. You do this using the Content-Transfer-Encoding header, which can be set to either base64 or quoted-printable (some modern MTAs also support 8bit or binary but these must be set explicitly and support still isn’t as universal as one would hope for in 2014). Of course the MIME part that follows this header also needs to be actually encoded using the method specified. Fortunately Base64-Encoding is only a base64_encode call away.

Alternatively, since your message is in HTML (and you don’t seem to care about providing a plaintext alternative – which you should), you could also use HTML’s escaping mechanisms (e.g. &iacute; instead of í), but Base64 is generally safer since it’s immune to the MTAs that take it upon themselves to break up long lines after 78 characters.

Autres conseils

Try using PHPMailer or SwiftMailer. Problems like these are there already solved, everything is tested and its much easier for you to work with.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top