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

Send-Email-Using-AWS-SES-and-PHPMailer-in-codeigniter

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!

 

 

 

 

 

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: , , , ,