views:

268

answers:

2

I am going to send mail through PHP website. Client may custom the mail subject and I will get the post data in UTF-8. But When I send out a html mail using the php mail(), I found the the mail subject cannot show properly while the mail body does.

How to send chinese word in PHP mail function?

Thanks.

+1  A: 

You need to encode the subject according to how encoded-word is specified (see RFC 2047):

encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

You can either use

  • the Base64 encoding (base64_encode):

    'Subject: =?UTF-8?B?'.base64_encode($subject).'?='
    

    or

  • the quoted-printable encoding (see RFC 2045, imap_8bit):

    'Subject: =?UTF-8?Q?'.imap_8bit($subject).'?='
    
Gumbo
For some newbies like me, set the $subject value using:$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';if you are using UTF-8
Jay
A: 

It's quite involved to send non-ASCII in mail header. Check out PHPMailer,

http://sourceforge.net/projects/phpmailer/

If it doesn't work for you, look at encoderHeader() to see how it's done.

ZZ Coder