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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?