Speed up Swiftmailer while using an external smtp

Do you send lots of emails with one task via swiftmailer and an external smtp? Here are some tips for you:

1. One mailer object only!

Create the mailer object once and use this object for all the emails. Every time you create and use a new mailer object, a new smtp connection will be established. This would cost lots of time.


2. Use the anti flood plugin

We dont want to send all 9999999 emails via one smtp connection. If there is an error in the connection, it will be very bad for the emails followed. Instead of this, we will close the connection after x emails and create a new one. Thanks to Fabien Potencier, there is already a plugin thats doing this job called Swift_Plugins_AntiFloodPlugin. The following code will register theplugin which reestablishes connection after 200 mails:

$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(200));

3. Use caching in memory

Especially when you use the decorator plugin you should use the memory to cache the body.

For example:


4. Update swiftmailer

There was an error in the qp encoding of swiftmailer. Upgrade to the newest version will fix this. If you cannot upgrade for example with symfony 1.4, you can try to set the encoding to 8 bit.



Passion for sports with boards, climbing, mountain biking, traveling, photography, videography and software engineering. /n/n Heilbronn // Germany

Leave a Reply