Send emails with international accent and special characters

19,530

Solution 1

You need to use MIME. Add mail headers:

MIME-Version: 1.0
Content-Type: text/plain;charset=utf-8

(If you are already using a MIME multipart/alternative to put HTML and text in the same mail, you put the Content-Type: text/plain;charset=utf-8 on the sub-headers of the text part instead.)

This is assuming that the encoding you'll be sending your “international” characters in is UTF-8. If you are expecting to cater for multiple countries UTF-8 is the only reasonable choice of encoding to use throughout your application, but if you haven't really thought about that yet your site may be defaulting to a Western European encoding. Check that things like Chinese characters work correctly in your site and database before worrying about them in mail.

Derail: there are locales where sending mail in UTF-8 isn't the most effective thing. I don't know about China, but in Japan there are still some backwards and ridiculous mail systems (especially webmail) that can't cope with Unicode and have to be given a locale-specific encoding such as Shift-JIS instead. If you are concentrating on those markets you'll often end up having to use iconv to create specially-encoded versions of the mail. Unpleasant.

Now, because many mail servers can't cope with non-ASCII characters in the mail body, you'll have to encode them. You can choose quoted-printable or base64 for this; quoted-printable is generally smaller and more readable for content that has ASCII characters in it too:

Content-Type: text/plain;charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hello! An a-acute is =C3=A1

The function to encode in this format is quoted_printable_encode. However you do need a reasonably up-to-date PHP to get that function; if you don't have it you could set the Content-Transfer-Encoding to base64 instead and use base64_encode.

Finally, if you want to include non-ASCII characters in the headers (for example in From, To or Subject), there is a completely different syntax:

Subject: =?utf-8?b?QW4gYS1hY3V0ZSBpcyDDoQ==?=

Where that QW...== mess in the middle is the base64_encode of “An a-acute is á” in UTF-8.

Solution 2

If your email content is encoded as UTF-8, and you are sending the email as UTF-8, then you shouldn't have to use any special handling of international characters in your Html or Text part.

Solution 3

Accented characters like á, í, é, ñ works on plain-text for most default browser encoing setting.

Asian ones like chinese characters will be usually turn out to be garbage.

Yes, plain-text + HTML is the recommended way.

Share:
19,530

Related videos on Youtube

Nathan H
Author by

Nathan H

Soluto #SOreadytohelp

Updated on April 15, 2022

Comments

  • Nathan H
    Nathan H about 2 years

    I am sending a newsletter via PHP mail() in Spanish, they have accents and special characters (which I'm going to try to paste here: á, í, é, ñ ...).

    For the HTML version of the email, I think I solved the issue by printing á, é ...

    However for the plain text version, I assume I can't put those, right?

    What is the best thing to do, both for the HTML and plain text version? Later, I might have to send emails in more complex languages such as hebrew or chinese ...

    Thanks!

  • Nathan H
    Nathan H over 14 years
    How do I make sure my email is being sent as UTF-8?
  • Synchro
    Synchro almost 12 years
    That 'completely different syntax' is RFC2047 'B' encoding. You're generally better off using 'Q' encoding from the same RFC as it's usually more readable (much like quoted-printable) and shorter. PHPMailer handles both, and is generally a much better way to sort out your encoding than trying to do it from scratch - it's what libraries are for!