Sending Emails from PHP using Amazon SES

To create a PHP script to sent email using Amazon SES, create a folder

mkdir ses-test
cd ses-test

Install phpMailer using composer

composer require phpmailer/phpmailer

Create a PHP file with following content

SMTPDebug = 2;
    $mail->isSMTP();
    $mail->Host = 'email-smtp.us-east-1.amazonaws.com';  // YOUR SES END POINT
    $mail->SMTPAuth = true;
    $mail->Username = 'AKIAJSKJUHBOU2ARXVLQ';                 // SMTP Username
    $mail->Password = 'AjMmd3gRkmTyCY2QoCDkWmkGKA345zuhseS7gV0X6uz0';                           // SMTP Password
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    //Recipients
    $mail->setFrom('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');
    $mail->addAddress('[email protected]', 'Your Name');     // If your SES is in Sandbox, you need to verify your email. 
    $mail->addReplyTo('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');


    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Now test it by running

root@ok:~/ses-test# php 1.php
2018-01-15 08:45:54 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2762711029 fDkTVziQIcNhSCkxwSpA
2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                      250-8BITMIME
                                      250-SIZE 10485760
                                      250-STARTTLS
                                      250-AUTH PLAIN LOGIN
                                      250 Ok
2018-01-15 08:45:54 CLIENT -> SERVER: STARTTLS
2018-01-15 08:45:54 SERVER -> CLIENT: 220 Ready to start TLS
2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                      250-8BITMIME
                                      250-SIZE 10485760
                                      250-STARTTLS
                                      250-AUTH PLAIN LOGIN
                                      250 Ok
2018-01-15 08:45:54 CLIENT -> SERVER: AUTH LOGIN
2018-01-15 08:45:54 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-01-15 08:45:54 CLIENT -> SERVER: 
Need help with Linux Server or WordPress? We can help!

Leave a Reply

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