Search code examples
phpsymfony-1.4swiftmailermailer

Factories.yml parameters for Multiple Mailer instances in Symfony 1.4


I'm working on a Symfony 1.4 project, this is factories.yml of the default mailer :

  mailer:
    class: sfMailer
    param:
      logging:           %SF_LOGGING_ENABLED%
      charset:           %SF_CHARSET%
      delivery_strategy: realtime
      transport:
        class: Swift_SmtpTransport
        param:
          host:       127.0.0.1
          port:       25
          encryption: ~
          username:   ~
          password:   ~

And this is the PHP code:

$config = sfFactoryConfigHandler::getConfiguration($applicationConfiguration->getConfigPaths('config/factories.yml'));

    self::$mailer = new $config['mailer']['class']($applicationConfiguration->getEventDispatcher(), $config['mailer']['param']);

Where $config['mailer']['class'] is the sfMailer.

What I want is to instianciate many Swift Mailers, with different parameters, and for each instance, there are configuration params in the factories.yml file.

How can I do that ? and how can I get the params I want by providing the mailer's name?


Solution

  • Not quite what you are after as its not actually defining the mailer in the yml file but you can achieve a similar behaviour by instantiating a new mailer like so (here an example with sendgrid):

    $transport  = Swift_SmtpTransport::newInstance('smtp.sendgrid.net');
    $transport->setUsername('YOU USERNAME');        
    $transport->setPassword('YOUR PASSWORD');
    
    $swift      = Swift_Mailer::newInstance($transport); 
    $swift->send($message);