Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Previously, I had some code to send an email without attachments, just text with html enabled so I had custom fonts, background colours etc. Now I've added attachment support to my code, but something is causing the font-family attributes to be ignored.

Current code:
<pre>$uid = md5(uniqid(time()));
        $filename = 'invoice.html';
        $file_size = filesize($filepath);
        $content = chunk_split(base64_encode(file_get_contents($filepath)));

        $headers = "From: Me <>\r\n";
        $headers .= "Reply-To:\r\n";
        $headers .= "MIME-Version: 1.0\r\n";

        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
        $headers .= "This is a multi-part message in MIME format.\r\n";
        $headers .= "--" . $uid . "\r\n";

        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        // html is now enabled
        $headers .= $message . "\r\n\r\n";
        $headers .= "--" . $uid . "\r\n";
        $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
        $headers .= $content . "\r\n\r\n";
        $headers .= "--" . $uid . "--";

This is now called using:
mail('email@whatever', 'subject', '', $headers);

But the previous version, where fonts worked, was
mail('email@whatever', 'subject', 'body', $headers); // headers are only mime-version and content-type of html

Any help to identify the issue would be appreciated c:

What I have tried:

Updated 23-Feb-18 6:55am

1 solution

All I can see is that there is a new line sequence missing after the HTML part headers (before the $message). These part headers must be separated from the content by an empty line like the headers from the content with simple single part mails. You have done it right with the file attachment part.

If the problem still exists after fixing that, it is not related to the shown code but to the creation of your message. To check this, write $message to a file and open that with a browser.
Share this answer
[no name] 23-Feb-18 14:41pm    
Do you mean I should have $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n\r\n"; instead of $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";?
Jochen Arndt 24-Feb-18 4:09am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900