Send Emails with Amazon AWS SES from Google Scripts

Photo by Markus Winkler on Unsplash
This image would reflect my feelings, but the “pain” arrow should repeat 10 times.
(1)Apps Script => 
(2)API Gateway =>
(3)Lambda =>
(4)SES: Send Email🥳

✏️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.

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()
};

Add Trigger => API Gateway

In my Lambda function, I go to

Configuration > Triggers 

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.

Apps Script

Hopefully, you won’t get the error that I’ve got several times:

error 500
{"message":"Internal Server Error"}
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());
}
(1)Run Apps Script => 
(2)Call API Gateway =>
(3)Run Lambda =>
(4)Run SES: Send Email🥳
200

{"ResponseMetadata":
{"RequestId":"XXX"},
"MessageId":"XXX"}

Finally

Check your email, if you’ve got one new from yourself!

Note this orange caution that takes half of my screen. Well, at least it was not automatically sent to SPAM.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Max Makhrov

Max Makhrov

Google Sheets Developer, master of Online Accounting