2

Im sending a mail using php, and the subject of the mail has accents.

Im receiving weird characters in Outlook instead the accented chars. If i see the email in a web email client i see the subject perfect.

This is the code that im using to send the email:

$to  = 'whateveremail@whatever.com';

$subject = '[WEBSITE] áccent – tésts';

$message = '<p>Test message</p>

$headers = 'MIME-Version: 1.0' . "\r\n";                                          

$headers.= 'From:equipothermomix@thermomix.com' . "\r\n";                                   

$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to, $subject, $message , $headers);

How i could solve the issue, do i have to use another charset?

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Jose3d
  • 9,149
  • 6
  • 33
  • 54
  • I had exactly the same problem and tried many things which seemed to work at first but always failed in some strange case. In the fin I ended up with pure ascii in subjects! – Tomas Sep 22 '11 at 16:43

2 Answers2

1

The only reliable way to send e-mail is using plain 7 bit ASCII so you need to encode strings properly when you want to use anything else. In your case, [WEBSITE] áccent – tésts should be encoded as (e.g.) =?windows-1252?B?W1dFQlNJVEVdIOFjY2VudCCWIHTpc3Rz?=, where the string splits as:

=?windows-1252?B?W1dFQlNJVEVdIOFjY2VudCCWIHTpc3Rz?=
  ^            ^ ^
  |            | |
 Charset       | Data
               |
              Base64

Check:

echo base64_decode('W1dFQlNJVEVdIOFjY2VudCCWIHTpc3Rz'); // [WEBSITE] áccent – tésts

This is just an example of one of the possible ways to encode non-ASCII strings.

When sending e-mail, there are many little details like this that need attention. That's why sooner or later you end up using a proper e-mail package like Swift Mailer or PHPMailer.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • @Álvaro G. Vicario: proper e-mail package like Swift Mailer (v 4.3.0.) fires a wonderful `Notice: iconv() [function.iconv]: Wrong charset, conversion from `windows-1252' to `utf-8//TRANSLIT//IGNORE' is not allowed in \lib\classes\Swift\Mime\MimePart.php` when attempting to use `Windows-1252` charset – Marco Demaio Mar 07 '13 at 14:04
  • @MarcoDemaio - If you think you've found a bug you should [report it](http://swiftmailer.org/bugs). If you think the issue is in your code, please ask your own question. – Álvaro González Mar 07 '13 at 15:06
  • @ÁlvaroG.Vicario: thanks I solved all my issues using PHPMailer, SwiftMialer is a just piece of junk with a fency looking documentation. – Marco Demaio Mar 08 '13 at 16:37
  • any ideas when the email address has accented characters? I have tried using Pear Mailer, and Swift Mailer. But non seem to work... – Richard Dev Sep 20 '13 at 15:11
1

If there is a mix of encoding types, special characters are destroyed.

PHP messing with HTML Charset Encoding

PHP also has an option for encoding messages.

mb_language('uni'); // mail() encoding

PHP - Multibyte String

Community
  • 1
  • 1
Diblo Dk
  • 585
  • 10
  • 26
  • I think it is a problem that, encoding types are mixed together as in "PHP messing with HTML Charset Encoding" – Diblo Dk Sep 22 '11 at 18:22
  • The subject of an e-mail message has nothing to do with HTML and does not support UTF-8. – Álvaro González Sep 23 '11 at 08:22
  • My answers in "PHP messing with HTML Charset Encoding" was not about HTML encoding but to keep track of ecoding against external modules. :) – Diblo Dk Sep 24 '11 at 15:04