#Background Jobs

1 messages · Page 1 of 1 (latest)

earnest blaze
#

Scenario:
I'm working on a ecomm-platform like gumroad where people could sell their products online.

The product seller adds their products.
after adding it and toast success message I want to send an emails for all the followers for that seller about that product that have been added but I don't want to interfere with the user usage of the website I want it to run in the background

keen falconBOT
#

Hi @earnest blaze. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

In your scenario, you can use Vercel's Cron Jobs feature to schedule the email sending task. Cron jobs are time-based scheduling tools used to automate repetitive tasks. By using a specific syntax called a cron expression, you can define the frequency and timing of each task. This helps improve efficiency and ensures that important processes are performed consistently source (https://vercel.com/docs/cron-jobs).

To send emails from within your application, you can use Serverless Functions. You can either rely on the outgoing SMTP connection of your Serverless Function or use the API of an external service. Vercel recommends the use of third-party mail services like Postmark, Resend, SendGrid, AWS SES, and MailChimp source (https://vercel.com/guides/sending-emails-from-an-application-on-vercel).

Here's an example of how you can set up a cron job for a dynamic route in your vercel.json:

{
 "crons": [
 {
 "path": "/api/send-emails",
 "schedule": "0 5 * * *"
 }
 ]
}

In this example, the cron job will trigger the /api/send-emails route at 5 AM every day. You can adjust the schedule according to your needs.

In your serverless function at /api/send-emails, you can implement the logic to fetch the followers of the seller and send them an email about the new product. Here's a simplified example:

export async function POST(req: Request) {
  const followers = await getFollowers(); // Fetch the followers
  const product = await getNewProduct(); // Fetch the new product
#

for (const follower of followers) {
try {
const response = await resend.emails.send({
from: 'seller@example.com',
to: follower.email,
subject: New product: ${product.name},
react: ProductEmail({ product }), // The email content
});
console.log('Email sent to', follower.email);
} catch (error) {
console.log('Error sending email to', follower.email, error.message);
}
}

return NextResponse.json({ status: 200 });
}

In this example, `getFollowers` and `getNewProduct` are placeholder functions that you would need to implement according to your application's logic. `ProductEmail` is a React component that returns the email content.

Please note that this is a simplified example and you might need to adjust it according to your needs and handle potential issues like rate limiting from the email service.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@earnest blaze kapa.ai is still learning and improving, please let us know how it did by reacting below