Utf-8 encoded PHP-mail (!subject umlauts problem!)

heinzketchup

New Member
Messages
25
Reaction score
0
Points
0
the problem:
you want to send a php mail with utf-8 encoded content and subject(so that the umlaut letters: ö ä ü ß etc. are not shown like this: ö ä ü)... this ain't as easy as you think! it took me about 1 1/2 days to find this solution from this resource: http://sagmueller.net/php-tut/mails.html

I roll it up from the beginning...

i start with the content:
you only have to put this in your php document into the <head> tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

that's it, now your content is internationally shown the right way... you don't have to worry about this anymore...

now to the problem with the subject that is (for some stupid ASCII reason) still shown wrong...
if your subject is this: $subject="ö ä ü ß";
you only need to write in the next line: $subject = "=?utf-8?b?".base64_encode($subject)."?=";

thats it! we can now start writing the mailing function:
I guess the best way to explain this is just to paste the complete document...

---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>your header here</title>
</head>
<body>
<?
$from="123@456.asdf";
$to = "12345@789.com";//your adress here
$subject = "ö ä ü ß";//your subject here
$subject = "=?utf-8?b?".base64_encode($subject)."?=";
$message = "ä ö ü ß are shown correctly."; //Mail content here (can be html!)
$headers = "From: $from";
$headers .= "\r\n"; //(This is to write paragraphs windows conform...)
$headers .= "Content-type: text/plain";
$headers .= "\r\n\r\n";
mail($to, $subject, $message, $headers);
echo 'mail sent!';
?>
</body>
</html>
---
Thanks for reading my Tut(was my first one, guess everyone noticed that)
PS: you can also send attachments with php!

Rock On Guys->bring me credits (what ever they are used for!?!?)
Edit:
oh man... this will noone ever read! why i do this?
 
Last edited:

mattura

Member
Messages
570
Reaction score
2
Points
18
Nice one.

You say the mail content can be html - well yes, but you should put "text/html" instead of "text/plain"

Also, you can specify the character set in the same header:
PHP:
 $headers.="Content-Type: text/html; charset=iso-8859-1\r\n"; //default character set

or

 $headers.="Content-Type: text/html; charset=utf-8\r\n"; //utf-8
 

heinzketchup

New Member
Messages
25
Reaction score
0
Points
0
Nice one.
$headers.="Content-Type: text/html; charset=utf-8\r\n"; //utf-8
[/php]

cool, didn't know that \r\n in the header is also possible! I copied a solution where they handled it with an if statement and i use it in a loop, so i guess this is also a real performance advantage, i will change this.

Thanks
PS: yeah i got someone reading this :biggrin:
 

heinzketchup

New Member
Messages
25
Reaction score
0
Points
0
well... suddenly this works too... but i still have the problem with the longer script...

header:
$headers .= 'From: '.$SenderName.' <'.$SenderMail.'>'.$eol;
$headers .= "Message-ID: <".$Momentn."@".$Servername.">".$eol;
$headers .= 'Date: '.date("r").$eol;
$headers .= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
$headers .= 'X-Mailser: iPublications Adv.PHP Mailer 1.6'.$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
 
Top