2015-04-01 96 views
0

我一直在嘗試一段時間來讓我的頭在這附近,我不知道。我試圖寫一個簡單的表單來發送帶有上傳文件的電子郵件(它最終會擴展成實際有用的東西),而且根本不起作用。PHPMailer拒絕發送附件

電子郵件是通過適當的機構,但沒有附件被包括在內。我已經用文件上傳的形式嘗試過了,AddAttachments鏈接到服務器上的一個文件,AddAttachments指向imgur上的圖像,它們都不起作用;附件永遠不會通過。我現在對我的耐心已經結束了,有沒有人知道我在做什麼錯誤或者沒有phpmailer的方法?

HTML表單

<form action="xxxx.php" id="upload" method="post" name="upload"> 
<input id="fileToUpload" name="fileToUpload" type="file" /> 
<input type="submit" /> 
</form> 

PHP代碼

require("../../../classes/class.phpmailer.php"); 
$mail = new PHPMailer(); 

$mail->From  = "[email protected]"; 
$mail->FromName = "Uploader"; 
$mail->AddAddress("[email protected]"); 

$mail->Subject = "First PHPMailer Message"; 
$mail->Body  = "Hi! \n\n This is my first e-mail sent through PHPMailer."; 
$mail->WordWrap = 50; 
$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'], $_FILES['fileToUpload']['name']); 

if(!$mail->Send()) { 
    echo 'Message was not sent.'; 
    echo 'Mailer error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
+0

這可能對你有用:http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data。我認爲你需要在表單上使用'enctype =「multipart/form-data」'。 – potame 2015-04-01 13:50:25

回答

0

看你的表格你沒有ENCTYPE = 「的multipart/form-data的」 在你的表單標籤設置。

此外,您需要在發送電子郵件之前進行文件附件檢查以確保其實際連接。舉一個例子,

if (isset($_FILES['uploaded_file']) && 
    $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) { 
    $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'], 
         $_FILES['fileToUpload']['name']); 
} 
+0

我已經將enctype添加到表單中,並且該附件檢查到php並且它仍然不起作用。我仍然收到沒有附件的電子郵件。 – AlexRS 2015-04-01 13:59:11

0

你正在使用一些舊例,和舊版本的PHPMailer的,所以我建議你update to the latest。您還需要知道how to handle file uploads,這是你所缺少的。下面是the example bundled with PHPMailer

<?php 
/** 
* PHPMailer simple file upload and send example 
*/ 
$msg = ''; 
if (array_key_exists('userfile', $_FILES)) { 
    // First handle the upload 
    // Don't trust provided filename - same goes for MIME types 
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation 
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'])); 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     // Upload handled successfully 
     // Now create a message 
     // This should be somewhere in your include_path 
     require 'PHPMailerAutoload.php'; 
     $mail = new PHPMailer; 
     $mail->setFrom('[email protected]', 'First Last'); 
     $mail->addAddress('[email protected]', 'John Doe'); 
     $mail->Subject = 'PHPMailer file sender'; 
     $mail->msgHTML("My message body"); 
     // Attach the uploaded file 
     $mail->addAttachment($uploadfile, 'My uploaded file'); 
     if (!$mail->send()) { 
      $msg = "Mailer Error: " . $mail->ErrorInfo; 
     } else { 
      $msg = "Message sent!"; 
     } 
    } else { 
     $msg = 'Failed to move file to ' . $uploadfile; 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"/> 
    <title>PHPMailer Upload</title> 
</head> 
<body> 
<?php if (empty($msg)) { ?> 
    <form method="post" enctype="multipart/form-data"> 
     <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file"> 
     <input type="submit" value="Send File"> 
    </form> 
<?php } else { 
    echo $msg; 
} ?> 
</body> 
</html> 
+0

我正在努力在現有網站上做一個小小的增加,這個網站已經設置了相當數量並且時間有限,所以將所有內容更改爲不同版本的phpmailer聽起來都不太實際。 – AlexRS 2015-04-01 14:39:37

+0

我試過運行那個例子,我得到的響應 「無法將文件移動到/ tmp/88d542d578b1fc7dae265b22296701a217343830LIhRZ0」 – AlexRS 2015-04-01 14:39:50

+0

因此,您的系統的臨時文件夾已損壞,或者您的Web服務器無法對其進行寫入訪問 - 修正它們它會工作。更新是將舊版本更換爲新的並更改一行代碼的問題。 – Synchro 2015-04-01 14:43:14