Author Topic: £ sign in php generated email  (Read 1690 times)

£ sign in php generated email
« on: 02 February, 2012, 09:05:40 pm »
I've got a php page that generates emails. That all works fine, looking up stuff from a database, etc etc.

What I can't get it to do is to send a £ sign. It comes out as £

All other characters come out fine. I am using the php "mail" function and sending the emails in plain text. I suppose that I could send in html but I would rather not.

Any ideas?

Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

Kim

  • Timelord
    • Fediverse
Re: £ sign in php generated email
« Reply #1 on: 02 February, 2012, 09:08:35 pm »
Doubtless it's character-encoding voodoo of some sort... what Content-Type header are you sending?

(Disclaimer: I know nothing about PHP)

Re: £ sign in php generated email
« Reply #2 on: 02 February, 2012, 09:19:20 pm »
Here is the header and the offending character:-

;-----------------------

To: sales@mtrak.co.uk
Subject: OWNER Unit monitoring
From: Unit monitoring <unitmonitoring@mtrak.co.uk>
Reply-To: monitoring@mtrak.co.uk
MIME-Version: 1.0
Content-type: text/plain; charset="utf8"
Message-ID: <1328091577 TheSystem@www.mtrak.co.uk>
Envelope-To: sales@mtrak.co.uk

 Â£0.66

;----------------------

All the other characters are explicitly generated by the php, so it says "0.66" because I want it to. I have tried to get it to output "£" and that is what comes out.
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

Re: £ sign in php generated email
« Reply #3 on: 02 February, 2012, 09:21:36 pm »
I reckon it's some kind of Latin-1/UTF8 mix up type thing going on...

If you're getting it from the database  - what is the collation?
Those wonderful norks are never far from my thoughts, oh yeah!

Mike J

  • Guinea Pig Person
Re: £ sign in php generated email
« Reply #4 on: 02 February, 2012, 09:25:18 pm »
Don't know if it would make any difference, but shouldn't charset be UTF-8 rather than utf8?

Kim

  • Timelord
    • Fediverse
Re: £ sign in php generated email
« Reply #5 on: 02 February, 2012, 09:28:36 pm »
Don't know if it would make any difference, but shouldn't charset be UTF-8 rather than utf8?

A random sampling of my emails suggests that "charset=UTF-8" is more common, and it does look like a failure to parse UTF-8 as such sort of issue...

Re: £ sign in php generated email
« Reply #6 on: 02 February, 2012, 09:40:11 pm »
I reckon it's some kind of Latin-1/UTF8 mix up type thing going on...

If you're getting it from the database  - what is the collation?

I don't think that the database has anything to do with it. The numbers come from the database fine. I can get it to write "0.66 pounds" but not "£0.66"

I don't know what you mean by "collation".

The email headers seem to contain exactly what I have put in them. What I don't understand is why the message has odd characters added.
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

Mike J

  • Guinea Pig Person
Re: £ sign in php generated email
« Reply #7 on: 02 February, 2012, 09:42:57 pm »
Have you tried &pound instead of £?

Not sure if it will work, found it via googling.

Re: £ sign in php generated email
« Reply #8 on: 02 February, 2012, 09:56:14 pm »
A bit of Googling shows:

0xC2 0xA3 = £ (UTF-8)

0xC2 0xA3 = £ (ISO-8859-1)

So it appears it is displaying as ISO-8859-1 rather than UTF-8. The reason - I don't know!
Those wonderful norks are never far from my thoughts, oh yeah!

Kim

  • Timelord
    • Fediverse
Re: £ sign in php generated email
« Reply #9 on: 02 February, 2012, 10:02:06 pm »
I would guess because the mail client doesn't recognise "charset=utf8" and defaults to ASCII or something.

Re: £ sign in php generated email
« Reply #10 on: 02 February, 2012, 10:11:47 pm »
There seems to be something wrong with the character set, but I don't know what.

I got the php page to show the message, and the problem doesn't seem to be in the emailing part.

The entire page is now:-
<html>
<body>
<?php
$message = " £ ". "\r\n";
echo $message;
echo 'Done' ;
?>
</body>
</html>

and it outputs:-

<html>
<body>


 Â£
Done
</body>
</html>


Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

Re: £ sign in php generated email
« Reply #11 on: 02 February, 2012, 10:23:26 pm »
Have you tried &pound instead of £?

Not sure if it will work, found it via googling.

The &pound is displayed as £ within the php page, but the source of the page is &pound. I think that the html code is supposed to be &pound; but the browser guesses that £ is meant.

It reverts to &pound within the email.
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

Re: £ sign in php generated email
« Reply #12 on: 02 February, 2012, 10:33:53 pm »
Don't know if it would make any difference, but shouldn't charset be UTF-8 rather than utf8?

This seems to have worked. It looks OK on the Mac, where the earlier ones didn't. I'll check on the Windows machine in the morning.
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

There's no such thing as plain text
« Reply #13 on: 03 February, 2012, 09:36:00 am »
The problem with the quick html example you did a few posts up is that because you didn't specify an encoding, the browser may randomly decide which one to try and use.  So it may use a different one on different browsers, or OS, or depending on the time of day or whatever the hell it feels like unfortunately.  To put it another way, if you look up a table of strictly ASCII characters, there is no pound sign - so to get a pound sign to display reliably, you must somehow tell the recipient what the encoding is.  All that's happened here is that telling failed.


This is more of a generic add-on than a helpful one, but your remark that "I am .. sending the emails in plain text." worried me.  You don't have 'odd characters added' in your output, either.  Hope this link might at least help with the terminology a bit:

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
Quote
The Single Most Important Fact About Encodings

If you completely forget everything I just explained, please remember one extremely important fact. It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that "plain" text is ASCII.

There Ain't No Such Thing As Plain Text.


Re: There's no such thing as plain text
« Reply #14 on: 03 February, 2012, 11:49:42 am »
The problem with the quick html example you did a few posts up is that because you didn't specify an encoding, the browser may randomly decide which one to try and use.  So it may use a different one on different browsers, or OS, or depending on the time of day or whatever the hell it feels like unfortunately.  To put it another way, if you look up a table of strictly ASCII characters, there is no pound sign - so to get a pound sign to display reliably, you must somehow tell the recipient what the encoding is.  All that's happened here is that telling failed.


This is more of a generic add-on than a helpful one, but your remark that "I am .. sending the emails in plain text." worried me.  You don't have 'odd characters added' in your output, either.  Hope this link might at least help with the terminology a bit:

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
Quote
The Single Most Important Fact About Encodings

If you completely forget everything I just explained, please remember one extremely important fact. It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that "plain" text is ASCII.

There Ain't No Such Thing As Plain Text.

Well that's my bedtime reading background research sorted for a bit. Thanks.
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...