Man, I hate all those problems with character encoding:
DB, scripting language, web server, mail server, web browser, other modules, etc.
Just found out what to do with TIdMessage character sets,
then Apache/MySQL/PHP came along...
I thought computers were supposed to make our lives easier, and not miserable...
(BTW, why do we put up with all those shitty technologies for web development?!?)
Anyway, today I'd like to write about sending e-mail in PHP.
This frustrates me to no end, so maybe the information below
will help others get around their problems with PHP and e-mail...
The task seemed easy enough:
get contact information, subject, and message body from a web page form
and then use PHP to send the e-mail.
Let's assume that these are available in $to, $from, $subject, and $body variables.
Let's also assume that the web page uses ISO-8859-2 encoding.
There's this nice PHP function: mail().
I naively though this would be enough:
$headers = "From: {$from}\r\n";
mail( $to, $subject, $body, $headers );
First, the message's body didn't display correctly.
Ok, I guess we need to add a Content-Type entry to the headers:
$headers =
"From: {$from}\r\n" .
"Content-Type: text/plain; charset=ISO-8859-2\r\n";
mail( $to, $subject, $body, $headers );
Wow! The message's body was ok now, but then in my e-mail client,
the message's subject was displayed correctly on the list of all messages,
but incorrectly in the window with the message...
After searching awhile on the net, I stumbled upon the
mb_encode_mimeheader() function.
Ok, let's modify the code a bit:
$headers =
"From: {$from}\r\n" .
"Content-Type: text/plain; charset=ISO-8859-2\r\n";
mail( $to, mb_encode_mimeheader( $subject, 'ISO-8859-2', 'Q' ), $body, $headers );
Hmm... what was being displayed correctly in the message list, now became garbage.
Since this actually worked worse than the previous solution above,
I started searching for something else.
Several hours later, I came back to the mb_encode_mimeheader function.
I just then noticed the text in the description of the charset parameter:
"mb_internal_encoding()
should be set to same encoding."
Duh! It couldn't be as easy as passing the encoding in the parameter,
you still need to set the same encoding in a different place... Nice!
$headers =
"From: {$from}\r\n" .
"Content-Type: text/plain; charset=ISO-8859-2\r\n";
mb_internal_encoding( 'ISO-8859-2' );
mail( $to, mb_encode_mimeheader( $subject, 'ISO-8859-2', 'Q' ), $body, $headers );
Finally! Now the message is displayed correctly in all (tested) mail readers.
However, I added one last thing here - since I needed to change the internal encoding for mail to work correctly,
I didn't want to risk that changing that encoding will break something else somewhere else.
So, you might want to save the current encoding and restore it later on:
$headers =
"From: {$from}\r\n" .
"Content-Type: text/plain; charset=ISO-8859-2\r\n";
$encoding = mb_internal_encoding();
mb_internal_encoding( 'ISO-8859-2' );
mail( $to, mb_encode_mimeheader( $subject, 'ISO-8859-2', 'Q' ), $body, $headers );
mb_internal_encoding( $encoding );
HTH
Top
|