we have always been using Sendgrid for configuring and sending emails. But I found an even easier solution. Literally within some lines of code, you’ll be able to set up emails. In this discussion, I’ll showcase how it is better than Sendgrid and how can you easily set it up in your code.
**
Why is it better?
**
- Modern simpler functionality and easy to use.
- Pricing is compromised (even though the SendGrid fee tire has a larger capacity)
- Simpler analytics
now let’s learn how to implement it in node js.
1.Go to Resend Website and register. You’ll get a page like this
- Now add a domain and add the configure value in your DNS record.
now you’ll be able to use your domain name’s email as the sender email such as contact@domain.
- Now add them to your node project add dependency
pnpm add resend
Then add your RESEND_KEY in your env file. During calling the function call it like the following. You’ll get the key from API Keys sub-section.
import { Resend } from "resend";
app.get("/send-email", async (req: Request, res: Response) => {
htmlScript="<strong>it works!</strong>";
const resend = new Resend(process.env.RESEND_KEY);
const { data, error } = await resend.emails.send({
from: process.env.SEND_EMAIL,
to: ['receiver@gmail.com'],
subject: "Testing Resend email platform",
html: htmlScript,
});
if (error) {
return res.status(400).json({ error });
}
res.status(200).json({ data });
});
And would you believe it! This is done. Now each time your endpoint is called you’ll get logs like the following.
Top comments (0)