zbateson / mail-mime-parser

An email parser written in PHP
https://mail-mime-parser.org/
BSD 2-Clause "Simplified" License
442 stars 56 forks source link

Attachment with special chars not added correctly. #145

Open miknaz opened 3 years ago

miknaz commented 3 years ago

When I add attachment to message and then convert message object to string, filename is broken.

$message->addAttachmentPartFromFile('imgæ÷ü.jpeg', $mimeType);
$mime = (string) $message;

And this is attachment headers from mime string:

Content-Transfer-Encoding: base64
Content-Type: image/jpeg;
    name="imgae??.jpeg"
Content-Disposition: attachment;
    filename="imgae??.jpeg"
0: base64

Filename is broken.. (

I use the last 1.2.3 version

zbateson commented 3 years ago

Hi @miknaz --

For now, mail-mime-parser has very basic 'write' functionality, and unfortunately the users are left responsible for ensuring that proper encoding, etc... has been applied. In this case, you'd need to follow RFC2231 encoding for parameter values, something like:

See Message::setRawHeader And Message::getPart

$message->addAttachmentPartFromFile('tmp-name.jpeg', $mimeType);
$part = $message->getAttachmentPart(0); // this may be enough depending on your use case, or you may need to use a different method like getPart() with a filter, I linked to the docs for that above
$part->setRawHeader("Content-Type: image/jpeg;\r\nname*=utf-8'en'" . urlencode('imgæ÷ü.jpeg'));

Edit: the attachment is a separate part.

All the best