Monday 15 July 2019

php - Gmail Account SMTP -> ERROR: Failed to connect to server: Connection refused (111)




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


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.



Here is my code





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");


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. Updated PHPMailer to latest version

  2. Tried both tls on 587 and ssl on 465

  3. Verified that both ports are listening

  4. tried to connect to smtp.gmail.com through telnet using both ports (ssl and tls)

  5. Tested all three different email addresses of google apps (a@a.com, b@b.com, c@c.com). All sites are giving same error - SMTP ERROR: Failed to connect to server: Connection refused (111)

  6. Tested my code on different VPS and it "looks" to run fine over there.




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:
SERVER -> CLIENT: 250 2.1.0 OK df1sm6922498wib.12 - gsmtp
CLIENT -> SERVER: RCPT TO:

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
CLIENT -> SERVER: From: Land Registry Documents
CLIENT -> SERVER: Reply-To: Land Registry Documents
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


When it doesn't send




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

Answer



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



nameserver 8.8.8.8



nameserver 8.8.4.4


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...