Email using AWS SES: How to Send Email using AWS SES in Codeigniter

 

how-to-send-email-using-aws-ses

 

In this post i would like to show how to send email using AWS SES(Simple Email Service). Here i will use codeigniter framework to send Email. Lets start with step by step.

You may also like this post How to Send SMS using AWS SNS.

I am assuming that you already know about setup of codeigniter.If you have any issue with codeigniter setup then you can check this post Codeigniter Setup.

Get Client key and Client Secret key from AWS:

Login to your aws console. here you can find the Menu “My Billing Dashboard” under profile area.Inside of Access keys (access key ID and secret access key) you need to create new access key and save it to use in future.

Installation and Configuration AWS SDK in codeigniter:

To setup AWS SDK with codeigniter, need to install AWS SDK using composer. You can get it easily installed.Go to your root folder of the codeigniter application and run Below command.

 
composer require aws/aws-sdk-php

Creating Controller( Email using AWS SES ):

Next, we are going to create a controller file named AwsSesController.php inside application/controllers where we will put all Email configuration and send email function.Please find below the code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Aws\Common\Aws;
use Aws\Ses\SesClient;
class AwsSesController extends CI_Controller {
publicfunction __construct()
{
parent::__construct();
}
publicfunction sendEmail(){

$params = array(
'credentials'=> array(
'key'=>'CLIENT KEY',
'secret'=>'CLIENT SECRET KEY',
),
'region'=>'us-east-1',
'version'=>'latest'
);
$SesClient = new SesClient($params);

$sender_email = 'hitesh.maity@gmail.com';
$recipient_emails = ['hitesh.maity@gmail.com'];

// Specify a configuration set. If you do not want to use a configuration comment it or delete.
//$configuration_set = 'ConfigSet';

$subject = 'Test Email From Techalltype';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>Test Email From Techalltype</h1>';
$char_set = 'UTF-8';

try {
$result = $SesClient->sendEmail([
'Destination'=> [
'ToAddresses'=> $recipient_emails,
],
'ReplyToAddresses'=> [$sender_email],
'Source'=> $sender_email,
'Message'=> [
'Body'=> [
'Html'=> [
'Charset'=> $char_set,
'Data'=> $html_body,
],
'Text'=> [
'Charset'=> $char_set,
'Data'=> $plaintext_body,
],
],
'Subject'=> [
'Charset'=> $char_set,
'Data'=> $subject,
],
],
// If you aren't using a configuration set, comment or delete the following line
//'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}
}
}

Moving Out from AWS SES Sandbox mode:

Above Email addresses must be verified with Amazon SES. By default Your Amazon SES account has “sandbox” access.With sandbox access you can only send email to the Amazon SES mailbox simulator and to email addresses or domains that you have verified. To move out from the sandbox access, you have to request a sending limit increase. After that you will get a reply from AWS Support which is about to how you are now ensuring that your mailing list contains only recipients who specifically signed up to receive email from you. And this question arises by AWS support, if you choose YES for the option (I only send to recipients who have specifically requested my mail).

Do not worry about reply.You just do reply them “Our mailing list contains only recipients who specifically signed up to receive email from us“.

 

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , ,