Throttling Emails with SwiftMailer

swiftmailer-logoIn some of my projects I send out email — sometimes using Core Drupal Email APIs, sometimes just using PHP’s native mail() function. But, usually I take a few minutes and integrate (somehow) my project with Swiftmailer.

Swift Mailer integrates into any web app written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features.

Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own

Support servers that require username & password and/or encryption

Protect from header injection attacks without stripping request data content


Send MIME compliant HTML/multipart emails


Use event-driven plugins to customize the library


Handle large attachments and inline/embedded images with low memory use

In this laundry list of features is the extensible Plugin architecture of Swiftmailer. When configuring our Transport layer we can tell swiftmailer to make use of a plugin:

$transport = Swift_SendmailTransport::newInstance($custom_sendmail_command);
$mailer    = Swift_Mailer::newInstance($transport);

$mailer->registerPlugin(
    new Swift_Plugins_ThrottlerPlugin(
      mycustom_outgoing_throttle_rate(), Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE
));

By doing this we can apply a Flood avoidance plugin, or a Throtlle plugin. There is pretty extensive documentation on these plugins.

In my above code mycustom_outgoing_throttle_rate() is a function that examines the length of an outgoing email list and figures out how many emails it can send per minute to send the whole list in say … about 1.5 hours.

Usage of these types of plugins in your webapp system can help to keep your (email) server from being blacklisted as a Spam system.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under 3rd Party Software | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


6 + 6 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>