【发布时间】:2017-07-20 17:18:04
【问题描述】:
我在网页上有一个简单的表单。我希望填写表格的人能够上传文件。然后我希望将消息和附件通过电子邮件发送给我。几天来我一直在玩很多代码,但没有找到任何适合我的东西。
我当前的代码:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$flags = 'style="display:none;"';
$message = $_POST['message'];
$email="me@me.com";
$to = "someone@somewhere.com";
$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
}
?>
消息通过正常,但没有附件。我尝试了很多不同的方法,包括在此处找到的几种方法,例如 [Simple PHP form: Attachment to email (code golf),但无法得到我想要的。
我还尝试了在 [http://form.guide/email-form/php-email-form-attachment.html][1] 上找到的东西,但这涉及安装 PEAR。我确实尝试过,但后来发现它覆盖了我的主页,所以我不得不卸载它,因为它似乎对我的需要来说太复杂和过度杀伤了。
我确信我一定在某个地方附近可以让它工作,但看不出有什么问题。
【问题讨论】:
-
你试过 PHPMailer 了吗?
-
file_get_contents($_FILES['file']['uploaded_file']一方面,你失败了。最好打开错误报告php.net/manual/en/function.error-reporting.php 并查阅问题已关闭的副本。 -
stackoverflow.com/questions/826265/… 的答案毕竟有效。我有一个错字:-)
标签: php forms email email-attachments