DEV Community

Cover image for 🔥 Building an email automation system with React Flow and Resend 🎉

🔥 Building an email automation system with React Flow and Resend 🎉

Nevo David on July 31, 2023

TL;DR In this tutorial, you'll learn how to build an email automation system to message people with a sequence of messages every 10 minu...
Collapse
 
sumitsaurabh927 profile image
Sumit Saurabh

Love this article! 🚀

Collapse
 
nevodavid profile image
Nevo David

Thank you so much Sumit!

Collapse
 
suede profile image
Yuval

good job, as always

Collapse
 
nevodavid profile image
Nevo David

Thank you so much Yuval!

Collapse
 
matijasos profile image
Matija Sosic

Awesome stuff Nevo - react-flow looks amazing!

Collapse
 
nevodavid profile image
Nevo David

Thank you so much Matija! 🔥

Collapse
 
bukinoshita profile image
Bu Kinoshita

WOW, that's awesome!

Collapse
 
nevodavid profile image
Nevo David

🙇🏻‍♂️

Collapse
 
gaurbprajapati profile image
gaurbprajapati

Content 🔥

Collapse
 
nevodavid profile image
Nevo David

🚀

Collapse
 
leandro_nnz profile image
Leandro Nuñez

Awesome. Thanks!

Collapse
 
nevodavid profile image
Nevo David

Thank you LEANDRO!

Collapse
 
mezieb profile image
Okoro chimezie bright

Thanks for sharing

Collapse
 
nevodavid profile image
Nevo David

Thank you for reading!

Collapse
 
abest45 profile image
abest

Love this thank you so much

Collapse
 
nevodavid profile image
Nevo David

Thank you 🚀

Collapse
 
costas8 profile image
Costasgk

Keep up the good work!

Collapse
 
nevodavid profile image
Nevo David

Thank you so much Costasgk!

Collapse
 
reconsumeralization profile image
reconsumeralization

Certainly! Here's an updated version of the code that includes a job queue (such as Bull) to handle the delayed email sending.

.env.local file:

RESEND_API_KEY=<place_your_API_key>
Enter fullscreen mode Exit fullscreen mode

For the send.js file (using Bull for job queue):

import { Resend } from "resend";
import Queue from "bull";

const resend = new Resend(process.env.RESEND_API_KEY);

const emailQueue = new Queue('emails', 'redis://127.0.0.1:6379');

emailQueue.process(async (job) => {
  const { email, subject, task } = job.data;
  await resend.emails.send({
    from: "name@yourcompany.dev",
    to: [email],
    subject,
    text: task,
  });
});

export default async function handler(req, res) {
  const { subject, email, tasks } = req.body;
  if (!subject || !tasks || !email) {
    return res.status(400).json({ invalid: true });
  }

  for (const task of tasks) {
    await emailQueue.add({ email, subject, task }, { delay: 600000 });
  }

  return res.status(200).json({ invalid: false });
}
Enter fullscreen mode Exit fullscreen mode

For the pages/index.js file:

const sendEmail = (index) => {
  fetch("/api/send", {
    method: "POST",
    body: JSON.stringify({
      email,
      subject,
      tasks: nodes.map(data => data.value),
    }),
    headers: {
      "Content-Type": "application/json",
    },
  })
    .then((data) => {
      alert(`Sent to processing`);
    })
    .catch((err) => {
      alert(`Encountered an error when message${index} ❌`);
      console.error(err);
    });
};

const handleSubmit = (e) => {
  e.preventDefault();
  sendEmail();
  setEmail("");
  setSubject("");
};
Enter fullscreen mode Exit fullscreen mode

This updated code snippet utilizes a Redis-backed job queue (Bull) to handle the 10-minute delay between email sends. In this way, it bypasses the request handling limitations of serverless platforms like Vercel, and it can be efficiently managed and scaled.

Bull will take care of scheduling the email jobs with a 10-minute delay, and you won't have to worry about request timeouts. Make sure you have Redis running on your server, and you might need to include Bull and Redis as dependencies in your project.

Now, you have a more scalable solution that's suitable for production. Enjoy your emailing without the horror of manual delays! 🌙💼

Collapse
 
priddybroderick profile image
Priddybroderick

I'm excited to learn how to build an email automation system using ReactFlow and Resend. The step-by-step explanations and code snippets are very helpful. I'll make sure to check out the GitHub repository and give it a star. Keep up the great work for further details resizeimageto50kb.com/

Collapse
 
sarkariprep profile image
Sarkariprep

thank u so much i like it your information sarkariprep.in/govt-jobs/

Collapse
 
sarkariprep profile image
Sarkariprep

sarkarijobprep best government jobs portal and sarkari yojana portal

Collapse
 
codehubxg9 profile image
Vivek

Really Good

Collapse
 
dev_bre profile image
Andy

Great article Nevo!

Collapse
 
samanmahmood profile image
Saman Mahmood

Fantastic

Collapse
 
catsarebetter profile image
Hide Shidara

Oh man, I definitely needed this. Going to make an outbounding system.

Collapse
 
mansijain173630 profile image
Mansi Jain

Great article, Thank you for sharing

Collapse
 
nevodavid profile image
Nevo David

Thank you so much Mansi!

Collapse
 
paulwinkler19 profile image
paulwinkler

scholarshiponline.net/ Your beacon of hope for a brighter future."

Collapse
 
chhattisgarhjobs profile image
peter parkar

Nice Article. Thank u so much for sharing valuable information

Regards,
Chhattisgarh Jobs

Collapse
 
paulwinkler19 profile image
paulwinkler

mrcaptions.com/ your captions are like the icing on the cake of my social media content."