Learn how to set up and use email with Resend and React Email templates
TanStarter uses Resend for sending emails, and React Email for building email templates.
Setup
Create Resend Account
Create a Resend account at resend.com.
Get API Key
Click API Keys > Create API Key in the Resend console, set permissions to Send emails or Full access. Copy the API key and add it to your .env file:
RESEND_API_KEY=your-resend-api-keyUpdate Website Configuration
Update website.ts to use Resend as the mail provider and set the default sender email address:
Note that fromEmail is the email address used to send emails, and supportEmail is the email address used to receive emails. The email domain suffix should be your email domain.
export const websiteConfig = {
// ...other config
mail: {
enable: true, // Whether to enable email functionality
provider: 'resend', // Mail provider to use
fromEmail: 'TanStarter <[email protected]>', // Sender email address
supportEmail: 'TanStarter <[email protected]>', // Support email address
},
// ...other config
}If you are setting up your environment, you can now go back to the Environment Configuration and continue. The rest of this document can be read later.
Environment Configuration
Set up environment variables
Customization
Creating New Email Templates
- Create an email template in the
src/mail/templatesdirectory:
import { EmailLayout } from '../components/email-layout';
import { EmailButton } from '../components/email-button';
interface MyCustomEmailProps {
username: string;
actionUrl: string;
}
export function MyCustomEmail({
username,
actionUrl,
}: MyCustomEmailProps) {
return (
<EmailLayout>
<p>Hello {username}!</p>
<p>Thanks for joining our platform. Click the button below to get started.</p>
<EmailButton href={actionUrl}>Get Started</EmailButton>
</EmailLayout>
);
}-
Update the
EmailTemplatesinterface intypes.tsto include your new email template. -
Preview your template locally:
pnpm email:dev- Use the template in your code:
await sendEmail({
to: '[email protected]',
template: 'myCustomEmail',
context: {
username: 'John',
actionUrl: 'https://example.com/start',
},
});Creating a New Mail Provider
TanStarter supports extending with new mail providers:
- Create a new file in the
src/mail/providerdirectory (e.g.,sendgrid.ts) - Implement the
MailProviderinterface, defined insrc/mail/types.ts, which requires the following methods:
import type {
MailProvider,
SendEmailResult,
SendRawEmailParams,
SendTemplateParams,
} from '../types';
export class SendGridProvider implements MailProvider {
getProviderName(): string {
return 'sendgrid';
}
async sendTemplate(params: SendTemplateParams): Promise<SendEmailResult> {
// Logic for sending emails using templates
}
async sendRawEmail(params: SendRawEmailParams): Promise<SendEmailResult> {
// Logic for sending raw HTML emails
}
}- Register the new provider in the
providerRegistryinsrc/mail/index.ts:
import { SendGridProvider } from './provider/sendgrid';
const providerRegistry: Record<MailProviderName, ProviderFactory> = {
resend: () => new ResendProvider(),
sendgrid: () => new SendGridProvider(),
};Next Steps
Now that you understand how to use email in TanStarter, you might want to explore these related features:
TanStarter Docs