homeprojectstemplates
 
   

Sending e-mails with Mailtrap

Published March 8, 2022Last updated April 12, 20241 min read

For local testing or testing in general, there is no need to send e-mails to real e-mail addresses. Mailtrap service can preview the e-mails for sending.

The inbox page can show credentials (username and password). A list of inboxes is available at Projects page.

import nodemailer from 'nodemailer';
(async () => {
const emailConfiguration = {
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
},
host: process.env.EMAIL_HOST, // 'smtp.mailtrap.io'
port: process.env.EMAIL_PORT, // 2525
secure: process.env.EMAIL_SECURE,
};
const transport = nodemailer.createTransport(emailConfiguration);
const info = await transport.sendMail({
from: '"Sender" <sender@example.com>',
to: 'recipient1@example.com, recipient2@example.com',
subject: 'Subject',
text: 'Text',
html: '<b>Text</b>'
});
console.log('Message sent: %s', info.messageId);
})();

Demo

The demo with the mentioned example is available here.