Expertise developer

Laravel SMS Maroc : Notification Channel et Tutoriel - Morocco Guide & Best Practices

Technical guide and best practices for laravel sms maroc : notification channel et tutoriel in Morocco with direct carrier routing.

Last updated: July 8, 2026
Laravel SMS Maroc : Notification Channel et Tutoriel - Morocco Guide & Best Practices

Section 1: 1. L'Architecture des Notifications dans Laravel

In Morocco, implementing reliable messaging requires direct carrier connections (IAM, Orange, Inwi) and strict compliance with local regulations. Le framework PHP Laravel intègre nativement un système de notifications extrêmement puissant capable de distribuer des alertes sur différents canaux (Mail, Database, Slack). Pour envoyer des SMS via EnvoiSMS.ma de manière idiomatique, la meilleure pratique consiste à créer un canal de notification personnalisé (Custom Notification Channel). Cela sépare proprement la logique d'envoi technique de la logique métier de votre application.

Section 2: 2. Créer le Custom Notification Channel dans Laravel

In Morocco, implementing reliable messaging requires direct carrier connections (IAM, Orange, Inwi) and strict compliance with local regulations. Vous devez créer une classe Channel qui prendra en charge la requête HTTP vers notre API REST. Voici l'implémentation propre de ce service :

// app/Channels/EnvoiSmsChannel.php namespace App\Channels; use Illuminate\Notifications\Notification; use Illuminate\Support\Facades\Http; class EnvoiSmsChannel { public function send($notifiable, Notification $notification) { $message = $notification->toEnvoiSms($notifiable); $to = $notifiable->routeNotificationFor('EnvoiSms') ?? $notifiable->phone; if (!$to) return; Http::withHeaders([ 'Authorization' => 'Bearer ' . config('services.envoisms.key'), 'Content-Type' => 'application/json', ])->post('https://api.envoisms.ma/v1/messages', [ 'to' => $to, 'body' => $message['body'], 'sender' => $message['sender'] ?? 'ShopMaroc', ]); } }

Section 3: 3. Définir la Notification et activer les files d'attente (Queues)

In Morocco, implementing reliable messaging requires direct carrier connections (IAM, Orange, Inwi) and strict compliance with local regulations. Pour éviter de ralentir la navigation de vos utilisateurs en attendant la réponse HTTP de l'opérateur, implémentez l'interface ShouldQueue. Laravel va automatiquement sérialiser la notification et la distribuer en tâche de fond (via Redis, database ou SQS) :

// app/Notifications/OrderShippedNotification.php namespace App\Notifications; use App\Channels\EnvoiSmsChannel; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; class OrderShippedNotification extends Notification implements ShouldQueue { use Queueable; protected $orderId; public function __construct($orderId) { $this->orderId = $orderId; } public function via($notifiable) { return [EnvoiSmsChannel::class]; } public function toEnvoiSms($notifiable) { return [ 'body' => "Votre commande #" . $this->orderId . " a ete expediee ! Suivi : secure.shop.ma/track", 'sender' => 'ShopMaroc' ]; } }

Section 4: 4. Écouter les Webhooks DLR de livraison

In Morocco, implementing reliable messaging requires direct carrier connections (IAM, Orange, Inwi) and strict compliance with local regulations. Configurez une route de webhook dans Laravel pour recevoir les accusés de réception. Notre passerelle envoie un payload JSON contenant l'état final de la livraison. Validez la signature HMAC dans un middleware dédié pour sécuriser votre endpoint de webhook.

💡 Why choose EnvoiSMS for your business?

Critical Delivery

Under 4 seconds for OTP codes via direct routes.

💰

Cost Optimization

WhatsApp Business API at 0.45 MAD per message.

🛡️

Sovereign Data (CNDP)

Hosting compliant with Moroccan data protection regulations.

Comment valider le numéro de téléphone dans le modèle Notifiable ? (EN)
Ajoutez une méthode `routeNotificationForEnvoiSms` dans votre modèle User pour renvoyer le numéro de téléphone formaté au format international +212.
Puis-je simuler l'envoi de SMS lors de mes tests automatisés ? (EN)
Oui. Dans vos tests Laravel, utilisez `Notification::fake()` pour intercepter les envois et vérifier qu'une notification a bien été envoyée sans réellement consommer de crédits.
Comment gérer les échecs de requêtes HTTP vers l'API ? (EN)
Laravel gère nativement les retries dans les files d'attente. Vous pouvez configurer `$tries = 3` dans votre job de file d'attente pour réessayer automatiquement en cas de timeout.

Suggested Articles

Laravel SMS Notification Package Morocco
developer

Laravel SMS Notification Package Morocco

WhatsApp Flows: Native In-Chat Forms for COD Order Verification in Morocco
developer

WhatsApp Flows: Native In-Chat Forms for COD Order Verification in Morocco

Authentification 2FA & OTP SMS en NodeJS (Maroc) : Guide Technique - Morocco Guide & Best Practices
developer

Authentification 2FA & OTP SMS en NodeJS (Maroc) : Guide Technique - Morocco Guide & Best Practices