PHP mail() function and Swedish characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hawdon
Forum Newbie
Posts: 4
Joined: Mon Jan 03, 2011 12:53 pm

PHP mail() function and Swedish characters

Post 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?
Last edited by Hawdon on Tue Jan 04, 2011 3:01 am, edited 1 time in total.
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

Re: PHP mail() function and Swedish characters

Post by jankidudel »

Maybe it 's problem with character encoding ? Try to use utf-8
Hawdon
Forum Newbie
Posts: 4
Joined: Mon Jan 03, 2011 12:53 pm

Re: PHP mail() function and Swedish characters

Post by Hawdon »

jankidudel wrote:Maybe it 's problem with character encoding ? Try to use utf-8
Could you give me some example code, maybe?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: PHP mail() function and Swedish characters

Post 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
Hawdon
Forum Newbie
Posts: 4
Joined: Mon Jan 03, 2011 12:53 pm

Re: PHP mail() function and Swedish characters

Post 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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: PHP mail() function and Swedish characters

Post by Darhazer »

mail($to, $subject, base64_encode($body), null, '-fme@mail.com')
You are not using the $header_info here
Hawdon
Forum Newbie
Posts: 4
Joined: Mon Jan 03, 2011 12:53 pm

Re: PHP mail() function and Swedish characters

Post 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!
Post Reply