constnodemailer=require("nodemailer");// create reusable transporter object using SMTP transportconsttransporter=nodemailer.createTransport({host:"smtp.ethereal.email",port:587,secure:false,// true for 465, false for other portsauth:{user:"hjembir.pagac@ethereal.email",// generated ethereal userpass:"KSMN5WY2989rK2gfnf",// generated ethereal password},});// setup email data with HTML contentconstmailOptions={from:'"John Doe" <john.doe@example.com>',// sender addressto:"recipient@example.com",// list of receiverssubject:"Hello from Nodemailer",// Subject linehtml:"<h1>Hello, world!</h1>",// html body};// send mail with defined transport objecttransporter.sendMail(mailOptions,(error,info)=>{if (error) {console.log(error);}else{console.log("Message sent: %s",info.messageId);}});
// Use the same setup as above, but change the 'html' option to 'text':
mailOptions.text = "Hello, world!";
// Use the same setup as above, but add an 'attachments' array to the mailOptions:
mailOptions.attachments = [
{
filename: "attachment1.txt",
path: "/path/to/attachment1.txt",
},
{
filename: "attachment2.pdf",
path: "/path/to/attachment2.pdf",
},
];
// Use the same setup as above, but use the 'cid' attribute in the HTML content:
mailOptions.html = '<img src="cid:logo" />';
// Define the inline image in the 'attachments' array:
mailOptions.attachments = [
{
filename: "logo.png",
path: "/path/to/logo.png",
cid: "logo", // same as the 'cid' in the HTML
},
];
// Use the same setup as above, but use a comma-separated list for the 'to' option:
mailOptions.to = "recipient1@example.com, recipient2@example.com";
// Use the same setup as above, but specify a custom SMTP server:
const transporter = nodemailer.createTransport({
host: "your-smtp-host",
port: 587,
secure: false,
auth: {
user: "your-smtp-username",
pass: "your-smtp-password",
},
});
// Use the same setup as above, but use OAuth2 instead of SMTP authentication:
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "your-gmail-username",
clientId: "your-gmail-client-id",
clientSecret: "your-gmail-client-secret",
refreshToken: "your-gmail-refresh-token",
},
});
const nodemailer = require("nodemailer");
const sendgridTransport = require("nodemailer-sendgrid-transport");
// Use the same setup as above, but use the sendgridTransport:
const transporter = nodemailer.createTransport(
sendgridTransport({
auth: {
api_key: "your-sendgrid-api-key",
},
})
);
const nodemailer = require("nodemailer");
const mailgunTransport = require("nodemailer-mailgun-transport");
// Use the same setup as above, but use the mailgunTransport:
const transporter = nodemailer.createTransport(
mailgunTransport({
auth: {
api_key: "your-mailgun-api-key",
domain: "your-mailgun-domain",
},
})
);
const nodemailer = require("nodemailer");
const sesTransport = require("nodemailer-ses-transport");
// Use the same setup as above, but use the sesTransport:
const transporter = nodemailer.createTransport(
sesTransport({
auth: {
accessKeyId: "your-aws-access-key-id",
secretAccessKey: "your-aws-secret-access-key",
region: "us-east-1", // your SES region
},
})
);
const nodemailer = require("nodemailer");
// create transporter object using SMTP transport
const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
});
// verify SMTP connection
transporter.verify((error, success) => {
if (error) {
console.log(error);
} else {
console.log("SMTP connection verified:", success);
}
});
// Use the same setup as above, and send a confirmation email after signup:
transporter.sendMail({
from: '"Your App Name" <info@yourapp.com>',
to: user.email,
subject: "Welcome to Your App!",
html: `<h1>Welcome to Your App, ${user.name}!</h1><p>Thank you for signing up.</p><p>Please click the link below to confirm your email address:</p><a href="https://yourapp.com/confirm-email/${user._id}">Confirm Email</a><p>Sincerely,</p><p>The Your App Team</p>`,
});
// Use the same setup as above, and send a reset password email:
transporter.sendMail({
from: '"Your App Name" <info@yourapp.com>',
to: user.email,
subject: "Reset your Password",
html: `<h1>Reset your Password</h1><p>You requested a password reset for your account, please click the link below to reset your password.</p><a href="https://yourapp.com/reset-password/${user._id}">Reset Password</a><p>Sincerely,</p><p>The Your App Team</p>`,
});
// Use the same setup as above, and send a transactional email for an order confirmation:
transporter.sendMail({
from: '"Your App Name" <info@yourapp.com>',
to: user.email,
subject: "Order Confirmed",
html: `<h1>Order Confirmed</h1><p>Thank you for your order!</p><p>Your order details are as follows:</p><ul><li><b>Order Number:</b> ${order.number}</li><li><b>Order Date:</b> ${order.date}</li><li><b>Total Amount:</b> ${order.amount}</li></ul><p>Sincerely,</p><p>The Your App Team</p>`,
});
// Use the same setup as above, but use a mailing list for sending email campaigns:
const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "your-smtp-username",
pass: "your-smtp-password",
},
});
const mailingList = ["recipient1@example.com", "recipient2@example.com"];
// Define the email campaign
const mailOptions = {
from: '"Your App Name" <info@yourapp.com>',
subject: "Your Awesome Email Campaign",
html: `<h1>Your Awesome Email Campaign Content</h1><p>This is the content of your email campaign.</p><a href="https://yourapp.com">Visit Your App</a><p>Sincerely,</p><p>The Your App Team</p>`,
};
// Send the email campaign to the mailing list
mailingList.forEach((recipient) => {
mailOptions.to = recipient;
transporter.sendMail(mailOptions);
});
// Create a custom transporter using a custom transport class:
class MyCustomTransport {
constructor(options) {
this.options = options;
}
sendMail(mailOptions, callback) {
// Implement your custom sendMail logic here
// ...
callback(null, { messageId: "custom-message-id" });
}
}
const transporter = nodemailer.createTransport(new MyCustomTransport({
// Custom transport options
}));
// Then use the transporter as usual
transporter.sendMail({
// ...
});
// Create a queue to send emails asynchronously:
const queue = new Queue({
name: "email-queue",
redis: {
host: "localhost",
port: 6379,
},
});
// Add a task to the queue
queue.add({
to: "recipient@example.com",
subject: "Hello from Nodemailer",
html: "<h1>Hello, world!</h1>",
}, (error) => {
if (error) {
console.log(error);
} else {
console.log("Email added to queue.");
}
});
// Start the queue worker
queue.start();
// Create a scheduler to send emails at specific intervals:
const scheduler = new Scheduler({
name: "email-scheduler",
redis: {
host: "localhost",
port: 6379,
},
});
// Add a job to the scheduler
scheduler.add({
name: "send-daily-emails",
cronTime: "0 0 * * *", // Every day at midnight
task: (params, callback) => {
// Send emails here
// ...
callback();
},
});
// Start the scheduler
scheduler.start();
// Create multiple transporters and use a custom transport picker:
const gmailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: "your-gmail-username",
pass: "your-gmail-password",
},
});
const sendgridTransport = nodemailer.createTransport({
service: "sendgrid",
auth: {
api_key: "your-sendgrid-api-key",
},
});
const transporterPicker = {
["gmail.com"]: gmailTransport,
["sendgrid.com"]: sendgridTransport,
};
const transporter = nodemailer.createTransport({
transportPicker,
});
// Determine the transport based on the recipient's domain
const recipient = "recipient@gmail.com";
const domain = recipient.split("@")[1];
const transport = transporterPicker[domain];
// Send the email
transporter.sendMail({
// ...
}, transport);
// Create a proxy to send emails through a proxy server:
const proxy = "http://localhost:8080";
const transporter = nodemailer.createTransport({
proxy,
// Other transport options
});
// Create a transport that uses a VPN connection:
const tunnel = require("tunneler");
const { createClient } = require("openvpn-api");
const vpn = createClient({
host: "vpn-host",
port: 1194,
auth: {
username: "username",
password: "password",
},
});
const server = tunnel.createServer({
socket: await vpn.connect(),
});
const transporter = nodemailer.createTransport({
socket: server,
// Other transport options
});