In my past projects, I have always used PHP and a Linux Server to send emails captured by web GUIs. Today, I need find a way to implementation a email function inside Cyclic.sh serverless platform.
The server setup uses ExpressJs as the backend; therefore, of course, PHP is out of window. I tried to search a solution with pure front end JavaScript. Everything I have found required me to hardcode secrets/passwords in the source code. But, who would want to do that?
Then, I stumbled upon Sendinblue; A solution that can support SMTP, and allow me to send transactional emails via API. I am not advertising this product since I have only using the free plan. It allows me to send 300 emails per day. I am doing a small project, this is the cheap, perfect option for me right now.
To use Sendinblue’s Email API, I need to install it SDK.
1 | yarn add sib-api-v3-sdk |
There are other options for PHP, Python, etc. But I am doing ExpressJS today, so I download the SDK package for NodeJS.
Create a new API key
New API authentication keys can be created at https://account.sendinblue.com/advanced/api. Just need to give a name for the purpose of monitoring individual project.
Keep in mind, they only show the key once during the creation. Make sure to copy that key down, otherwise that key is going to be wasted.
Store API KEY in code
For obvious reason, we do not want to write the key inside the code repo. Now we need to install dotenv to kepp all our environment variables.
1 | yarn add dotenv |
dotenv
allows us to utlized .env files. The .env file is located at the root of code repo. Don’t forget to include .env inside the .gitignore file. Otherwise, there’s not much point to use environment variables.
To use environment variables, we just need to:
1 | require("dotenv").config(); |
SIB_Api_key is the variable I defined for my API Key. The name can be anything we wanted, as long as we don’t use any keywords or possible functions names we use in this project.
Since we always have a default email address to either send or receive email notifications. We can add them in the .env file as well.
Code Sendinblue Email API
1 | require("dotenv").config(); |
Code above is pretty much self explantory. Initialize the SDK library, set up authentication method, define the API instance and the SMTP Email function, call, etc.. Just copy and paste them on top of the code, and call it done.
1 | function sendMail(data) { |
The next part is to fill all necessary email content. The full list of properties is located at Reference: sendtransacemailhttps://developers.sendinblue.com/reference/sendtransacemail). It contains a list of all body params and their definition.
As the code shows, The purpose of my code is to receive email. Therefore sendSmtpEmail.to is set to pre-defined variables.
I don’t know about anyone else, but I don’t like just display console.log in the backend. I need to return some value back to the front end. With that in mind, I modified sendMail
to an asynchronous function, so that whenever I called this function, I am promised to receive a result.
1 | async function sendMail(data) {} |
Don’t forget to use await
when calling this function.
And that’s the backbone of the email function.
Testing
It’s testing time! Postman, where are you?
I actually did not have Postman install on my current computer… Alright, I need to download it from https://www.postman.com/.
Postman is a tool to test API calls. I can manually edit a JSON object and treat it as form data.
Within a couple seconds, I can the email.
Yeah, yeah, yeah… It’s from localhost, It may not be safe, give me a break!!
Now try one more time with the Web form…
Done
It’s fairly simple. At this point, I just need to push the git repository to Github, and let Cyclic doing its auto deployment.