2013-10-07 67 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