Php 如何在Symfony的不同服务中注入不同的SwiftMailer邮件程序?
我有一个Symfony 2.8项目,需要使用两个独立的邮件服务(例如Google SMTP和Yahoo SMTP)。我已经根据文档进行了配置:Php 如何在Symfony的不同服务中注入不同的SwiftMailer邮件程序?,php,symfony,yaml,swiftmailer,Php,Symfony,Yaml,Swiftmailer,我有一个Symfony 2.8项目,需要使用两个独立的邮件服务(例如Google SMTP和Yahoo SMTP)。我已经根据文档进行了配置: swiftmailer: default_mailer: mailer_google mailers: mailer_google: transport: smtp host: %mailer_google_host% port: %mailer_go
swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%
%variables%
的值都是在parameters.yml
中定义的,在这里不相关
我尝试使用一个自定义的Mailer类,该类用于定义两个不同的服务,如下所示:
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @mailer
- '%mailer_google_sender%'
aalaap.services.mailer_yahoo:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @mailer_yahoo
- '%mailer_yahoo_sender%'
现在,由于默认的mailer设置为mailer\u google
,在第一个服务定义中,只指定@mailer
就可以了。但是,第二个服务找不到@mailer\u yahoo
CheckExceptionInValidReferenceBehaviorPass.php第58行中的ServiceNotFoundException:
服务“aalaap.services.mailer_yahoo”依赖于不存在的服务“mailer_yahoo”
我已经用@mailer.mailer\u yahoo
试过了,但也没用。如果我将第一个服务定义更改为使用特定的@mailer\u google
,而不是仅使用默认的@mailer
,这也不起作用。与@swiftmailer.mailers.mailers\u yahoo
没有任何关系
如何将第二个(或非默认)邮件程序注入服务?因此,以下是如何设置多个邮件程序帐户并使用Symfony处理注入: 设置swiftmailer配置:
swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%
然后通过以下方式在服务中注入想要的邮件程序(例如,google邮件程序):
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @swiftmailer.mailer.mailer_google
- '%mailer_google_sender%'
或者(因为在这种情况下,谷歌邮箱是默认的邮箱):
因此,以下是如何设置多个mailers帐户并使用Symfony处理注入: 设置swiftmailer配置:
swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%
然后通过以下方式在服务中注入想要的邮件程序(例如,google邮件程序):
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- @swiftmailer.mailer.mailer_google
- '%mailer_google_sender%'
或者(因为在这种情况下,谷歌邮箱是默认的邮箱):
我认为主要的服务是
swiftmailer
。请尝试在控制台中运行此命令,并检查容器中有哪些服务选项php-bin/console-debug:swiftmailer
您是否尝试过@swiftmailer.mailer.mailer\u yahoo(而不是@swiftmailer.mailers.mailer\u yahoo)?@UgoT。我刚在发帖后发现了这个问题!在这个网站上发布问题后,我几乎总是自己找出答案。不管怎样,如果你把这个作为一个答案,我会接受的@RyanVincent没有重复,因为这是专门针对注射问题的。我认为主要服务是swiftmailer
。请尝试在控制台中运行此命令,并检查容器中有哪些服务选项php-bin/console-debug:swiftmailer
您是否尝试过@swiftmailer.mailer.mailer\u yahoo(而不是@swiftmailer.mailers.mailer\u yahoo)?@UgoT。我刚在发帖后发现了这个问题!在这个网站上发布问题后,我几乎总是自己找出答案。不管怎样,如果你把这个作为一个答案,我会接受的@RyanVincent没有重复,因为这是专门针对注射问题的。