Page 1 of 1

PHP mail() function and Swedish characters

Posted: Mon Jan 03, 2011 1:03 pm
by Hawdon
Hey everyone!

I am trying to send a finnish e-mail using this code:

Code: Select all

$message = $_GET['message'];

$message = str_replace("%F6", "ö", $message);
$message = str_replace("%E4", "ä", $message);
$message = str_replace("%E5", "å", $message);
$message = str_replace("%C5", "Å", $message);
$message = str_replace("%C4", "Ä", $message);
$message = str_replace("%D6", "Ö", $message);

$subject = urldecode($_GET['subject']);
$users = $_GET['users'];
$userArray = explode(',', $users, -1);


$header_info = "MIME-Version: 1.0\r\n";  
$header_info .= "Content-type: text/plain; charset=utf-8\r\n"; 

$sent = true;
for($i = 0; $i<sizeof($userArray); $i++)
{
		
	$to = $userArray[$i];
	mail($to, $subject, $body, $headers, '-fmyemail@email.com');

}
My problem is that the letters Ä and Ö are converted to � in the e-mail body (I'm using hotmail for the testing).
In the subject Ä and Ö are clearly visible, but that is not the case in the body.
At first I also tried to use urldecode() at the body, but that didn't seem to work. I only got URL entities like "%F6".

I then tried to use str_replace() for each letter and it's URL entity, but for some reason this seams to randomly sometimes work and sometimes not.

Any ideas?

Re: PHP mail() function and Swedish characters

Posted: Mon Jan 03, 2011 4:03 pm
by jankidudel
Maybe it 's problem with character encoding ? Try to use utf-8

Re: PHP mail() function and Swedish characters

Posted: Tue Jan 04, 2011 2:50 am
by Hawdon
jankidudel wrote:Maybe it 's problem with character encoding ? Try to use utf-8
Could you give me some example code, maybe?

Re: PHP mail() function and Swedish characters

Posted: Tue Jan 04, 2011 4:11 am
by Darhazer
mail is ascii. If you want to send utf-8 mail, you have to encode in base64 the body (as well as any header, that contains utf8 characters, and indicate this in the content-encoding header. In any other case you are relying on the utf8 support of the servers your mail is going trough

Re: PHP mail() function and Swedish characters

Posted: Tue Jan 04, 2011 6:49 am
by Hawdon
Darhazer wrote:mail is ascii. If you want to send utf-8 mail, you have to encode in base64 the body (as well as any header, that contains utf8 characters, and indicate this in the content-encoding header. In any other case you are relying on the utf8 support of the servers your mail is going trough
I tried it by doing this:

Code: Select all

mail($to, $subject, base64_encode($body), null, '-fme@mail.com')
and then changing the headers to this:

Code: Select all

$header_info = "MIME-Version: 1.0\r\n";  
$header_info .= "Content-type: text/plain; charset=UTF-8\r\n"; 
$header_info .= "Content-Transfer-Encoding: base64\r\n";
But when I send the e-mail i get text like this:
VGVydmUhDQ1PbGlzaXR0ZWtvIG5paW4g

I'm guessing I'm doing something wrong with the headers?

Re: PHP mail() function and Swedish characters

Posted: Tue Jan 04, 2011 7:29 am
by Darhazer
mail($to, $subject, base64_encode($body), null, '-fme@mail.com')
You are not using the $header_info here

Re: PHP mail() function and Swedish characters

Posted: Tue Jan 04, 2011 10:25 am
by Hawdon
Darhazer wrote:
mail($to, $subject, base64_encode($body), null, '-fme@mail.com')
You are not using the $header_info here
Well that was a stupid mistake!

After changing it everything looked good, except that the � were still replacing the Ö and Ä.

A thing that is worth noteing is that after trying this out in both hotmail and gmail, I noticed that only Hotmail failed on this.
Gmail showed the characters without problems!