views:

104

answers:

1

Hi,

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 <[email protected]>\r\nReply-To: [email protected]\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""))    
A: 

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.

Sqoo
Hi, yeah I'm starting to get that. The problem however is that everything is run throuhg our intranet. THe webapplication is only for our support staff to use towards our customer and we can't host the pdf's for download, nor are we supposed to as we are only a middle-man between our taskmaster and their customers. But if I open up my mailclient right now and send an email to a gmail with a pdf attachment, it works fine. So something must be wrong with how I construct my email, something in the content headers etc some mail clients don't like.
Jonas B
One thing you could do is compare the headers in both emails.
Sqoo