使用 HTML 和 PHP 将表单数据发送到电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20927980/
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
Using HTML and PHP to send form data to an email
提问by Tyler
Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
就像标题所说的那样,将表单发送到我的电子邮件。我没有收到任何错误,但电子邮件从未出现。这是我的 HTML 表单(我认为您不需要其他任何东西,但文件中还有一些格式和 Java 验证)。
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
这是我的 PHP。显然我把我的电子邮件取出并放入 EMAIL,但除此之外,这是我完整的 PHP 文件。感谢 PHP 文件可以很好地拉出提交的页面,我没有收到任何错误。只是没有电子邮件。
<?php
$errors = '';
$myemail = '[email protected]';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
提前感谢您提供的任何帮助!如果您需要,我可以提供其余的 HTML 文件或我的其他 PHP 文件,但这就是所有真正的功能所在。
回答by Ferrakkem Bhuiyan
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
* PHP 将表单数据发送到电子邮件我已经使用了此代码及其对我的工作。您可以尝试 *
<?PHP
$email = $_POST["emailaddress"];
$to = "[email protected]";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: [email protected]\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
之后你可以添加你的进一步代码
回答by Guilherme Nascimento
Try add in top file:
error_reporting(E_ALL);
尝试添加顶级文件:
error_reporting(E_ALL);
and edit your code, see:
并编辑您的代码,请参阅:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
读这个:
回答by Chris
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
您在 $to = 之后将变量 $myemail 作为字符串值删除括号,您的代码将起作用
回答by The SuperKat
The problem is with your Fromfield in your $headersvariable. you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in fromfield, you need to configure your gmail username and password with your server first before using that email.
问题在于$headers变量中的From字段。你不能只是把任何电子邮件地址放在那里。例如:您应该放置您创建的服务器的现有电子邮件地址。或者,如果您想在from字段中使用 gmail 帐户,则需要在使用该电子邮件之前先在您的服务器上配置您的 gmail 用户名和密码。
The simplest solution is to create a new email address on your hosting server and then put that email address in your fromfield in $headers variable.
最简单的解决方案是在您的托管服务器上创建一个新的电子邮件地址,然后将该电子邮件地址放在 $headers 变量的from字段中。
I've already mentioned it in details here.

