tags:

views:

272

answers:

1

i'm having Problem with sending Email in Persian. It's Ok on gmail, all of the text shows fine. But in the orders like yahoo, cpanel webmail, etc I'm getting unknown characters. What should i do to fix this?

Here is my code:

<?php
function emailHtml($from, $subject, $message, $to) {
    require_once "Mail.php";

    $headers = array ('MIME-Version' => "1.0", 'Content-type' => "text/html; charset=utf-8;", 'From' => $from, 'To' => $to, 'Subject' => $subject);

    $m = Mail::factory('mail');

    $mail = $m->send($to, $headers, $message);
    if (PEAR::isError($mail)){
     return 0;
    }else{
     return 1;
    }
}
?>

I'm using PEAR mail to sending Email.

+2  A: 

You need to instantiate a Mail_Mime, set the headers and body HTML, retrieve them from your mime instance and pass them to your Mail instance. To quote the example from the docs:

<?php
include('Mail.php');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\n";
$hdrs = array(
              'From'    => '[email protected]',
              'Subject' => 'Test mime message',
              'Content-Type' => 'text/html; charset="UTF-8"' 
              );

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

//do not ever try to call these lines in reverse order
$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);
?>

I've edited the above documentation example to include the Content-Type header. It is advisable make your message body available as plain-text as well as HTML, in case HTML is not supported by the client. Furthermore, you won't need the parts related to adding attachments, but I've left them for knowledge's sake.

karim79