Integrate Amazon services into the Laravel application

Table Of Contents
Integrate Amazon services into the Laravel application

Hi Guys,

Your application can offer users reminders, communication from others, and other relevant information in a timely manner. Many platforms provide such notification functionality to users like Google and Amazon platforms. In this article, we integrate Amazon services like SES and SNS.

Here, we are going to implement SES and SNS integration. For that, we need the AWS package to install in Laravel. Run the below composer command.

composer require aws/aws-sdk-php

For SES:

Step 1: Check the AWS Server Mode ( Sandbox Mode Or Production Mode). If Server Mode is in Sandbox mode. You will send only 200 emails per day for testing. You must move your service to production mode. For SES, you need to verify the From email address to email.

Step 2: Get all the Credentials from the AWS server and put them inside the env file.  

MAIL_MAILER=smtp
MAIL_HOST=email-smtp.us-east-2.amazonaws.com
MAIL_PORT=587
MAIL_USERNAME=AKXXXXXXXXXXXXXXXX
MAIL_PASSWORD=BAXCwaIeyqxJXXXXXXXXXXX
MAIL_ENCRYPTION=tls
[email protected] 
MAIL_FROM_NAME="Laravel Project"
MAIL_AUTH_MODE=login

Step 3: We are ready to send an email to users. Check the below example,

Mail::send(‘email_template.test’, $data, function($message) {
             $message->to('[email protected]')->subject('Laravel Basic Testing Mail');
});

For SNS:

Step 1: Check the AWS Server Mode ( Sandbox Mode Or Production Mode). If Server Mode is in Sandbox mode. For SMS, you have a $1 credit per month for testing. You must move your service to production mode. In Sandbox mode, you need to verify the mobile number. You will receive the SMS on your registered mobile.

Step 2: Get all the Credentials like Key ID, Default Region, and Access Key from the AWS server and put them inside the env file.

AWS_ACCESS_KEY_ID=AKIAT53XXXXXXXXXX
AWS_SECRET_ACCESS_KEY=2Z96bXXXXXXXXXXXXXX
AWS_DEFAULT_REGION=us-east-1

Step 3: We can send the SMS to users. Check the below example.

use Aws\Sns\SnsClient;      //// Import this package

 /// Required variables to initialize SNS Client Object
         $params = [
            'credentials' => [
                'key' => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY')
            ],
            'region' => 'us-east-1',
            'version' => 'latest'
        ];

        
        $SnSclient = new SnsClient($params);

        /// Basic Configuration of messages like SMS type, message, and phone number
        $args = [
            'MessageAttributes' => [
                // 'AWS.SNS.SMS.SenderID' => [
                //     'DataType' => 'String',
                //     'StringValue'=>''
                // ],
                'AWS.SNS.SMS.SMSType' => [
                    'DataType' => 'String',
                    'StringValue'=>'Transactional'
                ]
            ],
            "Message" => $smsBody,
            "PhoneNumber" => $mobile
        ];

        
        $SnSclient->publish($args);

We have successfully integrated the Amazon services into the laravel application. Amazon provides greater services to developers and business levels. We sent bulk notifications to users using Amazon services.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top