How to define an additional mailer service to use the spool and send instant emails in Symfony2

前端 未结 1 1090
-上瘾入骨i
-上瘾入骨i 2020-12-17 01:43

In my Symfony2 web app I\'m supposed to send two kind of emails: instant and bulk. The instant emails should be send right away while the bulk emails should be send using th

相关标签:
1条回答
  • 2020-12-17 02:10

    I found the solution here

    This is how I implemented it:

    First, I configured the default mailer service to work as a spool to send bulk emails.

    (config.yml)

    swiftmailer:
        transport: %mailer_transport%
        encryption: %mailer_encryption%
        auth_mode: %mailer_auth_mode%
        host: %mailer_host%
        username: %mailer_user%
        password: %mailer_password%
        spool:
            type: file
            path: "%kernel.root_dir%/spool"
    

    Then, inside one of my bundles (CommonBundle) I registered a new service called "instant_mailer" that maps to the Swiftmailer class.

    (service.yml)

    instant_mailer:
        class: %swiftmailer.class%
        arguments: ["@?swiftmailer.transport.real"]
    

    Finally, in my controller, whenever I want to send an email usning the spool I just do:

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

    And to send an instant email:

    $mailer = $this->get('instant_mailer');
    
    0 讨论(0)
提交回复
热议问题