Search code examples
phplaravelemailsmtpswiftmailer

How do I use STARTTLS with swiftmailer in php?


this is my first question here so bare with me.

How do I use STARTTLS with swiftmailer in php?

I am currently on Laravel 7.x running on PHP 7.3.

I need to connect to the Office365 SMTP server, which unforunately only support STARTTLS. I cannot use another provider than Office365 due to another company infrastructure, so my options are limited.

My problem is that when i give STARTTLS as the mail encryption and attempt to connect to / send mails trough the SMTP server, I recieve the following error:

[2020-06-25 11:15:21] stage.ERROR: Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) at /home/forge/censoreddomain.com/releases/20200615132510/src/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)

My .env config mail section:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=XXXX@XXXX.se
MAIL_PASSWORD=XXXXXXXtXVF
MAIL_ENCRYPTION=starttls

Solution

  • Try to change "MAIL_ENCRYPTION=starttls" to "MAIL_ENCRYPTION=tls"