Send SMS Messages in Laravel using AWS SNS

Send SMS Messages in Laravel using AWS SNS

Simple Notification Service (SNS) is a full-featured notification service provided by Amazon Web Services (AWS). SNS provides a way to quickly and reliably send messages to a large number of subscribers. With SNS you can send Mobile Push Notifications, emails and Worldwide SMS messages!. Let me show you how to send SMS in Laravel using AWS SNS.

Prerequisites:

  • AWS access key ID and secret access key: To learn how to obtain your’s see this helpful article by AWS.
  • AWS_REGION (default = us-east-1): Region property is important because not all countries are supported by AWS for SMS messaging. See the list of supported countries to know whether your region is supported or not.

Installation and Configuration:

We will be using a composer package called aws/aws-sdk-php-laravel which provides a Laravel (5 and 4) service provider for the AWS SDK for PHP .

  • Require the aws-aws-sdk-php-laravel package in your Laravel project.
  • To register the AWS service provider. Add the line below to the providers key in your config/app.php.
  • You should also add the AWS facade alias to the aliases key in your config/app.php.
  • Publish the package’s configuration file using Artisan. This will generate a configuration file in the path config/aws.php.
  • Update your credentials and your preferred zone in the generated config/aws.php.

Send SMS in Laravel using AWS SNS:

  • We will quickly create a controller which will be responsible for sending SMS messages.
  • We will also define a route in routes/web.php which will call a function in SMSController to send a message. Note that here I am passing the phone number with the route itself for demo purposes. In a practical scenario, you may want to get your phone numbers from a model or aform request.
  • Import AWS facade and add sendSMS() function to SMSController.

    Here we create an SNS client ($sms) using AWS::createClient() method and then we can send a message by calling the publish() method on the client object ($sms) . We must pass some necessary options to publish() such as a message, phone_number and SMSType (Transactional or Promotional). A full list of available options can be found here.

Note:

  • The phone number must be a valid one and should also include the country code (+91 for India).
  • The Promotional type messages will only be delivered from 9 AM to 9 PM in India. (This is due to govt. regulations)

That is it!. Test if everything is working fine by sending a message using the route we defined earlier in this tutorial. In your browser, you should type something like

Did you find this tutorial useful? If yes, don’t forget to share your thoughts in the comment section below and you may also like my other useful posts on Laravel.

34
Leave a Reply

avatar
14 Comment threads
20 Thread replies
11 Followers
 
Most reacted comment
Hottest comment thread
15 Comment authors
zahid hasanJunbestcytshabeebdavid Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Filip
Guest
Filip

Works like a charm ! Thanks !

abu
Guest

Should I pay for per SMS I send and How Should I buy SMS bundle?

jose
Guest

Thanks you make it looks so easy,
also I think that there is an error on:
‘providers’ => array(
// …
Aws\Laravel\AwsServiceProvider::class,messages
)

in message text after class,

Aniruddh
Guest
Aniruddh

Hi,

Can we pass dynamic API key when creating object of SNS ?

Abhishek Sharma
Guest
Abhishek Sharma

Did not work for me. Though I got the MessageID in response but when I check monitor activity in AWS console, it shows failed.

Adriano
Guest

Hi, thank you very much for the information. Is it possible for the number to which I send the message to respond? For example, start an inquiry confirmation message and the person responds with a yes or no by causing the system to cancel or confirm the query as the response?

kranti
Guest
kranti

Hi,
I am getting this error.
SnsException in WrappedHttpHandler.php line 191:
Error executing “Publish” on “https://sns.us-east-1.amazonaws.com”; AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

kranti
Guest
kranti

This error was due to trying it locally.
I fixed this by turning off the verification in aws.php
Add this:

‘http’ => [
‘verify’ => false
]

aman
Guest
aman

i’m using this: ‘AWS.SNS.SMS.SenderID’ => [ ‘DataType’ => ‘String’, ‘StringValue’ => ‘abc’, ], ‘AWS.SNS.SMS.SMSType’ => [ ‘DataType’ => ‘String’, ‘StringValue’ => ‘Promotional’, ] with these option only one message is send per day.second message not visible on phone

Anupam
Guest
Anupam

can we send sms without extension i.e. +91

david
Guest

bro how to sent bulk sms for usa numbers

shabeeb
Guest
shabeeb

hi i used this package bt i got an error. when i create job for queue . error: RuntimeException : Instances of Aws\Sns\SnsClient cannot be serialized at C:\wamp64\www\TylerMessage\vendor\aws\aws-sdk-php\src\AwsClient.php:230 And my job: sms = AWS::createClient(‘sns’); $this->task = $task; $text = $this->task->template->message; $task1 = $this->task; } public function handle() { if($task->contact){ $this->keys[‘{{business_name}}’]=$task1->contact->office->name; $this->keys[‘{{name}}’]=$task1->contact->name; $this->sms->publish([ ‘Subject’ => ‘Tyler petroleum’, ‘Message’ => strtr(strip_tags($text),$this->keys), ‘PhoneNumber’ => ‘+’.$task1->contact->calling_code.$task1->contact->phone, ‘MessageAttributes’ => [ ‘AWS.SNS.SMS.SMSType’ => [ ‘DataType’ => ‘String’, ‘StringValue’ => ‘Transactional’, ] ], ]); }else{ foreach ($task1->group->contacts as $contact){ $this->keys[‘{{business_name}}’]=$contact->office->name; $this->keys[‘{{name}}’]=$contact->name; $trans = $this->keys; $this->sms->publish([ ‘Subject’ => ‘Tyler petroleum’, ‘Message’ => strtr(strip_tags($task1->template->message), $this->keys), ‘PhoneNumber’ => ‘+’.$contact->calling_code.$contact->phone, ‘MessageAttributes’… Read more »

shabeeb
Guest
shabeeb

helo…plz

bestcyt
Guest
bestcyt

effect , but how can i know whether the message send successfully ? is there has a callback function or other things ?

Jun
Guest
Jun

Thank you for this tutorial. It really works!
But I am encountering some issues. After a few sms sent, it would stop working unless I change its region. Hope you can help me with this. Thank you!

zahid hasan
Guest
zahid hasan

Working Perfectly