php邮件附件不能与某些客户端一起使用

时间:2010-01-25 14:25:12

标签: php email attachment

我有一个网络表单,允许我们的代理上传pdf附件,稍后通过电子邮件发送给我们的客户。我用来构建我在网站上发现的电子邮件的代码,直到最近它似乎运行良好。对于一些(例如只有少数,例如gmail)邮件客户端,附件被打印到电子邮件正文而不是被解释为附件。它适用于我们尝试的几乎所有客户,但少数几个。任何可能发生这种情况的解决方案都会非常感激,因为我对此并不了解。

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>\r\nReply-To: no-reply@hidden.com\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""))    

1 个答案:

答案 0 :(得分:0)

您无法控制客户端如何阅读电子邮件。有些人太半聪明,并试图在身体中包含附件,例如通常包括图像。你可以做的不多。一种解决方案是将PDF存储在您的服务器上,并通过电子邮件发送一个链接进行下载。