Contact form with SendGrid

Well, I’ve solved it. Yes, there was a problem with SendGrid. You need to make sure your API Key as full permissions, otherwise “post” won’t work. The majority of my problem really seemed to be in my page code, though. This finally works:

$w.onReady(function () {
	// Grab the current user's ID
	let user = wixUsers.currentUser;
	let userId = user.id; 
	
	// Get this user's name and email, and put it in the appropriate inputs
	wixData.query("OurTest")
    .eq('_id', userId)
    .find()
    .then( (results) => {
      $w('#input1').value = results.items[0].userName;
   	  $w('#input2').value = results.items[0].email;
   }
      
);}); 

$w.onReady(function () {
	$w('#button1').onClick(sendMsg);
});

function sendMsg() {
	
	let user = wixUsers.currentUser;
	let userId = user.id;
	
	wixData.query("OurTest")
    .eq('_id', userId)
    .find()
    .then( (results) => {
      const sender = results.items[0].email;
   	  const recipient = results.items[0].sendEmail;
   	  const subject = `New Mesage from ${$w("#input1").value}`;
   	  const body = `Name: ${$w("#input1").value}
    	\rEmail: ${$w("#input2").value}
    	\rSubject: ${$w("#input3").value}
    	\rMessage: ${$w("#textBox1").value}`;
    	
    	debugger;
    	
   	if (($w('#input3').value === "") || ($w('#textBox1').value === "")){
		$w('#input3').value = "Subject Required!";
		$w('#textBox1').value = "Message Required!";
	}
	else{ 	
		
    	console.log(body);
    	console.log (recipient);
    	console.log(sender);
    	sendEmail(subject, body)
    	.then(response => console.log(response)); 
    	$w('#input3').value = "";
		$w('#textBox1').value = "Message Sent";
	}
 });

This was just testing with static addresses on the backend, but I’m eventually going to change this to grab the recipient out of the database and possibly send to a second recipient as well (this works: https://www.wix.com/code/home/forum/questions-answers/wix-code-form-email-notifications).

–Al