尝试设置联系表单。表格提交电子邮件和可选的CC发件人。如果启用了javascript,表单将提交给contact-ajax.php(用感谢消息替换表单),否则提交contact.php(重定向到感谢页面)。后备表单工作正常,但每次ajax表单触发时它也会发送一个完全空白的电子邮件(如果选中CC选项,则发送给发件人和收件人)。我无法弄清楚为什么会这样。任何见解都将不胜感激。
表单HTML:
<form method="post" id="email_form" name="email_form" action="contact.php">
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" placeholder="John Doe" /><br />
<label for="email">Email Address:</label><br />
<input type="text" name="email" id="email" placeholder="johndoe@gmail.com" /><br />
<label for="subject">Subject </label></td><br />
<input type="text" name="subject" id="subject" placeholder="Subject" /><br />
<label for="message">Message:</label></td><br />
<textarea cols="35" rows="5" name="message" id="message" placeholder="Your Message"></textarea><br />
<input type="checkbox" name="cc" id="cc" checked value="Yes" /><label for="cc"> Send yourself a copy of this message</label><br />
<input class="submit pulldown button" name="submit" id="submit" type="submit" value="Send Message" />
</form>
使用Javascript:
$(document).ready(function() {
$('#email_form #submit').click(function(event) {
event.preventDefault();
$.ajaxSetup ({
cache: false
});
$.ajax({
type: 'POST',
url: 'contact-ajax.php',
data: $("#email_form").serializeArray(),
success: function(msg) {
if(msg=='Mail sent') {
alert(msg);
}
else {
alert('Error sending mail. Please try again.');
}
},
error: function(ob,errStr) {
alert('Error sending mail. Please try again.');
}
});
return false;
});
});
脚本调用的PHP表单(contact-ajax.php):
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$email_subject = $_POST['subject'];
$message = $_POST['message'];
$today = date('r');
$email_from = "address@domain.tld";
$to = "address@domain.tld";
$email_body = "On $today $name wrote:\n"."$message";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
if (isset($_POST['cc'])) {
$headers .= "CC: $visitor_email \r\n";
}
$headers .= "X-Mailer: PHP/". phpversion();
mail($to,$email_subject,$email_body,$headers);
if(mail($to, $subject, $body, $headers)) {
die('Mail sent');
}
else {
die('Error: Mail failed');
}
}
?>
非Ajax php脚本(contact.php)是相同的,除了它以header('Location: thanks_page.html');
而不是最后if
语句结尾。
空白电子邮件不包含主题或邮件正文,但From,Reply-To和CC字段的填写方式与正确(非空)邮件完全相同。我一直试图找出问题几天,但似乎无法追踪它。
答案 0 :(得分:3)
邮件功能发送邮件..你写邮件()2次..如果
,应该检查返回值mail($to,$email_subject,$email_body,$headers);
if(mail($to, $subject, $body, $headers)) { ......
只能这样使用:
if(mail($to, $subject, $body, $headers)) { //mail sent
} else {
//mail not sent
}
<强>更新强> 使用PHPMailer易于使用,我认为比简单的PHP邮件功能更好。
答案 1 :(得分:0)
我相信就是这样:
if (isset($_POST['cc'])) {
$headers .= "CC: $visitor_email \r\n";
}
$headers .= "X-Mailer: PHP/". phpversion();
mail($to,$email_subject,$email_body,$headers); <-- Sends FIRST email
if(mail($to, $subject, $body, $headers)) { <-- Sends ANOTHER email
die('Mail sent');
}
else {
die('Error: Mail failed');
}
}
也许试试:
$retVal = mail($to,$email_subject,$email_body,$headers);
if($retVal) {
感谢名单, 马特
答案 2 :(得分:0)
Bence有一个有效的点,你称之为邮件功能两次......
我不太确定为什么有些信息是空白的,但你可以在PHP中这样做吗? :
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
if (isset($_POST['cc'])) {
$headers .= "CC: $visitor_email \r\n";
}
我总是这样做我的连接:
$headers = "From: ".$email_from." \r\n";
$headers .= "Reply-To: ".$visitor_email." \r\n";
if (isset($_POST['cc'])) {
$headers .= "CC: ".$visitor_email." \r\n";
}
答案 3 :(得分:0)
除了为什么你收到两封邮件的所有答案之外,有一个相当明显的理由说明为什么第二封邮件是空的:
mail($to,$email_subject,$email_body,$headers);
if(mail($to, $subject, $body, $headers))
在if语句中,您有$subject
和$body
而不是$email_subject
和$email_body
。