Sending an Email on Form Submission Using SendGrid Template

Hello everyone! I have been diving deeper into the wix-fetch API so that I can use 3rd party services in some of my website functionality. I have designed a landing page for a client and that will be used to capture referral form data. I have been able to leverage the Wix Code Tutorial: Sending an Email on Form Submission to successful setup API integration,

However, I would like to make the design a bit more professional that the plain-text option. I was thinking I could do this through Transactional Templates. I have followed the steps and within Sending Template Emails with SendGrid V3 but am still having trouble. Nothing occurs. No email is generated or sent. My code snippets are below. Can anyone spot what I am missing?

//email.jsw

import {sendInstruction} from 'backend/sendGrid';

export function sendEmail(subject, body, Recipient) {
 const key = "XXXXXXXAPIKEYXXXXXXXXXXX";
 const sender = "recipientEmailHere";
 return sendInstruction(key, sender, Recipient, subject, body);
}

//sendGrid.js

export function SendInstruction(key, sender, RecipientEmail, subject, body) {
 const url = "https://api.sendgrid.com/v3/mail/send";
 
 const MyHeaders = {
 "Authorization": "Bearer " + key,
 "Content-Type": "application/json"
  };
 const MyBody = {
 "personalizations": [{
 "to": [{
 "email": RecipientEmail
      }]
    }],
 "from": {
 "email": sender
    },
 "subject": subject,
 "content": [{
 "type": "text/html",
 "value": body
    }],
 "template_id" : "d-9ccb08ff65b3405b82f430ee3f6e5014"
  };

 return fetch(url, {
 "method": "POST",
 "headers": MyHeaders,
 "body": JSON.stringify(MyBody)
    })
    .then(Response => Response.text);   
}

Page Code

import {sendEmail} from 'backend/email';

function SendClientEmail() {
const Subject = `New Submission from ${$w("#firstNameInput").value}`;
const body =  `Name: ${$w("#firstNameInput").value}     
\rEmail: ${$w("#referralEmailInput").value}`;

const RecipientEmail = 'recipientEmail';

  sendEmail(Subject, body, RecipientEmail)
    .then(response => console.log(response)); 
}

There may be a lot of issues here but any assistance would be great!

1 Like

Here are a couple of examples that will be helpful:

Send Email with SendGrid NPM Interface

Send an email using the SendGrid NPM library.

Send Email with SendGrid REST Interface

Send an email using the SendGrid REST API.

BUMP!
Here trying to use a template with sendgrid as well, to no avail. Here’s hoping we find an answer!

https://support.totallycodable.com/en/article/send-a-template-email-using-sendgrid-npm-and-wix-code

1 Like

Thank you for sharing our tutorial article :blush:

2 Likes

@johnathonlynch I was able to get everything to work by following Nayeli’s tutorial.

1 Like

I’ve followed all the instructions and it doesn’t work. Please help.

It seems like it stopped working for a reason ! Any idea why ?

I managed to send Sendgrid template variables, documentation here: