我遇到以下问题:我的Bootstrap 4 .html网站上有一个联系表。该表格是一个基于PHPMailer并带有Bootstrap验证程序的简单表格。以前,我是从联系表单中收到的电子邮件,其中没有SMTP,一切都很好。现在,我决定使用Gmail SMTP并更改了代码。我正在收到我的gmail收件箱中的邮件,但是一旦收到信件,我就会在网站上看到错误。
您能否让我知道我的问题在哪里?我摔断了头试图解决问题。
ajax.js文件:
$(document).ready(function() {
function submitForm(){
var name =$("input[name=name]").val();
var email =$("input[name=email]").val();
var comment =$("textarea[name=comment]").val();
var captcha=grecaptcha.getResponse();
var form = $('form')[0];
var formData = new FormData(form);
formData.append('name', name );
formData.append('email', email );
formData.append('comment', comment );
formData.append('captcha', captcha );
$.ajax({
url: "include/ajax/send.php",
type: "POST",
contentType: false,
processData: false,
data: formData,
cache: false,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
php文件:
// send email
$mail = new PHPMailer;
// Email Template
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "xxx";
/* Replace the email address and the Email Subject */
$EmailTo = "[email protected]"; // The Send To Email
$Subject= "New Message"; // Email Subject
// reCAPTCHA SEcret Key
$reCaptchaSecretKey = "xxx";
//retrive form variables from ajax
$name =@$_REQUEST["name"];
$email =@$_POST["email"];
$comment =@$_POST["comment"];
$captcha=@$_REQUEST["captcha"];
$formGoogleCaptcha = $captcha;
//check Catptcha
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$reCaptchaSecretKey."&response=".$formGoogleCaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$obj = json_decode($response);
if ($obj->success == 1 || $obj->success == true) {
$email_body="";
$email_body = $email_body . "<strong>First Name: </strong>" . $name . "\r\n\r\n";
$email_body = $email_body . "<strong>Email: </strong>" . $email . "\r\n\r\n";
$email_body = $email_body . "<strong>Message: </strong>" . $comment . "\r\n\r\n";
$email_body = nl2br($email_body);
$mail->SMTPDebug = 1;
我还附了一个错误示例
预先感谢您,队友。
最佳答案
您看到的是调试输出,它的出现是因为您启用了调试输出。更改此:
$mail->SMTPDebug = 1;
至
$mail->SMTPDebug = 0;
请注意,您可以在脚本的早期将其关闭,但是稍后再将其重新打开。
一个单独的问题是您使用的是旧版本的PHPMailer。 Get the latest。
关于javascript - PHPMailer SMTP麻烦-收到信件但网站上有错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60683378/