3

What could be causing this encoding problem? Am I encoding the header information correctly or could this be caused somewhere else?

I found a similar problem here: Email from PHP has broken Subject header encoding but from what I'm reading I think I'm doing this right.

Basically sometimes our clients send emails written in french. It works but every once in a while we get some encoding issues and I'm wondering if I did the encoding wrong (I can't figure out).

$from = '=?UTF-8?B?'.base64_encode($email['from_name']).'?= <'.$email['from_email'].'>';
        $to = '=?UTF-8?B?'.base64_encode($email['to_name']).'?= <'.$email['to_email'].'>';

        $get_param = array( 
                    'Content-Type' => 'text/plain;charset=utf-8',
                    'Content-Transfer-Encoding' => '8bit');

The code above is how I encode my header information and below is the sending:

$msg = $message_mime->get($get_param);

        $headers = array (
                'From' => $from,
                'To' => $to,
                'Reply-To' => $from,
                'Subject' => utf8_decode($subject));

        $headers = $message_mime->headers($headers);

        $smtp = Mail::factory('smtp',
            array ('host' => $smtp_settings['host'],
                'auth' => true,
                'username' => $smtp_settings['username'],
                'password' => $smtp_settings['password']));

        $mail = $smtp->send($to, $headers, $msg);

The problem is that the email they received was from:

Clientnamé de l'Ontario[=?UTF-8B?QXNzZW1ib(...)=?=]

Thanks for any help :)

Edit: The client's name basically uses the same characters as above. What I mean is that the email was supposed to be from "Clientnamé de l'Ontario"

Community
  • 1
  • 1
Gazillion
  • 4,822
  • 11
  • 42
  • 59
  • 1
    I've not used PEAR Mail, but your encoding is correct as far as I can see. Maybe a buggy e-mail client? Any chance you could get the whole e-mail source for debugging purposes? – Viktor Jun 26 '11 at 21:25
  • 1
    Btw, what does the base64 encoded part (QXNzZW1ib...) translate to? Same as the From name? – Viktor Jun 26 '11 at 21:28
  • Yeah the encoding seems to be correct. I ran the string through base64_decode and I get the client's name no problem. However a coworker just noticed that we're sending our email in text/plain. Could it be that some email clients will recognize HTML and others wouldn't... Testing now. – Gazillion Jun 27 '11 at 16:13
  • Yeah that wasn't the issue. When some of their clients receive their emails they have all the email headers in the message body. I don't know what exactly that means (only happens sometimes...) – Gazillion Jun 27 '11 at 17:30
  • 1
    The content type may be an issue if the e-mail is sent as `text/plain` when it really is `text/html` or `multipart`. In any decent mailer library, this should be set (semi-)automatically. Anyway, have a look at [this example](http://www.scottwang.net/blog/?p=39) as well as [this forum post](http://www.sitepoint.com/forums/php-34/pear-email-support-utf-8-a-685509.html). Setting additional MIME parameters `head_charset`, `text_charset` and `html_charset` might do the trick. Do you have a development server you can use for testing? – Viktor Jun 27 '11 at 22:51
  • 1
    Content-type shouldn't have any effect on From/To header encoding. It could be an issue with the recipient's mail client however. Do you happen to know which client they're using to view the message? It looks like it's decoding it fine, but then displaying the encoded values for some reason. The email address doesn't contain any non-ASCII, does it? – angrychimp Mar 06 '12 at 20:00

1 Answers1

0

Did you try replacing base64_encode with utf8_encode?

I'd try replacing:

$from = '=?UTF-8?B?'.base64_encode($email['from_name']).'?= <'.$email['from_email'].'>';
$to = '=?UTF-8?B?'.base64_encode($email['to_name']).'?= <'.$email['to_email'].'>';

with:

$email['from_name'] = utf8_encode($email['from_name']);
$email['to_name'] = utf8_encode($email['to_name']);

$from = $email['from_name'].' <' . $email['from_email'] . '>';
$to = $email['to_name']) . ' <' . $email['to_email'] . '>';

Might be worth a try...

Also detect_encoding could help (http://php.net/mb_detect_encoding/)?

nkkollaw
  • 1,947
  • 1
  • 19
  • 29
  • Hey, I no longer have access to this code so I can't verify whether this would have helped. Thanks for the reply though, hopefully it can help someone in the future! – Gazillion Apr 29 '13 at 15:29