Requested behaviour:
I would like to send out bulk emails using Sendgrid and Firestore Cloud functions together. The email should display some individual user data depending on the recipient (e.g. user id and user name).
Current State
I created a working cloud function. It gets the user data from firestore and sends out emails by using the sendgrid API to the given email addresses.
Issue
However, it sends the ids of all users to every email address, instead of only sending the id belonging to a certain subscriber. Example:
There are 3 subscribers with 3 ids in my firestore collection: "abc", "pqr", "xyz"
The function should deliver three emails including the id belonging to the email adress. Instead, the function sends "abcpqrxyz" to every address right now.
My cloud function:
export const sendAllEmails = functions.https.onCall(async (event) => {
const subscriberSnapshots = await admin.firestore().collection('subscribers').get();
const emails = subscriberSnapshots.docs.map(snap => snap.data().email);
const ids = subscriberSnapshots.docs.map(snap => snap.id);
const individualMail = {
to: emails,
from: senderEmail,
templateId: TEMPLATE_ID,
dynamic_template_data: {
subject: event.subject,
text: event.text,
id: ids // sends all ids to everyone instead of a single id in every mail
}
await sendGridMail.send(individualMail);
return {success: true};
});
Do I need to loop over emails and IDs or does the SendGrid API have a smarter implementation for this behaviour?