15

I'm in the last stages of re-coding a site that formally used iso-8859-1 character encoding, but now is UTF-8 throughout.

The problem is that the subject section of the mailto href link is not working with Outlook, when special characters are used. I get the usual garbled character representations, indicative of an obvious character encoding issue when the link is clicked and the new mail window pops up in outlook.

I've tried rawurlencode() to fix ths issue, but this doesn't seem to work with Outlook...

<a href="mailto:blah@blah.com?subject=<?=rawurlencode($subj);?>">send email</a>

So then thought, Outlook must insist on a different encoding, and tried utf-8 decoding the subject string first...

<a href="mailto:blah@blah.com?subject=<?=rawurlencode(utf8_decode($subj));?>">send email</a>

Bingo! Works great Outlook. But now fails in everything else :(

I can't find a solution that works accross all mail clients.

It worked fine across all mail clients when the whole page was displayed in iso-8859-1. But not when the page content is utf-8.

Unfortunately the client wants to retain this direct email link, despite having a functioning mail form directly beneath it!

Is there a happy solution to this?

Richt222
  • 165
  • 1
  • 1
  • 7
  • 1
    You want the Q variant of Quoted Printable for use in "subject" headers (or B variant of Base64, if not using a superset of 7-bit ASCII). See the MIME RFCs. – ninjalj Jun 19 '12 at 19:34
  • I think the Q variant is being used. The word "véhicule" is converted to "v%C3%A9hicule" in the mailto link url. As it's a URL, this would equate to "v=C3=A9hicule" at the mail client. But doesn't display right when reaching Outlook. I think this is because Outlook is expecting iso-8859-1 encoding (where the "é" would be "=E9". But then, this fails to work in all other clients. – Richt222 Jun 20 '12 at 10:07

3 Answers3

10

If you use utf-8 try this:

<?php $subject = "=?UTF-8?B?" . base64_encode($subject) . "?="; ?>
1

The reason the subject line is sometimes garbled is because when you specify an encoding, it doesn't apply to the email header. Your subject line is in the email header. Here's a function to apply UTF8 encoding on the subject line:

function EncodeSubject($_subject)
{
    $encodedSubject = str_replace("&quot;", '"', $_subject);
    $encodedSubject = preg_replace('/[^x09\x20-\x3C\x3E-\x7E]/e', 'sprintf("=%02X", ord("$0"));', $encodedSubject);
    $encodedSubject = str_replace(' ', '_', $encodedSubject);
    return ="?utf-8?q" . $encodedSubject . "?=";
}
Dave
  • 439
  • 1
  • 6
  • 18
-2

You need to check following

1) HTML Code

<meta contentType="text/html; charset=UTF-8"/>

2) Browser Setting for IE View --> Encoding --> Unicode (UTF-8)

3) Content Type / MIME Type should be "application/x-www-form-urlencoded"

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • 1 and 2 both prove positive. I'm not sure about 3 though. Why, or what, should be set to that content / MIME type? It's not a form, so I'm a bit confused. – Richt222 Jun 20 '12 at 09:19