2

I should send thousands of email in reaction to a specific event, so the email sending is not manual but it's automatic.

The server where is hosted my website has an hourly limit for email sending (about 500 for hour) so I need to partition it.

Is there some plugin that has that feature? Or some PHP library that helps me to do that?

Thanks

1
  • Be aware that any use of delay may hit limits on execution time and therefore terminate unexpectedly. Some hosting providers have a hard limit that cannot be overridded. In such circumstances you'd need to pop emails onto a queue stored to a database and send them in smaller batches using a cronjob.
    – Nick Amis
    Commented Feb 23, 2022 at 13:14

2 Answers 2

3

I don't think using sleep() is a good idea, because it takes up resources while it's sleeping, and you could run into script timeout issues.

I also don't think it's a good idea to be sending any important or bulk e-mails from a web server, because that's not really what's it's designed to do. Messages sent from standard mail() / wp_mail() functions are more likely to be flagged as spam than those sent from real MTAs; You don't get any notice when messages bounce; The host may drop the messages because you overstep some arbitrary limit, or because they think you're spamming, and there's a good chance they won't even let you know they're doing it; etc...

I think the best way to do this would be to use a service that's designed to send bulk e-mail, like MailChimp. It will be much more reliable and efficient. You can use plugins like Import Users to MailChimp to sync your WP user accounts with the campaign members. There are also plugins like MailChimp STS to relay WP-generated messages through MailChimp. You may need to search the plugin repository to find exactly what you need, but there are a lot of MailChimp related plugins available.

If you do insist on sending the messages from your web server, you should at least use a library like SwiftMailer instead of wp_mail(). It goes to great lengths to reduce the likelihood of the message being mistakenly flagged as spam, and has an advanced batch-sending system.

1

You'll need something like the following (maybe-working example code) using get_users();:

$users = get_users( /* Add your args here */ );
foreach ( $users as $user )
{
    $user_email_addresses[] = $user->email; // receive array of user mail addresses here
}

function wpse28212_send_delayed_bulkmail( $email_addresses )
{
    $i = 0;
    foreach ( $user_email_addresses as $email_addy )
    {
        ++$i; // count users up

        $delay = 0; // in miliseconds (ms)

        if ( $i >= 500 )
        {
            $delay = 3600000; // 1 hour
        }
        sleep( $delay );

        wp_mail( $email_addy, 'subject', 'message' );
    }
}

Links:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.