我现在完全是这样的。当灯箱联系表单过帐以显示在灯箱中时,我需要成功消息。邮件不会被发送,也不会显示成功消息。
我的网站中有以下代码来收集数据并将其发布到发送邮件的单独页面中。事情是电子邮件不是使用mail.php文件发送的。需要帮助。
<script>
function sendEmail() {
// 1. Create XHR instance - Start
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Form is not supported by this browser");
}
// 1. Create XHR instance - End
// 2. Define what to do when XHR feed you the response from the server - Start
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status == 200 && xhr.status < 300) {
// document.getElementById('loadix').innerHTML = xhr.responseText;
if(xhr.responseText == "success"){
$('#confirmation').html("<h2><i>Thank you for Registering for the webinar. An email with a link to the webinar will be emailed to you soon.</i></h2>");
$('#tosend').val(0);
}
else {
$('#confirmation').html("<h2>Email sending failed</h2>");
}
}
}
}
// 2. Define what to do when XHR feed you the response from the server - Start
var message = document.getElementById("message").value;
var subject = document.getElementById("subject").value;
var email = document.getElementById("email").value;
var Home_phone = document.getElementById("Home_phone").value;
var full_name = document.getElementById("full_name").value;
var day = document.getElementById("day").value;
var tosend = document.getElementById("tosend").value;
var post_str = "&message=" + message +"&subject=" + subject +"&email=" + email +"&Home_phone=" + Home_phone +"&full_name=" + full_name +"&day=" + day;
// 3. Specify your action, location and Send to the server - Start
if(tosend == 1){
xhr.open('POST', 'mail.php');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(post_str);
}
// 3. Specify your action, location and Send to the server - End
return true;
}
mail.php 包含以下代码。
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xxx@gmail.com";
$full_name = $_POST['full_name']; // required
$day = $_POST['day']; // required
$email_from = $_POST['email']; // required
$Home_phone = $_POST['Home_phone']; // not required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Webinar request details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Message: ".clean_string($message)."\n";
$email_message .= "Name: ".clean_string($full_name)."\n";
$email_message .= "Home_phone: ".clean_string($Home_phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Day: ".clean_string($day)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
?>
答案 0 :(得分:0)
也许你会看一下ajax调用的jQuery。这是一个简短的方法,可以在不到3行代码中生成复杂的发布请求。使用jqXHR对象,您可以处理响应。 http://api.jquery.com/jQuery.post/
在mail.php中你已经使用了邮件功能。在开头你使用了@,这意味着mail函数不会抛出任何错误。也许你必须删除它才能在发送电子邮件时看到任何错误。
这里有一个更新的mail.php
<?php
if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xxx@gmail.com";
$full_name = $_POST['full_name']; // required
$day = $_POST['day']; // required
$email_from = $_POST['email']; // required
$Home_phone = $_POST['Home_phone']; // not required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Webinar request details below.\n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Message: " . clean_string($message) . "\n";
$email_message .= "Name: " . clean_string($full_name) . "\n";
$email_message .= "Home_phone: " . clean_string($Home_phone) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Day: " . clean_string($day) . "\n";
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$error = @mail($email_to, $email_subject, $email_message, $headers);
if ($error) {
header($_SERVER['SERVER_PROTOCOL'] . ' Internal Server Error', true, 500);
}
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' Bad Request', true, 400);
}
请记住,没有验证正确的电子邮件。您的表单可用于发送垃圾邮件