SMTP Error: Could not connect to server: Connection refused (111) with Office365

Sorry if this is a road that has traveled heavily. I saw other messages about this, but nothing in them solved the problem that I encountered, or lit a light bulb that helped me solve it myself.

Here is my code:

require 'PHPMailerAutoload.php'; $config = parse_ini_file('/path/to/file/config.ini', true); $mail = new PHPMailer; $mail->SMTPDebug = 3; $mail->Debugoutput = 'html'; $mail->isSMTP(); $mail->Host = $config['host']; //smtp.office365.com $mail->SMTPAuth = true; $mail->Username = $config['username']; //an.existing.account@appinc.co $mail->Password = $config['password']; //confirmed this is being passed correctly $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->From = $config['username']; $mail->FromName = 'Website Forms'; $mail->addAddress('sales@appinc.co', 'Some Name'); $mail->addReplyTo('sender.email@somedomain.com', 'SenderFirst SenderLast'); $mail->addBCC('my.email.address@appinc.co'); $mail->isHTML(true); $mail->Subject = 'Contact Form Submission'; $mail->Body = 'Some html here'; $mail->AltBody = 'Some alt content here'; if(!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { //perform success actions exit(); } 

I have confirmed that the domain, username and password are correct and transferred correctly. It is important to note that this worked on our local dev server prior to launch. After the site has been moved to our hosting account (Hostgator), when it stops working. I confirmed with HG that port 587 is open on our server.

Here is the error message that I see:

 Connection: opening to smtp.office365.com:587, t=10, opt=array () SMTP ERROR: Failed to connect to server: Connection refused (111) SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed. 

Any help that can be provided is greatly appreciated, even if it is just a link to an article that explains why it will not work now when it is in our production environment.

+8
php phpmailer
source share
4 answers

None of the answers worked for me. After many hours, I found the problem, but only works for Cpanel / WHM

  • Log in to WHM.
  • Go to ConfigServer Security and Firewall configuration inside the plugins.
  • Choose a firewall configuration
  • SMTP Settings Filter
  • Find the SMTP_ALLOWUSER parameter and add the cpanel account username shared by coma
  • Restart the firewall.

If you do not have access to WHM, contact your ISP.

+4
source share

In PHP 5.5 and phpmailer there is an error with the port number. Do not set the port number (mail-> port = ....), this causes an error: "Error smtp failed to connect to the server connection refused 111"

Leave it with the default port number of 25 and it will work!

+1
source share

It turned out that HG needed to change the firewall settings on our server. Once they did, he did a great job. Thus, if you had a similar problem, I would recommend making sure everything is correct at your end, but then to check with your hosting provider to see what needs to be done from their end.

0
source share

If you are using cPanel / WHM, you need to make sure:

Restrict outgoing SMTP to root, exim and mailman (FKA SMTP Tweak) - disabled. (This can be edited inside "Server Settings" Settings "(Search: SMTP))

If you also have ConfigServer Security and firewall protection enabled, you will need to edit the firewall configuration. Click Firewall Settings, then select Filter by SMTP Settings. Now find the SMTP_ALLOWUSER parameter and add the cPanel username, shared by coma. Click Modify, and then restart the firewall.

0
source share

All Articles