Sunday, June 9, 2013

SwiftMailer/Symfony2: Expected response code 250 but got code "421", with message "421 Timeout waiting for data from client.

I assume you are using Symfony2 and SwiftMailer to send emails via crontab and symfony2 command. I was using For-Loop in Symfony2 command line to send over hundreds of emails with AWS SES, but the error below was popping up randomly:

Expected response code 250 but got code "421", with message "421 Timeout waiting for data from client

After some search and knocking my head against the wall, I found the answer:

$this->getContainer()->get('mailer')->getTransport()->start();
$this->getContainer()->get('mailer')->send($message);
$this->getContainer()->get('mailer')->getTransport()->stop();

Yes, that's it. You need to start and stop the connection each time. I know it sucks, but this fixed my random error.

My entire command class would like something like below:

class TestEmailCommand extends ContainerAwareCommand
{

    protected function configure()
    {
        $this
                ->setName('test:email')
                ->setDescription('Test Email System')
        ;
    }

    protected function execute(InputInterface $input , OutputInterface $output)
    {

        $em = $this->getContainer()->get("doctrine")->getManager();

        $users = $em->getRepository('UserBundle:User')->findAllforEmails();

        foreach($users as $user)
        {

            $message = \Swift_Message::newInstance()
                    ->setSubject('Dummy Subject')
                    ->setFrom('dummy@example.com')
                    ->setTo($user->getEmail())
                    ->setBody('Dummy Body of the Email' , 'text/html');

            try
            {

                $this->getContainer()->get('mailer')->getTransport()->start();
                $this->getContainer()->get('mailer')->send($message);
                $this->getContainer()->get('mailer')->getTransport()->stop();
            }
            catch(\Exception $exc)
            {
                $output->writeln($exc->getTraceAsString());
            }
        }
    }
}

15 comments:

  1. Very Good. www.webtasarimajansi.gen.tr Web Tasarım Ajansı olarak başarılar dileriz

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Wonderful article, very useful and well explanation. Thanks a lot for offering this unique post with us. I really enjoyed by reading your blog post.

    PHP Training in Chennai

    ReplyDelete
    Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
  4. Good post. Thanks for sharing such a informative post.

    web designing institute in chennai

    ReplyDelete
  5. Excellent post! Keep sharing such a useful post.

    html training in chennai

    ReplyDelete
  6. Thank you for the good write up. It in fact was a amusement account it.Look advanced to far added agreeable from you!


    Informatica Online Training
    Informatica Training
    Hadoop Training
    Tableau Online Training
    Angularjs Online Training

    ReplyDelete

  7. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Regards,'
    Performance Tuning Training in Chennai | Oracle PL/SQL Training in Chennai

    ReplyDelete
  8. Thanks for your informative article. Your post helped me a lot in understanding the career prospects in Web Development.
    Digital Marketing Course fees in Jaipur

    ReplyDelete
  9. Great and interesting post, thanks for sharing your views and ideas, it is very useful for me... thanks. .. keep rocks....
    PHP Training in chennai | Java Training in chennai

    ReplyDelete
  10. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    ReplyDelete
  11. hanks for this great share. This site is a fantastic resource. Keep up the great work.Embedded Projects Center in Chennai | Embedded Projects Center in Velachery

    ReplyDelete