Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts PHP's mail() YAC Software
  Back

List

Intraweb and MaxConnections

A Case for FreeAndNIL

Intraweb as an Apache DSO module

"Device not supported" in Intraweb

Automated GUI Testing

Fix for Highslide HTML Slides

Random()'s Determinism

Rounding and precision on the 8087 FPU

Clicking on links in JavaScript

PHP's mail()

File | Save Atavism

SessionTimeout in Intraweb

Using TChart with Intraweb

Unknown driver: MySQL

Automated GUI Testing in VMs

TIdMessage's CharSet

Product Peer Reviews - Introduction

Software Guarantees

Automated Testing of Window Forms

TChart - Missing Labels in Axes

Calculating Positions of HTML Elements

Memory Leaks and Connection Explosions in DBExpress

CSS for Scrollbars on Pages and in Frames

Controlling Conditional Defines and Compilation Switches

Turning Browser Caching off when Displaying Images Using PHP

Detecting Memory Leaks with DUnit

last_insert_id() and DBExpress

YAC Data Language

Registering Extensions

DBExpress and Thread Safety

Forms as Frames

Checking Dangling Pointers vs. the New Memory Manager

</form> ... <form>

Accessing Protected Members

PHP's mail()
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

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top