Send Email Using Sendinblue in Deno, Natively

Novri Anto
3 min readDec 12, 2022

--

Send Emails Programmatically with Deno and Sendinblue

Deno, the new JavaScript/TypeScript runtime built on top of V8, has gained popularity among developers for its modern design and features, such as built-in dependency management and security.

In this article, we will show you how to send emails using SendinBlue in Deno, natively, without external dependencies.

Setup

Project Setup

deno init <project-name>

Delete main.ts file, because we’re not gonna use that file. Instead, we’re going to use the main_test.ts file.

If you’re using VS Code, we need some steps to set our project as Deno Project.

Create a new directory, called .vscodein the root directory. Inside that directory, create a new file, called settings.json, and paste this code:

{
"deno.enable": true
}

Ok!

Send Email Function

Create a new file called sendEmail.ts.

First, we create User interface. This interface represents the sender and the receiver data. This interface contains name, and email, which are the data that Sendinblue needs for the sender, and the receiver information.

// sendEmail.ts

export interface User {
name: string;
email: string;
}

Next, we create the Message interface. This interface represents data that Sendinblue needs to send emails. Contains, to, htmlContent, suject, and sender.

// sendEmail.ts

export interface Message {
to: Array<User>;
htmlContent: string;
subject: string;
sender: User;
}

Our Interfaces:

export interface User {
name: string;
email: string;
}

export interface Message {
to: Array<User>;
htmlContent: string;
subject: string;
sender: User;
}

Directly export default, sendEmail function, with the first parameter, is the required data (Message interface).

export default async function sendEmail(options: Message): Promise<void> {

const SENDIN_BLUE_URL = "https://api.sendinblue.com/v3/smtp/email";
const mailOptions: Message = options

}

Before, we use the built-in fetch function, let’s define the header first.

    const requestHeaders: HeadersInit = {
"Content-Type": "application/json",
"api-key": Deno.env.get("SENDINBLUE_API_KEY")!,
}

In the header, we specify Content-Type of the request, and also our API Key for SendinBlue.

To get your API Key, first, register a new account, and then go to this URL: https://account.sendinblue.com/advanced/api.

Create and Get Sendinblue API Key

You will need to create a new API Key for your application if you don’t have one.

Next, let’s use the built-in fetch function.

First, we need to use tryand catch, since the fetch is asynchronous.

    try {

const resp = await fetch(SENDIN_BLUE_URL, {
headers: requestHeaders,
method: "POST",
body: JSON.stringify(mailOptions)
});

} catch (error) {
console.error(error.message);
throw new Error(error.message);
}

In the first parameter of the fetch function, we specify the Sendinblue API URL.

For the second parameter, we specify the options of the fetch function. Here, we set the header, with the requestHeaders variable, then we set the method to POST, and the body is a JSON string version of the mailOptions variable.

Next, we get the JSON result, from the resp variable, and then checks if there’s an error from the request.

        const res = await fetch(SENDIN_BLUE_URL, {
headers: requestHeaders,
method: "POST",
body: JSON.stringify(mailOptions)
});

const data = await res.json();
if (data?.message) throw new Error(data.message);

After that, we can actually log something like this:

        const resp = await fetch(SENDIN_BLUE_URL, {
headers: requestHeaders,
method: "POST",
body: JSON.stringify(mailOptions)
});

const data = await resp.json();
if (data?.message) throw new Error(data.message);

for (const user of mailOptions.to) {
console.log(`🔥 EMAIL SENT TO ${user.email} 🎯`);
console.log(JSON.stringify(data, null, 2));
}

Done!

Try It!

Go to main_test.ts file, and remove the entire code. Then, create a new Test for sendEmail function, like this:

import sendEmail from "./sendEmail.ts"

Deno.test("Send Email Test", async () => {

const result = await sendEmail({
htmlContent: "Hello World",
subject: "Hello World",
sender: {
name: "John Doe",
email: Deno.env.get("SENDER_EMAIL")!,
},
to: [
{
name: "John Doe 2",
email: Deno.env.get("RECEIVER_EMAIL")!,
},
// You can send to multiple users
],
});

console.log(result);

});

Try to run the test with:

deno test --allow-env --allow-net main_test.ts

That’s It

That’s how you send Emails using Sendinblue in Deno without any external dependencies. Hope you find it useful and as always ‘Happy Coding, folks’.

Source Code: https://github.com/NovqiGarrix/sendinblue-deno

Follow Me

Twitter: https://twitter.com/novqigarrix

GitHub: https://github.com/NovqiGarrix

Medium: https://medium.com/@novqigarrix

--

--