1
votes

I am having trouble sending emails to the email address specified in config_dev.yml:

swiftmailer:
    delivery_addresses: ['[email protected]']

I am trying to send from a console command:

php app/console ecard:task:run send --env=dev

Console command code:

...
foreach ($emailQueue as $item) {
    $transport = Swift_SmtpTransport::newInstance($item['smtp_host'])
        ->setPort($item['smtp_port'])
        ->setUserName($item['smtp_login'])
        ->setPassword($item['smtp_password'])
        ->setEncryption('ssl');

    $mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance()
           ->setSubject($item['email_subject'])
           ->setFrom($item['email_from'], $item['email_from_name'])
           ->setTo($item['email_to'])
           ->addPart($item['email_template'], 'text/html');

    $ret = $mailer->send($message, $failedRecipients);
        ....
}

The emails are sent to the real recipient addresses, and not the one I specified in config_dev.yml.

If I dump $this->getContainer()->getParameter('kernel.environment') in code, its 'dev'.

I don't get the $mailer this way: $this->getContainer()->get('mailer'), because I want specify/change the smtp settings in cycle - the config is stored in the database, and not in the parameters.yml. Maybe this is the problem?

(Symfony version 2.8.16, Swiftmailer 5.4.5)

1
do you have any log about it ?Mert Öksüz
nothing relevant in prod.log or dev.log, what do you mean?jalso

1 Answers

3
votes

If you get your mailer with:

$mailer = Swift_Mailer::newInstance($transport);

then it ignores all the parameters specified in config.yml.

In this case you must set the destination address in code, as you do with the other mailer parameters, something like:

  $message = Swift_Message::newInstance()
       ->setSubject($item['email_subject'])
       ->setFrom($item['email_from'], $item['email_from_name'])
       ->setTo($this->getContainer()->getParameter('swiftmailer.delivery_addresses')?:$item['email_to'])
       ->addPart($item['email_template'], 'text/html');