Send email using PHP Script with Amazon SMTP Authentication

On the off chance that you find yourself using Amazon SES for sending active messages in a PHP web application, getting everything set up is much less complex than it may appear. For my situation, this was on an AWS Linux picture, however it will work on any CentOS AMI.

Untitled-8-640x628

All the programming languages provides functions for sending emails. PHP also provides mail() function to send emails. Some time you require to check that Amazon SES smtp authentication are working with your PHP or not then follow below steps it will help you to check the email service.

Step 1: Download and Extract PHPMailer Library:

Download PHPMailer under your project or /var/www/html, you may change this location as per your need. Follow below step to download and extract PHPMailer:

# cd /var/www/html
# wget http://download.techoism.com/PHPmailer.zip
# unzip PHPMailer.zip
# mv PHPMailer-master /var/www/html/PHPMailer

Step 2: Create sendmail.php file:


Here you have to give Amazom SES SMTP credentials. Now create an simple sendmail.php script web document root or /var/www/html and add below content.

# vim sendmail.php
<?php
function Send_Mail($to,$subject,$body)
{
require '/var/www/html/PHPMailer/class.phpmailer.php';
$from = "from@techoism.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // SMTP
$mail->SMTPAuth   = true;  // SMTP authentication
$mail->Mailer = "smtp";
$mail->Host= "tls://email-smtp.us-east.amazonaws.com"; // Amazon SES
$mail->Port = 465;  // SMTP Port
$mail->Username = "Amazon SMTP Username";  // SMTP  Username
$mail->Password = "Amazon SMTP Password";  // SMTP Password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'Technical Support');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
if(!$mail->Send())
return false;
else
return true;
}
?>

Note: If you want to enable debug mode in php script then add following entry in sendmail.php file:

# vim /var/www/html/sendmail.php
$mail->SMTPDebug = 2;

Step 3: Create index.php file:


Here called sendmail function and passing to, subject and body values. Now create an simple index.php script in web document root or /var/www/html and add below content.

# vim index.php
<?php
require '/var/www/html/sendmail.php';
$to = "to@gmail.com";
$subject = "Test Mail Subject";
$body = "Hi
Email service is working
Amazon SES"; // HTML tags Send_Mail($to,$subject,$body); ?>

Step 4: Run PHP script:


Now access this application using browser with your domain name following by script name

http://example.com/index.php

Note: You can run script from command line also.

# php index.php
Comments
  1. 8 years ago
    • 8 years ago
  2. 6 years ago
    • 6 years ago
  3. 6 years ago

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.