Sherif
Sherif

Reputation: 509

Mass email sending using laravel queues and beanstalkd

I'm developing an application that is similar to mailchimp, and here is what I thought would be the best practice to handle all the mass email sending

There is a Letter model , that hasMany Lists , and a List model that hasMany Contacts

1- When the user sends a newsletter to some lists(each list contains a number of subscribed contacts), a new job will be queued using Queue::push , the function that handles this job simply uses a for loop (not foreach) to iterate through the Many contacts of each list that belongs to this letter (which is the worst thing in this solution)

2- For each contact , i use Mail::queue to send the mail to this certain contact

3- I'm using beanstalkd , and Amazon SES smtp

The problem is I have a bad feeling about iterating through the contacts using a for even if the whole process is queued, also what happens if the job fails at certain point after sending to x contacts? Does this mean when it restarts it will send the same email to the same contacts again?

I would appreciate it if anyone can propose the best practice to handle this situation.

Upvotes: 1

Views: 3334

Answers (2)

Merwin Poulose
Merwin Poulose

Reputation: 21

If you are sending bulk email as Laurence suggested you can use a Cache counter and if the queue fails you check the cache count and start from there. The other option is to make 10000 contact into different chunks using Laravel chunk(), so each chunk will have a limited number of emails.

Upvotes: 1

Laurence
Laurence

Reputation: 60048

also what happens if the job fails at certain point after sending to x contacts?

If you use Mail::queue() - it will queue each email as its own job. If a job fails, only that single job will be retried. Once each mail is sent, it will be deleted from the queue.

There are many ways to tackle this issue - but I think your approach seems ok.

Dont forgot about the failed jobs section of queues. You can create a failed-table, and tell your queue how many times to try and re-send a failed email

php artisan queue:retry 3

If the job fails more than 3 times - it will stop trying and put it in the failed-table for you to manage manually. Or you can delete failed jobs if you want

php artisan queue:forget 3

Upvotes: 2

Related Questions