我的php函数mail()有问题。它不会发送任何电子邮件:(。我是php的新手,并通过使用大量教程创建代码,所以我认为这可能是错误的。
此外,我在按钮代码下面的验证有问题。我想验证用户是否输入了有效的电子邮件,如果没有,则回显任何内容,因为它无论如何都会显示红色错误。
现在,当电子邮件无效时,它会显示“已发送电子邮件”消息但首先我要通过电子邮件实际发送。
如果没有标题“From ::”,托管也会阻止SSL协议。它被正确放置了吗?
<h4>Contact form</h4>
<?php
$NameErr = $EmailErr = $SubErr = $MessErr = "";
$Name = $Email = $Subject = $Message = "";
$To = "blackmagic@xxx.com";
$headers = "From: blackmagic@xxx.com Name: $Name\n $Message\n E-Mail: $Email\n";
$n = NULL;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Name"])) {
$NameErr = "Name is required";
} else {
$Name = test_input($_POST["Name"]);
}
if (!preg_match("/^[a-zA-Z ]*$/", $Name)) {
$NameErr = "Only letters and white space allowed!";
}
if (empty($_POST["Email"])) {
$EmailErr = "Email is required";
} else {
$Email = test_input($_POST["Email"]);
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
$EmailErr = "Invalid email format";
}
}
if (empty($_POST["Subject"])) {
$SubErr = "Subject is required";
} else {
$Message = test_input($_POST["Subject"]);
}
if (empty($_POST["Message"])) {
$MessErr = "Message is required";
} else {
$Message = test_input($_POST["Message"]);
}
}
$Name = test_input($_POST["Name"]);
$Email = test_input($_POST["Email"]);
$Subject = test_input($_POST["Subject"]);
$Message = test_input($_POST["Message"]);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<p><input class="w3-input w3-padding-16" type="text" placeholder="Name" name="Name"></p>
<span class="error"><?php echo $NameErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Email" name="Email"></p>
<span class="error"><?php echo $EmailErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Subject" name="Subject"></p>
<span class="error"><?php echo $SubErr; ?></span>
<p><input class="w3-input w3-padding-16" type="text" placeholder="Message" name="Message"></p>
<span class="error"><?php echo $MessErr; ?></span>
<p>
<button class="w3-btn w3-grey w3-padding-large w3-hover-green" type="submit" value="1" name="pressed">
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</p>
</form>
<?php
if (isset($_POST["pressed"])) {
mail($To, $Subject, $Message, $headers);
if (empty($_POST["Name" or "Email" or "Subject" or "Message"])) {
echo $n;
}
if (isset($Name) and ( $Email) and ( $Subject) and ( $Message)) {
echo "Email sent.";
}
}
?>
</div>
答案 0 :(得分:0)
据我了解,您发送邮件时遇到问题,如果没有标题“From ::”,则Hosting会阻止SSL协议。它被正确放置了吗?
我的解决方案是您的邮件不发送和用户邮件验证问题。这是我经常用来发送邮件的代码,它的工作非常好。
div id=existing-translation