user2576422
user2576422

Reputation: 93

Emails are not send to the user

I have a problem with sending emails with raports. I am create some data, and then try to send it as an email to the user. But emails are not deliwered. I am using swiftmailer configured as in the Symfony cookbool. Parameters for the swiftmailer are set properly because emails from FosUserBundle are working without problems. I have write a method to use it in comand line, the code is below

class DailyRaportCommand extends ContainerAwareCommand
{
protected function configure()
{
    $this
        ->setName('raport:daily')
        ->setDescription('Send daily raports to the Users');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $em = $this->getContainer()->get('doctrine')->getEntityManager();
    $users = $em->getRepository('GLUserBundle:User')->findAllByRaport('DAILY');

    foreach($users as $user)
    {
        $date_to = new \DateTime();
        $date_to = $date_to->sub(date_interval_create_from_date_string('1 day'));
        $date_from = new \DateTime();
        $date_from = $date_from->sub(date_interval_create_from_date_string('1 day'));
        $format = "Y-m-d";

        $raport = array();

        foreach($user->getShops() as $shop)
        {
            $raport[$shop->getName()] = array();

            $shop_list = array();               
            $shop_list[] = $shop->getId();
            $groupBy = array();
            $all_policies = $em->getRepository('GLPolicyBundle:Policy')
                        ->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
            $raport[$shop->getName()]['all'] = $all_policies;

            $groupBy[] = 'typePolicy';
            $policies_by_type = $em->getRepository('GLPolicyBundle:Policy')
                        ->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
            $raport[$shop->getName()]['type'] = $policies_by_type;          

            $groupBy[] = 'bundle';
            $policies_by_bundle = $em->getRepository('GLPolicyBundle:Policy')
                        ->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
            $raport[$shop->getName()]['bundle'] = $policies_by_bundle;

        }

        $message = \Swift_Message::newInstance()
                ->setSubject('Dzienny raport sprzedaży')
                ->setFrom('g#######[email protected]')
                ->setTo($user->getEmail())
                ->setBody(
                    $this->getContainer()->get('templating')->render(
                        'GLRaportBundle:Raport:raport.html.twig',
                        array('raport' => $raport)
                    ));
        $this->getContainer()->get('mailer')->send($message);
}

The wiev of the email is rendered in outside twig file. I will be grateful if someone point whots the reason of this problem.

Upvotes: 0

Views: 94

Answers (1)

Kevin Sharp
Kevin Sharp

Reputation: 529

You might need to manually flush the memory spool, see the following answer which helped me with the same problem:

Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app

Upvotes: 1

Related Questions