phpzend-framework2smtpgmailzend-mail

Encoding Issue with Gmail SMTP and Zend\Mail\Message- Zend Framework 2


I need to write accented letters into email body, but the utf-8 encode doesn't work. Into Gmail setting I've selected the option "Use Unicode (UTF-8) for outgoing messages".

I'm using Gmail SMTP and Zend\Mail\Messsage. I tried 4 different methods, but not one works.

complete function:

    public function sendRegistrationEmail(){
    $message = new Message();
    $message->addTo($this->email)
        ->addFrom(self::FROM)
        ->setSubject($this->subject)
        ->setEncoding('UTF-8')
        ->setBody('àèéòù');

    $transport = new SmtpTransport();
    $options   = new SmtpOptions($this->smtp);
    $transport->setOptions($options);
    $transport->send($message);
   }

1:

->setBody('àèéòù');
 output: à èéòù

2:

->setBody(utf8_encode('àèéòù'));
output: àèéòù

3:

 ->setEncoding('UTF-8')
 ->setBody('àèéòù');
 output: à èéòù

4:

->setEncoding('UTF-8')
->setBody(utf8_encode('àèéòù'));
output:àèéòù

I tried to select into Gmail settings "Avoid Unicode (UTF-8) encoding for outgoing messages", but the resuts are the same! Where am I doing wrong? thanks for the help!


Solution

  • I found a solution here:

    http://framework.zend.com/manual/current/en/modules/zend.mail.message.html

    If I need to use html email:

    $html = new MimePart($htmlMarkup);
    $html->type = "text/html; charset = UTF-8";
    

    else, pure text email:

    $text = new MimePart($textContent);
    $text->type = "text/plain; charset = UTF-8";
    
    $body = new MimeMessage();
    $body->setParts(array($text or $html));
    
    
    .....rest of message instance....
    ->setBody($body);
    

    I don't understand the reason why setEncoding('UTF-8') doesn't work. There are other solutions?