Amazon SES SMTP in PHPMailer: Send Email Using AWS SES and PHPMailer in CI

With having a large number of features, amazon become a popular hosting service provider. In this tutorial we will see how to send Email using AWS SES and PHPMailer with SMTP Configuration. We will see step by step this implementation.

Get SMTP Credentials from AWS SES:

To get SMTP Credentials from Amazon SES you need to do following simple things:

  • Login to your amazon console account.To login click here Sign in to AWS Account.
  • Go to the Simple Email Service Section(You can search it from search bar by typing Simple Email Service).
  • Select SMTP Settings from left sidebar menu.
  • Click on “Create My SMTP Credential” button to generate SMTP credentials.
  • Copy username and password to use in your application.

Setting up AWS SES SMTP Credentials in PHPMailer:

To use SMTP  in PHPMailer .

$mail->isSMTP();
To set from(Sender) Email Address.
$mail->setFrom('info@techalltype.com', 'Techalltype');
Replace info@techalltype.com with your “From” address. This address must be verified with Amazon SES. You can verify domain name on AWS SES. After verifying you will be able to send email from any email ID which is ended with this domain.
To add recipient address in PHPMailer.
$mail->addAddress('any@techalltype.com', 'Recepient Name');
Replace any@techalltype.com with a “To” address. You can include several address to send email to multiple recipients.
To set AWS SES SMTP user name in PHPMailer.
$mail->Username = 'Your AWS SMTP User Name';
Replace smtp_username with your Amazon SES SMTP user name.
To set AWS SES SMTP Password in PHPMailer.
$mail->Password = 'Your AWS SES SMTP Password';
Replace SMTP password with your Amazon SES SMTP password.
To setup configuration.
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
If you do not want to use a configuration set, comment or remove above line.
To set Host Name.
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
If you’re using Amazon SES in a region other than US West (Oregon),         replace email-smtp.us-east-1.amazonaws.com with the Amazon SES SMTP endpoint in the appropriate region.

Setting up PHPMailer Class in Codeigniter Application:

After generating SMTP Credentials now we will see how to Configure PHPMailer Class with AWS SES SMTP Credentials in our Codeigniter Application. To install PHPMailer inside of Codeigniter Application we will use composer.Please find below the command to install PHPMailer using composer.

$ composer require phpmailer/phpmailer

After installing, you will get a vendor folder inside project root folder.

Setup Composer Autoload for PHPMailer Class:

You need to set vendor directory path inside application/config/config.php.

$config['composer_autoload'] = 'vendor/autoload.php';

Creating Controller(Amazon SES SMTP Setup) and Complete Code:

Create a controller named AwsSesSMTPController.php and use PHPMailer class inside controller.Please find below the complete code of controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
class Welcome extends CI_Controller {
public function index()
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->setFrom('info@techalltype.com', 'Techalltype');
$mail->addAddress('any@techalltype.com', 'Recepient Name');
$mail->Username = 'Your AWS SMTP User Name';
$mail->Password = 'Your AWS SES SMTP Password';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->Subject = 'Amazon SES test using PHPMailer';
$mail->isHTML(true);
$mail->Body = '<h1>Email Test</h1> This email was sent through the Amazon SES SMTP interface using the PHPMailer class.';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

/* The alternative email body; this is only displayed when a recipient
opens the email in a non-HTML email client. The \r\n represents a
line break.*/
$mail->AltBody = "Email Test\r\nThis email was sent through the Amazon SES SMTP interface using the PHPMailer class.";
if(!$mail->send()) {
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;
}
}
}
Note: If your account is still in the sandbox, this address must be verified. Also note that you can include several addAddress() lines to send email to multiple recipients.
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! Stay tuned!

Connect using PuTTY: Connect to AWS Instance using SFTP(PuTTY)

Connect-to-AWS-Instance-using-SFTP

In this post I would like to show how to connect AWS account through SFTP(Secure File Transfer Protocol).PuTTY is a popular SSH, Telnet, and SFTP client for Windows. It is typically used for remote access to server computers over a network using the SSH protocol.Lets see how to connect.

Setup Host Name in PuTTY:

To start using putty we need host name or IP address.We can use Domain name or Public IP Address from AWS as a Host Name.

put-host-name-in-putty

Setup Your PPK File in PuTTY:

After you generating PPK file you have to use this to enable communication securely with AWS having the corresponding public key.To fut PPK file go to the SSH->Auth section and browse to your PPK file.

put-ppk-file-in-putty-auth

Connect to the Server:

Once you setup Host and PPK file then you can connect to the server by clicking open button.After that you will get below screen.

screen-after-connect-putty

Increase Connection Time in PuTTY:

If you want to increase connection time between PuTTY and AWS then go to your putty settings -> Connection and set the value of “Seconds between keepalives” to 30 seconds.After that connection time will be increase.

Thats All,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 tutorial let us know. We’d love to help!

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

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!

AWS SNS with Codeigniter: How to Send SMS Using AWS SNS Services in Codeigniter ?

Send Sms using AWS SNS

Amazon Simple Notification Service (SNS) is an exceedingly accessible, strong secure which enables you to build a serverless application. Serverless application enables you to build and run applications and administrations without considering servers. AWS SNS provides sevices at a very cheap price. You can check here AWS SNS pricing.

This tutorial illustrates how to send SMS using AWS services. Let see step by step how to implement this configuration.

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 get started, download a specific version of the zip file from https://github.com/aws/aws-sdk-php/releases, unzip it inside application/third_party directory of your codeigniter project.

Creating Base Model for Sending Email:

It is best practice to make a base model to put all send sms functionalities so that in every child model you can extend base model to avoid repetition of code.Create a file called My_Model inside application/core directory and put code below giving.Here is how code looks.

My_Model.php

<?php
require_once APPPATH.'third_party/awsSns/aws-autoloader.php';
class MY_Model extends CI_Model
{
public function SendAwsSms(){
$params = array(
'credentials'=> array(
'key'=>'YOUR API KEY',
'secret'=>'YOUR CLIENT SECRET KEY',
),
'region'=>'us-east-1',
'version'=>'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"SenderID"=>"SenderName",
"SMSType"=>"Transactional",
"Message"=>"Hello ! This is a test message",
"PhoneNumber"=>"Mobile No",
);
$result = $sns->publish($args);
}
}

Creating Model to Extend Base Model:

Now extend base model that already created.

<?php
  class AwsSnsModel extends My_Model {
     public function __construct() {
  }
}
?>

Creating Controller:

After done with the model, next we need to create controller and here we will load model.From controller we need to call the function that already implemented in base model. Also you can pass parameter like phone number and message to the function.

Create a file named AwsSendSmsController.phpinside application/controller directory. Please find below the code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AwsSendSmsController extends CI_Controller {
publicfunction __construct(){
parent::__construct();
$this->load->model('AwsSnsModel');
}
    public function index(){
        $this->AwsSnsModel->SendAwsSms();
    }
}
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!

.

Filezila SFTP: Upload Files on AWS EC2 instance Using Filezila SFTP

Upload-files-on-AWS-EC2-instance-Using-Filezila-SFTP

Can not locate where to upload files on AWS EC2 instance ? Dont worry ! I will show step by step how to upload and download files on AWS Ec2 instance using Filezila SFTP.Let see how can we achieve that.

1.) Download and Install Filezila:

Download filezila from here. After download successfully install it.

2.) Filezila Settings:

Once Filezila installed successfully into your local machine, open it and go to the Edit>>Setting menu. Here you need to add your PPK file you already get from AWS instance setup.Purpose of adding this file is to use it in future and no need to add this every time connect.

SFTP Filezila(adding PPK file)

3.) New Site Setup(Filezila SFTP):

To connect your AWS instance and filezila you need to setup new site.Purpose of new site creation is to store all details for future use. Go to the File>>SiteManager (To open Site manager using shortcut key use CTRL+S) Menu. Here you need to setup all details to connect AWS EC2 instance.

Filezila SFTP( New Site Setup Filezila)

Click on New site and after that choose protocol as SFTP(Secure File Transfer Protcol) and enter host details and port as 22. Once you have entered all details click on connect.

Since you are logging in as a user, you cannot upload the files to the root directory. Permission will be denied in that case. So by using any ssh client like putty you can change file permission.

sudo chmod 777 -R /opt/lampp/htdocs

After running this command you can upload your files.you can use this command to change other file permission that you want to modify.

That’s all. Now you can simply drag and drop files from and to Filezila. and also you can edit files from Filezila just you need to right click on the file that you want to edit and choose view/edit option it will be open in local text editor.Please find below the command

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