2

Hello dear people,

I spent the last 3 days searching the web for an answer and I couldn't find any.
I found plenty of "almost" cases but none was exactly what I was looking for.

I am able to get the subject and the body message in Hebrew, but I can't get the attached file name in Hebrew.

Btw, I'm not interested in third party programs like PHPMailer ect.

This is what I get:

W_W(W'W_W_.pdf

This is what I want to get:

שלום.pdf

Here is my code, very simple..

$boundary = uniqid("HTMLEMAIL");
$separator = md5(time());
$eol = PHP_EOL;

// attachment name
$fileName = "שלום.pdf";
var_dump($fileName);

$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header (multipart mandatory)
$headers = [];
$headers[] = "From: $from";
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$headers[] = "This is a MIME encoded message.";

// message
$msg = "--".$separator.$eol;
$msg .= "Content-Type: text/html; charset=UTF-8".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol.$eol;
$msg .= chunk_split(base64_encode($message)).$eol.$eol; 

// attachment
$msg .= "--".$separator.$eol;
$msg .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$msg .= "Content-Transfer-Encoding: base64".$eol.$eol;
$msg .= "Content-Disposition: attachment".$eol;
$msg .= $attachment.$eol;
$msg .= "--".$separator."--";

mail($to,'=?UTF-8?B?'.base64_encode($subject).'?=', $msg, implode("\n\r", $headers));
  • Have a look at [this answer to a similar question](http://stackoverflow.com/a/6745788/622391) - it uses RFC5987 urlencoding for the filename. – Simon MᶜKenzie Dec 12 '14 at 00:53
  • thanks for the quick replay but it's not what i'm looking for, it suppose to be a lot easier.. –  Dec 12 '14 at 01:00
  • No worries. So are you saying that `$msg .= "Content-Type: application/pdf; name*=UTF-8''".urlencode("שלום.pdf")."\"".$eol` doesn't work? – Simon MᶜKenzie Dec 12 '14 at 01:14
  • According to RFC 2047 encoded words MUST NOT be used in parameter of a Content-Type header. As such what you are trying to achieve violates specifications of protocol. – Xerkus Dec 12 '14 at 02:03
  • @Xerkus, could you please elaborate? Is an encoded word the only way in which one could specify a utf-8 encoded filename? You should write an answer, as it sounds like you're pretty familiar with MIME encoding. – Simon MᶜKenzie Dec 12 '14 at 02:54
  • 1
    I barely scratching surface there. There are dosens of related RFC. I checked how behaves thunderbird, looks like it uses encoded words: `Content-Type: application/pdf; name="=?UTF-8?B?16nXnNeV150ucGRm?=" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename*=utf-8''%D7%A9%D7%9C%D7%95%D7%9D%2E%70%64%66` – Xerkus Dec 12 '14 at 03:07
  • 1
    but rfc 2047 says `An 'encoded-word' MUST NOT be used in parameter of a MIME Content-Type or Content-Disposition field, or in any structured field body except within a 'comment' or 'phrase'.` and i found no other rfc so far that says otherwise – Xerkus Dec 12 '14 at 03:11
  • 1
    Here's how Outlook encodes it: `Content-Type: application/pdf; name="=?windows-1255?B?+ezl7S5wZGY=?=" Content-Description: =?windows-1255?B?+ezl7S5wZGY=?= Content-Disposition: attachment; filename="=?windows-1255?B?+ezl7S5wZGY=?=";` – Simon MᶜKenzie Dec 12 '14 at 03:20
  • tried all the above and still the same, no solution :(( –  Dec 12 '14 at 15:14

1 Answers1

7

According to RFC2047 you can't have encodings other than ascii in parameters of Content-Type header.

According to RFC2231 you can try to define extended parameter: Content-Type: application/pdf; name*=utf-8''%D7%A9%D7%9C%D7%95%D7%9D%2E%70%64%66

I have no idea how well it is supported.

I can't come up with oneliner for that, but you can try to adapt this one PHP convert string to hex and hex to string

Update as per comments:
While specification explicitly forbid that, most mail clients should understand following format 'name="?UTF-8?B?' . base64_encode($filename) . '?='

I suggest you to use it for sanity sake.

Community
  • 1
  • 1
Xerkus
  • 2,695
  • 1
  • 19
  • 32