Sending emails locally

March 8, 2022

For local testing or testing in general, there is no need to send emails to the real e-mail addresses. Mailtrap service helps preview the emails that should be sent. Credentials (username and password) can be found on an inbox page, a list of inboxes is available at Projects page. For sending emails in a production environment some other service can be used (Sendgrid, e.g.). Sendgrid API key can be created at API keys page.

// ...
const emailConfiguration = {
auth: {
user: process.env.EMAIL_USERNAME, // 'apiKey' for Sendgrid
pass: process.env.EMAIL_PASSWORD,
host: process.env.EMAIL_HOST, // 'smtp.mailtrap.io' for Mailtrap, 'smtp.sendgrid.net' for Sendgrid
port: process.env.EMAIL_PORT, // 2525 for Mailtrap, 465 for Sendgrid
secure: process.env.EMAIL_SECURE, // true for Sendgrid
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);
// ...


© 2022