Question

I am using Zend_Mail and want to customize the sender name.

I want the sender name to be FooBar爱你Ryan (where 'Ryan' gets replaced with the recipient name and 爱你 gets replaced with the translation for 'loves' in the language of the recipient, just like CD Baby does).

I've tried base64_encode and mb_encode_mimeheader() and other things like:

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
iconv_set_encoding("input_encoding", 'UTF-8');
iconv_set_encoding("output_encoding", 'UTF-8');
iconv_set_encoding("internal_encoding", 'UTF-8'); 
header('Content-Type:text/html; charset=' . 'UTF-8');

It generates this as the sender: '=?UTF-8?B?RXh0cmFidXjniLHkvaByY3dhbHNoQGV4dHJhYnV4LmNvbQ==?= <email@example.com>'

And then that appears in my Gmail as (unknown sender).

Any ideas?

Was it helpful?

Solution 2

I wish I had tried this earlier: when I hard code the Chinese string as the sender name (using utf8 characters), it works fine. (I tested in Gmail only.)

So the path I'd been going down was mistaken.

I need to figure out why a dynamically-generated sender name consisting of utf8 characters doesn't work when a hard-coded Chinese string does. But that seems to be a different question.

OTHER TIPS

For me the only solution worked was the following: As you'd set utf8 subject in a usual php sendmail case, you can make a utf8 noted base64 string like this:

$mail->addFrom($fromEmail, '=?utf-8?B?'.base64_encode($fromName).'?=');

With this solution every thing worked like a charm.

This is a great question and the answers are good - but ZendFramework advanced and the interfaces referenced became unfortunately obsolete.

So here is the same solution, but tested to work fine as of June/2017:

private static function ecvt($string)
{
    return mb_convert_encoding($string, 'ISO-2022-JP', 'UTF-8');
}
private static function hcvt($string)
{
    return "=?iso-2022-jp?B?" . base64_encode( self::ecvt($string) ) . "?=";
}
private function sendMail( )
{
    $mail = new Message();

    $content = 'Message body 日本語も';

    $mail->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=ISO-2022-JP');

    $mail->setFrom('sender@acme.com', self::hcvt('Sender 日本語も') );
    $mail->addTo('receiver@acme.com', self::hcvt('Receiver 日本語も') );
    $mail->setSubject(self::hcvt('Some subject 日本語も'));

    $mail->setBody( self::ecvt($content) );
    $mail->setEncoding('ISO-2022-JP');

    // this is critical - it works around a bug in zendframework3 where
    // MIME encoding is botched in headers. By switching headers to ASCII,
    // I basically do the encoding myself.
    $mail->getHeaders()->setEncoding('ASCII');

    $this->mailTransport->send( $mail );
}

The basis for all this really is here - it is good to read so you know what is going on: RFC2047 https://www.ietf.org/rfc/rfc2047.txt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top