twilio/sdk ^6.44 via the Guzzle HTTP client calling the Twilio Messages API directly.
What Twilio is used for
- Registration phone verification — When a new user registers, a 4-digit OTP is dispatched to their formatted phone number via the
SendOtpCodejob. - Login 2FA — When an existing user logs in and 2FA is enabled, an OTP is sent via
SendOtpCode. - Profile phone updates — When a user changes their phone number,
NewSendOtpCodeis dispatched. This job also updates thephone,formattedPhone, anduuidfields on the user record once the SMS is sent.
Prerequisites
- A Twilio account
- A Twilio phone number capable of sending SMS
- Your Account SID and Auth Token from the Twilio Console
Configuration
Get your Twilio credentials
Log in to the Twilio Console. Your Account SID and Auth Token are displayed on the dashboard. Copy both values.
Purchase or configure a Twilio phone number
Under Phone Numbers → Manage → Active Numbers, confirm you have a number with SMS capability. This number is used as the
From address for all OTP messages.Update the job configuration
The Account SID, Auth Token, and sender phone number are currently hardcoded in the Replace these values with your own credentials. For production deployments, move them to environment variables in
SendOtpCode and NewSendOtpCode jobs:app/Jobs/SendOtpCode.php
.env and reference them via config() or env() instead of hardcoding.How OTP sending works
Both jobs generate a 4-digit OTP using:POST request to:
SendOtpCodeupdatesusers.otpfor the user matched byformattedPhone.NewSendOtpCodeupdatesusers.otp,users.uuid,users.phone, andusers.formattedPhonefor the user matched byid.
ShouldQueue, so OTP messages are dispatched asynchronously via the Laravel queue. Ensure your queue worker is running.
Dispatch locations
| Job | Dispatched from | Trigger |
|---|---|---|
SendOtpCode | RegisterController | New user registration |
SendOtpCode | LoginController | Login 2FA |
SendOtpCode | NewRegistrationController | API v2 registration |
SendOtpCode | CustomerController (doss) | Phone verification |
NewSendOtpCode | NewLoginController (API v2) | Login with phone |
NewSendOtpCode | RegisterController (doss) | Doss app registration |
NewSendOtpCode | CashierLoginController | Cashier login OTP |
SMS delivery considerations
- Phone number format — The
formattedPhonefield must be in E.164 format (e.g.+13236120101). The application usesgiggsey/libphonenumber-for-phpelsewhere to format numbers. Ensure phone numbers are stored in E.164 format before dispatching an OTP. - Twilio free trial — Free trial accounts can only send SMS to verified numbers. Upgrade to a paid account for unrestricted delivery.
- Queue worker — Because both jobs use
ShouldQueue, OTPs are only sent when the queue worker is processing jobs. Runphp artisan queue:workor configure a process manager such as Supervisor. - Job failures — Both jobs throw
RegistrationExceptionon Guzzle request errors. Failed jobs are retried according to your queue configuration. Check thefailed_jobstable if users report not receiving OTPs.