Your cart is currently empty!
How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
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.
Configuration Steps
- 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',
)
- 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. - 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.
Comments
Grabber Pro
Original price was: $59.$39Current price is: $39.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19.Android App for Your Website
Original price was: $49.$35Current price is: $35.Abnomize Pro
Original price was: $30.$24Current price is: $24.Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Handle Sudden Traffic Spike in Website – Do Node Balancer Really Help
- How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
- How to Upload Images and PDFs in Android Apps Using Retrofit
- [Fix] File Permission Issue in Apache Website WordPress Not Writable to 775
- Best PHP ini Settings for WordPress & WooCommerce: Official Recommendations
Leave a Reply