views:

787

answers:

3

I'm trying to send a multipart/alternative MIME e-mail via PHP script ... all works fine but I've some problems with the encoding! The accentuated characters, in the e-mail body, are displayed wrongly in the mail client! How can encode the body to solve this problem? ... I've tried to use ..

utf8_encode($body)

Without good results!

In some raw format e-mail, I've notice that the accentuations are replaced from =XX (where XX are alphanumeric char) ... How can I do this?

Thanks in advance!

This is the code:

$header = "From: \n";
$header .= "Reply-To: \n";
$header .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n"; 
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";

$body .= "\n".wordwrap($txt_body, 70);

$body .= "\n\n--$alt_boundary\n";
$body .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\n";

$body .= "\n\n\n--$mixed_boundary\n";
$body .= "Content-Type: text/html; charset=utf-8\n";
$body .= "Content-Transfer-Encoding: 7bit\n";

$body .= "\n".wordwrap($html_body, 70);

$body .= "\n\n\n--$mixed_boundary\n";   
$body .= "Content-Disposition: attachment filename=\"test file\"\n";
$body .= "Content-Type: application/octet-stream; x-unix-mode=0644; name=\test file\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n";

$body .="\n$file";

$body .= "\n\n--$mixed_boundary--";
$body .= "\n\n--$alt_boundary--"; 

mail($to, $subject, $body, utf8_encode($header));

EDIT:

The $txt_body and $html_body are the contents of two files:

$txt_body = file_get_contents(...);
$html_body = file_get_contents(...);

In that files I replace some info that I receive from PayPal through IPN. I've notice that when I receive the e-mail, only the accentuations that occurs in the IPN info are wrongly displayed (in other words the additional info that I replace in the contents of the files)! The other accentuated characters are shown correctly!!

How can I solve this?

SOLVED:

I've solved the problem! utf8_encode() function must be applied only to papal info variables , in fact is I try to encode in utf8 the $txt_body ... the paypal variables are encoded 2 times in utf8. In other word I've made that:

$txt_body = utf8_encode(file_get_contents(...));
$html_body = utf8_encode(file_get_contents(...));

and than in $txt_body and $html_body I've replaced the info received through IPN!

Thanks to ererybody!

+1  A: 

You need to declare the character encoding you’ve used in the header of that specific part:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=boundary

--boundary
Content-Type: text/plain; charset=utf-8

This part uses UTF-8.
--boundary
Content-Type: text/plain; charset=iso-8859-1

This part uses ISO 8859-1.
--boundary--
Gumbo
Thanks to your reply! Yes, I've used utf-8 as charset but when I receive the mail, the accentuations are not displayed rightly!
BitDrink
Well then it’s probably your data that’s not UTF-8 encoded. The character encoding declaration doesn’t change the actual encoding. It just changes how the data is interpreted.
Gumbo
A: 

utf-8 encoded multipart mails can easily be created with SwiftMailer

$message->addPart($txt_body, 'text/plain', 'utf-8');
$message->addPart($html_body, 'text/html', 'utf-8');
$message->attach(Swift_Attachment::fromPath('/path/to/testfile'));
VolkerK
But my problem is the encoding only of the info that paypal sent to my php script through IPN
BitDrink
Then this data (from paypal) isn't utf-8 encoded?
VolkerK
A: 

Tekst om te proberen

HH