Configuring the Ubuntu 20.X server to work with AWS SES

Consider the application is written in php and the application owner needs to get an email when some customer fills the contact form.

?It used to be so nice to use the default programing language mailing functions to send any email through the application itself.

But, with time, these are becoming an issue as the email vendors are marking the emails as spam and so that forced us to use their email services like o-365, Gmail, etc.

?But, as we are so inclined towards the AWS services, so we have been using AWS SES for any of our mailing solutions.

But, there is some work that needs to be done on the server before using the AWS SES service.

I have tried to list them all down and that works for me well when I use any PHP application.

To make the server work with the AWS SES there need to be few configurations.
1.????Update the ubuntu : sudo apt update -y

2.????Install nginx server : sudo apt install nginx -y

3.????Install the PHP : sudo apt install php-fpm

4.????Install MySQL : sudo apt install php-mysql

5.????Install the composer

a.????php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

b.Get Hash  - HASH=`curl -sS https://composer.github.io/installer.sig`
c. echo $HASH
d. Then update the hash value on the below line

c.????php -r "if (hash_file('sha384', 'composer-setup.php') === 'HASH_Value') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

c.????php composer-setup.php

d.????php -r "unlink('composer-setup.php');"

6.????Move the composer to a path

a.????sudo mv composer.phar /usr/local/bin/composer

7.????Install the php mailer

a.????sudo /usr/local/bin/composer require phpmailer/phpmailer

8.????Then on AWS configure the AWS SES and whitelist either the domain or the email

a.????This is needed because for sandbox SES environment, it is required to verify both sender and recipient email address.        


If anyone wants to see how that works, you can use these two code snippets and update in your test server to see if that works.

index.php

<!DOCTYPE HTML>
<html lang="en">
? ? <head>
? ? ? ? <meta charset="UTF-8">
? ? ? ? <title>Here you can have your own website title</title>
? ? ? ? <meta name="viewport" content="width=device-width">


? ? ? <!--? -->


? ? ? ? <!-- End Include All CSS -->


? ? ? ? <!-- Favicon Icon -->
? ? </head>
? ? <body>


? ? ? ? <!-- Page Banner -->
? ? ? ? <section class="pageBanner2">
? ? ? ? ? ? <div class="container">
? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? <div class="col-lg-12">
? ? ? ? ? ? ? ? ? ? ? ? <div class="banner_content text-center">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <h4><a href="#">home</a> - contact</h4>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <h2>Contact us</h2>
? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? </section>
? ? ? ? <!-- Page Banner -->


? ? ? ? <!-- Contact Section -->
? ? ? ? <section class="commonSection ContactPage">
? ? ? ? ? ? <div class="container">
? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? <div class="col-lg-12 text-center">
? ? ? ? ? ? ? ? ? ? ? ? <h4 class="sub_title">Contact with us</h4>
? ? ? ? ? ? ? ? ? ? ? ? <h2 class="sec_title">write us a message</h2>
? ? ? ? ? ? ? ? ? ? ? ? <p class="sec_desc">
? ? ? ? ? ? ? ? ? ? ? ? ? ? We are committed to providing our customers with exceptional service while<br> offering our employees the best training.
? ? ? ? ? ? ? ? ? ? ? ? </p>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? <div class="col-lg-8 col-lg-offset-2 col-sm-12 col-md-10 col-md-offset-1">
? ? ? ? ? ? ? ? ? ? ? ? <form action="ses-mail.php" method="POST" class="contactFroms" id="contactForms">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-lg-6 col-sm-6">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input class="input-form required" type="text" name="f_name" id="f_name" placeholder="First Name">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-lg-6 col-sm-6">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input class="input-form required" type="text" name="l_name" id="l_name" placeholder="Last Name">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-lg-6 col-sm-6">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input class="input-form required" type="email" name="email" id="email" placeholder="Email Address">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-lg-6 col-sm-6">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input class="input-form" type="text" name="phone" id="phone" placeholder="Phone Number">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-lg-12 col-sm-12">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <textarea class="input-form required" name="message" id="message" placeholder="Write Message"></textarea>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <button class="common_btn red_bg" type="submit" id="submit"><span>Send Mesdfsage</span></button>
? ? ? ? ? ? ? ? ? ? ? ? </form>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? </section>
? ? ? ? <!-- Contact Section -->


? ? ? ? <!-- Contact Map Section -->
? ? ? ? <section class="gmapsection">
? ? ? ? ? ? <div class="container-fluid">
? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? <div class="col-lg-12 noPadding">
? ? ? ? ? ? ? ? ? ? ? ?<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d12760.61877641387!2d174.76265462338233!3d-36.91056557881305!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x6d0d461db7c7a21d%3A0x500ef6143a30160!2sRoyal%20Oak%2C%20Auckland%2C%20New%20Zealand!5e0!3m2!1sen!2sin!4v1626781491690!5m2!1sen!2sin" width="100%" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? </section>
? ? ? ? <!-- Contact Map Section -->


? ? ? <br><br>


? ? ? ? <!-- Footer Section -->
? ? ? ? <!-- Footer Section -->






? ? </body>
</html>        

ses-mail.php [ You can change that to any name you want]

Ensure the values are entered properly from your SES console at AWS.

<?php


?$name=$_POST['f_name'];
? ?$l_name=$_POST['l_name'];
? ? $email=$_POST['email'];
? ? $phone=$_POST['phone'];
? $message=htmlspecialchars($_POST['message']);


// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
require 'vendor/autoload.php';


// Replace [email protected] with your "From" address.
// This address must be verified with Amazon SES.
$sender = '<enter the email address which was already verified by SES>';
$senderName = 'Any Name';


// Replace [email protected] with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = '<enter the email address which was already verified by SES>';


// Replace smtp_username with your Amazon SES SMTP user name.
$usernameSmtp = '<Get it from AWS SES>';


// Replace smtp_password with your Amazon SES SMTP password.
$passwordSmtp = '<Get it from AWS SES>';


// Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
// $configurationSet = 'ConfigSet';


// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$host = '<Get it from AWS SES>';
$port = 587;


// The subject line of the email
//$subject = 'Amazon SES test (SMTP interface accessed using PHP)';


// The plain-text body of the email
$bodyText =? "Email Test\r\nThis email was sent through the
? ? Amazon SES SMTP interface using the PHPMailer class.";


// The HTML-formatted body of the email
//$bodyHtml = '<h1>Email Test</h1>
? //? <p>This email was sent through the
? ? //<a >Amazon SES</a> SMTP
? ?// interface using the <a >
? ? //PHPMailer</a> class.</p>';


? ?$subject='Contact Form ';
$adminBody = '<html><body>';
$adminBody .= '<table rules="all" style="border:1px solid #666;" cellpadding="10">';
$adminBody .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['f_name']) . "</td></tr>";
$adminBody .= "<tr><td><strong>LastName:</strong> </td><td>" . strip_tags($_POST['l_name']) . "</td></tr>";


$adminBody .= "<tr><td><strong>E-Mail:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$adminBody .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>";
$adminBody .= "<tr><td><strong>Message:</strong> </td><td>" . htmlspecialchars($_POST['message'] ). "</td></tr>";


$adminBody .= "</table>";
$adminBody .= "</body></html>";


$mail = new PHPMailer(true);


try {
? ? // Specify the SMTP settings.
? ? $mail->isSMTP();
? ? $mail->setFrom($sender, $senderName);
? ? $mail->Username? ?= $usernameSmtp;
? ? $mail->Password? ?= $passwordSmtp;
? ? $mail->Host? ? ? ?= $host;
? ? $mail->Port? ? ? ?= $port;
? ? $mail->SMTPAuth? ?= true;
? ? $mail->SMTPSecure = 'tls';
? ? $mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);


? ? // Specify the message recipients.
? ? $mail->addAddress($recipient);
? ? // You can also add CC, BCC, and additional To recipients here.


? ? // Specify the content of the message.
? ? $mail->isHTML(true);
? ? $mail->Subject? ? = $subject;
? ? $mail->Body? ? ? ?= $adminBody;
? ? $mail->AltBody? ? = $bodyText;
? ? $mail->Send();
? ? echo "Email sent!" , PHP_EOL;
} catch (phpmailerException $e) {
? ? echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
? ? echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}


?>        

Once the test is successful, you can request AWS to upgrade the SES from sandbox to production.

要查看或添加评论,请登录

Partho Das的更多文章

社区洞察

其他会员也浏览了