简体   繁体   中英

Default mail sender with Swiftmailer on Symfony 2.8

I'm desperately trying to send several emails using the same email address configured as following on config.yml

# Swiftmailer Configuration
    swiftmailer:
    transport: '%mailer_transport%'
    host: '%mailer_host%'
    username: '%mailer_user%'
    password: '%mailer_password%'
    sender_address : example@example.com
    spool: { type: memory }

The problem is, I can't find out how to use that particular sender_address parameter. The Symfony documentation says that if defined, it will be set automatically

    $new_campaign_mail = \Swift_Message::newInstance()
        ->setSubject('Test')
        ->setTo($user->getEmail())
        ->setBody($this->templating->render('AcmeBundle:Default:email.html.twig', array('data' => $object, 'user' => $user)), 'text/html');

 /* Is that useful ?
    $this->container->get('swiftmailer.plugin.impersonate'); */

    $this->mailer->send($new_campaign_mail);

Thanks everyone ! :)

You have to fetch that parameter sender_address and add it like:

    $new_campaign_mail = \Swift_Message::newInstance()
    ->setFrom($your_sender_address_parameter)
    ->setSubject('Test')
    ->setTo($user->getEmail())
    ->setBody($this->templating->render('AcmeBundle:Default:email.html.twig', array('data' => $object, 'user' => $user)), 'text/html');

You can find out here how to get parameter, basically if you are doing this in controller $this->getParameter('sender_address'); or you can inject it in service as:

services:
    app.service.mail:
        class:  AppBundle\Service\MailService
        arguments: [%sender_address%]

EDIT:

I just read your post one more time and I see that there is an option in swiftmailer for sender address but I do not think that it is what you are looking for as this is the explanation that can be read here :

If set, all messages will be delivered with this address as the "return path" address, which is where bounced messages should go. This is handled internally by Swift Mailer's Swift_Plugins_ImpersonatePlugin class.

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