Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sulieman's avatar

sending more than 10,000 sms

hello , i'm working on project with laravel that will send sms to many contacts, the contacts more than 10,000 , and i am using nexmo or twilio, they have good api ,but my question is what the best salution to do that ::

is to insert 10,000 job in the queue, esh job will send 1 sms ??

$contact = Contacts::all();
foreach ($contacts as $contact) {
    $this->dispatch(new SendSMS($contact)); 
}

or make it run without queue

$contact = Contacts::all();
foreach ($contacts as $contact) {
    $contact->sendSMS();
}

what is the best saluotion ? if there another one ?

0 likes
16 replies
Goldenstat's avatar

I would definitely suggest using the queue as the response time would be immense i expect. Maybe grouping them in batches? I'm not the best person but i think queues are the way forward

1 like
boynet's avatar

I am pretty sure they have some api the handle bulk sending... so start by checking theire api

Sulieman's avatar

@Goldenstat do you mean using chunk , each job will send 100 sms ??

Contacts::chunk(100, function ($contacts ) {
    $this->dispatch(new SendSMS($contacts )); 
});
Sulieman's avatar

@boynet no they dont , they are sending 1 sms per sec, each request will send just 1 sms

Goldenstat's avatar

@Sulieman Sort of i meant more as

$contact = Contacts::all();
$chunk = 0;
$i = 0;
$group = [];
foreach ($contacts as $contact) {
    $i++;
        $group[$chunk][] = $contact;
    if($i == 10)
    {
        $chunk++;
    }
}

Then dispatch the array?

martinbean's avatar
Level 80

@Sulieman Use the queue. You do not want to be trying to send 10,000 SMS message via a HTTP API in a single page load. If one fails and kills the loop, or the page load times out (or a hundred other scenarios where things can go wrong), how do you know which one it was and where to pick up again?

Instead, use the queue component. It’s exactly what it’s built for. Add a job per SMS, and have a queue worker run them. The queue worker will process each SMS one at a time. If you need SMS messages to be sent quicker than that, then that’s where you would scale and have more than one worker working on your queue. The theory is: twice as many queue workers would process the queue in half the time.

Some example code:

foreach ($users as $user) {
    $this->dispatch(SendSmsToUser($user, $message));
}

Your SendSmsToUser job class will need to know how to fetch an SMS number for a user. And you’ll also need to specify the $message body.

3 likes
Sulieman's avatar

@martinbean , yes , and then when the worker make a job i can insert a row in the database and save the status for that sms, this way i will know exactly wich sms sent and wich not

but my question if the loob to inset 10000 job will work fine on the server ?

foreach ($users as $user) {
    $this->dispatch(SendSmsToUser($user, $message));
}
martinbean's avatar

i can insert a row in the database and save the status for that sms

@Sulieman There’s no harm in doing that, but you will know if an SMS is sent or not by whether it has been removed from the queue or not. Queues will usually attempt to process a job a set number of times and if the job fails, will be placed in a “failed” table in your database for manually investigation. Jobs where the SMS has been successfully sent will just be removed from the queue once processed.

monsterdream's avatar

Do I understand correctly 1 sms per second ? That means if you would like to send 10 000 sms it will take 10 000 seconds ? Or maybe this is different kind of calculating overall time ?

Sulieman's avatar

@monsterdream yes it 1 sms per second . If more messages per second are sent, Twilio queues them up and sends them out at a rate of 1 per second.

monsterdream's avatar

@Sulieman What a crap. I've done for my client sms system where his clients send for example 4000 sms and it takes something about 1 min... For a second I thought maybe those services you mentioned above are worth my interest but... No.

Nana-Odai's avatar

Hello, how were you able to implement the sms with jobs. need some help on this. doing similer.

Please or to participate in this conversation.