Search code examples
phpoutlooksmtpfsockopen

fsockopen timesout, cannot connect to Office 365 SMTP


I recently asked another question here that failed to get any traction. In summary, on a Drupal 8 site I am working on, the SMTP Authentication module test email failed to send and was throwing the error 'Error sending e-mail from [email protected] to [email protected]: SMTP Error: Could not connect to SMTP host.' I have narrowed down the issue quite a bit and it's different enough to, in my opinion, warrant a separate thread. Hopefully it's concise enough to get me some answers.

The Issue: The module is attempting the following fsockopen connection:

$this->smtp_conn = @fsockopen($host,
                              $port,
                              $errno,
                              $errstr,
                              $tval);

On lines 130-134 in file SMTP.php. The values for each variable are as follows:

  1. $host = 'smtp.office365.com'
  2. $port = '587' (also tried 25)
  3. $tval = 10

The error values are returned as follows:

  1. $errorno = 20
  2. $errstr = 'Connection timed out'

This seems like a pretty basic step for an SMTP connection, and I'm not sure why it's failing. Obviously that's a pretty vague error, but is there anything else I need to set up on the server for this to work? Maybe add a DNS record? Can anyone think of any reason for this to be timing out?


Solution

  • Two parts to this answer:

    Thanks to help from @Sammitch in the comments, I was able to determine that the issue was an issue with the webserver I am using, not an issue with Outlook's server. I talked to the support for the shared server this site is hosted on and they said 'By default, outgoing SMTP connections to external servers are not allowed for security reasons. We have now allowed outgoing SMTP connections for your account.' Once they allowed that, this immediate issue was fixed.

    Then another issue popped up. When sending the test email I was receiving the following error: 'Client does not have permissions to send as this sender'. This was caused by the 'E-mail from address' not being set to the username email. Once it was set to that, the email was sent successfully. I know that's not entirely relevant to this specific question, but it might help someone struggling with this in the future.