【问题标题】:Multiple instances of Swiftmailer in Symfony2Symfony2 中多个 Swiftmailer 实例
【发布时间】:2017-08-28 06:57:32
【问题描述】:

Symfony2 使用 Swiftmailer 包发送电子邮件。

要在 Symfony2 中使用和配置 Swiftmailer,必须在文档中使用 explained 这样的配置,例如通过使用 YAML:

swiftmailer:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       smtp.gmail.com
    username:   your_username
    password:   your_password

Swiftmailer 在 Symfony2 中被定义为服务,它的实例可以在控制器中获取,如下所示:

$mailerinstance = $this->get('mailer');

现在,假设 Swiftmailer 需要两种不同的配置,例如一个使用电子邮件假脱机(例如,用于预定的时事通讯),另一个则立即发送所有新电子邮件(例如,用于密码丢失服务)。因此,我想应该定义两个单独的 Swiftmailer 实例。 如何在 Symfony2 中做到这一点?

【问题讨论】:

标签: symfony swiftmailer


【解决方案1】:

没有默认的 symfony 方式来拥有 2 个不同的实例。但是您可以创建一个扩展 swiftmailer 的新类,使其成为服务,然后将您的不同配置传递给父构造函数。

【讨论】:

  • 真的吗?!?!天哪,这太疯狂了!顺便说一句……我会听从你的建议!
  • 实体经理应该有类似的东西!一个可以定义多个实体管理器...如果定义多个 Swiftmailer 实例就好了!
【解决方案2】:
swiftmailer:
    default_mailer: second_mailer
    mailers:
        first_mailer:
        # ...
        second_mailer:
        # ...

// ...

// returns the first mailer
$container->get('swiftmailer.mailer.first_mailer');

// also returns the second mailer since it is the default mailer
$container->get('swiftmailer.mailer');

// returns the second mailer
$container->get('swiftmailer.mailer.second_mailer');

http://symfony.com/doc/current/reference/configuration/swiftmailer.html#using-multiple-mailers

【讨论】:

  • 我认为您在第二个配置行中缺少一个选项卡。
  • 2.1 已经很老了。您可能需要考虑更新。
  • 我通过创建不同的环境找到了一种方法。因为我会为命令调用第二个帐户,所以只要我在调用后添加 --env=OTHER_ENV 就可以了。
猜你喜欢
  • 2015-06-05
  • 2011-09-10
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多