tags:

views:

937

answers:

2

I would like to be able to send e-mails with PHP mail() containing the 8-bit characters åäö. They will be used in the subject, the message and in the "From:"-header. How can I do this without using the PEAR packages?

+1  A: 

Simplest solution if you don't mind encoding even words that don't need it is to put everything in a base64 RFC 2047 encoded-word:

$subject= "=?utf-8?b?".base64_encode($subject)."?=";
$body= "blah blah $utf8text blah";
$headers= "MIME-Version: 1.0\r\n";
$headers.= "From: =?utf-8?b?".base64_encode($fromname)."?= <$fromaddress>\r\n";
$headers.= "Content-Type: text/plain;charset=utf-8";

mail($toaddress, $subject, $body, $headers);
bobince
Thank you, but that answer was rather incomplete. I need to use åäö in the message, the "From:" header and the subject. How can I do that? Thanks for your time.
Johan
Added more context. The From header is just the same method of encoding as the Subject header. The mail body encoding is controlled by Content-Type. I'm taking it as read that you have already have your åäö characters encoded as the UTF-8 byte string '\xc3\xa5\xc3\xa4\xc3\xb6'.
bobince
+1  A: 

Use the swiftmailer library. http://swiftmailer.org/

turbod
Excellent recommendation, thanks!
Johan