Sending e-mails using PHP 'mail': getting the charset right

I have a web site which allows guests to make on line bookings for our B&B. The booking system sends us (and the guest) a ‘plain text’ e-mail. This has worked well for several years. I did not specify a charset in the e-mail headers.
The UK pound sign ‘£’ seemed to display without difficulty (at least in the copies I received).

Recently I’ve updated the site. I read that one should specify a charset, to ensure characters are correctly displayed (particularly on machines using non-ASCII charsets). So for the headers for ‘mail’ I have:

$headers = "From: $sender" . "\\r\
" . "Reply-to: $sender" . "\\r\
" . "Content-Type: text/plain; charset='UTF-8'";

...and further down...

mail ($email, $subject, $message, $headers);

That works OK for the copies I receive from the web site when someone books. The ‘£’ sign is correctly displayed.
At first I didn’t have the single quotes around the charset itself, but I found that the ‘£’ sign was either replaced by a black diamond with question mark inside (UTF-8) or preceded by an upper case ‘A’ with a small circle above it (ISO-8859-1). The single quotes took care of that and the e-mails rendered correctly when they arrived on my computer (using Thunderbird as my e-mail client).

Sorted, I thought, until I responded to a new booking (using ‘Reply’ in Thunderbird) and discovered that the e-mail that I was sending was blank. The culprit seems to be the single quotes. Thunderbird will send a message with “Content-Type: text/plain; charset=UTF-8” (no single quotes) as normal, but can’t cope with “Content-Type: text/plain; charset=‘UTF-8’”, and sends a blank message (although the subject line appears). Thunderbird seems to be using the charset of the incoming e-mail for the outgoing, rather than using a default. I don’t know if it’s possible to change that, I haven’t yet found a way yet.

I’ve read a lot of stuff on the Internet, where I’ve found a fair bit of conflicting advice, but everyone seems to recommend specifying a charset.

This may not be directly a PHP problem, but since it starts with setting the headers in the ‘mail’ function, I thought I should start there too.

Can anyone suggest a solution, please ?

Further investigation suggests the single quotes ‘worked’ because they were invalid and forced my e-mail client (Thunderbird) to use its default setting (ISO-8859-1) to display the message. But when it came to sending it tried to use the invalid settings, resulting in a blank e-mail.

Best results at my computer are obtained with NO character settings in the PHP, and relying on my e-mail client default to display ‘£’ correctly. However, that means I have little idea what others (our potential guests) may see, and they are more important. It is also contrary to recommended practice.

My provisional conclusion at this point is that it IS a PHP problem (or possibly in the mail server). I’m no nearer a solution.

Is character encoding an option?

Hello Ralph,

Thanks for your reply. It seems to me the whole thing is about character encoding. That’s what I’ve been trying to get right.

My very latest tests suggest that the problem is with my e-mail client (Thunderbird) not displaying the ‘£’ (UK pound) sign correctly when the charset is UTF-8, NOT with PHP or my code. (Although I have ‘proved’ that single quotes around the charset value don’t work.)

When I specify ISO-8859-1 for the e-mail in my PHP code, Thunderbird displays ‘£’ correctly, so that’s my solution, subject to further testing.

I was specifying UTF-8 because that’s what I use for the web pages and the PHP files. But it looks as if I’ll be safer using ISO-8859-1 for the e-mail.

My first tests (previous posting) were done on my localhost site (this computer). When I tried on my remote (public-facing) site it didn’t work, I’m back to ‘A£’ (A circumflex).

It appears that the routing of the messages makes a difference.
My localhost is on this computer, I use gmail for SMTP, and the received messages are OK (‘£’ is ‘£’).
The live site is on a remote host with it’s own SMTP, and the messages are forwarded via an e-mail account at another ISP, and ‘£’ becomes ‘A£’. The PHP code is identical.

I do not begin to understand this, though one can speculate, of course. It seems to make a mockery of attempting to set the Character Encoding in the mail headers via the PHP script.

I would really appreciate ANY insights. Meanwhile it looks as if I’ll be forced to use ‘GBP’, which at least is reliable, instead of ‘£’.