4 Dec 2004 simos   » (Journeyer)

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.


; 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.

$from = "From: \"" .  mb_encode_mimeheader('Αυτά είναι ελληνικά') .  "\" <root@localhost>";
$to = mb_encode_mimeheader('Παραλήπτης') . " <somemail@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.

Latest blog entries     Older blog entries

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!