簡體   English   中英

Symfony 3 - 使用模板服務發送郵件有些困難

[英]Symfony 3 - Some difficulties to use templating service to send mail

我有一個Mail.php文件,其中包含一些將由我的幾個控制器使用的sendMail函數。

我必須使用“模板”服務。 但是我把它放到適當位置時遇到了問題。

我的Services.yml:

email_management:
    class: Site\PagesBundle\Utils\Mails
    arguments: ['@templating']
    public: true

我的Mail.php:

<?php

namespace Site\PagesBundle\Utils;

use Site\PagesBundle\Entity\User;
use Site\PagesBundle\Entity\UserCas;

class Mails
{
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    public function sendMail($user,$raisonMail)
    {
        $transport = \Swift_SmtpTransport::newInstance();
        $mailer = new \Swift_Mailer($transport);

        // Entête
        $message = \Swift_Message::newInstance()
            ->setFrom(array('############' => '############'))
            //->setTo($user->getEmail());
            ->setTo("############")
            ->setCharset('utf-8')
            ->setContentType('text/html');

        switch($raisonMail)
        {
            case 'formulaireInscription':
                dump($user);
                // (1) Confirmation de demande d'inscription
                $message->setSubject("subject")
                        ->setBody($this->templating->render("@Pages/swiftmail/CreationCompte/DemandeCreationCompte.html.twig",array(
                            'prenom'=>$user->getPrenom(),
                            'nom'=>$user->getNom(),
                            )));
                break;

//... other cases

在我的控制器中:

 $templating = new EngineInterface;
    $mail = new Mail($templating);
    $mail->get('email_management')->sendEmail($user,$motif);

但現在我有這個錯誤:

您必須先設置裝載程序。

在此輸入圖像描述

有人能幫助我嗎 ? 謝謝 !

假設打算選擇基於服務的選項。 請注意,服務類通常會被移動到項目中的不同文件夾中(位於PagesBundle / Service文件夾下)。

services.yml(請注意更改的路徑)

email_management:
    class: Site\PagesBundle\Service\EmailManagementService
    arguments: ['@templating']
    public: true

EmailManagementService.php(請注意更改的位置和命名空間)

<?php

namespace Site\PagesBundle\Service;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Site\PagesBundle\Entity\User;
use Site\PagesBundle\Entity\UserCas;

class Mails
{
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    ...
}

控制器中的用法:

$this->get('email_management')->sendMail($user,'formulaireInscription');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM