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

33 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
  12. There are so many useful information in your post, I like it all the time. And I want to share this boost app ranking with you, you can take a look. This is very useful.

    ReplyDelete
  13. really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
    Java Training Center in Chennai | Best J2EE Training Center in Chennai | No.1 Java Training Institution in Velachery

    ReplyDelete
  14. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. digital marketing training in chennai

    digital marketing training in bangalore

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. The best thing is that your blog really informative thanks for your great information..Java Project Center in Chennai | Java Project Center in Velachery

    ReplyDelete
  17. Extraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging…
    Python Courses Certifications Training in Chennai | Excellent Python Classes in Saidapet

    ReplyDelete
  18. Great article, really very helpful content you made. Thank you, keep sharing.
    Cisco Certifications Coaching Center in Chennai | Best Cisco Courses in Chromepet

    ReplyDelete
  19. Interesting blog. This is really helpful for me. I like it. Thanks for sharing.
    ISTQB Certifications Course in Chennai | QA Testing in Medavakkam

    ReplyDelete
  20. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  21. Expected to form you a next to no word to thank you once more with respect to the decent recommendations you've contributed here.fire and safety course in chennai

    ReplyDelete
  22. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    safety course in chennai

    ReplyDelete