在苗条框架中使用swigmailer emal的twig模板

时间:2015-09-22 10:43:06

标签: email twig slim swiftmailer

我使用

来实现我的苗条应用程序
    $app = new Slim([
    'view' => new Twig(),
    'templates.path' => INC_ROOT . '/app/views'
]);

$transport = Swift_MailTransport::newInstance();

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

我的路线我有这个代码

$app->get('/test-email', function() use ($app, $mailer){

    // Here I'm fetching my email template from my template directory.
    $welcomeEmail = $app->view('emails/test_email.twig');

    // Setting all needed info and passing in my email template.
    $message = Swift_Message::newInstance('Wonderful Subject')
                    ->setFrom(array('email@seznam.cz' => 'Me'))
                    ->setTo(array('email2@seznam.cz' => 'You'))
                    ->setBody($welcomeEmail)
                    ->setContentType("text/html");


    $results = $mailer->send($message);

    // Print the results, 1 = message sent!
    print($results);

});

代码正确发送电子邮件到地址results = 1,但是来自twig模板的正文是回显到屏幕而不是作为电子邮件发送。我想用twig模板生成发送邮件的正文。

1 个答案:

答案 0 :(得分:1)

我在

的路线中编辑我的代码
$app->get('/test-email', function() use ($app, $mailer){

    // Here I'm fetching my email template from my template directory.
    $welcomeEmail = $app->view()->render('emails/test_email.twig');

    // Setting all needed info and passing in my email template.
    $message = Swift_Message::newInstance('Wonderful Subject')
                    ->setFrom(array('email@seznam.cz' => 'Me'))
                    ->setTo(array('email2@seznam.cz' => 'You'))
                    ->setBody($welcomeEmail)
                    ->setContentType("text/html");


    $results = $mailer->send($message);

    // Print the results, 1 = message sent!
    print($results);

});` 

现在代码正在从twig模板发送带有正文的邮件。我认为$ app->视图会将tempalte渲染为屏幕,而$ app-> view() - >渲染将其渲染为变量