Hello there,
It is always so rewarding when you achieve something after long research. And this is one such instance as I had to at least read 40+ blogs to finally set up a fully running Mailing service. So just thought of collating everything together in a blog to help lambda newbies like me.
Prerequisites:
Having Contact Me
like the one below is an integral part of most of the personal and small business websites that are built.
I was building one such website, as the entire website is static, I honestly do not want to set up a server just to expose a single endpoint.
I know that cloud functions
are something that solves my problem of having an endpoint without actually setting up the server. I chose AWS Lambda as it was much popular. But, the resources and blogs were not enough to give me a step by step guideline.
We are going to build a node emailer service that accepts a message
in our POST request body and triggers an email to the predefined set of recipients from your Gmail account.
Create an AWS account here. Your account setup will be complete with you entering your Credit card details and verifying your email. Charges are usage-based.
Naviagte to AWS Console
Choose Lambda
under the Find Services
input field.
You should now be on the lambda functions dashboard which shows the list of your available lambda functions.
Click on the Create Function
button
In the next screen, fill in your Function name - emailer
and choose Nodejs runtime as we are implementing this using node.
On Clicking the Create function
button you should see Successfully created the function emailer
message on the next screen.
On scrolling down the page, you will see a sample nodeJS code with index.js
Create a new test with any name of your choice and click on the Test
button, you should be getting the response in the Execution Result
tab.
The Aws lambda IDE for nodeJS does not allow us to install our npm packages on the go. Due to this, we have to get this set up locally in our machine and then upload the code to lambda by zipping it.
nodemailer
dependency and some code to send an email. Make sure to npm install and create a zip from the root directory including your node_modules
folder.Actions
-> Upload a .zip file
option.index.js
you should be able to see the code where we have given our Email credentials and sending an email.{
clientId: '<YOUR_CLIENT_ID>',
clientSecret: '<YOUR_CLIENT_SECRET>',
refreshToken: '<YOUR_REFRESH_TOKEN>',
accessToken: '<YOUR_ACCESS_TOKEN>'
}
I know it could be a lot of jargon. But trust me it is simple.
Select Project
and then Create new project
button.mailer
and click on create
.https://developers.google.com/oauthplayground
as Authorised redirect URIs and Save it.clientID
and clientSecret
copy both.clientID
& Oatuh clientSecret
that you got from the above steps -> Close.https://mail.google.com
-> Authorize APIs -> Login with the account that you want to send email from.Now we got all the keys needed.
Now update your clientId
, clientSecret
, refreshToken
, and accessToken
and your Full email ID in the AWS Lambda code.
Click Deploy
-> Test
-> Configure your test to include message
parameter.
You should get an email with your message on clicking Test
.
Add the following headers
Access-Control-Allow-Headers
Access-Control-Allow-Methods
Access-Control-Allow-Origin
Access-Control-Allow-Origin : '<YOUR_DOMAIN>'
{
"message": "HELLO"
}
INVOKE URL
INVOKE URL
with message param in body to send the email.Similarly, create the OPTIONS method, and update the headers. You should get an 'OK' response to testing the Same. It is mandatory otherwise your Cross-site requests will fail.
Now do this
fetch(INVOKE URL, {
method: 'POST',
body: JSON.stringify({ message: 'hi'})
}).
then(res => res.json()).
then(res => console.log(res)); // {"message":"Email processed succesfully!"}
You have Done it!