Sending emails using office 365 via php, connection refused (GoDaddy)

I'm at my wits end. We were having issues with sending email on a GoDaddy hosting, mostly that it was too unreliable (sometimes it worked flawlessly, sometimes I have to send several emails for one to go through, long queues, and so on. This is not the first time I've had these issues with them, by the way). So, I called them and their recommended solution was to set up an office 365 account and use that instead. I passed that information to my client, and he did it (albeit not via Godaddy's office 365 product, but directly with Microsoft, which certainly helps explain the difficulty), but I still cannot connect. I started to research this issue, but at this point I can say I am just lost. Here's everything I tried / learned, as well as the connections I'm trying.

My PHP setup is laravel 5.2, sending email via swiftmailer. The connection data is on an .env file:

MAIL_DRIVER=smtp

MAIL_HOST=smtp.office365.com

MAIL_PORT=587

[email protected]

MAIL_PASSWORD=password

MAIL_ENCRYPTION=tls

And a sample php script sending emails

$logger = new \Swift_Plugins_Loggers_EchoLogger();                      
        Mail::getSwiftMailer()->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));                       

        $master_email = "[email protected]";
        Mail::send('emails.contact_us', $data, function ($message) use ($master_email, $request) {
            $message->from("[email protected]");
            $message->to($master_email)->subject('Subject');
        });

And it fails with the message

++ Starting Swift_SmtpTransport !! Connection could not be established with host smtp.office365.com [Connection refused #111] (code: 0)

And so, I'm lost. I need this to work somehow, sending emails via microsoft's smtp server (and not godaddy's) specially because my client already set up his office 365 account and paid for it, so...yeah. Am I missing some configuration beyond the DNS changes? Should I connect to some other smtp server? Change hosts? Am I missing some parameter on my PHP scripts?

This seems to be fairly common issue, but it is surprisingly hard to find some definite answer. Hopefully my case will help others attempting something similar.

Upvotes: 1

Views: 2264

Answers (1)

Hevelson Rosario
Hevelson Rosario

Reputation: 440

I had with the same problem in Laravel OctoberCMS on Godaddy. The only config that worked for me is:

MAIL_DRIVER=sendmail

MAIL_HOST=smtp.office365.com

MAIL_PORT=587

[email protected]

MAIL_PASSWORD=password

MAIL_ENCRYPTION=tls

I hope this helps

Upvotes: 1

Related Questions