php HTML表单的提交按钮如何在不打开用户电子邮件客户端并随后重定向到另一个页面的情况下向网站管理员发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Can Submit Button of HTML Form send email to webmaster without opening user's email client and redirect to another page afterwards?
提问by JQNoob
I have a very basic HTML Form:
我有一个非常基本的 HTML 表单:
<form method="post" action="http://someurl.com" target="_new">
<input name="name" value="" type="text" placeholder="Your First Name" />
<input name="from" value="" type="text" placeholder="Your Best Email" />
<input type="image" name="image" src="images/submit.gif" type="submit" />
</form>
Upon form submit, I'd like for it to redirect to the URL indicated in the form action above AND also for it to send me an email WITHOUT OPENING USER'S MAIL CLIENT notifying of a user filling the form.
提交表单后,我希望它重定向到上面表单操作中指示的 URL,并且还希望它向我发送一封电子邮件,而无需打开用户邮件客户端通知用户填写表单。
For what I gathered online and from this site as well I'd need something like below in php form
对于我在网上和从这个网站收集的内容,我需要像下面这样的 php 形式
<?php
///subscribe form
$recipient = "[email protected]"; /// Your Email address
if (isset($_POST['email']))
{
//Send Mail To Webmaster
$email = $_POST['email'] ;
$subject = 'New Subscriber';
$message = $email . ' has been subscribed to your website.';
mail("$recipient", $subject,
$message, "From:" . $recipient);
;}
?>
However I'm not proficient enough to create AJAX code to combine the two of them?
但是,我不够熟练,无法创建 AJAX 代码来将两者结合起来?
I APPRECIATE YOUR HELP, PLEASE BE SPECIFIC! I've seen tons of replies to similar questions with generic answers, if you know I have to use php, ajax etc. please indicate how and provide a sample code. ie. do I need to create a file with that code and how can I call the file and make it work from my HTML form? etc.
感谢您的帮助,请具体说明!我已经看到大量对类似问题的回复,并提供了通用答案,如果您知道我必须使用 php、ajax 等,请说明如何使用并提供示例代码。IE。我是否需要使用该代码创建一个文件,以及如何调用该文件并使其从我的 HTML 表单中工作?等等。
UPDATE, ok I found script.js below which is what calls the above php file called mail.php IT WORKS! however ... it sends the email to myself with the email information of the person that filled the form, not the name.
更新,好的,我在下面找到了 script.js,它调用了上面名为 mail.php 的 php 文件,它可以工作!但是...它会向我自己发送电子邮件,其中包含填写表格的人的电子邮件信息,而不是姓名。
$(function() {
$(".submit-btn").click(function() {
var x=$(".mail-input").val();
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
var email = $(".mail-input").val();
var dataString = 'email='+ email;
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
$(".mail-input").css({"background-color":"rgba(216, 70, 55, 0.64)"});
}
else
{
$.ajax({
type: "POST",
url: "mail.php",
data: dataString,
success: function(){
window.location.href = "http://someurl.com";
}
});
}
return false;
});
CAN ANYONE TELL ME how to also capture the name field data of my form and send it in the email as well, looks like the code above is meant for a form with only an email field and sends that data to the webmaster, I need to add the name field from my form to the code.
谁能告诉我如何同时捕获我的表单的名称字段数据并将其发送到电子邮件中,看起来上面的代码适用于只有一个电子邮件字段的表单并将该数据发送给网站管理员,我需要将我的表单中的名称字段添加到代码中。
Thanks!
谢谢!
回答by peaceman
You don't need ajax for this, just set the form action to your php scripts url.
您不需要 ajax,只需将表单操作设置为您的 php 脚本 url。
To redirect the user, you can use the php function location(demo inside). Just place the call to locationafter the call to mail.
要重定向用户,可以使用 php 函数位置(demo 里面)。只需在调用 tolocation之后调用 to mail。
回答by Anthony
Okay, a few things:
好的,有几点:
I'm not sure if I'm reading the sample php correctly, but do you intend to set the From address to the value in
$_POST['email']? If so, this is verybad practice and will likely result in your web host disabling your ability to send emails from their servers. Make sure that the From address is one that you own or control, not one provided by the user. See my comments and others for a question where this was the intent.If you want to send the email and then redirect, redirect from the script that handles sending the email instead of setting the redirect URL in the form:
<form method="post" action="/form_handler.php" target="_new"></form><?php ///subscribe form $recipient = "[email protected]"; /// Your Email address if (isset($_POST['email'])) { //Send Mail To Webmaster $email = $_POST['email'] ; $subject = 'New Subscriber'; $message = $email . ' has been subscribed to your website.'; mail("$recipient", $subject, $message, "From:" . $recipient); } header('Location: http://someurl.com/');In addition to a lot of other issues with the oversimplified script above (it doesn't sanitize the user input, it's very open to a DoS attack, etc), it's not always a good idea to have an externally-dependent event--such as connecting to an email server and sending an email--occur in the actual request/response of the form script. This is something I've learned the hard way. If for some reason the email hangs or the mail server freezes up while sending, etc., the user is left sitting there waiting for the script to move on to the next part (in this case the redirect) and has no idea why the page is not updating (and fyi this is an issue with AJAX as well, it just has slightly different annoying symptoms and can be harder to debug, so don't think that's the easy solution). You may want to consider storing the visitor's email address and other details to a local database or similar and sending the user on their way and then having a scheduler of some kind (usually running via cron on the backend) that periodically checks if there are new email addresses since it last checked and if so sends the emails independent of the user's form submit request.
Finally, I would bet a million dollars that there are at least 4 good php frameworks that would handle the security, manage sending the email (and would use SMTP instead of php's limited
mail()function), would redirect the user and utilizes a scheduler or job queue like I described. If you're wanting to learn how to handle the scenario you described to improve you skills and understanding of web apps and php, awesome. If you intend to implement the solutions offered here in a real-world site, I strongly recommend stopping now before you get too in over your head (or worse, implement an unsafe, non-secure, unstable solution) and instead look into some of the better lightweight php frameworks.
我不确定我是否正确阅读了示例 php,但是您是否打算将 From 地址设置为 中的值
$_POST['email']?如果是这样,这是非常糟糕的做法,可能会导致您的网络主机禁用您从他们的服务器发送电子邮件的能力。确保发件人地址是您拥有或控制的地址,而不是用户提供的地址。有关这是意图的问题,请参阅我的评论和其他人的评论。如果要发送电子邮件然后重定向,请从处理发送电子邮件的脚本重定向,而不是在表单中设置重定向 URL:
<form method="post" action="/form_handler.php" target="_new"></form><?php ///subscribe form $recipient = "[email protected]"; /// Your Email address if (isset($_POST['email'])) { //Send Mail To Webmaster $email = $_POST['email'] ; $subject = 'New Subscriber'; $message = $email . ' has been subscribed to your website.'; mail("$recipient", $subject, $message, "From:" . $recipient); } header('Location: http://someurl.com/');除了上面过于简化的脚本的许多其他问题(它不会清理用户输入,它对 DoS 攻击非常开放等),拥有一个依赖于外部的事件并不总是一个好主意——例如如连接到电子邮件服务器并发送电子邮件——发生在表单脚本的实际请求/响应中。这是我通过艰难的方式学到的东西。如果由于某种原因电子邮件挂起或邮件服务器在发送时冻结等,用户会坐在那里等待脚本继续下一部分(在这种情况下是重定向)并且不知道页面的原因没有更新(仅供参考,这也是 AJAX 的一个问题,它只是具有略微不同的烦人症状并且可能更难调试,所以不要认为这是简单的解决方案)。您可能需要考虑存储访问者的
最后,我敢打赌至少有 4 个优秀的 php 框架可以处理安全性、管理发送电子邮件(并且将使用 SMTP 而不是 php 的有限
mail()功能)、重定向用户并利用调度程序或作业队列就像我描述的那样。如果您想学习如何处理您所描述的场景以提高您对 Web 应用程序和 php 的技能和理解,那就太棒了。如果您打算在真实世界的站点中实施此处提供的解决方案,我强烈建议您在您不知所措之前立即停止(或者更糟的是,实施不安全、不安全、不稳定的解决方案),转而研究一些更好的轻量级 php 框架。

