简体   繁体   中英

Encoding problem in From email and name when sending email in PHP?

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"

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/ )?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM