【问题标题】:Sending email using office365 server with swiftmailer in symfony在 symfony 中使用 office365 服务器和 swiftmailer 发送电子邮件
【发布时间】:2020-01-12 00:03:03
【问题描述】:

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

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

MAILER_URL=smtp://smtp.office365.com:587?encryption=ssl&auth_mode=login&username="myusername@mycompany.com"&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('myusername@mycompany.com')
            ->setTo('test@gmail.com')
            ->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 我得到一个有效的响应,所以我认为问题与网络无关,端口没有被阻塞。

【问题讨论】:

  • 你检查邮件地址是否被正确解析了吗?既然你提到了双引号,我可以想象它们可能会在某个时候被转义。
  • @dbrumann 我不认为双引号有什么不同,当我删除它们并对特殊字符进行编码时,我得到了同样的错误

标签: php symfony office365 symfony4 swiftmailer


【解决方案1】:

我也遇到过类似的问题,我通过登录账号修改密码解决了。

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

我的环境:

MAILER_DSN=smtp://support@company.com:password@smtp.office365.com:587

【讨论】:

    【解决方案2】:

    Symfony 4 使用Mailer Component,如果您更熟悉,那么您可以使用如下环境变量

    # Office 365 Outlook
    MAILER_DSN=smtp://support@company.com:password@smtp.office365.com:587
    

    AND config/packages/mailer.yml

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

    预装的唯一传输是 SMTP。不需要任何其他传输包,不像 google-mailer 或 amazon-mailer..

    它对我有用。

    【讨论】:

      【解决方案3】:

      您必须使用encryption=tls,对您的 USER_NAME 和 PASSWORD 进行 urlencode,并且您不应像以前那样将 USER_NAME 和 PASSWORd 括在引号中。

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

      USERNAME = `email@domain.tld` → `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
      

      【讨论】:

        【解决方案4】:

        试试这个:

        MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&username="myusername@mycompany.com"&password="mypassword"
        

        【讨论】:

        • 这个组合不走运。我得到Failed to authenticate on SMTP server with username "myusername@mycompany.com" using 1 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535 5.7.3 Authentication unsuccessful [VI1PR02CA0063.eurprd02.prod.outlook.com] ".
        • @Mario 你确定使用 username@mycompany.com,而不是 emailaddress@mycompany.com 吗?
        • 我已经尝试了所有可能的用户名和电子邮件地址组合:(
        • @MarioKlisanic 注意 如果用户名、密码或主机包含在 URI 中被视为特殊的任何字符(例如 +、@、$、#、/、:、*、!),则必须对其进行编码.有关保留字符的完整列表,请参阅 RFC 3986 或使用 urlencode 函数对其进行编码。 symfony.com/doc/4.2/email.html#configuration
        • 尝试像urlencode ( $username ) 一样编码$username/password 并将结果复制到.env 中
        猜你喜欢
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 2014-11-19
        • 1970-01-01
        • 2018-04-03
        相关资源
        最近更新 更多