使用office365服务器和symfony中的swiftmailer发送电子邮件

问题描述 投票:0回答:5

我尝试在 symfony 中使用 swiftmailer 和 office365 服务器注册后发送确认电子邮件。我已经尝试了我遇到的主机、端口和加密类型的每种组合。

目前我的 .env 文件包含这一行:

MAILER_URL=smtp://smtp.office365.com:587?encryption=ssl&auth_mode=login&username="[email protected]"&password="mypassword"

*注意:我使用“”作为我的用户名和密码,因为它们包含特殊字符,并且我在某处读到这可能会导致 MAILER_URL 出现问题。

我的 swiftmailer.yaml 文件包含以下内容:

swiftmailer:
    url: '%env(MAILER_URL)%'
    stream-options:
        ssl:
            allow_self_signed : true
            verify_peer: false

最后,我在控制器中使用此代码发送电子邮件:

 $message = (new \Swift_Message('Referral tool registration'))
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
                $this->renderView(
                    'email/notification/user_registered.html.twig',
                    ['firstName' => $user->getFirstName(),
                     'lastName' => $user->getLastName()
                    ]
                    ),
                'text/html'
            );

 $mailer->send($message);

通过当前选择的主机、端口和加密,我得到:

"Connection could not be established with host smtp.office365.com [ #0]"

更新: 当我输入 telnet smtp.office365.com 587 时,我得到了有效的响应,所以我认为问题与网络无关,端口没有被阻止。

php symfony office365 symfony4 swiftmailer
5个回答
9
投票

您必须使用

encryption=tls
,对您的 USER_NAME 和 PASSWORD 进行 urlencode,并且您不应该像以前那样将 USER_NAME 和 PASSWORD 用引号引起来。

让我们假设这些凭据并对它们进行 urlencode:

USERNAME = `[email protected]` → `email%40domain.tld`
PASSWORD = `pa$$w#rd` → `pa%24%24w%23rd`

使用上述凭据的正确配置将如下所示:

MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&auth_mode=login&username=email%40domain.tld&password=pa%24%24w%23rd

2
投票

我也有类似的问题,我通过登录账号并更改密码解决了。

我的案例 - 帐户是新的,登录后的第一个屏幕是输入字段:当前密码、新密码、重复密码。

我的环境:

MAILER_DSN=smtp://[email protected]:[email protected]:587

1
投票

试试这个:

MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&username="[email protected]"&password="mypassword"

0
投票

Symfony 4 使用 Mailer 组件,如果您熟悉的话可以使用 env 变量,如下所示

# Office 365 Outlook
MAILER_DSN=smtp://[email protected]:[email protected]:587

和配置/packages/mailer.yml

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

唯一预装的传输是 SMTP。不需要任何其他传输包,不像谷歌邮件程序或亚马逊邮件程序..

它对我有用。


0
投票

在 Symfony 4 中,它与我一起工作

#.env.local
MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&auth_mode=login&username=mymail%40outlook.com&password=MyPassword
# config/packages/mailer.yaml
framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'
© www.soinside.com 2019 - 2024. All rights reserved.