composer require devcodesms/devcode-sms-bundle in your Symfony 6/7 project.config/bundles.php (Symfony Flex handles this automatically) and define .env with DEVCODE_SMS_API_KEY.DevcodeSmsClient into a service/controller and call sendSms() with recipient, message, and sender ID.Send a one-time password (OTP) to a user:
use DevcodeSms\Bundle\DevcodeSmsClient;
class AuthController
{
public function __construct(private DevcodeSmsClient $smsClient) {}
public function sendOtp(string $phoneNumber): void
{
$this->smsClient->sendSms(
$phoneNumber,
'Your OTP is: 123456',
'DevCode' // Sender ID
);
}
}
Message Sending:
sendSms() for basic SMS delivery.getResponse() to verify success:
$response = $this->smsClient->sendSms($phone, $message, $sender)->getResponse();
if ($response->isSuccess()) { /* Handle success */ }
Balance Checks:
getBalance() in critical paths (e.g., pre-sending):
$balance = $this->smsClient->getBalance();
if ($balance->getRemainingCredit() < 1) {
throw new \RuntimeException('Insufficient SMS credits');
}
Batch Processing:
foreach ($users as $user) {
$this->smsClient->sendSms($user->phone, $user->message, 'SenderID');
sleep(1); // Avoid API throttling
}
UserRegisteredEvent):
public function onUserRegistered(UserRegisteredEvent $event): void
{
$this->smsClient->sendSms($event->getUser()->phone, 'Welcome!');
}
$this->messageBus->dispatch(new SendSmsMessage($phone, $message));
$message = $this->twig->render('emails/sms_template.txt.twig', ['user' => $user]);
$this->smsClient->sendSms($user->phone, $message, 'SenderID');
API Key Exposure:
DEVCODE_SMS_API_KEY in config files. Use .env strictly..env file permissions (chmod 600 .env).Character Limits:
strlen($message) > 160 before sending.Rate Limiting:
timeout in devcode_sms.yaml if needed.DevcodeSmsException for HTTP 429 (Too Many Requests).Sender ID Validation:
getValidSenderIds() to check:
$validSenders = $this->smsClient->getValidSenderIds();
if (!in_array('MySender', $validSenders)) {
throw new \InvalidArgumentException('Sender ID not approved');
}
Enable Logging:
Add to config/packages/devcode_sms.yaml:
devcode_sms:
debug: true
Logs appear in var/log/dev_sms.log.
Mocking for Tests:
Use dependency injection to swap DevcodeSmsClient with a mock:
$this->container->set(DevcodeSmsClient::class, new MockSmsClient());
Custom Responses:
Extend DevcodeSmsResponse to add domain-specific logic:
class ExtendedSmsResponse extends DevcodeSmsResponse {
public function isDelivered(): bool {
return $this->getStatus() === 'DELIVERED';
}
}
Webhook Handling: Implement a controller to process DevCode SMS delivery reports:
#[Route('/devcode/sms/webhook', name: 'devcode_sms_webhook', methods: ['POST'])]
public function handleWebhook(Request $request): Response {
$webhookData = json_decode($request->getContent(), true);
// Process $webhookData['status'], $webhookData['messageId']
}
Fallback Mechanisms:
Decorate DevcodeSmsClient to retry failed sends or fall back to email:
class FallbackSmsClient implements DevcodeSmsClientInterface {
public function sendSms(string $phone, string $message, string $sender): self {
try {
return $this->innerClient->sendSms($phone, $message, $sender);
} catch (DevcodeSmsException $e) {
$this->fallbackToEmail($phone, $message);
}
}
}
How can I help you explore Laravel packages today?