简体   繁体   中英

set default FROM for SwiftMailer in Symfony

mailer:
  class: sfMailer
  param:
    logging:           %SF_LOGGING_ENABLED%
    charset:           %SF_CHARSET%
    delivery_strategy: realtime
    transport:
      class: Swift_SmtpTransport
      param:
        host:       localhost
        port:       25
        encryption: ~
        username:   user@gmail.com
        password:   pass

this is config for swift mailer. if i would like send email then i must use:

    $message = $this->getMailer()->compose(
      array('user@gmail.com' => 'user'),
      $affiliate->getEmail(),
      'Jobeet affiliate token',
      body
    );

    $this->getMailer()->send($message);

i would like set this line:

array('user@gmail.com' => 'user'),

automatically. if i change user@gmail.com in config then i would like automatically change in this line. i have mailer in few place in my site. Is possible?

maybe same as in app.yml ?

thanks for help

You can make next:

in app.yml:

all:
from_mail: user@gmail.com

And make:

array(sfConfig::get("app_from_mail") => 'user'),

If you use Symfony version ≥ 2, you can do the following.

Install the Swift Mailer Defaults plugin by running composer require finesse/swiftmailer-defaults-plugin in the console. Then add the following code to your yml config:

services:
    # Swift Mailer plugins
    app.swiftmailer.defaults_plugin:
        class: Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin
        tags:
            - { name: swiftmailer.default.plugin }
        arguments:
            $defaults:
                from:
                    user@gmail.com: user
                # You can add more default properties here, e.g. sender, reply to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM