首页 > 解决方案 > 在 Laravel 中使用 Office365 直接发送 SMTP

问题描述

我正在尝试为 Office365 设置直接发送 SMTP 方法,但收到一条错误消息:

Swift_TransportException:无法与主机 {our-tenant-id}.mail.protection.outlook.com 建立连接 [无法建立连接,因为目标机器主动拒绝了它。

Microsoft 文档说这些是必需的设置:

| Server/smart host | Your MX endpoint, for example, contoso-com.mail.protection.outlook.com
| Port              | Port 25
| TLS/StartTLS      | Enabled
| Email address     | Any email address for one of your Office 365 accepted domains. 
|                   | This email address does not need to have a mailbox.

所以这些是我在.env文件中使用的设置:

MAIL_DRIVER=smtp
MAIL_HOST={our-tenant-id}.mail.protection.outlook.com
MAIL_PORT=25
MAIL_USERNAME=mailbox@ourdomain.co.uk
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

根据文档,我还检查了我们的 SPF 记录,我可以确认它包含正确的条目。

以前有没有其他人设置过这个或者知道我可能哪里出错了?

标签: phplaraveloffice365swiftmailer

解决方案


我们使用类似的设置。请注意,建立与办公室的连接可能会有点慢,因此您需要对电子邮件进行排队,以确保用户无需等待。

MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=mailbox@ourdomain.co.uk
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

推荐阅读