Symfony 2.8 上带有 Swiftmailer 的默认邮件发件人

Default mail sender with Swiftmailer on Symfony 2.8

我正拼命尝试使用在 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 }

问题是,我不知道如何使用那个特定的 sender_address 参数。 Symfony 文档说如果定义了,它会被自动设置

    $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);

谢谢大家! :)

您必须获取该参数 sender_address 并将其添加为:

    $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');

您可以找到 here 如何获取参数,基本上如果您在控制器中执行此操作 $this->getParameter('sender_address'); 或者您可以将其注入服务中:

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

编辑:

我刚刚又读了一遍你的 post,我看到 swiftmailer 中有一个发件人地址选项,但我认为这不是你要找的,因为这是可以解释的被阅读 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.