Dhesend

Node.js

Next.js

Get started with sending emails in Next.js using Dhesend Node.js SDK.

Prerequisites
To get the most out of this guide, you’ll need to:
1. Install
Get the Dhesend Node.js SDK.
1npm install dhesend
2. Create an email template

Start by creating your email template on components/email-template.tsx.

1interface EmailTemplateProps {
2    firstName: string;
3};
4
5export const EmailTemplate = ({ firstName }: EmailTemplateProps) => (
6    `<div>
7        <h1>Welcome, ${firstName}!</h1>
8    </div>`
9);
3. Send email using html

Create an API file under pages/api/send-email.ts if you're using Pages Router or create a route file under app/api/send-email/route.ts if you're using the App router.


Import the html email template and send an email using html parameter.

1import type { NextApiRequest, NextApiResponse } from 'next';
2import { EmailTemplate } from '../../components/email-template';
3import Dhesend from 'dhesend';
4
5const resend = new Dhesend(process.env.DHESEND_API_KEY);
6
7export default function handler(req: NextApiRequest, res: NextApiResponse) {
8    const { data, error } = await resend.emails.send({
9        from: "Dhesend <example@domain.com>",
10        to: ['example@dhesend.com'],
11        subject: 'Welcome to Dhesend',
12        htmlBody: EmailTemplate({ firstName: 'Ali' }),
13    });
14
15    if (error) {
16        return res.status(400).json(error);
17    };
18
19    res.status(200).json(data);
20};

Dhesend @ 2024 - Made with ❤ by  DHEM