контактная форма лайтбокса в Stack Overflow

Я сейчас к этому вообще. Мне нужно сообщение об успехе, когда контактная форма лайтбокса опубликована, чтобы появиться в лайтбоксе. Письмо не отправляется и сообщение об успехе не отображается.

У меня есть следующий код на моем сайте, чтобы собрать данные и разместить их на отдельной странице, которая рассылает почту. Дело в том, что электронная почта не отправляется с использованием файла 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

Решение

Может быть, вы посмотрите на jQuery для вызова ajax. это короткий способ сделать сложный почтовый запрос менее чем за 3 строки кода. с объектами jqXHR вы можете обработать ответ.
http://api.jquery.com/jQuery.post/

в mail.php вы использовали функцию mail. в начале вы использовали @, это означает, что функция 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);
}

пожалуйста, помните, что для правильного электронного письма нет подтверждения. Ваша форма может быть использована для отправки спам-писем

0

Другие решения

Других решений пока нет …