I have a webform that allows our agents to upload a pdf attachment that later gets emailed out to our customer. The code I use to build the email I found on a website and it seemed to work well until recently. It seems with some (only a few, gmail for example) mail-clients the attachment is printed out to the body of the email instead of being interpreted as an attachment. It works with almost every client we tried however except a few. Any solution to why this might happen would be greatly appreciated as I do not have much knowledge of this.
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
return;
}
if ($_FILES["file"]["type"] != "application/pdf")
{
echo "<center>Attachment has to be valid .pdf<br><a href='../index.php'>Tillbaka</a></center>";
return;
}
$file = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
/*
striped out some parts about how the body is constructed
note that there are no comment fields here in the real version of this sourcecode.
*/
$msg = "msg here.." //i striped this part out as there's nothing odd about how the body is constructed its plain text
$random_hash = md5(date('r', time()));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $msg;?>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="<?php echo $_FILES["file"]["name"];?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $file;?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
$message = ob_get_clean();
//I removed some info about recievers, reply-to, from etc..
mail($hidden_variable, "hidden subject", $message, "From:Hidden Name <no-reply@hidden.com>
Reply-To: no-reply@hidden.com
Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""))
You cannot garatee how emails will be read by a client. Some are too clever by halves and will try to include the attachments in the body, for example images are often included. Not much you can do about that. One solution would be to store the PDF on your server and email a link to it for download.