We use Resend for sending transactional emails and React Email for building templates.
Setup
Environment Variables: Ensure
RESEND_API_KEYis set inapps/backend/.env. Optionally setEMAIL_FROM(defaults toonboarding@resend.dev).Dependencies:
resend: The official Node.js SDK.@react-email/components: React components for building emails.@react-email/render: Renders React templates to HTML.
Adding New Emails
Create a Template: Add a new
.tsxfile inapps/backend/src/emails/. Export a component that accepts props for dynamic content.Update Service: Add a new function in
apps/backend/src/services/email.ts. This function should:- Render the template using
render(). - Call
resend.emails.send(). - Handle errors gracefully.
- Render the template using
Usage: Import the service function in your route or background job and call it.
Example
typescript
// apps/backend/src/services/email.ts
export async function sendInviteEmail({ email, inviteLink }) {
const html = await render(InviteEmail({ inviteLink }));
await resend.emails.send({ to: email, subject: 'Join the team', html });
}Scalability & Extensibility
- Templates: React Email allows for reusable components (Header, Footer, Button) to maintain consistency.
- Service Layer: The
email.tsservice abstracts the provider. If we switch providers, we only update this file. - Async Sending: Emails are sent asynchronously in the route handlers to avoid blocking the response. For high volume, we should move this to a background job (BullMQ).