SwiftMailer 无法发送 TemplatedEmail ,symfony 4

这是 swiftmailer 所说的,尽管 symfony 4 文档说我们可以发送这样的 TemplatedEmail 对象,但这是不可能的:

传递给 Swift_Mailer::send() 的参数 1 必须是 Swift_Mime_SimpleMessage 的实例,给定的 Symfony\Bridge\Twig\Mime\TemplatedEmail 实例,在 /home/tk/html/src/Service/MailService.php 的第 103 行调用

在我的 MailService 中发送我的 html 邮件的代码:

// ...

use Swift_Mailer;

use Symfony\Bridge\Twig\Mime\TemplatedEmail;


class MailService {


// ...


public function sendOwnerPollsAction( Owner $foundOwner ) {


        // anti spam , limit to every minute TODO

//      $lastSend = $admin_user->getRequestedPollsDate();

//      $now      = new \DateTime();


//      if ( date_diff( $lastSend, $now ) < 60 ) {

//          // too soon!

//          die( 'too soon!' );

//      }

//      $admin_user->setRequestedPollsDate( $now );

//      $em->persist( $admin_user );

//      $em->flush();

        $titleEmail = 'Framadate | Mes sondages';


        $templateVars = [

            'owner'          => $foundOwner,

            'title'          => $titleEmail,

            'email_template' => 'emails/owner-list.html.twig',

        ];


        $email = ( new TemplatedEmail() )

            ->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )

            ->to( new Address( $foundOwner->getEmail() ) )

            ->subject( $titleEmail )

            ->htmlTemplate( $templateVars[ 'email_template' ] )

            ->context( $templateVars );


        // send email

        return $this->mailer->send( $email );

    }

symfony 4 的 swiftmailer 文档说我们可以像那样发送邮件,并且 templatedemail 扩展了电子邮件。 https://symfony.com/doc/4.3/mailer.html#creating-sending-messages 所以我不知道我们如何发送模板化的 html 电子邮件。


包裹:


"symfony/framework-bundle": "4.3.*",

"symfony/swiftmailer-bundle": "^3.4",

"php": "^7.1.3",


慕桂英546537
浏览 119回答 1
1回答

手掌心

您正在将 Symfony Mailer 与 Swift Mailer 混合使用。TemplatedEmail 来自Symfony Mailer,但您正在尝试使用Swift Mailer 发送。因此错误。如果你真的想使用 Swift Mailer,那么需要你的 TemplateEmail 部分。Symfony 的 SwiftMailer摘录:$message = (new \Swift_Message($titleEmail))&nbsp; &nbsp; &nbsp; &nbsp; ->setFrom('ne-pas-repondre@framadate-api.cipherbliss.com')&nbsp; &nbsp; &nbsp; &nbsp; ->setTo(new Address( $foundOwner->getEmail() ))&nbsp; &nbsp; &nbsp; &nbsp; ->setBody(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->renderView(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'emails/owner-list.html.twig',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $titleEmail,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email_template' => 'emails/owner-list.html.twig'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text/html'&nbsp; &nbsp; &nbsp; &nbsp; )$mailer->send($message);如果您想将 TemplatedEmail 与Symfony Mailer 一起使用,请关注Symfony 上的Mailer 。还有其他配置需要完成。
打开App,查看更多内容
随时随地看视频慕课网APP