DEV Community

Željko Šević
Željko Šević

Posted on • Originally published at sevic.dev on

1

Sending e-mails with Mailtrap

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);
})();
Enter fullscreen mode Exit fullscreen mode

Course

Build your SaaS in 2 weeks - Start Now

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay