[英]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. 我有一个Mail.php文件,其中包含一些将由我的几个控制器使用的sendMail函数。
I got to have to use the "templating" service. 我必须使用“模板”服务。 But I have problems putting it in place.
但是我把它放到适当位置时遇到了问题。
My Services.yml: 我的Services.yml:
email_management:
class: Site\PagesBundle\Utils\Mails
arguments: ['@templating']
public: true
My Mail.php: 我的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).
请注意,服务类通常会被移动到项目中的不同文件夹中(位于PagesBundle / Service文件夹下)。
services.yml (please note the changed path) services.yml(请注意更改的路径)
email_management:
class: Site\PagesBundle\Service\EmailManagementService
arguments: ['@templating']
public: true
EmailManagementService.php (please note the changed location & namespace) 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;
}
...
}
Usage in controller: 控制器中的用法:
$this->get('email_management')->sendMail($user,'formulaireInscription');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.