$(function() {
// Get the form.
var form = $('#main-contact-form');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#message').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
这是我的Ajax代码:
<?php
if(isset($_POST['submit']))
{
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$mess=
'Full Name: $name<br />
Email: $email<br />
Comments: $message<br />
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
//$mail->Encoding = '7bit';
// Authentication
$mail->Username = "Enter Your Email-ID For Testing"; // Your full Gmail address
$mail->Password = "Your Email Password"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = "hello"; // Subject (which isn't required)
$mail->MsgHTML($mess);
echo"hello";
// Send To
$mail->AddAddress("aa@gmail.com", "Bla Bla"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$mess = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
echo 'Successfully Sent!';
}
?>
这是我的Php Mailer代码:
OUTER
我认为$ mess关键字存在问题,但我想将这些前缀与发送邮件中的详细信息连接起来。
但如果我在那之后也纠正了,那邮件也不会发生。
任何人都可以帮我解决为什么会出现这个问题。
请使用phpmailer类,因为需要发送邮件。
谢谢你的帮助。
答案 0 :(得分:0)
如果您使用的是XAMPP,WAMP,LAMP等本地服务器。也许您必须在服务器的php.ini上配置SMTP
如果您要在网络服务器中部署应用,请检查您的Gmail帐户是否已激活允许“安全性较低的应用”通过您的帐户进行连接的权限。
您可以使用PHPMailer Debug来跟踪日志中的错误,并为我们提供有关该问题的更多信息:
$ mail-&gt; SMTPDebug = 2; //启用SMTP调试信息(用于测试) // 1 =错误和消息 // 2 =仅消息