以 HTML/PHP 形式将文件附加到电子邮件
我有一个简单的联系表单,我想在其中添加文件上传按钮。
我怎样才能将电子邮件中上传的内容附加到收到
以下代码的
<form id="contact-form" action="<?= base_url("../email.php") ?>" method="post">
<div class="row mb-n3">
<div class="col-md-6 col-12 mb-3"><input type="text" name="name" placeholder="Your Name" required></div>
<div class="col-md-6 col-12 mb-3"><input type="email" name="email" placeholder="Email Address" required></div>
<div class="col-12 mb-3"><input name="attachment" type="file"></div>
<div class="col-12 mb-3"><input class="btn btn-primary px-5" type="submit" value="Submit"></div>
</div>
</form>
人email.php
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
// Check that data was sent to the mailer.
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.f
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "[email protected]";
// Set the email subject.
$subject = "New Message from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}?>
非常感谢您的帮助,请告知我如何对此进行排序或修复。
谢谢
I have a simple contact form where I want to add a file upload button.
How can I attach the uploaded in the email to whoever is receiving this
Codes below
<form id="contact-form" action="<?= base_url("../email.php") ?>" method="post">
<div class="row mb-n3">
<div class="col-md-6 col-12 mb-3"><input type="text" name="name" placeholder="Your Name" required></div>
<div class="col-md-6 col-12 mb-3"><input type="email" name="email" placeholder="Email Address" required></div>
<div class="col-12 mb-3"><input name="attachment" type="file"></div>
<div class="col-12 mb-3"><input class="btn btn-primary px-5" type="submit" value="Submit"></div>
</div>
</form>
email.php
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
// Check that data was sent to the mailer.
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.f
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "[email protected]";
// Set the email subject.
$subject = "New Message from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}?>
Your help is highly appreciated, please let know as to how can I get this sorted or fixed.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PHPMailer,一个用于 PHP 的全功能电子邮件创建和传输类
安装:< /strong>
用法
You can use PHPMailer, A full-featured email creation and transfer class for PHP
Installtion:
Usage