[英]Why is this PHP script not sending the e-mail?
Note: E-Mail address removed for privacy purposes. 注意:出于隐私目的,已删除电子邮件地址。
Whenever the form executes this script, it returns the success JSON array at the end, however, the e-mail is never received at the e-mail address specified. 每当表单执行此脚本时,它都会在末尾返回成功JSON数组,但是,永远不会在指定的电子邮件地址接收到该电子邮件。
<?php
if(empty($_POST['fullname']) || empty($_POST['phonenumber']) || empty($_POST['emailaddress']))
{
$response = array('status' => 0, 'txt' => 'Please verify all required fields are complete.');
echo json_encode($response);
die();
}
if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['emailaddress'])))
{
$response = array('status' => 0, 'txt' => 'Please Provide a Valid E-Mail Address.');
echo json_encode($response);
die();
}
$name = mysql_real_escape_string($_POST['fullname']);
$phonenumber = mysql_real_escape_string($_POST['phonenumber']);
$email = mysql_real_escape_string($_POST['emailaddress']);
$comments = mysql_real_escape_string($_POST['comments']);
$emailbody = 'Name: ' . $name . '
Phone Number: ' . $phonenumber . '
E-Mail Address: ' . $email . '
Comments: ' . $comments . ' ';
mail("example@example.com","New Consultation Request",$emailbody,"From: noreply@example.com");
$response = array('status' => 1, 'txt' => 'consultation-request-successful');
echo json_encode($response);
?>
You've never bothered connecting to the database, so mysql_real_escape_string()
is going to be turning a boolean FALSE for failure - it REQUIRES an active connection to the DB to do its work. 您无需费心连接数据库,因此mysql_real_escape_string()
将因失败而变为布尔值FALSE-它需要与数据库的活动连接才能完成其工作。
That means your $name, $phonenumber, $email, $comments
are all going to be boolean FALSE, and translated into an empty string when you build your mail text. 这意味着您的$name, $phonenumber, $email, $comments
均为布尔值FALSE,并且在构建邮件文本时会转换为空字符串。
As well, using mysql escaping for a simple email is beyond utterly pointless. 同样,对于简单的电子邮件使用mysql转义完全是毫无意义的。 Email is not vulnerable to SQL injection attacks. 电子邮件不容易受到SQL注入攻击的攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.