Forum Moderators: coopster

Message Too Old, No Replies

PHP mail question

How to encode foreign chars in subject?

         

Bonusbana

2:58 pm on May 11, 2004 (gmt 0)

10+ Year Member



Hi

I have an automated mail service on a page in swedish. When the admin fills out a form, the message is sent to a specific number of recievers.

The problem is, that when the admin uses special chars like å ä ö in the subject line, the email subject is transformed to some weird chars in the end.

I use a:

$header.="Content-type: text/html; charset=ISO-8859-1\r\n";

and a html special char converter function that seems to work fine in the main message, but not in the subject.

When I send a message from my main email to myself using a special char in subject, and then look at the source code it looks something like:=?ISO-8859-1?B?5TrkOvY6xTrEOtY=?=

Now, is there some kind of function that converts special chars to theese alien ISO format, or is there anything else i can do to make sure the admin can use any chars he/she likes in the subject?

Any reply would be greatly aprechiated.

Timotheos

3:30 pm on May 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope these functions [zend.com] might help.

Bonusbana

4:41 pm on May 11, 2004 (gmt 0)

10+ Year Member



Thank you for your reply.

The functions you pointed me at did not work as intended, but they gave me something to think about.

It seems that those functions work for icelandic letters, but clearly not swedish.

Two things in their function bothers me:

$text = '=?iso-8859-1?q?';

In my emails when I send special chars it starts with:
=?iso-8859-1?B?

So its a "B" instead of "q". But simply changing the q to a B just caused more strange results.

Second I dont think their code:
$val = ord($string[$i]);
$val = dechex($val);

does what I want with the swedish chars.

I need some kind of conversion table that works for =?iso-8859-1?B?. Does anyone know anything about this?

thanx

Timotheos

5:14 pm on May 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm no expert but I'm interested...

Here's the RFC which explains more of the standard
[rfc.net...]

Bonusbana

5:58 pm on May 11, 2004 (gmt 0)

10+ Year Member



Thanx I found that. I also found some other threads that gave me some more meat.

In theory, you could use this:
$subject="whatever";
$mail_subject = "=?iso-8859-1?B?".base64_encode("$subject")."?=";

This works, since the "B" stands for base64 encoding.
But unfortunately, it doesnt work for special chars like åäö.

Im lost here, and nothing out there seems to help.

thanx

Bonusbana

11:00 am on May 12, 2004 (gmt 0)

10+ Year Member



Ok, I solved it.

Apparently, just defining a string like this:

$subject="åäöÅÄÖ";

and then trying to encode it like:

$encoded="=?ISO-8859-1?B?".base64_encode($subject)."?=";

doesnt work. Why? because .php cant hande special chars in its own document. But if I made a form and the collected the value from the form and converted it like:

$subject = $_POST['subject'];

$encoded="=?ISO-8859-1?B?".base64_encode($subject)."?=";

It suddenly worked! And I even found a better way of converting subjects:

$subject = encode_subject($_POST['subject'],"ISO-8859-1");

function encode_subject($subject,$charset) {
$return = "=?".$charset."?Q?".str_replace("=\r\n", "", preg_replace("/\?/", "=3F", imap_8bit($subject)))."?=";
return $return;
}

Feel free to use it if you ever find it useful.

Timotheos

5:01 pm on May 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good job! I knew you could do it. Thanks for the little function.

Still seems to me there should be a way to do it within php but my brain cells are taken up by other problems at the moment ;-)