Node.js example

You can use this Node.js code and enter your own details to run the smtp relay.

Install nodemailer in your Node project

Run this command in your working directory

1npm install nodemailer

Include the library in your working class

1const nodemailer = require("nodemailer");

Declare your SMTP credentials

You should specify your Brevo credentials in order to establish a connection between our server and your application. You will need to make use of the createTransport obeject to define these values.

1 let transporter = nodemailer.createTransport({
2 host: "smtp-relay.brevo.com",
3 port: 587,
4 secure: false, // true for 465, false for other ports
5 auth: {
6 user: "example@brevo.com", // generated ethereal user
7 pass: "xxxxxxxxx", // generated ethereal password
8 },
9 });

Compose your email object

Next, specify the content and metadata of the email message you want to send through the previously declared transporter object. In this example we have only included text content, but you can also make use of a predefined templateId .

1 // send mail with defined transport object
2 let info = await transporter.sendMail({
3 from: '"Joe" example@brevo.com', // sender address
4 to: "hi@mail.com", // list of receivers
5 subject: "Hello ✔", // Subject line
6 text: "Hello {{ contact.FIRSTNAME }} , This is an SMTP message with customizations", // plain text body
7 });

Trigger your email

Next, specify the content and metadata of the email message you want to send through the previously declared transporter object. In this example we have only included text content, but you can also make use of a predefined templateId. Here you can also see the full example we just went through.

1const nodemailer = require("nodemailer");
2
3async function main() {
4
5
6 // create reusable transporter object using the default SMTP transport
7 let transporter = nodemailer.createTransport({
8 host: "smtp-relay.brevo.com",
9 port: 587,
10 secure: false, // true for 465, false for other ports
11 auth: {
12 user: "user@brevo.com", // generated brevo user
13 pass: "xxxxxxxxx", // generated brevo password
14 },
15 });
16
17 // send mail with defined transport object
18 let info = await transporter.sendMail({
19 from: '"mauricio@brevo.com', // sender address
20 to: "john@domain.com", // list of receivers
21 subject: "Hello ✔", // Subject line
22 text: "Hello {{ contact.FIRSTNAME }} , This is an SMTP message with customizations", // plain text body
23 });
24
25 console.log("Message sent: %s", info.messageId);
26 // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
27}
28
29main().catch(console.error);

Note: This example is if you want to add more customisations into your SMTP email, it will take the attributes if the contact exists within Brevo. You can this JS code in a file through the command line interface or through the API endpoint for Send a Transactional Email.