目前,在我的网站上,联系表格是通过一个JS验证的(我知道这有风险,现在我已转移到PHPMailer)。
当前在我的联系表单下方触发成功或错误消息的是javascript的这一部分:
if (isValidEmail(email) && (message.length > 1) && (name.length > 1)) {
$.ajax({
type: "POST",
url: "sendphpmailer.php",
data: dataString,
success: function() {
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').fadeIn(1000);
}
});
} else {
$('.error').fadeIn(1000);
}
return false;
});
});
现在我正在使用PHPMailer我希望从我的联系表单下面的成功/错误消息来自PHP文件(并且不再是JS文件,因为它有时会返回成功消息,即使消息不是发送),所以我试过这个(没有成功)。有什么问题?
$mail->Subject = "New message from " . $_POST['name'];
$mail->Body = $_POST['message'];
if(!$mail->send())
{
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').fadeIn(1000);}
else
{
$('.error').fadeIn(1000);
}
如果有帮助,这是原始的完整PHP代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
//Enable SMTP debugging.
$mail->SMTPDebug = false;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.elasticemail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "web@dshsjjh.com";
$mail->Password = "***";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 2525;
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->addAddress("xyz@gmail.com");
//CC and BCC
$mail->addCC("xyz@outlook.com");
$mail->addBCC("");
$mail->isHTML(true);
$mail->Subject = "New message from " . $_POST['name'];
$mail->Body = $_POST['message'];
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
HTML:
<section class="contact clearfix" id="contact">
<div class="contact-container clearfix">
<form class="clearfix" id="contactfrm" method="post" name="contactfrm">
<div class="tagbox wow fadeInRightBig"><i class="fa fa-pencil"></i> Formulaire de contact
</div>
<input class="_input-1 floatlabel" id="name" name="name" placeholder="Votre nom et prénom" type="text">
<input class="_input-2 floatlabel" id="email" name="email" placeholder="Votre adresse e-mail" type="email">
<textarea class="floatlabel" id="message" name="message" placeholder="Votre message et votre numéro de téléphone"></textarea>
<button class="wow fadeInUp" name="submit" type="submit" value="Envoyer"><i class="fa fa-sign-in"></i> ENVOYER </button>
</form>
</div>
</section>
<footer class="clearfix">
<div class="success" style="display:none">
Votre message a bien été envoyé! Merci
</div>
<div class="error" style="display:none;">
E-mail non valide et/ou message vide
</div>
</footer>
答案 0 :(得分:0)
`如果您使用ajax发送并使用PHPMailer发送电子邮件,那么您无法在javascript文件中检查$ mail-&gt; send()
您需要做的是以下内容 在php文件中
返回json_encode(['success'=&gt; $ mail-&gt; send()]);
并在你的ajax成功函数中执行此操作
success: function(response) {
if(response.success)
{
//success message
}
else
{
//error message
}
}
答案 1 :(得分:0)
Ajax电话:
$.ajax({
type: "POST",
url: "sendphpmailer.php",
data: dataString,
dataType:'json',
success: function(response) {
if(response.status){
/* success */
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').html(response.message).fadeIn(1000);
}else{
/* error occurred show to user */
$('.success').hide();
$('button[name="submit"]').show();
$('.error').html(response.message).fadeIn(1000);
}
}
});
PHP回复:
$response = array();
if(!$mail->send()) {
$response = array('message'=>"Mailer Error: " . $mail->ErrorInfo, 'status'=> 0);
} else {
$response = array('message'=>"Message has been sent successfully", 'status'=> 1);
}
/* send content type header */
header('Content-Type: application/json');
/* send response as json */
echo json_encode($response);