# New Sendgrid node module, send email to more than one address with variable template\[SOLVED\]

**URL:** <https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [June 5, 2019, 2:38pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305 "2019-06-05T14:38:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [June 5, 2019, 2:38pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/1 "2019-06-05T14:38:43Z")

</div>

Hi I was hoping someone might be able to help me with my code, i’m trying to get some variable templates through sendgrid to deliver with an onClick through a repeater. The template works as I have tested through onAfterUpdate. I’m just trying to get this to happen with a button click.

**Backend**

```javascript
import {fetch} from 'wix-fetch';
import sgMail from '@sendgrid/mail';
import wixData from 'wix-data';

export function sendOutstandinglnvoiceEmail(){
    sgMail.setApiKey('API KEY here');
    wixData.query("CourseAvailability")
    .find()
    .then((results) => {
 let item = results.items[0];
 let fullname = item.fullName;
 let courseTitle = item.title;
 let totalPrice = item.totalPrice;
 let outstandingBalance = item.outstandingBalance;
 const msg = {
            to: item.customerEmail,
 from: "info@scottishrockandwater.com",
            templateId: 'd-c68106a391a24fe0b08c22285fc2427e', //Outstanding Invoice
            dynamic_template_data: {
                name: fullname,
                courseTitle: courseTitle,
                totalPrice: totalPrice,
                outstandingBalance: outstandingBalance,
            },
        };
        sgMail.send(msg);
        console.log(fullname)
        console.log(courseTitle)
        console.log(totalPrice)
        console.log(outstandingBalance)
    })
}

```

Frontend

```javascript
import {sendOutstandinglnvoiceEmail} from 'backend/email';

//various repeater data above this hence the itemData
$w("#sendReminderButton").onClick((event) => {
                        sendOutstandinglnvoiceEmail(
                                itemData.customerEmail,
                                itemData.fullname,
                                itemData.title,
                                itemData.oustandingBalance,
                                itemData.totalPrice)
                            .then(function () {
                                console.log("email was sent");
                            })
                            .catch((err) => {
                                console.log("Error!");
                            });
                    })

```

Errors

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/9/69575295fa084e93b6b4c6c97e71add229cdc125.jpeg)  
I’m sure a few links will be sent over but I’ve been trying to achieve this for the 36 hours and I’m pretty confident I have read them I just can’t figure this out.

I feel close with this one just need to get the data transferred to send the email I think…

Thank you in advance and I hope this is clear enough to be able to help.

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [June 5, 2019, 8:21pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/2 "2019-06-05T20:21:20Z")

</div>

[https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission](https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission)  
[https://www.youtube.com/watch?v=0SVvNKNEmWk](https://www.youtube.com/watch?v=0SVvNKNEmWk)  
[https://www.youtube.com/watch?v=bPd7o7hUfGk](https://www.youtube.com/watch?v=bPd7o7hUfGk)  
[https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-rest-interface](https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-rest-interface)  
[https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-npm-interface](https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-npm-interface)

---

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [June 5, 2019, 8:38pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/3 "2019-06-05T20:38:22Z")

</div>

Hey, i really appreciate your help, as I mentioned I have watched studied these for the past 3 days between 12-14 hours per day… so I thought I’d ask for some help to adjust the code.

---

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [June 6, 2019, 11:36am UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/4 "2019-06-06T11:36:18Z")

</div>

It works!

 ![](https://us1.discourse-cdn.com/wix/original/3X/a/a/aa3f2449d5738b6422d46cc73ac0742a85660a0a.jpeg)

**Frontend**

```javascript
import {invoiceOutstandingEmail} from 'backend/email';

//various repeater data above here providing the data (itemData.yourFieldKeys)
let customerEmail = itemData.customerEmail;
                    $w("#sendReminderButton").onClick((event) => {
                        console.log(customerEmail)
                        invoiceOutstandingEmail(
                                customerEmail, //'Recipient' from backend
                                itemData.fullName, //'Name' from backend
                                itemData.title, //'CourseTitle' from backend
                                itemData.totalPrice, //'TotalPrice' from backend
                                itemData.outstandingBalance) // 'OutsandingBalance' from back end
 
                            .then(() => {
                            let dataObj = {
                                    status: "success",
                                };
                            })
                            .catch((error) => {
                            let dataObj = {
                                    status: "fail",
                                };
                            })
                    })
//rest of repeater data

```

Backend in my example called: email.jsw

```javascript
import sgMail from '@sendgrid/mail';
 
export function invoiceOutstandingEmail(Recipient, Name, CourseTitle, TotalPrice, OutsandingBalance) {
 const apiKey = "------ Your APIKey ----------";
    sgMail.setApiKey(apiKey);
 let Sender = "info@scottishrockandwater.com"
 const msg = {
        to: Recipient, //send to the email address in your own database
        cc: Sender, //send to your own address
 "from": {
 "email": Sender,
 "name": "Scottish Rock & Water" //shows this upon email receipt
            },
        templateId: '------- Your template Id -------', //Outstanding Invoice
        dynamic_template_data: {
            name: Name, courseTitle: CourseTitle ,totalPrice:TotalPrice, outstandingBalance:OutsandingBalance
 // name, courseTitle, totalPrice, outstandingBalance (lowercase first letter) is template variable
 // Name, CourseTitle, TotalPrice, OutstandingBalance (Upper case first letter is used to pass to the front end)
        },
    };
    sgMail.send(msg);
}

```

 ![](https://us1.discourse-cdn.com/wix/original/3X/8/a/8ae5948c23ca7e6819cbc5784847f6e54550422f.jpeg)

 ![](https://us1.discourse-cdn.com/wix/original/3X/d/5/d555de8adabe648151f6513b9000883d446f4c93.jpeg)

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [June 11, 2019, 12:23pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/5 "2019-06-11T12:23:36Z")

</div>

Well done Stephen :+1:

The only thing I would look at changing on it is this line.

```javascript
cc: Sender, //send to your own address

```

I would do it as bcc so that the email sent to yourself is hidden from the clients own email.

```javascript
bcc: Sender, //send to your own address privately

```

There are many of websites out there about it too.  
[https://www.microsoft.com/en-us/microsoft-365/blog/2012/03/23/5-tips-on-using-bcc-in-outlook-email/](https://www.microsoft.com/en-us/microsoft-365/blog/2012/03/23/5-tips-on-using-bcc-in-outlook-email/)  
[Blind carbon copy - Wikipedia](https://en.wikipedia.org/wiki/Blind_carbon_copy)  
[https://www.technology.pitt.edu/help-desk/how-to-documents/using-blind-carbon-copy-bcc-feature-protect-privacy-email-addresses](https://www.technology.pitt.edu/help-desk/how-to-documents/using-blind-carbon-copy-bcc-feature-protect-privacy-email-addresses)

---

<div class="post-metadata">

**Author:** ![contact22620](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@contact22620](https://forum.wixstudio.com/u/contact22620)\
**Post date:** [August 11, 2020, 11:28pm UTC](https://forum.wixstudio.com/t/new-sendgrid-node-module-send-email-to-more-than-one-address-with-variable-template-solved/18305/6 "2020-08-11T23:28:12Z")

</div>

Hey @stephenmccall ,  
Thanks a lot for your solved comment. It helped me a lot, you’re amazing.
