homeprojectsresume
 
   
💻 Take your coding skills to the next level with "500+ Ultimate ChatGPT Prompts For Developers" – the game-changer every developer needs, click here to order now.

Sending e-mails in different environments

Published March 8, 2022Last updated May 28, 20231 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.

Use other services to send e-mails in a production environment (Sendgrid, e.g.). Create SendGrid API key at API keys page.

require('dotenv/config');
const nodemailer = require('nodemailer');
(async () => {
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);
})();

 

© 2023