Amazon Simple Email Service (SES) not only sends emails but also receives email statuses such as sent, delivered, or read. Amazon SES can only receive the status of emails it delivers, but it can’t send these statuses to your application. To track them, you need to use Amazon Simple Notification Service (SNS). Amazon SES assigns a unique message ID to each email that it successfully submits to send. When Amazon SES receives a bounce or complaint message from an ISP, it forwards the feedback message to you. The format of bounce and complaint messages varies between ISPs, but Amazon SES interprets these messages and, if you choose to set up Amazon SNS topics for them, categorizes them into JSON objects.
Process to handle.
To implement, we will use the following.
1. Create SNS Topic
2. Create subscription
3. Create Configuration Set
4. Confirm subscription
Create an SNS topic with standard configuration.
After creating a topic, go to the subscription panel and select the topic you created. Then select the HTTP protocol and set your site URL as the endpoint.
Each Subscription needs to be confirmed, after creation they are in a PendingConfirmation state.To confirm our subscription, we need to implement the endpoints in our backend, and call Request confirmations from the SNS dashboard.
For this we will create an API which handles the request and a controller to process responses.
Furthermore created configuration set from SES dashboard and link to your created SNS topic. Using a configuration set, you’ll be able to determine what information about the emails SES will be sending to the topic.
Once the configuration set is added, you’ll see it in the list.You need to click on the name of
the configuration set to open the Edit Configuration Set tab with configurations. Then, click Select a destination type.
Creating webhook endpoint
Lets create an api which handles the request and a controller to process the response.
Route::post('handle-notifications', 'SesController@handleNotification'); |
CSRF error – When the request comes from outside of the Laravel, it will throw an unauthenticated error like the CSRF token is missing.You have to edit the except variable with the below code to exclude the SNS route from the CSRF Verification.For such cases lets edit VerifyCsrfToken.php
/** |
Also Lets add a custom log file ses.log where it stores logs only related to our current SES email and SNS data.
'ses-log'=>[ |
The above routes file are to handle notifications and send test email respectively.
Here handleNotification will handle both tracking emails and verification.Once you get the request, you have to verify the subscription first.
We get the following json response structure before confirmation subscription , after that our subscription will be confirmed.
{ |
public function handleNotification(Request $request) |
Let’s add a test method to send mail. Since we are not using our environment settings lets fetch our configuration from the database.
public function testEmail(Response $request): void |
We must add headers while sending emails x-ses-configuration-set. Without x-ses-configuration-set webhook won’t be triggered. unique-id header is used to keep tracking emails.
$msg->getHeaders()->addTextHeader('x-ses-configuration-set', 'HandleNotification') |
Here our configuration set is HandleNotification which we set earlier in SES.
But make sure you use config file or store in database not hardcode.In config/services.php you can add those.
'ses' => [ |
We can use email simulators to send different types of emails from aws also .To send test emails go to AWS SES and on Verified Identities select email to send.
Sending Test Email From AWS SES
When emails are sent after the subscription is confirmed SNS call this method and get the following response.
[ |
{ |
public function handleNotification(Request $request) |
Here we have simply added a log for email by type for testing purposes but we can handle lots from it. We can further store it in the database. With the above message response we can handle various things in our system.
Sign up to stay updated with the latest insights, news, and more.