I have been trying for days to use the Wix Crm to trigger an email to the Admin of the site. It hasn’t worked. I finally gave in and went back to Send Grid and it is still not working. I am using session.setItem and getItem to insert into these forms and everything is going in great, but the email won’t send. I have tried it on all pages but ideally I would just like the info from the first page to go into the email. I have used sendGrid just fine on my other sites I don’t know what changed here.
I have used OnafterSave and Onafterinsert. PLEASE I need a solution today. I have taken Identifying info out
emailJSW
import {sendWithService} from 'backend/sendGrid';
export function sendEmail(subject, body) {
const key = "SG.";
const sender = "info@zzzzzzz.com";
const recipient = "e_hey@meeee.com";
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = "SG.";
const sender = "info@zzzzzzz.com";
return sendWithService(key, sender, recipient, subject, body);
}
sendGridJS
import {fetch} from 'wix-fetch';
export function sendWithService(key, sender, recipient, subject, body) {
const url = "https://api.sendgrid.com/api/mail.send.json";
const headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/x-www-form-urlencoded"
};
const data = `from=${sender}&to=${recipient}&subject=${subject}&text=${body}`;
const request = {
"method": "post",
"headers": headers,
"body": data
};
return fetch(url, request)
.then(response => response.json());
}
Page Code for Email. I have removed the button trigger etc… that is before
function sendFormData() {
because that is what I need to know. This email format is taken from my old site
(‘#formIdInput’)
(‘#formType’)
(‘#businessName’)
(‘#email’)
Are all hidden
import wixData from 'wix-data';
import {session} from 'wix-storage';
import wixCrm from 'wix-crm';
import {sendEmail, sendEmailWithRecipient} from 'backend/email';
//-------------Notification Code-------------//
$w.onReady(function () {
$w('#formIdInput').value = session.getItem("formId");
$w('#formType').value = session.getItem("title");
$w('#businessName').value = session.getItem("companyName");
$w('#email').value = session.getItem("email");
});
function sendFormData() {
const subject = `${$w('#businessName').value} Is applying to be a 2020 TT Street Fair Vendor`;
const body = `${ $w('#businessName').value} is applying for their Street Fair Vendor Space. Should you need to track them down,here are their details:
\rCompany: ${$w('#businessName').value}
\rName:
\rEmail:
\rPhone:
\rPlease Approve Them Here: https:`;
debugger;
console.log(body);
sendEmail(subject, body)
.then(response => console.log(response)).catch(err => console.log('ERR:', err));
}
}
1st part of the form. I would like the email to come after this part of the form is submitted.
//-------------Imports-------------//
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';
import wixCrm from 'wix-crm';
//-------------Form ID------------//
$w.onReady( function() {
function randomNumber (len) {
var x;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}
$w('#formIdInput').value = randomNumber(10);
//-------------Status------------//
$w('#status').value = "PENDING";
});
//-------------Form Submission-------------//
export function next_click(event) {
session.setItem("formId", $w('#formIdInput').value);
session.setItem("title", $w('#conDropdown').value);
session.setItem("status", $w('#status').value);
session.setItem("companyName", $w('#businessName').value);
session.setItem("firstName", $w('#firstName').value);
session.setItem("lastName", $w('#lastName').value);
session.setItem("email", $w('#email').value);
session.setItem("phone", $w('#phone').value);
//-------------Dropdown Code-------------//
if ($w('#conDropdown').value === 'Membership Application') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/member-applications-business")
} else if ($w('#conDropdown').value === 'Flower Basket Plaques') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/flower-basket-plaques")
} else if ($w('#conDropdown').value === 'Mailing a Payment') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/payment-by-mail")
} else if ($w('#conDropdown').value === 'Membership Deal') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/membership-deals")
} else if ($w('#conDropdown').value === 'Sleepy Hollow Street Fair') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/streetfair-application")
} else if ($w('#conDropdown').value === 'Tarrytown Street Fair') {
$w('#next').link = wixLocation.to("https://elizabethjhay.wixsite.com/mysite-9/streetfair-application")
}
}
//-------------Create Contact-------------//
$w.onReady(function () {
$w("#next").onClick( () => {
wixCrm.createContact( {
"firstName": $w("#firstName").value,
"lastName": $w("#lastName").value,
"emails": [$w("#email").value],
"phones": [$w("#phone").value],
"Business Name": $w('#businessName').value
}
);
});
});