Link14
Link14

Reputation: 936

Handle thousands emails sending by groups of 500 with Symfony 2

I am working on a mailing tool with Symfony 2.1 framework and SwiftMailer.

This tool needs to handle thousands of emails, but by groups of 500 because of a smtp server constraint (I can't modify it). It needs to wait few minutes between each wave.

I totally don't know how to do this.

The server running the app is on a Windows machine with Apache, and PHP 5.4. I could use a CRON task but I didn't find anything about CRON and Symfony (I found that with Symfony 1.1, not really up to date).

Upvotes: 2

Views: 1712

Answers (2)

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10136

You can find the solution for this issue in the How to Spool Email part of the Symfony Cookbook.

Upvotes: 1

Thomas Kelley
Thomas Kelley

Reputation: 10302

I'd use Symfony's built-in Command component here. You're probably familiar with the normal Commands that come packaged with Symfony... Things like app/console generate:bundle, etc.

You can use the ContainerAwareCommand class to write your own.

Here are the steps you'll need to take:

  • Keep a queue of emails that need to be sent (probably in a database table)
  • Create a class extending ContainerAwareCommand, and make the command name something like "email:send:partial"
    • This class should check for the existence of unsent emails from the database. If there is any, it should send up to 500 of them, and then either remove or update the records in the database to reflect the fact that they were sent.
    • By the way, ContainerAwareCommand is, of course, container-aware... Which means it has access to services like Monolog, Twig, and, most importantly here, Doctrine. So you'll be able to retrieve unsent emails from the database table using $this->getContainer()->get("doctrine").
  • In cron, set up a regular job using the interval provided by your mail server (plus a minute or two to account for execution delays). The command for this will be /path/to/symfony/app/console email:send:partial

That should get you there. Here are a few references for the ContainerAwareCommand class:

http://symfony.com/doc/2.0/cookbook/console/console_command.html

http://api.symfony.com/2.1/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.html

Upvotes: 1

Related Questions