2015-09-28 149 views
3

當我試圖通過附加文件的網站上的表單發送電子郵件時遇到問題。 我使用PHPMailer,郵件總是發送,但始終沒有附加文件。如何使用PHPMailer將文件附加到電子郵件

我不知道我應該修復或添加到我的代碼。

我一直在尋找一些教程,如何使用PHPMailer和附加文件。

代碼如下:

HTML:

<form action="" method="post" enctype="multipart/form-data"> 
<input type="text" name="name" style="width: 70%;" placeholder="Name"><br /> 
<input type="text" name="email" style="width: 70%;" placeholder="Email"><br /> 
<input type="text" name="phone" style="width: 70%;" placeholder="Phone"><br /> 
<input type="file" name="fileToUpload" id="fileToUpload"><br /> 
<input type="submit" name="send" id="gobutton" value="Send"> 
</form> 

PHP:

<? 
require_once("phpmailer/class.phpmailer.php"); 

if (isset($_POST['odeslat'])){ 
    $allowedExts = array("doc", "docx", "xls", "xlsx", "pdf"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 
if ((($_FILES["file"]["type"] == "application/pdf") 
|| ($_FILES["file"]["type"] == "application/msword") 
|| ($_FILES["file"]["type"] == "application/excel") 
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel") 
|| ($_FILES["file"]["type"] == "application/x-excel") 
|| ($_FILES["file"]["type"] == "application/x-msexcel") 
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") 
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) 

&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
    echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>"; 
    } 
    else 
    { 
     $d='upload/'; 
     $de=$d . basename($_FILES['file']['name']); 
    move_uploaded_file($_FILES["file"]["tmp_name"], $de); 
$fileName = $_FILES['file']['name']; 
    $filePath = $_FILES['file']['tmp_name']; 
    //add only if the file is an upload 
    } 
    } 
else 
    { 
    echo "<script>alert('Invalid file')</script>"; 
    } 

$to = $_POST['email']; 
$name = $_POST['name']; 
$phone = $_POST['phone']; 
$mail = new PHPMailer(); 



$mail->From = "[email protected]"; 
$mail->FromName = "My website"; 



$mail->addAddress("[email protected]"); 
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); 


$mail->isHTML(true); 

$mail->Subject = "Subject Text"; 
$mail->Body = "<i>Mail body in HTML</i>"; 
$mail->AltBody = "This is the plain text version of the email content"; 

if(!$mail->send()) 
{ 
    header ("Location: /?e=1"); 
} 
else 
{ 
header ("Location: /?s=1"); 
} 
} 
?> 
+0

您需要先查看'$ _FILES'超全局。這就是您可以在上傳的文件中找到數據的地方。看看[文檔](http://php.net/manual/en/features.file-upload.post-method.php)。 –

+0

閱讀文檔和PHPmailer提供的示例。您在別處找到的建議很可能是錯誤的或過時的。 – Synchro

回答

0

Send File Attachment from Form Using phpMailer and PHP

Basicly您需要的TM上傳到了服務器和文件名p名

$_FILES['uploaded_file']['tmp_name'] 
$_FILES['uploaded_file']['name'] 
+0

謝謝,我已經在這裏找到了解決方案,在另一個話題中,它對我也很好。 –

+0

@HSturma:你必須搜索你的需求然後問一個問題。 – EniGma

1

如果你不關心需要保持系統上的文件,你可以直接發送的文件是這樣的:

if (isset($_FILES['fileToUpload']) && 
    $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) { 

    //check MIME TYPE 
    $finfo = new finfo(FILEINFO_MIME_TYPE); 
    if (false === $ext = array_search(
    $finfo->file($_FILES['fileToUpload']['tmp_name']), 
    array(
     'jpg' => 'image/jpeg', 
     'png' => 'image/png', 
     'gif' => 'image/gif', 
    ), 
    true 
)) { 
     throw new RuntimeException('Invalid file format.'); 
    } 
    $destination = sprintf('./uploads/%s.%s', 
     sha1_file($_FILES['fileToUpload']['tmp_name']), 
     $ext 
); 

    //move the file to a temp folder 
    if (!move_uploaded_file(
    $_FILES['fileToUpload']['tmp_name'], 
    $destination 
)) { 
    throw new RuntimeException('Failed to move uploaded file.'); 
    } 

    //Attach file 
    $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'], 
        basename($destination)); 
    //delete the file 
    unlink($destination); 
} 

This subject has already been treated

+0

我用我的解決方案編輯了我的問題 –

+0

不要這樣做,這是安全風險。 – Synchro

+0

@Synchro,你能開發嗎? –

0

你在做什麼以及在其他答案中提出的建議是非常錯誤的。

如果您對此類事情沒有任何線索,或許您應該嘗試reading the docs第一個

使用$_FILES屬性直接具有安全隱患,通過使用move_uploaded_file可以緩解這個問題,因爲您無緣無故避免了這些問題。

an example provided with PHPMailer這完全符合你的要求,但沒有安全漏洞。套用最重要的部分:

if (array_key_exists('fileToUpload', $_FILES)) { 
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['fileToUpload']['name'])); 
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) { 
     $mail->addAttachment($uploadfile, 'My uploaded file'); 
    } 
} 

有關更詳盡的實施使用前確認上傳的,請參閱PHP文檔this comment

相關問題