我正在使用 phpmailer 从 php/Javascript 发送电子邮件,它正在工作,但没有返回成功:函数(响应)。
我的 JavaScript:
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('.pendente').click(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'send_email.php',
async: true,
data: dados,
context: this,
success: function(response) {
alert("Ok!");
$(this).removeClass('btn-default').addClass('btn-warning');
}
});
});
我的send_email.php:
<?php
include "ligacao.php"; //connection string to BD
$encweb = $_POST['enc'];
$datapendente = date("Y-m-d H:i:s");
$texto = "'Pendente em ".$datapendente;
$texto = $texto."'";
$sql="UPDATE Texoleo.dbo.bo set bo.logi2=0, bo.logi4=1 ";
$sql=$sql.", bo.dataopen='".$datapendente."', bo.u_msgshist=";
$sql=$sql." concat(".$texto.", char(13)+char(10))+(cast(bo.u_msgshist as nvarchar(600))) ";
$sql=$sql." where bo.fref='".$encweb."'";
$result = odbc_exec($connect, $sql);
/**** Fim sql ****/
/**** Enviar email ****/
$clientenome = $_POST['nome'];
$clienteemail = $_POST['email'];
include('lib/class.phpmailer.php');
include('lib/class.smtp.php');
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'myhost.com';
$mail->SMTPAuth = true;
$mail->Username ='user@user.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('my@email.com', 'Me');
$mail->addAddress('customer email', 'Customer name');
$mail->isHTML(true);
$texto = 'bla, bla, bla ';
$mail->Subject = 'My subject';
$mail->Body = $texto;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
};
$response = array("success" => true);
echo json_encode($response);
?>
在不使用 $mail->send() 的情况下测试相同的 Javascript,它会返回 success true,但使用 $mail->send() 时,它会执行它必须执行的操作(一些 SQL 更新和发送电子邮件),但不会返回成功后继续执行 Javascript 代码。
最佳答案
正如我的评论:
调试输出与 JSON 格式混淆。通过设置 SMTPDebug = 0
禁用它,并删除任何其他非 JSON 输出。
如果您在浏览器的开发工具中检查 XHR 请求的响应,通常会看到这种情况发生。
关于javascript - 通过 phpmailer 发送电子邮件不会返回 Javascript 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49900902/