打开的PHP电子邮件附件未正确解码?

时间:2013-10-07 14:57:11

标签: php email pdf email-attachments

我在一个脚本中创建了两个脚本,因此它完成了需要完成的任务。但事实并非如此。不知何故,电子邮件附件部分不能正常工作,我已尝试过多次更改但无法使其工作。

问题

在邮箱中,收件人会收到以下附件(filename.pdf.pdf .dat),这应该是filename.pdf

我在下面添加了我正在使用的脚本和html表单。在这两个页面中,我只会添加与附件有关的代码,以使问题更加清晰。

这是send.php的一部分:

    <form action="send_script.php" method="post" name="form1" id="form1" enctype="multipart/form-data">
    <div><input type="file" name="fileAttach" value="Zoeken"></div>
    <div><input type="submit" name="Submit" value="Verzenden"></div>
    </form>

这是send_script.php的一部分:

if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
}

    ob_start(); //Turn on output buffering 
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/octet-stream; name="<?php echo $strFilesName?>"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment; filename="<?php echo $strFilesName?>" 
    <?php echo $strContent; ?> 
    $message = ob_get_clean(); 
    $mail_sent = @mail( $to, $subject, $message, $headers ); 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
    ?>

修改1 上面的代码现在通过以下评论的帮助改变了,感谢大家帮助我到目前为止。该文件现在正确显示op为test.pdf,名称为test。这是完美的。唯一的问题是它无法打开它会产生以下错误。

错误Adobe Acrobat:它是作为电子邮件附件发送的,未正确解码!

编辑2 放弃上面的代码并开始更改以下内容,以便获得动态名称并使用pdf。

   $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

   --PHP-mixed-<?php echo $random_hash; ?>  
   Content-Type: application/zip; name="attachment.zip"  
   Content-Transfer-Encoding: base64  
   Content-Disposition: attachment  
   <?php echo $attachment; ?> 

到目前为止我改变了:

    $attachment = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
    --PHP-mixed-<?php echo $random_hash; ?>  
    Content-Type: application/octet-stream; name="eenbestand.pdf"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
    <?php echo $attachment; ?> 

现在可以接收文件并且它正确打开但现在是动态名称!

2 个答案:

答案 0 :(得分:0)

Content-Type标题不应该是application/pdf吗?

(如果可以,我会将此作为评论添加,因为我不确定这是否正确/相关。)

答案 1 :(得分:-1)

我通过简单地输入正确的代码来回答它。每个帮助我的人都要感谢。

    if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $headers .= "--".$strSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $headers .= $strContent."\n\n";
}
相关问题