dopnpoh056622 2016-09-12 14:43
浏览 29
已采纳

PHPMailer使用Ajax返回SMTP错误

I have a simple form set up with AJAX and PHP. This all works when I type the unix command php send_nl.php, but every time it returns a SMTP error from the ajax request.

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

newsletter.php

$(document).ready(function() {
    $('#risultato tbody').on( 'click', '.invianl', function (e) {
        e.preventDefault();

        var jqxhr = $.ajax({
            url: includes/api_send_nl.php, 
            type: "POST", 
            data: {}
        }).done(function(json) {
            alert(json);
        }).fail(function(data) {
            alert( "error" +data );
        });

        console.log(jqxhr);
        return false;
    });
});

includes/api_send_nl.php

require "common.php" ;
require 'PHPMailer/PHPMailerAutoload.php';

$id = 2;
//$email_id=$_GET['email_id'];

//function sendMail($id){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->Port = 25;

//$mail->SMTPDebug = 1;

$stmt=$db->prepare("select * from newsletter_email where email_id='$id'");
if (!$stmt) {
    log_msg($db->error);
    die();
}
if (!test_execute($stmt->execute())) die("ERRORE QUERY 2");
$newsletter=fetchArray($stmt);
$stmt->close();
$newsletter=$newsletter[0];

$mail->setFrom($newsletter['from_email'], $newsletter['from_name']);
$mail->addAddress('info@mydomain.com');
$mail->addReplyTo($newsletter['replyto_email'], $newsletter['replyto_name']);

$mail->isHTML(true);

$mail->Subject = $newsletter['subject'];
$mail->Body    = $newsletter['body'];
$mail->AltBody = 'Se non riesci a visualizzare la mail, clicca qui';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
//}
  • 写回答

1条回答 默认 最新

  • dongzg2006 2016-09-12 15:07
    关注

    Settings $mail->SMTPDebug = 2 I discovered this issue:

    PHPMailer SMTP -> ERROR: Failed to connect to server: Permission denied (13)
    

    And I solved with: https://gistpages.com/posts/phpmailer_smtp_error_failed_to_connect_to_server_permission_denied_13_fix

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Qt 不小心删除了自带的类,该怎么办
    • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
    • ¥15 有没有哪位厉害的人可以用C#可视化呀
    • ¥15 可以帮我看看代码哪里错了吗
    • ¥15 设计一个成绩管理系统
    • ¥15 PCL注册的选点等函数如何取消注册
    • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
    • ¥15 Python爬虫程序
    • ¥15 crypto 这种的应该怎么找flag?
    • ¥15 代码已写好,求帮我指出错误,有偿!