This document outlines the email service implementation using React Email for templating and Mailgun for delivery.
The email service provides a robust, type-safe way to send transactional emails using:
- React Email for building and maintaining email templates
- Mailgun for reliable email delivery
- TypeScript for type safety and better developer experience
Add the following variables to your .env
file:
MAILGUN_API_KEY="your-mailgun-api-key"
MAILGUN_DOMAIN="your-mailgun-domain"
MAILGUN_FROM_EMAIL="noreply@yourdomain.com"
Email templates are built using React Email components and are located in src/email/templates/
. Each template is a React component that accepts typed props for the dynamic content.
- Reset Password Email (
ResetPassword.tsx
)interface ResetPasswordEmailProps { userName: string; resetLink: string; }
import { sendResetPasswordEmail } from '../email/email.service';
await sendResetPasswordEmail({
email: 'user@example.com',
userName: 'John Doe',
resetLink: 'https://yourdomain.com/reset-password?token=xyz'
});
- Create a new template in
src/email/templates/
- Use React Email components for consistent styling
- Export the template component with proper TypeScript interfaces
- Add a new method in
EmailService
class to send the email
Example:
// src/email/templates/WelcomeEmail.tsx
import * as React from 'react';
import { Button, Container, Head, Html, Preview, Text } from '@react-email/components';
interface WelcomeEmailProps {
userName: string;
}
export const WelcomeEmail = ({ userName }: WelcomeEmailProps) => (
<Html>
<Head />
<Preview>Welcome to our platform</Preview>
<Container>
<Text>Welcome {userName}!</Text>
<Button href="https://yourdomain.com/getting-started">
Get Started
</Button>
</Container>
</Html>
);
export default WelcomeEmail;
The email service includes comprehensive error handling:
- Custom
EmailError
class for email-specific errors - Detailed error logging using the application logger
- Type-safe error propagation
- Type Safety: Full TypeScript support for templates and service methods
- Maintainable Templates: React components for building and maintaining email templates
- Reliable Delivery: Mailgun integration for professional email delivery
- Error Handling: Comprehensive error handling and logging
- Developer Experience: Easy to create and modify email templates using React
The service maintains backward compatibility with the previous Nodemailer implementation through exported functions. The internal implementation has been updated to use React Email and Mailgun while keeping the same interface.
To test emails in development:
- Set up a Mailgun sandbox domain (free)
- Use the sandbox domain and API key in your
.env.development
- Add verified recipient emails in Mailgun sandbox settings
- Use these verified emails for testing
- Always use TypeScript interfaces for template props
- Include proper error handling in your email sending logic
- Use React Email components for consistent styling
- Test emails with different email clients
- Keep templates simple and mobile-responsive