Using the AntiFlood Plugin
The AntiFlood Plugin – like all plugins – is added with the Mailer class' registerPlugin() method. It takes two constructor parameters: the number of emails to pause after, and optionally the number of seconds to pause for.
To use the AntiFlood plugin:
- Create an instance of the Mailer using any Transport you choose.
- Create an instance of the Swift_Plugins_AntiFloodPlugin class, passing in one or two constructor parameters.
- Register the plugin using the Mailer's registerPlugin() method.
- Continue using Swift Mailer to send messages as normal.
When Swift Mailer sends messages it will count the number of messages that have been sent since the last re-connect. Once the number hits your specified threshold it will disconnect and re-connect, optionally pausing for a specified amount of time.
Both batchSend() and send() methods will work with this plugin.
<?php
require_once 'lib/swift_required.php';
//Create the Mailer using any Transport
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.example.org', 25)
);
//Use AntiFlood to re-connect after 100 emails
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100));
//Or specify a time in seconds to pause for (30 secs)
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30));
//Continue sending as normal
for ($lotsOfRecipients as $recipient) {
...
$mailer->send( ... );
}
Parent topic: AntiFlood Plugin