setting encoding for attachment in phpmailer

Mathias Kiekens asked
php phpmailer

I am using phpmailer to send a monthly report.

It includes the € sign. Thus I need to set encoding to utf-8. I ran into a problem here.

Without encoding like this:

$mail->addStringAttachment($csvstring, $fileName);

the mail gets sent but in the attachment the € sign does not show up

when I try to add encoding:

$mail->addStringAttachment($csvstring, $fileName,  "utf-8", "text/csv");

The mail is sent but body and attachment are not present. I tried looking it up but there seems to be very little documentation.

Am I missing the obvious?


See: this line in PHPMailer’s source code

$mail->CharSet = 'utf-8';

If you want the CSV to be read as UTF-8 in programs that open it, add a BOM (Byte Order Marker; i.e., "\xef\xbb\xbf") to the string file contents.



You can also do it like this.

$mail->addStringAttachment($csvstring, $fileName, "base64", "text/csv; charset=utf-8");

See also, the specs on MIME Content-Type, as it points out the proper way to set encoding; i.e., text/csv; charset=utf-8.

Share This
Posted in: