Sending Unicode e-mail through a script (as in PHP)

Posted 4 Dec 2004 at 12:41 UTC by simos Share This

Using any moderm graphical e-mail client you are able to send e-mails in Unicode, so that your local script is shown in the To:, From:, Subject:, and body fields. However, when you design a Website that sends e-mails to users or you want to send notification e-mail from a script, often it is difficult to get the encoding right and show the local language properly.

Here we see how to send fully localised e-mail from PHP. Similar steps can be taken with other scripting languages.

You need to configure PHP on the server to use the mbstring extension. In popular distributions it is already installed with PHP. Verify that along with the package "php" you also have "php-mbstring" installed as well.

Then, you configure the default language settings for mbstring, so that your scripts are simpler. As we opt for Unicode and utf-8, these settings are universal. The following section should already exist in your distribution (at least on my Fedora Core 2), but commented out.

/etc/php.ini

[mbstring]
; language for internal character representation.
; -> "Neutral" is for Unicode.
mbstring.language = Neutral
                                                                                                                                                                          
; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
mbstring.internal_encoding = UTF-8
                                                                                                                                                                          
; http input encoding.
; -> Setup your Web pages to be in utf-8 and specify utf-8 in HTML headers.
; -> Sometimes Apache is configured to send the encoding in the HTTP headers.
; -> ..which is set to iso-8859-1 (Debian?). Don't omit that setting.
mbstring.http_input = UTF-8
                                                                                                                                                                          
; http output encoding. mb_output_handler must be
; registered as output buffer to function
mbstring.http_output = UTF-8
                                                                                                                                                                          
; enable automatic encoding translation accoding to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
mbstring.encoding_translation = On
                                                                                                                                                                          
; automatic encoding detection order.
; auto means
mbstring.detect_order = auto
                                                                                                                                                                          
; substitute_character used when character cannot be converted
; one from another
; -> if character code not found, show as U+xxxx. Good for finding issues.
mbstring.substitute_character = long;

Finally, you can send localised e-mail through the following script. You can either execute it from command line (php mailtest.php) or add it as a Web page and visit it.

<?php
include('Mail.php');
include('Mail/mime.php');
 
$from = "From: \"" .  mb_encode_mimeheader('Αυτά είναι ελληνικά') .  "\" <root@localhost>";
$to = mb_encode_mimeheader('Παραλήπτης') . " <waterguava@gmail.com>";
$subject = 'Θέμα';
$body = 'Περιεχόμενο του γράμματος';
 
mb_send_mail($to, $subject, $body, $from);
?>

The strings in Unicode (utf-8) I am using for the example should be Greek to you.


hum, posted 5 Dec 2004 at 17:02 UTC by yeupou » (Master)

Hum. PHP tips section?

Why?, posted 9 Dec 2004 at 02:00 UTC by hacker » (Master)

What exactly does this have to do with Advogato's articles section, and why was it posted twice, with two different titles?

Because, posted 9 Dec 2004 at 20:32 UTC by simos » (Journeyer)

the entry was posted by mistake (twice) on the front page.

Does advogato allow to edit posted articles?

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page