Uri Even-Chen wrote:
> Hi people,
> 
> I'm trying to encode Hebrew in PHP, when sending automatic mail (using
> UTF-8).  I searched and found function mb_encode_mimeheader(), but it
> doesn't work.  It sends the mail, but the Hebrew becomes Jibrish.  I
> tried to do it using mb_encode_mimeheader().  Here is my mail sending
> code:
> 
> <?php
>      $tmp_addresses_clone= $tmp_addresses;
> 
>      $tmp_count= count($tmp_addresses_clone);
>      while ($tmp_count > 0)
>      {
>         $tmp_random= rand(0, $tmp_count - 1);
> 
>         // Open Mail Command.
>         $tmp_mail_command= '/usr/sbin/sendmail -f ' . $tmp_email . '
> ' . $tmp_addresses_clone[$tmp_random] . ' > /dev/null 2>&1';
> 
>         $tmp_mail_file_pointer= popen($tmp_mail_command, "w");
>         if ($tmp_mail_file_pointer)
>         {
>            // Print mail header.
>            fputs($tmp_mail_file_pointer, "From: " .
> mb_encode_mimeheader('"' . $tmp_name . '"', 'UTF-8', 'B', "\n") . ' '
> '<' . $tmp_email . '>' . "\n");
>            fputs($tmp_mail_file_pointer, "To: " .
> $tmp_addresses_clone[$tmp_random] . "\n");
>            fputs($tmp_mail_file_pointer, "Subject: " .
> mb_encode_mimeheader($tmp_subject, 'UTF-8', 'B', "\n") . "\n");
>            fputs($tmp_mail_file_pointer, "MIME-Version: 1.0\n");
>            fputs($tmp_mail_file_pointer, "Content-Type: text/plain;
> charset=UTF-8\n");
>            fputs($tmp_mail_file_pointer, "\n");
> 
>            // Print mail body.
>            fputs($tmp_mail_file_pointer, $tmp_content . "\n");
>            fputs($tmp_mail_file_pointer, "\n");
> 
>            // Close file.
>            pclose($tmp_mail_file_pointer);
>         }
> 
>         echo ($tmp_addresses_clone[$tmp_random] . '<br>' . "\n");
>         $tmp_addresses_clone[$tmp_random]=
> $tmp_addresses_clone[$tmp_count - 1];
>         unset($tmp_addresses_clone[$tmp_count - 1]);
>         $tmp_count= count($tmp_addresses_clone);
>      }
> ?>
> 
> How do I correctly encode Hebrew in PHP?
> 
> By the way, the body of the message looks OK.
> 
> I'm using PHP 4, I can't use PHP 5 functions such as iconv_mime_encode().


Usually what needs to be encoded are the headers. It's a simple format,
and base64 [1] for the content, something like:


=?encoding_name?B?bas64_encoded_content_blabla?=

The body itself can be left as utf-8 (specifying the content-type). You
can use the mail() function [2] to send it (configure the program
sending the mail in php.ini). Don't execute sendmail directly (prevents
code portability)


Here's a sample code which worked for me in many projects (php4 included):

$msubj="=?utf-8?B?" . base64_encode($subject) . "?=";
$mextra="From: =?utf-8?B?".base64_encode($from)."?= <".$email.">\n";
$mextra.="Content-type: text/plain; charset=utf-8\n\n";
                                        
mail($to, $msubj, $mmsg, $utf8_message);


[1] http://il2.php.net/manual/en/function.base64-encode.php
[2] http://il2.php.net/manual/en/function.mail.php


Cheers
--
Meir Kriheli

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to