6

I have the following script:

<?php
    $subject = "Testmail — Special Characters";
    $msg = "Hi there,\n\nthis isn’t something easy.\n\nI haven’t thought that it’s that complicated!";

    mail($to,$subject,$msg,$from."\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n");
?>

In the e-mail:

Subject: Testmail ? Special Characters

Body:

Hi there,

this isn?t something easy.

I haven?t thought that it?s that complicated!

I tried a lot of things, but I have no ideas anymore. Can you help me? Did you ever got this working?

THX!

John Doe Smith
  • 1,623
  • 4
  • 24
  • 39

3 Answers3

20

Did you try iconv_set_encoding ?

This should work :

<?php
 iconv_set_encoding("internal_encoding", "UTF-8");

$subject = "Testmail — Special Characters";
$msg = "Hi there,\n\nthis isn’t something easy.\n\nI haven’t thought that it’s that complicated!";

mail(utf8_decode($to), utf8_decode($subject), utf8_decode($msg), utf8_decode($from)."\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n");?>
Gregosaurus
  • 591
  • 4
  • 5
  • 2
    wow, it works well for the body, but not for the subject: "Testmail � Special Characters" Ideas? – John Doe Smith Oct 31 '13 at 13:58
  • @JohnDoeSmith: the reason is that you cannot utf8_decode() a character, such as the long dash, that has no equivalent 1-byte representation. I don't see how you can accept this answer – Walter Tross Oct 31 '13 at 14:15
  • 3
    Use [mb_encode_mimeheader](http://php.net/manual/en/function.mb-encode-mimeheader.php) for the subject : $subject = mb_encode_mimeheader($subject,"UTF-8"); – Gregosaurus Oct 31 '13 at 14:17
  • 1
    Adding the correct mail headers instead of trying to decode stuff back into weaker encodings would be better (read: worked out better for me). Cleaner code, and less bugs. http://stackoverflow.com/questions/7266935/how-to-send-utf-8-email – Arno Teigseth Apr 16 '17 at 11:33
  • It's not recommended to use the utf8_decode function to fix issues with special characters, as it may cause unintended consequences in the email body or subject line. Instead, you should set the appropriate character set in the email headers. – bskool Apr 18 '23 at 21:58
0

While iconv_set_encoding did not work for me, the following did:

// Force PHP to use the UTF-8 charset
header('Content-Type: text/html; charset=utf-8'); 

// Define and Base64 encode the subject line
$subject_text = 'Test email with German Umlauts öäüß';
$subject = '=?UTF-8?B?' . base64_encode($subject_text) . '?=';

// Add custom headers
$headers = 'Content-Type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64';

// Define and Base64 the email body text
$message = base64_encode('This email contains German Umlauts öäüß.');

// Send mail with custom headers
if (mail('recipient@domain.com', $subject, $message, $headers)) {
    echo 'Email has been sent!';
} else {
    echo 'Oops, something gone wrong!';
}

Source: https://dev.to/lutvit/how-to-make-the-php-mail-function-awesome-3cii

Moritz
  • 249
  • 2
  • 11
-1

Use &rsquo; in place of the ’.

Example:

The dog&rsquo;s bone.

Make sure you change the content-type from text/plain to text/html.

dead beef
  • 673
  • 2
  • 6
  • 20