Send Emails with Amazon AWS SES from Google Scripts
This week was the first time I’ve tried AWS. I wanted to test AWS SES to write some emails with the help of Amazon.
Amazon Simple Email Service (SES) is the email service that enables developers to send mail from within any application (boom)
The learning curve was painful for me because I was totally lost in the documentation. Here’s what I came through:
That’s why I’ve decided to write this down here! Please don’t ask me questions if you fail, the answer is “😢I don’t know, please try to do it somehow else ”.
The workflow is like this:
(1)Apps Script =>
(2)API Gateway =>
(3)Lambda =>
(4)SES: Send Email🥳
And each part of it must be set properly!
💪Challenge Excepted!
✏️Send Email with Amazon Ses from Google Apps Script.
Amazon Account
Not an easy task, but I’ve created an Amazon account. Lot’s of fields and checks to pass, but I did it. Phew!
SES
Go to Ses settings. Verify your sender’s info in order to be able to send emails. Send test email. Do what else it asks you for.
Privacy
Find it in IAM → Create new Privacy → JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ses:*",
"Resource": "*"
}
]
}
Role
Find Roles in IAM → Create a role → Link it to Privacy
Lambda Function
Find Lambda Functions → Create New. Remember your function’s name for the future step.
Here’s the code for your function:
var aws = require("aws-sdk");
var ses = new aws.SES({ region: "eu-west-2" });
exports.handler = async function (event) {
var params = {
Destination: {
ToAddresses: [event.email_to],
},
Message: {
Body: {
Html: { Data: event.body },
},
Subject: { Data: event.subject },
},
Source: event.email_from,
};
return ses.sendEmail(params).promise()
};
And of course, it is super naive, the purpose was to test things.
Add Trigger => API Gateway
In my Lambda function, I go to
Configuration > Triggers
and create a new trigger to link my function with API.
I create a new Trigger > from Source = API Gateway and create a new REST API.
In Security section I’ve selected Open which is simpler, but not safe. Now my API will work by URL without credentials, so I’d better to keep this URL in secret. If you want to play with safety settings, please look here, I wish you luck, and patience.
API Gateway
Go to API page > add a few methods there. For my purpose, I’ve selected POST. Added Lambda function name in settings. And here is the button “TEST”, I’ve clicked it too. Worked.
Try to deploy the API, and go back to your Lambda to grab the URL. This video may help.
Apps Script
Hopefully, you won’t get the error that I’ve got several times:
error 500
{"message":"Internal Server Error"}
Create your usual Google Apps Script project. Use this code:
function test_send_ses() {var options = {
method: 'post',
payload : JSON.stringify({
// 👉change
email_to: "YOUREMAIL@gmail.com",
body: '<p>Hello from <i>Amazon</i>!</p>',
subject: 'test mail from Amazon',
// 👉change
email_from: 'YOUREMAIL@gmail.com'
}),
contentType: 'application/json',
muteHttpExceptions: true
};// 👉👉👉change to your url
var path = 'YOUR_URL_GOES_HERE';var result = UrlFetchApp.fetch(path, options)Logger.log(result.getResponseCode()); // ➝ 200
Logger.log(result.getContentText());}
The code will knock on the door, and do this:
(1)Run Apps Script =>
(2)Call API Gateway =>
(3)Run Lambda =>
(4)Run SES: Send Email🥳
And … if all goes right … you’ll get the result like this:
200
{"ResponseMetadata":
{"RequestId":"XXX"},
"MessageId":"XXX"}
Finally
Check your email, if you’ve got one new from yourself!
Here’s mine:
That’s all!
I’m totally exhausted, but I hope you will join the path and find my notes helpful. Please feel free to add any suggestions in the comments as I’m new to AWS.