Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Sending UTF-8 e-mails with PHP

Comments Blog About Development Research Sites

Sending UTF-8 e-mails with PHP

Jun 2, 2011
Finding an appartment in Stockholm is difficult. I found this out when I started looking for one after being accepted to the Machine Learning master at the Royal Institute of Technology located there. While exchange students are generally well cared for, those considered 'free movers' will have to fend for themselfs.

Luckily, a webpage exists listing appartments for rent by other students and locals which generally match my requirements quite well. Since the page updates frequently and swift action is required to get a good appartment I wrote a small script that at half hour intervals checks the page, analyses its contents, locates new ads and sends those to me by e-mail. Whereupon, much of the contents being in Swedish, I receive a garbled mess of non-latin characters.

After fiddling around with the default charset configuration options, I stumbled upon the solution which is simple as it is elegant: change the e-mail header to use the correct character set.

Code (php) (nieuw venster):
1
2
3
$headers  = 'MIME-Version: 1.0' . PHP_EOL
          . 'Content-type: text/html; charset=utf-8' . PHP_EOL;
mail('name@domain.ext', $title, $content, $headers);


This sends the e-mail as HTML using the utf-8 character set, which works wonders for Swedish content. Of course, assuming the e-mail configuration directives are set correctly. Alternatively there exists a PEAR mail package which can handle all that and more, although for a simple small script it might be a bit overkill.

FragFrog out!

New comment

Your name:
Comment: