Email Module

Learn how to use the email module in your application. This module is pre-configured to send emails using the EmailService and the Resend API.

Prerequisites

Before using the email module, ensure that the RESEND_KEY is set up in your environment variables. The key is required to authenticate with the Resend API.

# .env file
RESEND_KEY=your-resend-api-key

Replace your-resend-api-key with your actual Resend API key.

How It Works

The email module in your NestJS application integrates with the Resend API to send emails. It’s configured to use the EmailService, which abstracts the implementation for sending emails.

Example Usage

Sending an Email

The EmailService uses the Resend API to send emails.

Ensure the RESEND_KEY environment variable is correctly configured to avoid errors.

Using the EmailService in a Controller

Here’s how the EmailService is used to send an account activation email after user registration:

const content = `
  Hi!<br><br>
  Thanks for your registration<br><br>
  <a href="http://localhost:3000/api/auth/email/verify/${emailToken}">Click here to activate your account</a>
`;

await this.emailService.sendEmail(
  content,
  'Account Activation',
  user.email,
);

This example demonstrates how to send an email with custom HTML content.

API Reference

  • sendEmail(content: string, subject: string, email: string): Sends an email with the provided content, subject, and recipient email address.
  • The EmailService integrates with the Resend API, configured with the RESEND_KEY from your environment variables.

Best Practices

  • Keep your RESEND_KEY secure and avoid committing it to version control.
  • Use environment variables for all sensitive configurations.
  • Handle errors gracefully to ensure a smooth user experience.
  • Test email delivery thoroughly in both development and production environments.

For more information on the Resend API, refer to the official Resend documentation.

MatureStack LogoBuilt with MatureStack