Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

phpmailer

Sorry if this is a road heavily traveled. I've seen the other posts about this but nothing in them has either solved the problem I'm having or ignited a lightbulb that helped me solve it myself.

Here's 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']; //[email protected]
$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('[email protected]', 'Some Name');
$mail->addReplyTo('[email protected]', 'SenderFirst SenderLast');
$mail->addBCC('[email protected]');
$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've confirmed that the domain, username and password are all correct and being passed in correctly. Important to note that this worked on our local dev server prior to launch. Once the site was moved to our hosting account (Hostgator) is when it stopped working. I've confirmed with HG that port 587 is open on our server.

Here is the error message I'm seeing:

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 very much appreciated, even if it's just a link to an article that explains why it won't work now that it's in our production environment.

like image 249
Neal Williams Avatar asked Oct 11 '14 20:10

Neal Williams


People also ask

How do you fix SMTP error failed to connect to server connection refused 111?

First, ensure that the email address to which you're sending the email is spelled correctly (especially the domain name). For example, if you're sending the email to a Gmail domain, ensure that the domain is "gmail.com." Then, ensure that you've provided the correct username and password for SMTP authentication.

How do I fix SMTP failed to connect to server?

Solution - Here's How To Resolve ItEnsure that the email client has stable and uninterrupted connectivity to the email server. Request and understand any email server restrictions. Ensure that the email client does not surpass or exceed any restrictions. Ensure that the client configuration does not contain errors.

How do I enable SMTP in exchange 365?

Open the Microsoft 365 admin center and go to Users > Active users. Select the user, and in the flyout that appears, click Mail. In the Email apps section, click Manage email apps. Verify the Authenticated SMTP setting: unchecked = disabled, checked = enabled.


2 Answers

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

  • Login into WHM.
  • Go to ConfigServer Security & Firewall inside plugins option.
  • Click on Firewall configuration
  • Filter by SMTP Settings
  • Look for SMTP_ALLOWUSER option and add the Cpanel account's username separated by coma
  • Restart the firewall.

If you don't have access to WHM ask your provider.

like image 61
Juan Angel Avatar answered Oct 02 '22 14:10

Juan Angel


In PHP 5.5 and phpmailer there's a bug with the port number. Don't set port number ( mail->port = ....) this causes the error: "smtp error failed to connect to server connection refused 111"

Leave it at the default port number of 25 and it works !

like image 44
vundek Avatar answered Oct 02 '22 15:10

vundek