Node.js example

View as Markdown

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:

npm install nodemailer

Include the library in your working class

const nodemailer = require("nodemailer");

Declare your SMTP credentials

Specify your Brevo credentials to establish a connection between our server and your application. Use the createTransport object to define these values.

let transporter = nodemailer.createTransport({
host: "smtp-relay.brevo.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "example@brevo.com", // generated brevo user
pass: "xxxxxxxxx", // generated brevo password
},
});

Compose your email object

Specify the content and metadata of the email message to send through the transporter object. This example uses text content only, but you can also use a predefined templateId.

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

Trigger your email

Specify the content and metadata of the email message to send through the transporter object. This example uses text content only, but you can also use a predefined templateId. The full example is below.

const nodemailer = require("nodemailer");
async function main() {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp-relay.brevo.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "user@brevo.com", // generated brevo user
pass: "xxxxxxxxx", // generated brevo password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Sender Name" <sender@brevo.com>', // sender address
to: "john@domain.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello {{ contact.FIRSTNAME }} , This is an SMTP message with customizations", // plain text body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
}
main().catch(console.error);

Note: this example adds customizations to your SMTP email. It uses the contact’s attributes if the contact exists in Brevo. Run this JS code from the command line, or use the Send a Transactional Email API endpoint.