10

I'm trying to send a mail using Zend_Mail using the following code:

 function sendMail() {

     $config = array('auth' => 'login',
                'username' => 'UserName',
                'password' => 'Password',
                'port'=>'27');    

    $mail = new Zend_Mail(); 

    $mail->setBodyText($mailBody);

    $mail->setFrom('example@host.com', 'The Company Name');
    $mail->addTo('example@host.com', 'Recipient Name');
    $mail->setSubject('Mail subject');    
    $mail->send(new Zend_Mail_Transport_Smtp('example@server.com', $config));
}

Now the problem is that $mailBody has french characters. for example:

Merci d'avoir passé commande avec Lovre. Voici le récapitulatif de votre commande

When the sent mail is then viewed the same line appears like this:

Merci d'avoir pass? commande avec Lovre. Voici le r?capitulatif de votre commande

The accents were replaced by a question mark! I tried to encode the mail body using utf8_encode, but the problem still persisted.

Note: The body contents are read from a text file using file_get_contents.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Songo
  • 5,618
  • 8
  • 58
  • 96
  • Did you ensure the text file has the right encoding? You can check it in a text editor. Doing that and constructing `Zend_Mail` with the `UTF-8` option (as suggested below) should be enough. – bububaba Feb 01 '12 at 12:26

2 Answers2

24

You have to set the encoding to UTF-8 in Zend_Mail constructor :

$mail = new Zend_Mail('UTF-8'); 

Make sure also that $mailBody contains UTF-8 text.

Maxence
  • 12,868
  • 5
  • 57
  • 69
  • 1
    is there any way to set 'UTF-8' in the global $config? Like this: Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config)); – adamnyberg Jul 02 '13 at 08:40
1

Use the :

$mail->setBodyHtml();

instead of :

$mail->setBodyText();

the problem will be short out.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75