第一个版本被标记为重复,所以我将重写这个以显示为什么我认为代码应该工作但不起作用。
我有两个独立的网站托管在000webhost上 - 我已经有一个php表单作为一个网站上的联系表单,用户可以填写它,我将收到所有邮件到指定的邮箱 - 这适用于下面代码:
contact.php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$human = intval($_POST['human']);
$to = 'justjalebi@hotmail.com';
$from = 'JustJalebi Contact Form';
$subject = 'JustJalebi - New Message From '.$name;
$body ="From: $name\nE-Mail: $email\nPhone number: $phone\nMessage:\n\n$message";
// Check if name has been entered
if (empty($name)) {
$errName = 'Please enter your name';
} elseif(!preg_match("/^[a-z A-Z'-]+$/",$name)) {
$errNameInval = "Invalid name";
}
// Check if email has been entered and is valid
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || !preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/", $email)) {
$errEmail = 'Please enter a valid email address';
}
// Check if UK phone has been entered and is valid
if (empty($phone) || !preg_match("/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/", $phone)) {
$errPhone = 'Please enter a valid UK phone number';
}
//Check if message has been entered
if (empty($message)) {
$errMessage = 'Please enter your message';
}
// //Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
else {
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errMessage && !$errNameInval) {
if (@mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success" style="margin-bottom: 0px;">Thank You! A member of the JustJalebi team will be in touch.</div>';
// header("refresh:4; url=http://www.justjalebi.co.uk/bootindex.html" );
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
} else {
$result='<div class="alert alert-danger" style="margin-bottom: 0px;">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
}
在我的其他网站上,我试图在可能的情况下使用相同的逻辑,但稍有不同的是,此表单是重置密码表单 - 用户将输入他们的电子邮件地址,执行检查以查看是否存在然后随机生成的密码将被发送到他们的电子邮件地址(不是我的电子邮件地址,另一个细微差别),然后使用新生成的密码更新数据库。在测试下面的代码时,它向我显示“抱歉发送您的消息时出错。请稍后重试”,这是代码的最终if语句。
这对我来说表明在使用mail函数之前一切正常,我不确定我在$ to和$ body变量中添加变量的方式是否导致问题?
forgot.php
<?php include('config.php');
include('passwordGen.php');
$errRemail = "";
$errNoEmail = "";
$password = randomPassword(8,1,"lower_case,upper_case,numbers");
$result = "";
if (isset($_POST['submit'])) {
$email = $_POST['email'];
// Check if email has been entered and is valid
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || !preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/", $email)) {
$errRemail = '<div class="alert alert-danger alert-dismissable fade in" role="alert" style="margin-bottom: 0px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close" style="font-family:sans-serif;">×</a>Please enter a valid email address</div>';
}
$stmt = $conn->prepare("SELECT username FROM blog_members WHERE email= ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$to = $email;
$from = '<admin@nicksahota.co.uk>';
$subject = 'Account Details Recovery';
$body = 'Hi'.$row['username'].',<br>You have requested your account details. Here is your account information
please keep this email safe as you may need it at a later stage.<br>Username: '.$row['username'].'
<br>NEW Password: '.$password.'<br>Please login and change your password to something more memorable.<br>Regards Site Admin';
$headers .= "MIME-Version: 1.0\n\n";
$headers .= "Content-type: text/html\n\n";
$headers .= 'From: admin@nicksahota.co.uk' . "\n\n" .
'Reply-To: noreply@nicksahota.co.uk' . "\n\n" .
'X-Mailer: PHP/' . phpversion();
if($row == 0) {
$errNoEmail = '<div class="alert alert-danger alert-dismissable fade in" role="alert" style="margin-bottom: 0px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close" style="font-family:sans-serif;">×</a>Sorry, we cannot find your account details please try another email address.</div>';
}
else {
if (!$errRemail && !$errNoEmail) {
if (@mail ($to, $from, $subject, $body, $headers))
{
$stmt = $conn->prepare("UPDATE blog_members SET password = PASSWORD(?) WHERE email = ?");
$stmt->bind_param("ss", $password, $email);
$stmt->execute();
$stmt->close();
$result = '<div class="alert alert-success" style="margin-bottom: 0px;">An email has been sent to you containing your new login data.</div>';
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
} else {
$result='<div class="alert alert-danger alert-dismissable fade in" role="alert" style="margin-bottom: 0px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close" style="font-family:sans-serif;">×</a>Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
$conn->close();
}
?>
<form class="form-horizontal" role="form" method="post" id="reset" action="iforgot.php#reset">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa-1x"></i></span>
<input id="email" name="email" placeholder="email address" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<input id="submit" name="submit" class="btn btn-lg btn-primary btn-block" value="Reset Password" type="submit">
<?php echo "<p class='text-danger'>$errRemail $errNoEmail</p>" ;?>
<?php echo $result; ?>
</div>
</form>
我的联系表单工作的事实向我表明这不是主机提供商的问题,任何帮助将不胜感激。我已经把头发拉过来好几天了:(