因此,使用下面的PHP代码,我正在尝试发送电子邮件。出于某种原因,当我点击我的html联系页面上的提交按钮时,php会同时出错,
Error: all fields are required
Error: Invalid email address
我知道它是一封正确的电子邮件,并填写了所有字段,我99%确定所有输入字段都是HTML文件中的正确名称..这里是PHP代码
<?php
$errors = '';
$myemail = 'support@Blocked for security.host22.com';//<-----Put Your email address here.
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);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
表单的HTML:
<form action="contact-form-handler.php" method="POST" enctype="text/plain" name="contactform">
<label for='name'>Name:</label><br/><input type="text" name="name"><br/>
<label for='email'>Email:</label><br/><input type="text" name="email"><br/>
<label for='message'>Message:</label><br/><textarea name="message" cols=48 rows=4></textarea><br/>
<input type="submit" name="submit" value="Send">
</form>
答案 0 :(得分:1)
表单中的问题出现在:
enctype="text/plain"
要让PHP将变量放入$_POST
,enctype
应该被移除或等于application/x-www-form-urlencoded
或multipart/form-data
。
答案 1 :(得分:0)
所以电子邮件地址!preg_match。我个人不打算尝试使用正则表达式来尝试验证人们正在输入真实的电子邮件地址,您最终将从SMTP本身进行验证。
至少现在只需删除代码的这一部分:
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";
}
然后重新运行您的脚本,您将知道正则表达式是问题。希望您以一种人们真正想要收到您发送的电子邮件的方式使用此脚本。如果是这种情况,我会让任何人都烦恼的最多可能是通过在表格中输入两次来验证电子邮件地址吗?