Emails
SaaSInMinutes uses SendGrid for sending transactional emails. This guide shows you how to send emails from your app.
Setup
1. Create a SendGrid account at sendgrid.com
2. Create an API key in Settings → API Keys
3. Add it to your .env.local file:
.env.local
1SENDGRID_API_KEY=your-api-keySend an email
libs/sendgrid.ts
1import sgMail from '@sendgrid/mail';
2
3sgMail.setApiKey(process.env.SENDGRID_API_KEY!);
4
5export async function sendEmail(to: string, subject: string, html: string) {
6 const msg = {
7 to,
8 from: 'noreply@yoursaas.com',
9 subject,
10 html,
11 };
12
13 try {
14 await sgMail.send(msg);
15 return { success: true };
16 } catch (error) {
17 console.error('Error sending email:', error);
18 return { success: false, error };
19 }
20}Send from API route
app/api/send-email/route.ts
1import { NextRequest, NextResponse } from 'next/server';
2import { sendEmail } from '@/libs/sendgrid';
3
4export async function POST(req: NextRequest) {
5 const { to, subject, html } = await req.json();
6
7 const result = await sendEmail(to, subject, html);
8
9 if (result.success) {
10 return NextResponse.json({ message: 'Email sent' });
11 } else {
12 return NextResponse.json(
13 { error: 'Failed to send email' },
14 { status: 500 }
15 );
16 }
17}Email templates
libs/email-templates.ts
1export function welcomeEmail(name: string) {
2 return `
3 <html>
4 <body>
5 <h1>Welcome, ${name}!</h1>
6 <p>Thanks for signing up for our SaaS.</p>
7 </body>
8 </html>
9 `;
10}
11
12export function resetPasswordEmail(resetLink: string) {
13 return `
14 <html>
15 <body>
16 <h1>Reset Your Password</h1>
17 <p>Click the link below to reset your password:</p>
18 <a href="${resetLink}">Reset Password</a>
19 </body>
20 </html>
21 `;
22}