NodeJS SMTP relay 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
npm install nodemailer
Include the library in your working class
const nodemailer = require("nodemailer");
Declare your STMP 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.
let transporter = nodemailer.createTransport({
host: "smtp-relay.brevo.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "[email protected]", // generated ethereal user
pass: "xxxxxxxxx", // generated ethereal password
},
});
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
.
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Joe" [email protected]', // sender address
to: "[email protected]", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello {{ contact.FIRSTNAME }} , This is an SMTP message with customizations", // plain text body
});
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.
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: "[email protected]", // generated brevo user
pass: "xxxxxxxxx", // generated brevo password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"[email protected]', // sender address
to: "[email protected]", // 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: <[email protected]>
}
main().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.
Updated 12 months ago