Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP configuration not working in production

Tags:

I'm trying to send an email when a form is submitted. I'm using PHPMailer to send the mail using the below configuration.

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.example.in'; 
$mail->Port = 25; 
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; 
$mail->Password = 'password'; 

$mail->setFrom("[email protected]" , "User");
$mail->addAddress('[email protected]', 'Receiver');
$mail->addBCC('[email protected]', 'Another user');
$mail->AddReplyTo('[email protected]', 'User');

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body    = $message;
if($mail->send())
    echo "Your request has been received. We will soon contact you.";
else echo "Unable to send your request. Please try again";

This works fine in localhost. But, when I deploy it to my server (example.in) I get the below exception.

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

--EDIT--

I tried connecting to the SMTP server using telnet command, but I'm unable to add the recipient. I get the below error?

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.
Escape character is '^]'.
220 fbs-ho-mailserver.example.in ESMTP Service (Lotus Domino Release 8.5.3FP3) ready at Fri, 16 Sep 2016 11:36:01 +0530
HELO example.in
250 fbs-ho-mailserver.example.in Hello example.in ([111.91.127.222]), pleased to meet you
MAIL from: [email protected]
250 [email protected]... Sender OK
RCPT to: [email protected]
554 Relay rejected for policy reasons.

-- EDIT --

I was able to setup this account in outlook. I'm really confused what's happening.

enter image description here

like image 814
Aakash Avatar asked Sep 16 '16 05:09

Aakash


People also ask

Why is SMTP server not working?

Check whether there is network access from CSO to the SMTP server. Check whether the firewall is blocking SMTP traffic to SMTP server or whether the ports are blocked. If the server settings and authentication settings are correct, check whether the firewall is blocking port 587 and 465 and SMTP traffic.

Why SMTP is not used to receive emails?

It takes the information it has and gives it to other computers, which lets it find the correct inboxes for delivery. But SMTP servers don't work in both directions; they can only deliver outgoing mail, not receive incoming messages. The counterpart for receiving emails would be POP3 or IMAP.


1 Answers

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.

like image 106
LSerni Avatar answered Sep 28 '22 22:09

LSerni