How to wp_mail sends 50 email every hour?

242 Views Asked by At

I created a function in cron that sends about 250 emails via wp_mail(). However, my hosting provider does not allow sending more than 50 emails every hour. Is there a way to force wordpress to cache all emails and send 50 of them every hour?

Thanks!

2

There are 2 best solutions below

2
On

You can create a new table named like tasklist then put all "to send" emails in it and write your cronjob to run 50 of these entries for every hour. Put a flag column to table to mark which one is send and set your script to send the not flagged ones.

0
On

Here's the Logic you can follow:

  • When you want to trigger 250 Emails then call a function to save all 250 emails in the array format. An Email Array which will include Email ID, Email Subject, Email Content, Headers if any, etc.
  • Create server-side CRON to run every 5 mins.
  • Server CRON will go to settings and fetch OLDEST 50 email which will be deleted from the settings as well. That means if we have 250 emails then we will be left with 200 emails in the settings.
  • Next, Server CRON will simply call the wp_email function and send 50 emails successfully.
  • Likewise, it will keep sending emails until all the emails from the settings are empty.

I hope that makes sense. Thanks.