我想通过单击HTML发送到邮件。我使用ajax来调用PHP文件。我包括了用于发送电子邮件的PHP邮件程序代码。而且我还在底部包括了一个打印json_encode(“ Success”)。我无法将成功消息从php发送回JS。我已经在PHP中使用了Print和许多其他输出命令,但无法使用。
$.ajax({
url:"api/collect_money.php",
type:"POST",
data:ajax_data,
async:false,
success:function(response)
{
console.log(response,"hello");
}
});
这是我的PHP:
function sendmail($bills_count_data)
{
//print_r($bills_count_data[0]['end_date']);
//Load Composer's autoloader
require 'php/vendor/phpmailer/phpmailer/src/Exception.php';
require 'php/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'php/vendor/phpmailer/phpmailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try
{
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '****@gmail.com'; // SMTP username
$mail->Password = '****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
// Add a recipient
$mail->addAddress('****@gmail.com');
//$mail->addAddress('****@thewashhouseinc.com'); // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Collect Money Notification';
$mail->Body = "Collection has been made on ".$bills_count_data[0]['end_date']." The list of bills: one's ".$bills_count_data[0]['one']." two's ".$bills_count_data[0]['two']."";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
//echo 'Message has been sent';
}
catch (Exception $e)
{
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
sendmail($bills_count_data);
}
print ("success");
我在控制台上看不到任何响应。当我执行这段代码时
答案 0 :(得分:0)
这可能是您的问题
$mail->SMTPDebug = 2;
这会输出各种各样的东西,这些东西与JSON并非遥不可及,并且将导致您解析响应失败。如果您在浏览器的开发工具中查看原始响应,则应将其全部看到。使用以下命令禁用调试输出:
$mail->SMTPDebug = false;