大家好我正在开发一个网站并使用php生成一个电子邮件表单,我之前遇到过各种问题,例如"无法连接到"本地主机"的邮件服务器端口25.验证SMTP等。"因此,一个潜在的解决方案是使网站在线,因此我有和PHP代码插入实时网站,错误确实消失,但没有邮件发送到列出的电子邮件地址。我已经在这个表格上工作了大约一个星期,并尝试了各种不同的表格,这些表格可以在互联网上找到,但仍然没有收到电子邮件。我的PHP代码如下:
更新的PHP代码
<?php
//Checks for header injection
function has_header_injection($str){
return preg_match("/[\r\n]/",$str);
}
if(isset($_POST['contact_submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
//check to see if name or email have header injecion
if(has_header_injection($name) || has_header_injection ($email)){
die(); //if true kill script
}
if(!$name || !$email || !$msg){
echo '<h4 class = "error"> All fields are required </h4> <a href = "contact.php" class = "button block"> Go Bacck and Try again </a>';
exit;
}
//add recipient email to variable
$to = "mahdi.mashrafi@yahoo.com";
$subject = "$name sent you a message via your contact form";
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap($message, 72);
//set mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .="Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority:High\r\n\r\n";
$status = mail ($to, $subject , $message , $headers);
var_dump($status);
?>
**HTML FORM**
<h5>Thanks for contacting </h5>
<p>Please allow 24 housr to respond</p>
<p> <a href = "/final" class = "bitton block">« Go to homepage </a> </p>
<?php } else {?>
<form method = "post" action = "" id = "contact-form">
<label for = "name">Your name </label>
<input type = "text" id = "name" name = "name">
<label for = "email">Your email</label>
<input type = "email" id = "email" name = "email">
<label for = "message">and your message </label>
<textarea type = "message" id = "message" name = "message"></textarea>
<input type = "submit" class = "button next" name = "contact_submit" value ="Send Message">
</form>
<?php } ?>
</div>
任何有关电子邮件表格的好教程都会很棒,潜在的解决方案也会受到极大的赞赏。我真的不知道为什么邮件没有发送/收到。
--------更新了PHP代码已更新-------
答案 0 :(得分:0)
尝试执行以下操作以调试错误消息。变化
mail ($to, $subject , $message , $headers);
到
$status = mail ($to, $subject , $message , $headers);
var_dump($status);
如果您的$status
为false,则表示发送邮件时出错。让我们知道这是怎么回事。
更新: https://stackoverflow.com/a/24644450/688924非常清楚地描述了mail()
功能可能出现的问题。按照那里提供的说明操作。即使按照您发现mail()
的所有说明都不起作用,我建议您尝试使用phpMailer
,swiftmailer
外部库等备选方案。