Symfony 4 Swift Mailer无法发送邮件。

3
我正在使用Symfony 4和FOSUserBundle在本地服务器上进行工作。但我无法在新用户注册时收到电子邮件确认。
我尝试了以下文章,但在我的情况下它没有起作用:Symfony 4 SwiftMailer Gmail : Email not sent 我已经尝试配置SwiftMailer使用gmail smtp服务器和mailtrap smtp服务器发送邮件,但没有成功。我还检查了dev.log文件,没有发现错误。
我不确定该如何正确配置Swift Mailer(.env或packages/dev/swiftmailer.yaml)。目前的配置如下:
MAILER_URL=gmail://***@gmail.com:***@localhost

swiftmailer.yaml:

swiftmailer:
transport:        gmail
username:         ***@gmail.com
password:         ***
host:             localhost
port:             465
encryption:       ssl
auth-mode:        login
spool: { type: 'memory' }
stream_options:
    ssl:
        allow_self_signed: true
        verify_peer: false
        verify_peer_name: false 

有什么想法吗?使用gmail作为smtp服务器并不是必需的。

提前感谢。

解决方案:

问题出在/config/test/fos_user.yaml文件中:

我进行了更改:

service:
  mailer: 'fos_user.mailer.noop'

致:

service:
  mailer: 'fos_user.mailer.default'

文档:http://symfony.com/doc/master/bundles/FOSUserBundle/emails.html

此外,为了使其正常工作,我已经在Gmail帐户设置中接受了较不安全的连接。


请尝试移除参数port、host、auth-mode和retry a send。 - Mcsky
仍然无法工作。 - Alvaro Garcia Solano
如果您尝试通过swiftmailer:email:send命令发送邮件,输出结果是什么? - Philippe-B-
我已经编辑了问题并添加了解决方案。感谢评论和帮助 ;) - Alvaro Garcia Solano
很高兴你解决了问题。请从你的问题中删除解决方案,将其放入答案中并接受你的答案,这可能会帮助其他人。 - Philippe-B-
2个回答

4

我在Symfony 4上遇到了同样的问题。我的包版本是 swiftmailer/swiftmailer v6.1.0 symfony/swiftmailer-bundle v3.2.2 当我使用以下配置时: swiftmailer: url: '%env(MAILER_URL)%' spool: { type: 'memory' }

邮件没有发送,也没有出现异常。 然后我把设置改成了:

swiftmailer: url: '%env(MAILER_URL)%' spool: type: 'file' path: '%kernel.cache_dir%/swiftmailer/spool'

并尝试了以下命令:

php bin/console swiftmailer:spool:send --env=dev -vvv

我看到了错误:

[Swift_SwiftException]
No IDN encoder found (install the intl extension or the true/punycode package
于是我通过以下方式安装了true/punycode包:

composer req true/punycode

现在邮件正常发送,内存中的spool也正常运作。


2

Symfony邮件程序的默认行为是立即发送电子邮件,但是根据您的配置,它将“缓存”电子邮件而不是直接发送它们。

spool: { type: 'memory' }

从 spool 发送消息是通过一个控制台命令单独完成的:
php bin/console swiftmailer:spool:send --env=dev

更新: 正如第一条评论中@nek所说,内存 spool 会立即发送邮件(如果没有发生异常)。只有在使用文件 spool 时才需要使用 spool:send 命令。

更多文档请在此处查看。


1
memory 暂存区会立即发送邮件(如果没有发生异常)。只有在使用 file 暂存区时才需要使用 spool:send 命令。 - j-guyon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接