Symfony/Mailer 6.x with antiflood plugin?

368 Views Asked by At

In want replace deprecated SwiftMailer v6.2.5 with new Symfony/Mailer v6.x. SwiftMailer have a AntiFlood Plugin

$mailer = new Swift_Mailer($transport);
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 5));

I can't find this for Symfony/Mailer. Does this function(plugin) not yet exist or is this now solved in a completely different way?

1

There are 1 best solutions below

0
On

Symfony Mailer 6.2 (current) has some parameters that might help to get similar approach as Swifmailer's AntiFlood plugin.

First, lets see how AntiFlood works. According to plugin's docs:

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

// re-connect after 100 emails with 30 seconds pause in between
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30));

If we see official documentation of Symfony Mailer component. We can see some parameters that do exactly the same thing:

restart_threshold (Introduced in Symfony 5.2)

The maximum number of messages to send before re-starting the transport.

restart_threshold_sleep (Introduced in Symfony 5.2)

The number of seconds to sleep between stopping and re-starting the transport.

Here is an example using both parameters together:

$dsn = 'smtps://smtp.example.com?restart_threshold=1000&restart_threshold_sleep=30'

There are two more options in case you don't want to restart the transport, but instead just limit the amount of emails sent per second. See the documentation linked above if that's the case.