2017-07-20 158 views
0

我在網頁上有一個簡單的表單。我希望填寫表單的人能夠上傳文件。然後我希望郵件和附件能夠通過電子郵件發送給我。現在我一直在玩很多代碼,但還沒有找到任何適合我的東西。如何獲取PHP電子郵件表單中的附件?

我當前的代碼:

<?php 

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 

     $flags = 'style="display:none;"'; 
     $message = $_POST['message']; 
     $email="[email protected]"; 
     $to = "[email protected]"; 
     $subject = "test Message with attachment"; 

     $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['uploaded_file']))); 
     $filename = $_FILES['file']['uploaded_file']; 

     $boundary =md5(date('r', time())); 

$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n"; 
$headers .= "From: ".$email."\r\n"; 
$headers .= "Reply-To: ".$email."\r\n" . 

$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
exit(); 

    } // end of what happens if form submitted 
else{ 
?> 
<!-- end of form processing--> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test e-mail attachment form</title> 
</head> 
<body> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
    <input type="hidden" name="status" value="sent"> 
    <table > 
    <tr> 
     <td> 
      Message: 
     </td> 
     <td> 
      <input type="text" name="message" size="20" required> 
     </td> 
    </tr> 


    <tr> 
     <td> 
      Attach a file: 
     </td> 
     <td> 

<input type="file" name="uploaded_file"> 
       </td> 
    </tr> 
     <tr> 
     <td colspan="2"> 
      <input type="submit" value="Send message"> 
     </td> 
    </tr> 
</table> 

</form> 

</body> 
</html> 
<?php 
} 
?> 

消息來通過OK,但沒有附件。我嘗試了很多不同的方法,包括在這裏找到的一對夫婦,例如[Simple PHP form: Attachment to email (code golf),但不能得到我想要的。

我也嘗試了一些我發現在:[http://form.guide/email-form/php-email-form-attachment.html][1],但涉及安裝PEAR。我確實嘗試過,但後來發現它覆蓋了我的主頁,所以我不得不卸載它,因爲它似乎太複雜,並且我需要的東西過度。

我確定我必須在某個靠近這個地方工作的地方,但是看不出有什麼問題。

+0

您是否試過PHPMailer? –

+0

'file_get_contents($ _ FILES ['file'] ['uploaded_file']'一件事,你失敗了。最好你打開錯誤報告http://php.net/manual/en/function.error-reporting.php並諮詢重複的問題已關閉。 –

+0

答案在:https://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golf確實工作,我有一個錯字:-) –

回答

-1

我建議你使用PHPMailer庫。
您可能想要使用send_file_upload示例。
此外,大量的實例被列爲here.

所以,你的代碼可以改寫如下:

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    $flags = 'style="display:none;"'; 
    $message = $_POST['message']; 
    $email="[email protected]"; 
    $to = "[email protected]"; 
    $subject = "test Message with attachment"; 
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['file']['name'])); 
    $msg = ''; 

    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); 
     $mail->addAddress($to); 
     $mail->Subject = $subject; 
     $mail->Body = $message; 

     // 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; 
    } 

    exit($msg); 
} else { ?> 
<!-- end of form processing--> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test e-mail attachment form</title> 
</head> 
<body> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
    <input type="hidden" name="status" value="sent"> 
    <table > 
    <tr> 
     <td> 
      Message: 
     </td> 
     <td> 
      <input type="text" name="message" size="20" required> 
     </td> 
    </tr> 


    <tr> 
     <td> 
      Attach a file: 
     </td> 
     <td> 

<input type="file" name="uploaded_file"> 
       </td> 
    </tr> 
     <tr> 
     <td colspan="2"> 
      <input type="submit" value="Send message"> 
     </td> 
    </tr> 
</table> 

</form> 

</body> 
</html> 
<?php }?> 

附:使用這樣的庫會給你更多的靈活性和工作代碼。
僅僅因爲它們形成了所有必需的標題並提供了簡單的OOP接口。