Answer the question
In order to leave comments, you need to log in
How to send a Swift_Mailer email from a service?
From the controller, mail is sent normally:
public function indexAction(SecurityHelper $securityHelper, \Swift_Mailer $mailer)
{
$message = (new \Swift_Message('Hello Email'))
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Привет мир')
->setBody('Проверка связи, Проверка связи, Проверка связи', 'text/html');
$mailer->send($message);
}
namespace AppBundle\Service;
class MyService
{
private $mailer;
public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $swift_Mailer)
{
$this->entityManager = $entityManager;
$this->mailer = $swift_Mailer;
}
public function sendReport()
{
$message = (new \Swift_Message('Hello Email'))
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Привет мир')
->setBody('Проверка связи, Проверка связи, Проверка связи', 'text/html');
$this->mailer->send($message);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question