Reputation: 789
I'm pretty new in php symfony2. I want to manage one class for sending emails.
services.yml:
mail_helper:
class: HelpBundle\MailSender\EmailManager
arguments: ['@mailer','@templating']
EmailManager:
class EmailManager
{
protected $mailer;
protected $templating;
public function __construct(\Swift_Mailer $mailer,$templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
public function sendEmail($fromEmail,$toEmail,$subject,$comments,$status)
{
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($this->templating->renderView('HelpBundle:Emails:add-comment.html.twig', array('comments' => $comments,
'status' => $status,
'subject' => $subject)))
;
$this->mailer->send($message);
}
}
In my controller I have called like this:
public newAction()
{
$mailer = $this->get('mail_helper');
$mailer->sendEmail($fromEmail,$toEmail,$subject,$comments,$ticketStatus);
}
Error when controller action is called is next:
Attempted to call an undefined method named "renderView" of class "HelpBundle\MailSender\EmailManager".
I want to understand how I can fix this ?
Thanks a lot
Upvotes: 0
Views: 3870
Reputation: 492
You have to inject into your service a templating so in services.yml use:
arguments: ["@mailer", "@templating"]
and then use it in your service:
public function __construct(\Swift_Mailer $mailer, $templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
public function sendEmail($fromEmail, $toEmail, $subject, $comments, $status)
{
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($this->templating->render('
HelpBundle:Emails:add-comment.html.twig',
array('comments' => $comments,
'status' => $status,
'subject' => $subject
)
)
);
$this->mailer->send($message);
}
And change renderView() to render(), because renderView() is controller shortcut
Upvotes: 3