Symfony SwiftMailer:电子邮件仅在AppBundle中发送,而不在其他bundle中发送



我有单独的前端(FrontendBundle(和后端(AppBundle(捆绑包。我想在两个捆绑包的控制器方法中发送电子邮件。我用来发送电子邮件的代码如下所示:

$message = (new Swift_Message('Hello Email'))
->setFrom('xxxxxxx')
->setTo('xxxxxxxx')
->setBody(
$this->templating->render(
'@AppBundle/emails/test.html.twig'
),
'text/html'
)
;
$this->mailer->send($message);

当我将此代码放在 AppBundle 中的控制器方法中时,它运行完美。但是,当我将代码放在 FrontendBundle 中的控制器方法中时,电子邮件不会发送,也不会收到错误。

因此,代码本身似乎可以正常工作。我想这可能是一个配置问题,但我不知道去哪里看。

提前致谢

更新:

配置.yml

# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'

参数.yml

parameters:
mailer_transport: smtp
mailer_host: xxxxxxxx
mailer_user: xxxxxxxx
mailer_password: xxxxxxxx

这可能也是相关的:我设置了虚拟主机,以便AppBundle在app.localhost上运行,FrontendBundle在本地主机上运行。这反映在Symfony Profiler中显示的消息ID中(例如,f4f2a2604asd5618d4caf892e5e4e5@app.localhost(。

我会查看配置多个邮件程序,因为您似乎在使用两个不同的主机时遇到问题:

config.yml:

swiftmailer:
default_mailer: app_mailer
mailers:
app_mailer:
transport: '%mailer_transport%'
host: '%mailer_host_app%'
username: '%mailer_user%'
password: '%mailer_password%'
frontend_mailer:
transport: '%mailer_transport%'
host: '%mailer_host_frontend%'
username: '%mailer_user%'
password: '%mailer_password%'

参数.yml:

parameters:
mailer_transport: smtp
mailer_host_app: app.localhost
mailer_host_frontend: localhost
mailer_user: xxxxxxxx
mailer_password: xxxxxxxx

然后,您需要根据服务中所需的邮件程序注入正确的邮件程序。 如果您可以直接访问容器,则这些服务如下所示:

// since app_mailer was the default, these are equivalent
$container->get('swiftmailer.mailer.app_mailer');
$container->get('swiftmailer.mailer');
$container->get('swiftmailer.mailer.frontend_mailer');

最新更新