Skip to main content

Email Plugin

Send transactional emails with support for multiple providers.

Supported Providers

ProviderDescription
ResendModern email API (recommended)
SendGridPopular email service
MailgunDeveloper-friendly API
AWS SESAmazon Simple Email Service
PostmarkTransactional email focus
SMTPAny SMTP server

Installation

npx autodeploybase add email

Configuration

autodeploy.config.json
{
"plugins": {
"email": {
"enabled": true,
"settings": {
"provider": "resend",
"from": "noreply@myapp.com",
"fromName": "My App"
}
}
}
}

Environment Variables

Resend

EMAIL_PROVIDER=resend
RESEND_API_KEY=re_...

SendGrid

EMAIL_PROVIDER=sendgrid
SENDGRID_API_KEY=SG....

SMTP

EMAIL_PROVIDER=smtp
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=user
SMTP_PASSWORD=password
SMTP_SECURE=true

Usage

Basic Email

import { sendEmail } from '@/plugins/email';

await sendEmail({
to: 'user@example.com',
subject: 'Welcome!',
html: '<h1>Welcome to our app!</h1>'
});

With Template

import { sendEmail } from '@/plugins/email';
import { WelcomeEmail } from '@/plugins/email/templates/welcome';

await sendEmail({
to: 'user@example.com',
subject: 'Welcome!',
react: WelcomeEmail({ name: 'John' })
});

Multiple Recipients

await sendEmail({
to: ['user1@example.com', 'user2@example.com'],
subject: 'Announcement',
html: '<p>Important update...</p>'
});

With Attachments

await sendEmail({
to: 'user@example.com',
subject: 'Your Report',
html: '<p>Please find your report attached.</p>',
attachments: [
{
filename: 'report.pdf',
content: pdfBuffer
}
]
});

Email Templates

React Email

plugins/email/templates/welcome.tsx
import { Html, Head, Body, Container, Text, Button } from '@react-email/components';

interface WelcomeEmailProps {
name: string;
verifyUrl: string;
}

export function WelcomeEmail({ name, verifyUrl }: WelcomeEmailProps) {
return (
<Html>
<Head />
<Body style={{ fontFamily: 'sans-serif' }}>
<Container>
<Text>Hi {name},</Text>
<Text>Welcome to our platform! Please verify your email:</Text>
<Button href={verifyUrl}>Verify Email</Button>
</Container>
</Body>
</Html>
);
}

Using Templates

import { sendEmail } from '@/plugins/email';
import { WelcomeEmail } from '@/plugins/email/templates/welcome';

await sendEmail({
to: user.email,
subject: 'Welcome to My App',
react: WelcomeEmail({
name: user.name,
verifyUrl: `${process.env.APP_URL}/verify?token=${token}`
})
});

Built-in Templates

TemplateUse Case
WelcomeEmailNew user welcome
VerifyEmailEmail verification
ResetPasswordPassword reset
InvoiceEmailPayment receipts
NotificationEmailGeneral notifications

API

sendEmail

interface SendEmailOptions {
to: string | string[];
subject: string;
html?: string;
text?: string;
react?: React.ReactNode;
from?: string;
replyTo?: string;
cc?: string | string[];
bcc?: string | string[];
attachments?: Attachment[];
tags?: Record<string, string>;
}

interface Attachment {
filename: string;
content: Buffer | string;
contentType?: string;
}

async function sendEmail(options: SendEmailOptions): Promise<{ id: string }>;

sendBulkEmail

await sendBulkEmail({
recipients: [
{ email: 'user1@example.com', data: { name: 'User 1' } },
{ email: 'user2@example.com', data: { name: 'User 2' } }
],
subject: 'Monthly Newsletter',
template: NewsletterTemplate
});

Queue Integration

For high-volume sending, use with a queue:

import { emailQueue } from '@/plugins/email/queue';

// Add to queue
await emailQueue.add('send-welcome', {
to: user.email,
template: 'welcome',
data: { name: user.name }
});

// Process queue (in worker)
emailQueue.process('send-welcome', async job => {
await sendEmail({
to: job.data.to,
subject: 'Welcome!',
react: WelcomeEmail(job.data.data)
});
});

Testing

Development Mode

In development, emails are logged instead of sent:

[EMAIL] To: user@example.com
[EMAIL] Subject: Welcome!
[EMAIL] Preview: http://localhost:3000/__email-preview/abc123

Test Mode

import { mockSendEmail } from '@/plugins/email/testing';

// In tests
mockSendEmail();

await sendEmail({ to: 'test@example.com', subject: 'Test' });

expect(mockSendEmail).toHaveBeenCalledWith({
to: 'test@example.com',
subject: 'Test'
});