【问题标题】:PHPMailer returns SMTP error with AjaxPHPMailer 使用 Ajax 返回 SMTP 错误
【发布时间】:2016-09-12 14:43:28
【问题描述】:

我有一个使用 AJAX 和 PHP 设置的简单表单。当我键入 unix 命令php send_nl.php 时,这一切都有效,但每次它从 ajax 请求返回一个 SMTP 错误。

无法发送邮件。邮件程序错误:SMTP 连接()失败。 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;
    });
});

包括/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';
}
//}

【问题讨论】:

  • 您需要提供用户名/密码
  • 点击错误消息中的链接可能会对您有所帮助。设置$mail->SMTPDebug = 2;。由于您正在连接到 localhost(这不太可能需要用户/密码),这表明您没有运行本地邮件服务器。尽管您检查了错误,但您并没有检查查询是否真的找到了任何东西。
  • 感谢 $mail->SMTPDebug = 2;我发现了这个问题:“PHPMailer SMTP -> 错误:无法连接到服务器:权限被拒绝(13)”

标签: php jquery ajax forms phpmailer


【解决方案1】:

设置$mail->SMTPDebug = 2我发现了这个问题:

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

我解决了:https://gistpages.com/posts/phpmailer_smtp_error_failed_to_connect_to_server_permission_denied_13_fix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多