Background Sending Emails
Answered
Trigg Hound posted this in #help-forum
Trigg HoundOP
Hi fellas I have this case in my project which requires me when the vendor add a product it get all the followers of that vendor and start to send emails to them, any suggestion for the best way to do this in Next.js? I want to use nodemailer but I want to do it as background tasks so it doesn't affect the response to the end user. thank you ~
Answered by averydelusionalperson
const action = async () => {
const res = await addProduct();
if(res.success) toast('hooray!');
const ress = await doYourEmailThing();
if(ress.success) // if you want to toast or something that mail is sent, you can
}25 Replies
I want to use nodemailer but I want to do it as background tasks so it doesn't affect the response to the end user.
How does it matter on which service you use
it's the implementation of your code
if I'm not wrong
Trigg HoundOP
Yeah, you're right it doesn't matter which service I use Just the main idea will be enough
yeah, you can use nodemailer, resend, Amazon SES, etc.
Trigg HoundOP
Yes, but the main concept of what is the best way to do it in next js like the background tasks do you have any idea?
wdym by background task?
where does this action of sending mails start?
like at a particular button click?
or form submit?
or something?
Trigg HoundOP
Like when vendor add a product I show him a success toast and after that trigger a function that run in the background maybe ( doesn't affect the application that what I meant by background)
that get the followers of that vendor then sending emails to them that this vendor added new product
that get the followers of that vendor then sending emails to them that this vendor added new product
yeah, this will work, what's the problem?
have a function, add a product, if the product is added, show the toast. but do the logic of sending emails regardless.
have a function, add a product, if the product is added, show the toast. but do the logic of sending emails regardless.
Trigg HoundOP
Umm, like after that send a request to an api endpoint that do this stuff right?
api or server action, up to you.
whatever you want to use
Trigg HoundOP
I thought at the first place like making it that way may affect my app at certain way 😅 , but I think I'm wrong
Like regularly when you do api or server action you're waiting for a response from them but I'm not waiting for this one
const action = async () => {
const res = await addProduct();
if(res.success) toast('hooray!');
const ress = await doYourEmailThing();
if(ress.success) // if you want to toast or something that mail is sent, you can
}Answer
^ you can do something like this ig 🤷♂️
just a thought
there are many ways you can do it
Trigg HoundOP
Got it, thanks alot for your time.