Sending Email (SendGrid) From Dataset

Hi everyone,
In my website, I want to send email to everyone whose email is in my dataset, like -

My existing code is

export function button6_click(event) {

if(food1 == 1){
 
function sendFormData() {
 const subject = `New Submission from ${$w('#input1').value}`;
 const body = `Name: ${$w('#input1')}
    \ritem: ${"food"}
    \rname: ${$w('#input1').value}`;
 const recipient = //here;
 
 
  sendEmailWithRecipient(subject, body, recipient)
    .then(response => console.log(response)); 
}
}

In the above code, what I am trying to achieve is

  1. Get all the email from the dataset
  2. Send the email to the respected emails.

Thats’s all,
Thank You

You can send triggered emails to the member who triggered the email. The person fills out a form, then the email gets sent to that member. I don’t think you can send an email to all members. You can make an email campaign inside the dashboard. Have you even made your email? Once you have published it, you will see some links to articles.

Hi :raised_hand_with_fingers_splayed:

You’re declaring a function inside the event handler, but you’re not calling it, move your function declaration outside the event handler and only call it inside the event handler.

Also, try a for loop to send the email to each member in the database, first get all the items from the database that has a valid email address with a query , then loop through the items to email each one individually.

Hope this helps~!
Ahmad

Thanks Ahmad ! Can you just provide an example for loop function. I am a bit confused.
Ajith

Regarding the query, you need to get all the entries, then check each entry if it has a valid email or not using the for loop.

wixData.query('Contact').ne('email', '').limit(1000).find()
    .then(async(results) => {
        if (results.length > 0) {
            
            for (let i = 0; i < results.length; i++) {
                let item = results.items[i];
                
                if (item.email) {
                    await sendFormData();
                }
            }
        } else {
            // no results
        }
    })

You need to change these fields dynamically instead of using the input fields, I suggest that you retrieve them from the query result.

Very very thanks for your assistance and valuable time.

Thanks,
Ajith

You’re welcome :wink:

Hai Ahmad,
Can you check on my new post

Ajith

Post the link of your post here…