douzuan2814 2010-01-25 14:25
浏览 20

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

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."\""))    
  • 写回答

1条回答 默认 最新

  • dongyi6668 2010-01-25 14:29
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog