email/sendEmail.ts

saas-starter-kit · lib/email/sendEmail.ts · typescript · Apache-2.0
email

Async function that sends HTML emails via nodemailer using environment SMTP settings.

// Source: https://github.com/boxyhq/saas-starter-kit | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
import nodemailer from 'nodemailer';

import env from '../env';

const transporter = nodemailer.createTransport({
  host: env.smtp.host,
  port: env.smtp.port,
  secure: false,
  auth: {
    user: env.smtp.user,
    pass: env.smtp.password,
  },
});

interface EmailData {
  to: string;
  subject: string;
  html: string;
  text?: string;
}

export const sendEmail = async (data: EmailData) => {
  if (!env.smtp.host) {
    return;
  }

  const emailDefaults = {
    from: env.smtp.from,
  };

  await transporter.sendMail({ ...emailDefaults, ...data });
};

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key