Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Mail with french characters in mail body

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('[email protected]', 'The Company Name');
    $mail->addTo('[email protected]', 'Recipient Name');
    $mail->setSubject('Mail subject');    
    $mail->send(new Zend_Mail_Transport_Smtp('[email protected]', $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.

like image 682
Songo Avatar asked Feb 01 '12 10:02

Songo


2 Answers

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.

like image 100
Maxence Avatar answered Nov 15 '22 21:11

Maxence


Use the :

$mail->setBodyHtml();

instead of :

$mail->setBodyText();

the problem will be short out.

like image 27
Code Lღver Avatar answered Nov 15 '22 22:11

Code Lღver