4 Methods to Send Emails Using Node.js (w/ Codes - Nodemailer Module, Gmail API, Postmark API & SuprSend)

Search for a command to run...

No comments yet. Be the first to comment.
Toast messages are a common feature in modern mobile apps, offering users quick feedback or guidance. Crafting an effective toast message involves attention to several elements, including platform-specific styles, usability, typography, color, and lo...

I set out to tackle a common issue faced by users of social media and collaborative platforms: notification fatigue. Inspired by the notification strategies employed by major platforms like LinkedIn, MS Teams, and Google Workspace, I aimed to create ...

In our previous setup, the initial problem seemed straightforward: implementing a scheduling mechanism for database queries using goroutines. This approach worked well with minimal resources and SQLite in a Go service. However, when integrating this ...

The problem initially appeared straightforward, but it quickly became apparent that we had underestimated its complexity. In our prior setup, we relied on goroutines to schedule database queries, enabling us to operate efficiently with minimal resour...

A good notification service is more than a communication channel; it can bridge the gap between the product and the users, increasing product adoption. Think of it this way: timely and relevant #alerts can nudge users with some actionable intent for ...

Some of our articles:
npm install nodemailer --save
Create emailSender.js:
const nodemailer = require('nodemailer');
// Setup Nodemailer transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password'
}
});
// ... rest of the code
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password'
}
});
const mailOptions = {
to: 'test@example.com',
from: 'your-email@gmail.com',
subject: 'Subject',
text: 'Body'
};
const mailOptionsWithAttachment = {
// ... other options
attachments: [
{
filename: 'file.txt',
path: '/path/to/file.txt'
}
]
};
Install email-templates and pug:
npm install email-templates pug --save
Update emailSender.js as per your template.
Let's focus on a common scenario: sending a welcome email for new user registration.
subject.pug:= `Hi #{firstName} #{lastName}, welcome to My App!`
html.pug:h1 Hello #{firstName} #{lastName}
p.
Welcome to My App! Now your test emails will be safe.
We just need to make sure your account is real.
a(href='https://example.com/confirmation') Confirm!
├── app.js
├── emails
│ └── welcome
│ ├── html.pug
│ └── subject.pug
Now, let's modify our Node.js application to use these templates:
// ... (previous code)
// Step 5: Create an email-templates instance
const Email = require('email-templates');
const email = new Email({
message: {
from: 'hi@example.com',
},
send: true,
transport: {
host: 'smtp.mailtrap.io',
port: 2525,
ssl: false,
tls: true,
auth: {
user: 'your-mailtrap-username',
pass: 'your-mailtrap-password',
},
},
});
// Step 6: Define user data
const people = [
{ firstName: 'Diana', lastName: 'One', email: 'diana@example.com' },
{ firstName: 'Alex', lastName: 'Another', email: 'alex@example.com' },
];
// Step 7: Send emails for each user
people.forEach((person) => {
email
.send({
template: 'welcome',
message: {
to: person.email,
},
locals: person,
})
.then(console.log)
.catch(console.error);
});
Replace placeholder values in the code with your actual credentials.
npm install nodemailer --save
Create gmailSender.js:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-email-password'
}
});
// ... rest of the code
Follow the steps for generating an App Password.
Update gmailSender.js:
const mailOptions = {
to: 'test@example.com',
from: 'your-email@gmail.com',
subject: 'Subject',
text: 'Body'
};
Include oAuth2 authentication in gmailSender.js:
// oAuth2 authentication
const oauth2Client = new OAuth2(
'YOUR_CLIENT_ID',
'YOUR_CLIENT_SECRET',
'YOUR_REDIRECT_URL'
);
// ... rest of the code
npm install --save postmark
postmark.jsCreate a file named postmark.js:
const postmark = require('postmark');
const client = new postmark.ServerClient('YOUR_POSTMARK_API_TOKEN');
const message = {
From: 'your-email@example.com',
To: 'recipient@example.com',
Subject: 'Subject of the Email',
TextBody: 'Text content of the email',
HtmlBody: '<strong>HTML content of the email</strong>',
};
client
.sendEmail(message)
.then((response) => {
console.log('Email sent\n', response);
})
.catch((error) => {
console.error(error);
});
postmark.jsReplace 'YOUR_POSTMARK_API_TOKEN' with the API token you obtained from your Postmark account.
Visit the SuprSend login page and enter your Email Address and Password. Try SuprSend for free.

Install SuprSend using npm or yarn.

Create templates using the drag & drop editor.

Define rules and logic for notification flows.

Integrate the SuprSend API into your Node application.
npm install @suprsend/node-sdk
# to upgrade to latest SDK version
npm install @suprsend/node-sdk@latest
Installation
const {Suprsend} = require("@suprsend/node-sdk");
// Initialize SDK
const supr_client = new Suprsend("WORKSPACE KEY", "WORKSPACE SECRET");
View granular channel-wise and vendor-wise analytics through the unified dashboard. Explore more about logs.

You may want to check out other SuprSend SDKs too. Consider giving us a star after usage. It's free and open.
%[INVALID_URL]{% github suprsend/suprsend-py-sdk %}
{% github suprsend/suprsend-react-inbox %}