Search code examples
symfonymailer

symfony2 dont send email


I'm developing a application in Symfony2 I have a command that will be run in a crontab

This is the command:

<?php
namespace project\projBundle\Service;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Templating\EngineInterface;
class Sender {
    protected $em;
    protected $templating;
    protected $mailer;
    public function __construct($em, $templating,\Swift_Mailer $mailer) {
        $this->em = $em;
        $this->templating = $templating;
        $this->mailer = $mailer; }

    public function runSender() {
        $proj = $this->em->createQuery("query")->setMaxResults(20)->getResult();
        $message = \Swift_Message::newInstance()
            ->setSubject('Contact enquiry from symblog')
            ->setFrom('...@gmail.com')
            ->setTo('...@gmail.com')
            ->setBody($this->templating->render('projectprojBundle:Others:emailNew.html.twig', array('proj' => $proj)));

        $this->mailer->send($message); } }

The parameters.yml:

parameters:
    mailer_transport: gmail
    mailer_host: ~
    mailer_user: ...@gmail.com
    mailer_password: ...

In config_test:

swiftmailer:
    disable_delivery: false

But for some reason this not sending the email. What I'm doing wrong?


Solution

  • By default emails are stored in files and you need to run the following command to send them:

    console swiftmailer:spool:send
    

    If you want to send emails directly without spooling them add spool: { type: memory } to your config file:

    swiftmailer:
        disable_delivery: false
        spool: { type: memory }
    

    More details can be found in the Symfony2 documentation.