【问题标题】:Symfony: Email in test mode: Indicate in email where the mail would really be sent toSymfony:测试模式下的电子邮件:在电子邮件中指明邮件真正发送到的位置
【发布时间】:2018-04-22 20:25:09
【问题描述】:

当我使用 symfony 3.4 和 swiftmailer 发送电子邮件时,我可以设置一个参数以将所有电子邮件发送到定义的电子邮件(在开发环境中有意义):

swiftmailer:
    delivery_address: "development.email@myProjectDomain.com"

现在在电子邮件本身中,如果不设置此参数,我看不到它会去哪里。我可以看看 X-Swift-To 变量的标题,但这非常麻烦......

我想要的是一个简单的信息作为邮件正文的第一行,比如

This email would be sent to customer@yahoo.com in production

我怎样才能做到这一点?是否有一些 swiftmailer 配置可以做到这一点?因为当我设置 swiftmailer 并发送电子邮件时,我无法知道电子邮件实际将发送到哪里......

【问题讨论】:

    标签: symfony swiftmailer symfony-3.4


    【解决方案1】:

    我创建了一个为我处理这类事情的服务:

    <?php
    
    namespace AppBundle\Util;
    
    use Doctrine\ORM\EntityManager;
    use Symfony\Component\Templating\EngineInterface;
    use Swift_Mailer;
    
    use AppBundle\Entity\User;
    
    class MailService
    {
        private $em;
        private $templating;
        private $mailer;
        private $env;
    
        public function __construct(EntityManager $entityManager, EngineInterface $templating, Swift_Mailer $mailer,$env)
        {
            $this->em = $entityManager;
            $this->templating = $templating;
            $this->mailer = $mailer;
            $this->env = $env;
        }
    
        private function doSend($subject, $to, $template, $vars = array())
        {
            $message = \Swift_Message::newInstance();
    
            $body = $this->templating->render($template, $vars);
    
            $message->setSubject($subject)
                    ->setFrom("foo@bar.com")
                    ->setTo($to)
                    ->setBody(
                        $body,
                        "text/html"
                    );
    
            if ($this->env != "prod") // Redirect mails in dev/staging environments
            {
                $message->setSubject("[".$this->env." ".$to."] ".$subject);
                $message->setTo("my_dev_mail@foo.com"); // hardcoded for my case, could be changed to config parameter though...
            }
    
            $this->mailer->send($message);
        }
    
        public function Send($subject, $to, $template, $vars = array())
        {
            $type = gettype($to);
    
            switch($type)
            {
                case "string":
                    $this->doSend($subject, $to, $template, $vars);
                    break;
                case "array":
                    foreach($to as $t)
                    {
                        $this->Send($subject, $to, $template, $vars);
                    }
                    break;
                case "object":
                    if ($to instanceof User)
                    {
                        $this->Send($subject, $to->getEmail(), $template, $vars);
                    } elseif (is_iterable($to))
                    {
                        foreach($to as $t)
                        {
                            $this->Send($subject, $t, $template, $vars);
                        }
                    }
                    break;
                default:
                    throw \Exception("Unknown Recipient Type.");
            }
        }
    }
    

    然后我可以使用$this-&gt;get("my.mailer")-&gt;Send($subject, $recipient, $template, $templateVars);(我的邮件使用树枝模板)

    prod 以外的环境中的邮件会被重定向,并且其主题行会更改为包含原始收件人和当前环境。

    请注意,我添加了一些额外的检查以允许 $recipient 成为 User 实体或邮件/实体数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2012-01-29
      • 2015-04-16
      • 2016-08-18
      • 2011-06-06
      • 2016-03-10
      • 1970-01-01
      相关资源
      最近更新 更多