UserNameHere
UserNameHere

Reputation: 113

PHP mail subject with german letters

I have here maybe a simple problem, but I don't understand why it doesnt' work, I have research and tried many examples, I am posting you my current example, I have this from a tutorial where they have explain this is the solution, but in my case it doesn't work, the subject in the email which I get does not show the letters "Ä Ö Ü ß ü" correctly.

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: {$emailfrom}";

$to = "[email protected]";
$subject = 'Betreff mit Ä, Ö und Ü ß ü';
$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';


$txt = "Hallo Sie haben das gewählt, am ä ";
mail($to,$subject,$txt,implode("\r\n",$headers));

Normally they write everywhere in the different tutorials this is the solution but in my case it doesn't work, why? Maybe have it something to do with the moon in my case? lol ;)

Upvotes: 7

Views: 2592

Answers (4)

Manjot Singh
Manjot Singh

Reputation: 21

So you need to pick one encoding, save the HTML file using that encoding, and make sure that you declare that encoding in at least one of the ways. As for what encoding to use, Germans usually use ISO/IEC 8859-15, but UTF-8 is a good alternative that can handle any kind of non-ASCII characters at the same time.

First ways is that is using utf 8

    $headers.="MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n";

Second Way is using charset according to german language

    $headers.= "MIME-Version: 1.0\r\nContent-Type: text/html; charset=ISO/IEC 8859-15\r\n";

Upvotes: 2

Faisal
Faisal

Reputation: 4765

You should use mb_encode_mimeheader,

mb_internal_encoding("UTF-8"); 
$subject = mb_encode_mimeheader($subject,'UTF-8','Q');

It will take care of encoding to Quoted-printable when needed(the human readable). Hope it should be solve your problem.

Upvotes: 10

Vijaya Vignesh Kumar
Vijaya Vignesh Kumar

Reputation: 444

Try this
$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: {$emailfrom}";

$to = "[email protected]";
$subject = 'Betreff mit Ä, Ö und Ü ß ü';
$subject = str_replace("");
$subjects = '=?UTF-8?B?'.$subject.'?=';


$txt = "Hallo Sie haben das gewählt, am ä ";
mail($to,$subjects,$txt,implode("\r\n",$headers));

If you have the same wrong output means please refer the iconv() in php it gives solution for your problem.. all the best buddy..

Upvotes: -1

1sloc
1sloc

Reputation: 1180

No need to base64 encode your subject... Just the following works fine:

<?php

$emailfrom = 'Some Name <[email protected]>';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: $emailfrom";

$to = "[email protected]";
$subject = 'Betreff mit Ä, Ö und Ü ß ü';
$txt = "Hallo Sie haben das gewählt, am &#xE4; ";

mail($to, $subject, $txt, implode("\r\n", $headers));

EDIT: Some of the headers that Gmail displays upon executing the above code snippet with a Gmail address:

Subject: Betreff mit Ä, Ö und Ü ß ü
X-PHP-Originating-Script: 1000:test.php
MIME-Version: 1.0
Content-type: text/html; charset=utf-8

Hallo Sie haben das gewählt, am &#xE4; 

Upvotes: 1

Related Questions