2016-06-23 85 views
2

希望有人在那裏可以指出我錯過了什麼!用PHPmailer發送附件 - 返回.dat文件

我有一個腳本,我從PHPMailer's Github example here我放在我的頁面,並更新了HTML。

儘管圖片上傳正常並且電子郵件通過,但文件附件是.dat文件,而不是正確的擴展名。

這裏是我的情況下,PHP的東西我已經改變並不像它應該是:

if (array_key_exists('userfile', $_FILES)) { 

$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'])); 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 

    require 'PHPMailerAutoload.php'; 
    $mail = new PHPMailer; 
    $mail->setFrom('[email protected]', 'Competitions'); 
    $mail->addAddress('[email protected]', 'JG'); 
    $mail->Subject = 'Competition Entry'; 
    $mail->msgHTML("My message body"); 

    $mail->addAttachment($uploadfile, 'Competiton Entry Attachment'); 
    if (!$mail->send()) { 
     $err[] = "Mailer Error - didn't send email" . $mail->ErrorInfo; 
    } else { 
     $done[] = "Message sent!"; 
    } 

} else { 
    $err[] = 'Failed to move file to ' . $uploadfile; 
} 

} 

提前感謝

+0

你不給文件名extensi on:'競爭者條目附件'沒有。試試:''Competiton_Entry_Attachment.doc''或任何其他擴展名。如果這不起作用,請給它一個類型。請參閱文檔。 –

+0

@KIKOSoftware - 謝謝,這是有效的,但我希望我可以上傳任何類型的文件....不僅限於一個ie ie .doc' – joel2703

+2

這是我希望自己可以解決的另一個問題...但沒有必要,Shijin TR告訴你如何。 –

回答

2

變化

$mail->addAttachment($uploadfile, 'Competiton Entry Attachment'); 

$name = $_FILES['userfile']['name']; 

$ext = end((explode(".", $name))); 

$mail->addAttachment($uploadfile, 'Competiton Entry Attachment.'.$ext); 
+0

感謝您的回覆。當我嘗試過時,它返回了「比賽輸入附件._tmp_php54Qilm」?謝謝 – joel2703

+0

出色地工作 – joel2703

+2

有關記錄,[這裏是'addAttachment']的文檔(http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addAttachment)。 – Synchro