C
C
Celsius2018-08-02 18:30:16
symfony
Celsius, 2018-08-02 18:30:16

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

and in the service, the same code no longer sends mail and does not give errors:
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);
  }
}

What could be the reason?
I didn’t understand it anymore and installed https://github.com/PHPMailer/PHPMailer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2018-08-02
@Cels

See application configuration.
By default, spool is used, which is usually hung on cron.
Use memory type in swiftmailer configuration to send mail on KernelEvents::TERMINATE event

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question