Website: cosworthvega.com
Page: Contact Form
I have obtained the user.ID of a group of users(officers) for my site that need to be notified when a contact form has been filled out. I did this by having each officer reset their password to "Password123". I then logged in as each user and using wix code, logged the wixID of each user and stored in a database CVOA-Officers. They then reset their password back to what it was.
The following code uses the wixUsers.emailUser API....
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
import wixPay from 'wix-pay';
import wixCRM from 'wix-crm';
//>>>>>>>>>>>>>>>>>>> Multi Email >>>>>>>>>>>
// These variables are loaded in the on ready
//Presidents email
let presidentEmail;
let presidentId;
//Webmasters Email
let webmasterEmail;
let webmasterId;
//Registras Email
let registrarEmail;
let registrarId;
//VP Merchandise Director Email
let vpMerchDirEmail;
let vpMerchDirId;
// VP Membership & Finance Director
let vpMembFinanceEmail;
let vpMembFinanceId;
//the regigional director is loaded in the sendEmail function
let RegDirEmail;
let RegDirId;
//>>>>>>>>>>>>>>>>>>> Multi Email variable ends >>>>>>>>>>>
$w.onReady(function () {
//loads Registra Email
wixData.query("CVOA-Officials")
.eq("title", "Registrar")
.find()
.then(results => {
let firstItem = results.items[0];
registrarEmail = firstItem.eMail;
registrarId = firstItem.wixId;
});
//loads Webmaster Email
wixData.query("CVOA-Officials")
.eq("title", "Webmaster")
.find()
.then(results => {
let firstItem = results.items[0];
webmasterEmail = firstItem.eMail;
webmasterId = firstItem.wixId;
});
//loads the VP Merch email
wixData.query("CVOA-Officials")
.eq("title", "VP Merchandise Director")
.find()
.then(results => {
let firstItem = results.items[0];
vpMerchDirEmail = firstItem.eMail;
vpMerchDirId = firstItem.wixId;
});
//loads the VP Member & Finance email
wixData.query("CVOA-Officials")
.eq("title", "VP Membership and Finance")
.find()
.then(results => {
let firstItem = results.items[0];
vpMembFinanceEmail = firstItem.eMail;
vpMembFinanceId = firstItem.wixId;
});
//loads the president email
wixData.query("CVOA-Officials")
.eq("title", "President")
.find()
.then(results => {
let firstItem = results.items[0];
presidentEmail = firstItem.eMail;
presidentEmail = firstItem.wixId;
});
});
export function sendButton_click(event) {
console.log("Sending to: ", webmasterEmail + " " + webmasterId)
wixUsers.emailUser("RC70ht8", webmasterId, {
variables: {
"name": $w("#name").value,
"email": $w("#email").value,
"member": $w("#member").value,
"message": $w("#message").value
}
} )
.then( () => {
console.log("MESSAGE SENT TO: " + webmasterEmail + " " + webmasterId);
} )
.catch( (err) => {
console.log("ERROR OCURRED SENDING...." + " " + webmasterEmail +" " + err.message);
} );
// Send to President
console.log("Sending to: ", presidentEmail + " " + presidentId)
wixUsers.emailUser("RC70ht8", presidentId , {
variables: {
"name": $w("#name").value,
"email": $w("#email").value,
"member": $w("#member").value,
"message": $w("#message").value
}
} )
.then( () => {
console.log("MESSAGE SENT TO: " + presidentEmail + " " + presidentId );
} )
.catch( (err) => {
console.log("ERROR OCURRED SENDING...." + " " + presidentEmail +" " + err.message);
} );
// Send to VP Membership
console.log("Sending to: ", vpMembFinanceEmail + " " + vpMembFinanceId)
wixUsers.emailUser("RC70ht8", vpMembFinanceId , {
variables: {
"name": $w("#name").value,
"email": $w("#email").value,
"member": $w("#member").value,
"message": $w("#message").value
}
} )
.then( () => {
console.log("MESSAGE SENT TO: " + vpMembFinanceEmail + " " + vpMembFinanceId );
} )
.catch( (err) => {
console.log("ERROR OCURRED SENDING...." + " " + vpMembFinanceEmail +" " + err.message);
} );
}
===============END CODE ===========================
Upon clicking SUBMIT I get the following errors in Developers Tools....
The FIRST message goes but the other two do not???
I have a paid Wix Premium account and AM able to send emails to the logged in user. What I am trying to do is send emails to the other administrators of the site.
Yep, I figured as much once I printed out the error message. Can I use wixCRM.emailContact to accomplish my need? As noted above... had tired to use sendgrid but the free account uses pubic servers and it was not just reliable. These notifications would number around a dozen a month. A paid email service cannot be justified.
Thanks
JD
Yep. Fixed that... now on to next error??
since you're retrieving 9 emails, you should remove or change the
.limit(5)
from your query
Hmmm.. After noticing that same item was logged as Got.... I added the item number...
Seems the query does not return them the way I thought it would???
Thanks. I think you were right. I redid the code to grab all the IDs in one query, Console log showed it got them... BUT... sending the mail failed.... maybe need to put a delay timer between each one???
Here is the new code, followed by screen shot of Console Log...
import wixData from 'wix-data'; import wixUsers from 'wix-users'; import wixWindow from 'wix-window'; import wixPay from 'wix-pay'; import wixCRM from 'wix-crm'; //>>>>>>>>>>>>>>>>>>> Multi Email >>>>>>>>>>>
// These variables are loaded in the on ready //Presidents email let presidentEmail; let presidentId; //Webmasters Email let webmasterEmail; let webmasterId; //Registras Email let registrarEmail; let registrarId; //VP Merchandise Director Email let vpMerchDirEmail; let vpMerchDirId; // VP Membership & Finance Director let vpMembFinanceEmail; let vpMembFinanceId; //the regigional director is loaded in the sendEmail function let rdMwEmail; let rdMwId; let rdNeEmail; let rdNeId; let rdSeEmail; let rdSeId; let rdWEmail; let rdWId; //>>>>>>>>>>>>>>>>>>> Multi Email variable ends >>>>>>>>>>> $w.onReady(function () { //loads Registra Email wixData.query("CVOA-Officials") .limit(5) .ascending("title") .ne("title", "XTest") .find() .then(results => { let firstItem = results.items[0]; // President presidentEmail = firstItem.eMail; presidentId = firstItem.wixId; console.log("Got: " +presidentEmail + "/" + presidentId) firstItem = results.items[1]; rdMwEmail = firstItem.eMail; rdMwId = firstItem.wixId; console.log("Got: " +rdMwEmail + "/" + rdMwId) firstItem = results.items[2]; rdNeEmail = firstItem.eMail; rdNeId = firstItem.wixId; console.log("Got: " +rdNeEmail + "/" + rdNeId) firstItem = results.items[3]; rdSeEmail = firstItem.eMail; rdSeId = firstItem.wixId; console.log("Got: " +rdSeEmail + "/" + rdSeId) firstItem = results.items[4]; rdWEmail = firstItem.eMail; rdWId = firstItem.wixId; console.log("Got: " +rdWEmail + "/" + rdWId) firstItem = results.items[5]; registrarEmail = firstItem.eMail; registrarId = firstItem.wixId; console.log("Got: " +registrarEmail + "/" + registrarId) firstItem = results.items[6]; vpMembFinanceEmail = firstItem.eMail; vpMembFinanceId = firstItem.wixId; console.log("Got: " +vpMembFinanceEmail + "/" + vpMembFinanceId) firstItem = results.items[7]; vpMerchDirEmail = firstItem.eMail; vpMerchDirId = firstItem.wixId; console.log("Got: " +vpMerchDirEmail + "/" + vpMerchDirId) firstItem = results.items[8]; webmasterEmail = firstItem.eMail; webmasterId = firstItem.wixId; console.log("Got: " +webmasterEmail + "/" + webmasterId) }); });
export function sendButton_click(event) { console.log("Sending to: ", webmasterEmail + " " + webmasterId) wixUsers.emailUser("RC70ht8", webmasterId, { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + webmasterEmail + " " + webmasterId); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to President console.log("Sending to: ", presidentEmail + " " + presidentId) wixUsers.emailUser("RC70ht8", presidentId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + presidentEmail + " " + presidentId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to VP Membership console.log("Sending to: ", vpMembFinanceEmail + " " + vpMembFinanceId) wixUsers.emailUser("RC70ht8", vpMembFinanceId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + vpMembFinanceEmail + " " + vpMembFinanceId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to VP Merchandise Dir console.log("Sending to: ", vpMerchDirEmail + " " + vpMerchDirId) wixUsers.emailUser("RC70ht8", vpMerchDirId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + vpMembFinanceEmail + " " + vpMembFinanceId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...." ); } ); // Send to NE Regional Dir console.log("Sending to: ", rdNeEmail + " " + rdNeId) wixUsers.emailUser("RC70ht8", rdNeId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + rdNeEmail + " " + rdNeId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to SE Regional Dir console.log("Sending to: ", rdSeEmail + " " + rdSeId) wixUsers.emailUser("RC70ht8", rdSeId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + rdSeEmail + " " + rdSeId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to MW Regional Dir console.log("Sending to: ", rdMwEmail + " " + rdMwId) wixUsers.emailUser("RC70ht8", rdMwId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + rdMwEmail + " " + rdMwId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Send to West Regional Dir console.log("Sending to: ", rdWEmail + " " + rdWId) wixUsers.emailUser("RC70ht8", rdWId , { variables: { "name": $w("#name").value, "email": $w("#email").value, "member": $w("#member").value, "message": $w("#message").value } } ) .then( () => { console.log("MESSAGE SENT TO: " + rdWEmail + " " + rdWId ); } ) .catch( (err) => { console.log("ERROR OCURRED SENDING...."); } ); // Done Sending.. }
Is it possible that the all queries to fetch the recipients email and id didn't return by the time you clicked "send"? It seems so from your logs. You can verify it by making sure the id and email are set before sending the email.
Also, you should consider a different approach to get the recipients details. 4 different queries to the collection might be slow. I suggest to try to get all the info in one query.
Also, I tried SendGrid and since it uses public servers, it intermittently drops email because the server used may have that domain blocked. To send a dozen or so messages a month to 6 people does not justify buying the paid plan.