Gmail帐户SMTP->错误:无法连接到服务器:连接被拒绝(111)

问题描述:

我正在使用phpmailer使用Google商业应用发送电子邮件.一切运行正常,但是从最近三天开始,我开始出现以下错误

Im using phpmailer to send emails using google business apps. Everything was running fine but since last three days, I've started to get the following error

SMTP -> ERROR: Failed to connect to server: Connection refused (111)
The following From address failed: example@example.com 

奇怪的是,这不会一直发生.例如,在10封电子邮件中,我收到9封邮件的上述错误,有时有1封电子邮件发送给收件人.

The strange thing is that, this doesn't happen all the time. For example, Out of 10 emails, I get the above error for 9 and 1 email sometimes is sent out to recipient.

这是我的代码

<?php

function sendmail($mail_to,$mail_sub,$mail_mesg){

//mail("casper.kotwal@gmail.com", "LRD sendmail", "inside send mail");

require_once('classes/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug  = 2;
$mail->SMTPAuth   = true;
$mail->SMTPSecure = "ssl";
$mail->Host       = "smtp.gmail.com";
$mail->Port       = 465;
$mail->Username   = "example@example.com";
$mail->Password   = "example";
$mail->AddReplyTo('example@example.com', 'Example Documents');
$mail->AddAddress($mail_to);
$mail->SetFrom('example@example.com', 'Example Documents');
$mail->Subject = $mail_sub;
$mail->MsgHTML($mail_mesg);
$mail->Send();
}

sendmail("casper.kotwal@gmail.com", "test", "test");

编辑 我已经将phpmailer更新为5.2(最新),但仍然出现相同的错误.最糟糕的是,有时它可以工作,有时却不能.我已经检查了两个端口的tls和ssl,两者都很好听.

EDIT I've updated my phpmailer to 5.2 (latest) but still I get this same error. The worst thing is sometimes it works and sometimes it doesn't. I've checked both ports for tls and ssl, both are listening fine.

这是我到目前为止所做的

Here is what I've done so far

  1. 将PHPMailer更新到最新版本
  2. 在tls上尝试过587,在465上尝试过ssl
  3. 验证两个端口都在监听
  4. 试图使用两个端口(ssl和tls)通过telnet连接到smtp.gmail.com
  5. 测试了Google应用程序的所有三个不同的电子邮件地址(a @ a.com,b @ b.com,c @ c.com).所有站点都给出相同的错误-SMTP错误:无法连接到服务器:连接被拒绝(111)
  6. 在不同的VPS上测试了我的代码,看起来"可以在那儿正常运行.

这是发送电子邮件时调试的输出

Here is the output of debug when email is sent

SERVER -> CLIENT: 220 mx.google.com ESMTP df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: EHLO www.land-registry-documents.co.uk
SERVER -> CLIENT: 250-mx.google.com at your service, [2001:41c8:51:609:fcff:ff:fe00:3b86]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO www.land-registry-documents.co.uk
SERVER -> CLIENT: 250-mx.google.com at your service, [2001:41c8:51:609:fcff:ff:fe00:3b86]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: c3lzdGVtQGxhbmQtcmVnaXN0cnktZG9jdW1lbnRzLmNvLnVr
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: bmV3Y2FzdGxlbGVlZHM=
SERVER -> CLIENT: 235 2.7.0 Accepted
CLIENT -> SERVER: MAIL FROM:<system@land-registry-documents.co.uk>
SERVER -> CLIENT: 250 2.1.0 OK df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: RCPT TO:<casper.kotwal@gmail.com>
SERVER -> CLIENT: 250 2.1.5 OK df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: DATA
SERVER -> CLIENT: 354 Go ahead df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: Date: Wed, 13 May 2015 08:56:37 +0000
CLIENT -> SERVER: To: Casper Kotwal <casper.kotwal@gmail.com>
CLIENT -> SERVER: From: Land Registry Documents <system@land-registry-documents.co.uk>
CLIENT -> SERVER: Reply-To: Land Registry Documents <system@land-registry-documents.co.uk>
CLIENT -> SERVER: Subject: PHPMailer GMail SMTP test
CLIENT -> SERVER: Message-ID: <59cec7626d25f2022430e0660778e492@www.land-registry-documents.co.uk>
CLIENT -> SERVER: X-Priority: 3
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.10 (https://github.com/PHPMailer/PHPMailer/)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: multipart/alternative;
CLIENT -> SERVER: boundary="b1_59cec7626d25f2022430e0660778e492"
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER:
CLIENT -> SERVER: This is a multi-part message in MIME format.
CLIENT -> SERVER:
CLIENT -> SERVER: --b1_59cec7626d25f2022430e0660778e492
CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
CLIENT -> SERVER:
CLIENT -> SERVER: This is a plain-text message body
CLIENT -> SERVER:
CLIENT -> SERVER:
CLIENT -> SERVER: --b1_59cec7626d25f2022430e0660778e492
CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
CLIENT -> SERVER:
CLIENT -> SERVER: test
CLIENT -> SERVER:
CLIENT -> SERVER:
CLIENT -> SERVER:
CLIENT -> SERVER: --b1_59cec7626d25f2022430e0660778e492--
CLIENT -> SERVER:
CLIENT -> SERVER: .
SERVER -> CLIENT: 250 2.0.0 OK 1431507399 df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection df1sm6922498wib.12 - gsmtp

不发送时

SMTP ERROR: Failed to connect to server: Connection refused (111)

可能存在dns问题.检查您的/etc/resolve.conf文件中的设置,

There might be dns issues. Check your /etc/resolve.conf file set these,

名称服务器8.8.8.8

nameserver 8.8.8.8

名称服务器8.8.4.4

nameserver 8.8.4.4