Triggered Emails Code Help

Question:
I am seeking some help debugging some code. My site uses Triggered Emails and the code is located on a certain page on the site. The snippet is here:

await wixCrm.contacts.appendOrCreateContact({
            name: {
                first: "first",
                last: "last"
            },
            emails: [
                { email: "email_1", primary: true },
                { email: "email_2", primary: true },
                { email: "email_3", primary: true },
                { email: "email_4", primary: true },
                { email: "email_5", primary: true }
                ]
            // emails: [{ email: "email_1", primary: true }]
        }).then(async ({ contactId: contact_id }) => {
            console.log(contact_id)

            console.log("Variables", variables)
            await myEmailContactFunction("<form_id>", contact_id, variables)
                .then(() => { console.log("Emailed Contact") })
                .catch(console.log)
        })
        .catch((e) => { return { res: false, reason: e } })

    await wixCrm.contacts.appendOrCreateContact({
            name: {
                first: "first",
                last: "last"
            },
            emails: [{ email: "email_2", primary: true }]
            // emails: [{ email: "email_1", primary: true }]
        }).then(async ({ contactId: contact_id }) => {
            console.log(contact_id)

            console.log("Variables", variables)
            await myEmailContactFunction("<form_id>", contact_id, variables)
                .then(() => { console.log("Emailed Contact") })
                .catch(console.log)
        })

The issue is that I want all email addresses (email_1-5) to receive the triggered email, but it appears that only email_1 is receiving it. Any thoughts or suggestions?

Product:
Wix Editor

This issue is related to the fact that 1 contact is being created, but with 5 email addresses attached to it.

Triggered emails can be sent via a contactID or memberID depending on how you have it setup.

But currently, you only have one contactID, because only one contact is being created. If you want to send to all the addresses (I’m guessing they might be separate entities), then separate contacts is the way to go.

1 Like

Thanks for your reply, noah. Could you help with any code suggestions to email 5 site members when this triggered email occurs?

I haven’t tested it (and used a little AI to help along the way) as I don’t know your full setup, but it would likely look something like:

// Array of emails
const emails = [
    { email: "email_1", primary: true },
    { email: "email_2", primary: true },
    { email: "email_3", primary: true },
    { email: "email_4", primary: true },
    { email: "email_5", primary: true }
];

// Loop through the emails and create a separate contact for each one
for (const emailObj of emails) {
    await wixCrm.contacts.appendOrCreateContact({
        name: {
            first: "first",
            last: "last"
        },
        emails: [emailObj] // Create one contact with one email
    })
    .then(async ({ contactId: contact_id }) => {
        console.log(contact_id);
        console.log("Variables", variables);

        // Send the email for the current contact
        await myEmailContactFunction("<form_id>", contact_id, variables)
            .then(() => { console.log("Emailed Contact"); })
            .catch(console.log);
    })
    .catch((e) => {
        return { res: false, reason: e };
    });
}

Something to take into consideration is that this will probably create 5 separate contacts.

If your emails are actually for 5 different people, then this should be fine. But if this is actually one person and you want to email each of their emails, you might need an external service like SendGrid so you remain with one contact entity for that one individual