Symfony 3-SwiftMail-主体的renderView

时间:2019-04-08 12:39:10

标签: function symfony email static

我想在symfony 3.4上用swiftmail发送n封邮件 我将使用-> setBody($ this-> renderView(“ ....”)作为邮件内容。

只有两个问题。首先是我使用swiftMail的函数是静态函数,因此您必须使用self ::而不是$ this->,但是self :: renderView不起作用。

另一方面,我不知道我应该以哪种形式指向链接,我在互联网上找到了所有内容,而且我不知道该采取哪种方式

目前,我已经:

$transport = \Swift_SmtpTransport::newInstance();

        // Create the Mailer using your created Transport
        $mailer = new \Swift_Mailer($transport);

        $message = \Swift_Message::newInstance()
                        ->setSubject($subject)
                        ->setFrom(array($mailFrom => 'aliasTest'))
                        ->setTo($emailAdress)
                        ->setCharset('utf-8')
                        ->setContentType('text/html')
                        ->setBody("hello"); // ->setBody(self::renderView(" ???")

        $mailer->send($message);

1 个答案:

答案 0 :(得分:0)

I would go one of two ways here:

  • make the function non-static in a controller / maybe extract it into a service
  • provide the body or the renderer as a parameter to the function.

If you remove it from the/a controller, a twig environment (\Twig_Environment $twig) can be used ($twig->render('templatename', $paramarray)).

(Disclaimer: the \Twig_Environment may be called differently / have a proper namespace in your version of twig ;o))