SMTP configuration not working in production

The problem - rather the two unrelated problems - that you're experiencing are quite straightforward:

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed.

and you have verified that the server is indeed accepting connections:

I tried connecting to the SMTP server using telnet command

Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.

Your script cannot connect to the SMTP server when run from its production server.

The likely cause is that the production server has a firewall that, to avoid abuse, prevents any connection to the outside. The server can serve Web requests, but no more.

If your test had verified that port 25 was not responding, then (after checking that the host address was correct) you could have tried telnet mail.example.in 587 instead. If that worked, it could have meant that the server is not accepting insecure connections (port 25) but is accepting secure connections. With PHPMailer you could then have tried activating secure connection:

$mail->SMTPSecure = 'tls';

or

$mail->SMTPSecure = 'ssl';

If that does not work, you might still have a firewall issue; or you might need to look at phpinfo() and verify you do have OpenSSL support available in PHP.

What you need to do

  • ask the IT people that maintain the production server to open the firewall;
  • more promisingly, ask them how to send emails from that server. Chances are that you need to use the mail() function, or use localhost or 127.0.0.1 as SMTP server. Then the emails will go out through your production server's service network.

They might tell you that port 25 is not allowed, but port (say) 465 or 567 would be allowed. You will have to update your configuration and/or add TLS/SSL accordingly (see above).

  • or you might be allowed to connect to a third party SMTP server of which you will have to supply the IP address, to allow the IT guys to open a suitable firewall window. Then the emails will go out through the third party server.

The second problem (possibly NOT a problem)

250 [email protected]... Sender OK RCPT to: [email protected] 554 Relay rejected for policy reasons

Also to avoid abuse, SMTP Servers will not let everyone connect and send emails, but only their own customers. I see that in the PHPMailer configuration you specified an user and a password. In the telnet session you did not. So it might well be that PHPmailer could send, but not connect, while your telnet can connect, but not send.

Once you solve the connection problem, your authentication problem will either be solved or will have gone away (because you'll be using a different server supplied to you by the IT guys, for example localhost).

The third problem (might never arise)

A third way of abusing services is over-use - sending too many emails to too many people. Verify with the IT guys what the acceptable policies are for sending emails.

Problems, problems

Other things to look into are the credibility of the source (you might want to send emails on behalf of some domain which has not designated your SMTP server of choice as permitted sender), and the confidentiality of the data (even with TLS/SSL connections, if you are given localhost as the SMTP server, your IT guys will have complete, unfettered, undetectable access to any email you send. You might, or might not, be okay with that).


Your error says:

SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

So let's look at https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting :

"SMTP Error: Could not connect to SMTP host."

This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).

Some techniques to diagnose the source of this error are discussed below.

GoDaddy

Popular US hosting provider GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending email. They block outbound SMTP to ports 25, 465 and 587 to all servers except their own. This problem is the subject of many frustrating questions on Stack Overflow. If you find your script works on your local machine, but not when you upload it to GoDaddy, this will be what's happening to you. The solution is extremely poorly documented by GoDaddy: you must send through their servers, and also disable all security features, username and password (great, huh?!), giving you this config for PHPMailer:

$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;

GoDaddy also refuses to send with a From address belonging to any aol, gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This is because all those domains deploy SPF and DKIM anti-forgery measures, and faking your from address is forgery.

You may find it easier to switch to a more enlightened hosting provider.


1st quote (from ibm community):

[email protected] was a member of a group (Reject) that was listed in gmail.com's "Deny messages intended for the following internet addresses" field (in the destination server's Domino Configuration document's Router/SMTP, Restrictions and Controls, SMTP Inbound Controls tab's "Inbound Intended Recipient's Controls" section).

Removing Mary's hierarchical name (Mary Jones/ABC) from the members list in the Reject (group) document allows Mary to receive messages from the Internet.

2nd quote:

Most mail servers, to prevent them being used as anonymous spam relays, are configured only to relay mail from certain hosts.


It's a bad idea to use your own SMTP. Depending of what you have to do with it, you have some great chances to have your emails blocked in some ways or marked as SPAM. And you will have to spend some times to keep your server up to date.

Use online services that are white-listed for every provider and that expose API to send your transactionnal mails : https://www.mailjet.com/ http://mailchimp.com/ ...

They often propose a free account for small volume (under 2000 emails per days). Using the API is quite trivial and can be put in place in some minutes (ex : https://dev.mailjet.com/)