首页 > 解决方案 > SMTP 错误:421 4.7.0 连接被拒绝 - PHPMailer

问题描述

我想从服务器(ubuntu server 11.10 php version 5.3.x)发送电子邮件,但我收到如下错误:

2021-01-18 05:15:24     Connection: opening to mail.domain.co.id:587, timeout=300, options=array ()
2021-01-18 05:15:24     Connection: opened
2021-01-18 05:15:24     SERVER -> CLIENT: 421 4.7.0 Connection refused
2021-01-18 05:15:24     CLIENT -> SERVER: EHLO reporting
2021-01-18 05:15:24     SERVER -> CLIENT:
2021-01-18 05:15:24     SMTP ERROR: EHLO command failed:
2021-01-18 05:15:24     SMTP NOTICE: EOF caught while checking if connected
2021-01-18 05:15:24     Connection: closed
2021-01-18 05:15:24     SMTP Error: Could not connect to SMTP host.
2021-01-18 05:15:24     SMTP connect() failed. https://github.com/PHPMailer/PHPMailer wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

在我的php代码中:

<?php
require 'PHPMailer/PHPMailerAutoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = 3;                                       // Enable verbose debug output
$mail->isSMTP();                                            // Send using SMTP
$mail->Host       = 'mail.domain.co.id';                    // Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
$mail->Username   = 'donotreply@domain.co.id';              // SMTP username
$mail->Password   = 'password';                             // SMTP password
$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
$mail->setFrom('donotreply@domain.co.id', 'Mailer');
$mail->addAddress('fajar.a@domain.co.id', 'Joe User');      // Add a recipient

// Content
$mail->isHTML(true);                                        // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()){
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
} else {
    echo "Message has been sent";
}

?>

使用相同的php代码,我尝试在 localhost 中运行此代码并成功。仅供参考,我正在使用电子邮件开发应用程序通知,并且在开发时尝试发送许多电子邮件,服务器中是否有任何问题或服务器中的某些东西阻止了我的SMTP连接?

感谢您的帮助

标签: phpapacheubuntuphpmailer

解决方案


我今天仍然能够为 GMAIL 解决这个问题。您应该将您的应用程序作为安全应用程序添加到Gmail > 管理您的 Google 帐户 > 安全性 > 应用程序密码 它会给您一个 16 个字符的密码。

在访问应用密码之前,您需要为该 Gmail 帐户设置两步验证,有关此检查的更多信息 > https://support.google.com/accounts/answer/185833

这是给 GMAIL 的。亲切的问候,瓦瑞。


推荐阅读