How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP

aws ses setup

Amazon Simple Email Service (SES) is a cloud-based email sending service that allows you to send transactional, marketing, and notification emails. In this guide, we’ll walk you through the process of setting up and using AWS SES to send emails from your localhost or website using PHP.

What you needed

  • An AWS account with SES enabled.
  • AWS SDK for PHP installed via Composer.
  • PHP installed on your localhost or web server.
aws ses setup

Configuration Steps

  1. Set Up AWS Credentials
    • Obtain your AWS access key ID and secret access key from the AWS Management Console.
    • Ensure that your IAM user has permissions to send emails using SES.
    • Install the AWS SDK for PHP via Composer if you haven’t already:
composer require aws/aws-sdk-php

If above doesn’t work “‘composer’ is not recognized as an internal or external command” Make sure you have composer installed Download from here and Try below command.

php composer.phar require aws/aws-sdk-php

Create a PHP Script

Create a PHP script (send_email.php, for example) with the following code:

<?php
require 'vendor/autoload.php'; // Include the Composer autoload file

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;

// Set up AWS credentials
$credentials = [
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => array(
        'key' => 'YOUR_ACCESS_KEY_ID',
        'secret'  => 'YOUR_SECRET_ACCESS_KEY',
    ) 
];

// Create an SES client
$client = new SesClient($credentials);

// Define the email parameters
$emailParams = [
    'Destination' => [
        'ToAddresses' => ['[email protected]'], // Replace with recipient's email
    ],
    'Message' => [
        'Body' => [
            'Text' => [
                'Charset' => 'UTF-8',
                'Data' => 'This is the body of the email.',
            ],
        ],
        'Subject' => [
            'Charset' => 'UTF-8',
            'Data' => 'Test email subject',
        ],
    ],
    'Source' => '[email protected]', // Replace with sender's email
];

try {
    // Send the email
    $result = $client->sendEmail($emailParams);
    echo "Email sent! Message ID: " . $result['MessageId'] . "\n";
} catch (AwsException $e) {
    echo "Error sending email: " . $e->getMessage() . "\n";
}
?>

If you receive error like “Fatal error: Uncaught Aws\Exception\CredentialsException: Error retrieving credentials from the instance profile metadata service.” That’s because below format should be exactly like this as per latest version. make sure to change your region as well.

'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => array(
        'key' => 'YOUR_ACCESS_KEY_ID',
        'secret'  => 'YOUR_SECRET_ACCESS_KEY',
    ) 
  1. Replace 'YOUR_ACCESS_KEY_ID' and 'YOUR_SECRET_ACCESS_KEY' with your actual AWS credentials. Also, replace '[email protected]' with the recipient’s email address and '[email protected]' with your sender’s email address.
  2. Run the Script
    • Save the PHP script and run it using PHP CLI or from your web server environment.

As I installed the script in HTTP://localhost/aws/index.php file after that I got the result

Email sent! Message ID: 0100018e368asdasdasdbbfa-456d-9637-1175545119e4f5-324

Conclusion

You have now successfully configured and used AWS SES to send emails from your localhost or website using PHP. Make sure to adhere to SES sending limits and best practices to ensure deliverability and compliance with AWS policies.

Sandy

Sandy