php - Issues with sending large email with Laravel job
This is my controller that get contact emails of over 50k and iterate over it then batched it to queue
This is the job that send the email
The problem is that the email can only send to less than 500 contacts without any issue but anything above 1000 contact will result to maximum execution timeout
How can I solve this problem.
Answer
Solution:
This statement is going to retrieve and store all your 50k in memory:
$contacts = Contact::where(...)->where(...)->get();
I suggest chunking the results to avoid memory exhaustion.
Back to your problem, I am thinking about chunking the results (about 500 each) and dispatch an intermediary job that will eventually send emails.
class SendEmailsInChunk implements ShouldQueue
{
public $contacts;
public $batch;
function __construct(public $batch, public $contacts) {}
public function handle()
{
foreach ($this->contacts as $contact) {
$this->batch->add(new BroadCampaignJob(..., $contact, ...));
}
}
Then you can chunk the results and dispatch the above job with each chunk:
$batch = ...;
$query = Contact::where(...)->chunk(500, function ($contacts) use ($batch) {
SendEmailsInChunk::dispatch($batch, $contacts);
})
Source