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

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!