PHP Mail headers + European chars

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi there,

I've built a mailing function in PHP using mail().
My code partially looks like this:

$email['headers']  = 'From: "'.$contact_form['sender']['name'].'" <'.
$email['headers'] .=3D 'Return-Path: '.$contact_form['sender']['name'].'
$email['headers'] 0.=3D ( $is_copy =3D true ) ? 'Cc: "'.
$contact_form['sender']['name'].'"  <'.$contact_form['sender']
['email'].'>'."\r\n" : '';
$email['headers'] .=3D 'MIME-Version: 1.0'."\r\n";
$email['headers'] .=3D 'Content-Type: text/plain; charset=3D"utf-8"'."\r

All $contact_form() data contains validated inputs. Characters like,
=F6 etc. look OK in the bodytext, but give
unexpected results in the headers, especially in the name part, so
_G=FCnther_ looks like crap.
I've tried changing the charset to iso-8859-1 and iso-8859-2 but that
doesn't take away the errors.

Any tips out there?

Thanks in advance,

Re: PHP Mail headers + European chars

On Mon, 12 Apr 2010 12:20:47 -0700 (PDT), knal wrote:
Quoted text here. Click to load it

Sure. Pay attention to what characterset the data is coming in via the
form you're apparently parsing earlier. It may not be what you think.
Examining the contents of those variables through hexidecimal
representation might be enlightening.

Further, trying to push 8-bit data through mail headers is ...
unreliable. You may want to look at
for information about how to do that properly. (Be aware at the onset
that "ascii" in there means 7-bit characters.)

I don't have a sense of humour, merely an over-exaggerated sense of
              -- Stephen Harris

Re: PHP Mail headers + European chars

Quoted text here. Click to load it
Thanks for the advice. I will read it later on. FYI the data comes
from a
form's $_POST data. In the meta-data of that page i've defined the

<meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-8" />

Thanks again,

Quoted text here. Click to load it

Re: PHP Mail headers + European chars

El 12/04/2010 21:20, knal escribió/wrote:
Quoted text here. Click to load it

If you look at the headers generated by a regular e-mail client, you'll
see that they look something like this:

Subject: =?iso-8859-1?Q?=5FG=FCnther=5F?=

Basically, you have to pick a charset (UTF-8 in your case) and an
encoding (such as quoted-printable) and encode your data in plain 7 bit
ASCII while providing information about the original charset and encoding.

Doing it manually can be kind of tricky but it can definitively be done,
although I'm not sure about what PHP builtin functions can help you.
There seem to be a function to *decode*:

... but I can't find one to *encode*. I've browsed the source code of
PHPMailer (a very popular mailer written in PHP) and they have written
their own custom functions.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Re: PHP Mail headers + European chars

Hmm, seems there is no clear approach to this then.
I found the mime_header_decode as well, and also couldn't find eny

Thanks for your reply,

On Apr 13, 9:40=A0am, "=C1lvaro G. Vicario"
Quoted text here. Click to load it

Site Timeline