2013-10-07 38 views
0

我在一个脚本中创建了两个脚本,因此它完成了需要完成的任务。但事实并非如此。不知何故,电子邮件附件部分不工作,它应该如何,我已经尝试了很多变化,但无法使其工作。开放的PHP电子邮件附件未正确解码?

的问题

在收件人收到以下附件(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; ?> 

文件现在可以接受并正确,但现在将打开动态域名!

+3

'echo strFilesName'在变量名开头缺少'$'。 – andrewsi

+1

这是一个愚蠢的事情要忘记。我改变了它,但它仍然没有给我上传的文件。它现在给我一个文件名.pdf.pdf .dat – HennySmafter

+1

@HennySmafter然后删除'.pdf' –

回答

-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"; 
} 
0

如果您的Content-Type标题不是application/pdf

(我已经添加了这个,就好像我可以评论,因为我不知道这是正确的/相关的。)

+1

我刚刚尝试过,它给出了比另一个错误:'无法打开损坏不支持或错误编码'但感谢您的帮助。 – HennySmafter