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.
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-keyReplace your-resend-api-key with your actual Resend API key.
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.
The EmailService uses the Resend API to send emails.
Ensure the RESEND_KEY environment variable is correctly configured to avoid errors.
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.
sendEmail(content: string, subject: string, email: string): Sends an email with the provided content, subject, and recipient email address.EmailService integrates with the Resend API, configured with the RESEND_KEY from your environment variables.RESEND_KEY secure and avoid committing it to version control.For more information on the Resend API, refer to the official Resend documentation.