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 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?