Buy Pro
Docs
Emails

Emails

Instructions for setting up and using emails.

Nodemailer

Nodemailer is a powerful module for Node.js applications to allow easy email sending. It's the de facto solution for handling emails in Node.js.

React Email

React Email is a framework for building and sending emails using React components. It allows you to create dynamic and responsive email templates with ease.

Available email templates

Following email templates are already implemented:

  1. Verify Email Address
  2. Welcome
  3. Password Reset
  4. Invitation
  5. Revoked Invitation
  6. Connected Account Security Alert
  7. Confirm Email Address Change
  8. Feedback

Rendered email template example

Example email

Environment Variables

To configure your SMTP server, add the following environment variables to your .env file. Here's an example using Gmail:

EMAIL_SERVER_HOST=smtp.gmail.com
EMAIL_SERVER_PORT=465
EMAIL_SERVER_USER=example@gmail.com
EMAIL_SERVER_PASS=suyz yeba qtgv xrnp
EMAIL_SENDER=example@gmail.com

Note: For Gmail, you'll need to use an app-specific password. Follow Google's instructions to generate one.

Sending emails

To streamline the email sending process, create a dedicated function for each use case. Here's an example for sending a password reset email:

import { renderAsync } from '@react-email/render';
import nodemailer from 'nodemailer';
 
import {
  PasswordResetEmail,
  type PasswordResetEmailData
} from '@/emails/password-reset-email';
import type { NodeMailerPayload } from '@/lib/smtp/mailer/node-mailer-payload';
import { serverConfig } from '@/lib/smtp/mailer/server-config';
 
export async function sendPasswordResetEmail(
  data: PasswordResetEmailData
): Promise<void> {
  const component = PasswordResetEmail(data);
  const html = await renderAsync(component);
  const text = await renderAsync(component, { plainText: true });
  const payload: NodeMailerPayload = {
    from: serverConfig.from,
    to: data.recipient,
    subject: 'Reset password instructions',
    html,
    text
  };
 
  await nodemailer.createTransport(serverConfig.transport).sendMail(payload);
}

then simply call the function within the server context

await sendPasswordResetEmail({
  recipient,
  name,
  resetPasswordLink
});