14

How can I send an email formatted as "Name <user@example.com>" to:

ŠŒŽœžŸ¥µÀÁÃÄÅÆÇÉÊËÍÎÏÐÒÓÕÖØÙÜÝßàáâåæçèéëìíîïðñóôõöøùûýÿ <user@example.com>

Obviously, many of these characters will never show up in a name, but in case they do, I would prefer that they do not prevent an email from being successfully sent.

Currently, this fails as noted in Apache's error.log with

Ignoring invalid 'To:' recipient address '¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ ' Transaction aborted: no recipients specified

If possible, I would like to keep the special characters 'as they are.' Otherwise, can I use some sort of transliteration function to clean-up the name?

Example of usage:

 <?php
 $to = "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ <CHANGED@gmail.com>";
 $subject = "Test Subject";
 $body = "Test Body";
 if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
 ?>
Citricguy
  • 412
  • 7
  • 21

3 Answers3

21

mb_encode_mimeheader should do it, just as shown in the example:

mb_internal_encoding('UTF-8');

$name  = '山本';
$email = 'yamamoto@example.com';
$addr  = mb_encode_mimeheader($name, 'UTF-8', 'Q') . " <$email>";

For better compatibility you should set the header Mime-Version: 1.0 so all mail clients understand you're using MIME encoding.

The final email headers should look like this:

To: =?UTF-8?Q?=E5=B0=81=E3=83=90=E3=83=BC?= <yamamoto@example.com>
Subject: =?UTF-8?Q?=E3=81=93=E3=82=93=E3=81=AB=E3=81=A1=E3=81=AF?=
Mime-Version: 1.0

Renders as:

To: 山本 <yamamoto@example.com>
Subject: こんにちは

Related: https://stackoverflow.com/a/13569317/476

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Perhaps also add a pointer to RFC2048 which defines this behavior. MIME is essential if you want to handle email. – tripleee Oct 06 '11 at 04:35
  • Thank you deceze, this works perfectly. @tripleee Thank you for the RFC reference. I didn't realize this applied to all parts of an email. – Citricguy Oct 06 '11 at 06:32
  • Is it also possible to encode special charaters **and** spaces? (http://stackoverflow.com/questions/11963712/how-to-correctly-encode-strings-for-usage-in-mail-headers-in-php) Unfortunately `mb_encode_mimeheader` does not help in that case. – R_User Aug 15 '12 at 11:26
2

In addition to @deceze's answer, when using Quoted Printable mode it may be necessary to escape any quotes in the name on any to/from/reply-to/etc headers:

$addr = '"'.str_replace('"', '\"', mb_encode_mimeheader($name, 'UTF-8', 'Q'))."\" <$email>";

Perhaps this isn't necessary for all MTAs, but at least for me a name with a " lead the MTA to interpret the header as multiple local addresses.

frEEk
  • 23
  • 2
1

RFC-821 (2821) tells us, that all and any 8bit-data in headers field must be encoded. Base64 or QuotedPrintable, as you want and can. Most e-mail readers automatically decode encoded strings

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110