简体   繁体   中英

Symfony 3 - Some difficulties to use templating service to send mail

I have a Mail.php file that contains a sendMail function that will be used by several of my controllers.

I got to have to use the "templating" service. But I have problems putting it in place.

My Services.yml:

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

My 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

In my controller :

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

But now I've this error :

You must set a loader first.

在此输入图像描述

Can someone help me please ? Thanks !

Assuming that the intention is to go for the service based option. Please note in general that the service class is intended to be moved into different folder in the project (to be under PagesBundle/Service folder).

services.yml (please note the changed path)

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

EmailManagementService.php (please note the changed location & namespace)

<?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;
    }

    ...
}

Usage in controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM