Emails
Email service integration with multiple providers
Installation
Install the Package
Add Emails to your project using the Tailar CLI:
Set Environment Variables
Create a `.env` file in the root of your project and add the following environment variables. For detailed setup instructions, see how to create an email client.
1# Email Configuration2EMAIL_FROM="noreply@example.com"3EMAIL_REPLY_TO="support@example.com"4NEXT_PUBLIC_APP_URL="http://localhost:3000"5 6# Resend Configuration7RESEND_API_KEY="your_resend_api_key_here"8# Get your API key from: https://resend.com/api-keysPreview
Explore representative UI flows and snippets generated for this module (same previews as on the home page modules section).
Server action pattern
1import { sendEmail } from '@/lib/emails/actions'2 3await sendEmail({4 to: 'user@example.com',5 subject: 'Welcome!',6 html: '<h1>Welcome</h1>',7 text: 'Welcome to our app',8})File Tree
The following files are installed when you add Emails:
1├─ lib/2│ └─ emails/3│ ├─ actions.ts4│ ├─ config.ts5│ ├─ index.ts6│ ├─ templates.tsx7│ └─ types.ts8└─ components/9 └─ emails/10 └─ email-form.tsxUsage
Once you've installed Emails, you can start using the following actions:
Send Email
Send emails using the sendEmail function. You can send plain HTML emails without templates:
1import { sendEmail } from '@/lib/emails/actions'2 3await sendEmail({4 to: 'user@example.com',5 subject: 'Welcome!',6 html: '<h1>Welcome to our app</h1><p>Thank you for signing up!</p>',7 text: 'Welcome to our app. Thank you for signing up!'8})Send Email with Template
The best practice is to create template functions that use baseEmailWrapper. Here's a real-world example from the auth configuration showing how to send a verification email:
1import { renderVerifyEmail } from '@/lib/auth/templates'2import { sendEmail } from '@/lib/emails/actions'3 4// In your auth configuration or API route5const html = renderVerifyEmail({6 verifyLink: url,7})8 9await sendEmail({10 html,11 to: user.email,12 subject: 'Verify your email address',13 text: `Click the link to verify your email: ${url}`,14})Create an Email Template
Create reusable email template functions that use baseEmailWrapper. Here's an example of a verification email template from the auth feature to show how it works:
1import { baseEmailWrapper } from '@/lib/emails/templates'2import { APP_NAME } from '@/lib/constants'3 4const primaryColor = '#000000'5 6export function renderVerifyEmail({7 verifyLink,8}: {9 verifyLink: string10}): string {11 const content = `12 <h2 style="margin: 0; font-size: 18px; font-weight: 500; color: #000;">Welcome to ${APP_NAME}!</h2>13 <p style="font-size: 14px; color: #a1a1aa">14 Thank you for signing up. Please click the button below to verify your email address and activate your account.15 </p>16 <a17 href="${verifyLink}"18 style="19 display: inline-block;20 margin: 24px 0;21 padding: 8px 24px;22 background: ${primaryColor};23 color: #fff;24 border-radius: 6px;25 text-decoration: none;26 font-weight: 500;27 font-size: 14px;">28 Verify Email29 </a>30 <p style="font-size: 12px; color: #a1a1aa; margin-top: 24px;">31 If you did not create this account, you can safely ignore this email.32 </p>33 `34 return baseEmailWrapper(content)35}Customize the Base Email Wrapper
The baseEmailWrapper function wraps your content with a branded layout. You can fully customize this wrapper or use emails without it, but it's recommended for a professional and consistent look. Customize it by updating APP_NAME in lib/constants.ts, changing the logoUrl, and modifying the styles. The wrapper includes a logo header, your content area, and a footer with copyright.
1// lib/emails/templates.tsx2import { APP_NAME } from '@/lib/constants'3 4export function baseEmailWrapper(content: string): string {5 // Update this URL to your logo (e.g., from public folder)6 const logoUrl = 'https://www.tailar.dev/logo-dark.png'7 8 return `9 <div style="font-family: Inter, Arial, sans-serif; background: #ffffff; padding: 32px;">10 <div style="max-width: 480px; margin: 0 auto; background: #fff; border-radius: 12px; border: 1px solid #00000033; box-shadow: 0 2px 2px rgba(0,0,0,0.04); overflow: hidden;">11 <div style="padding: 24px 32px; font-weight: 500; font-size: 20px;">12 <img src="${logoUrl}" alt="${APP_NAME} Logo" width="40" height="40" style="border-radius: 8px; overflow:hidden;" >13 </div>14 <div style="padding: 0 32px;">15 ${content}16 </div>17 <div style="padding: 24px 32px; font-size: 12px; color: #a1a1aa">18 © ${new Date().getFullYear()} ${APP_NAME}. All rights reserved.19 </div>20 </div>21 </div>22 `23}References
For more information about Emails, check out these resources: