HI
Can anyone help me with this?
i basically need to fill out a form as an aidministrator (Me) and then have the triggered email sent to a specific email address of a user.
I have that email address of the user as a text box in the same form i just need to amend the above code some how to email that person rather than as the code is written here to email logged in user.
Thanks in advance
Dan
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
$w.onReady(function () {
$w("#checkInsWrite").onAfterSave( () => {
const userId = 'ff1990c7-eb13-481c-ae60-6fae9f4c6060';
const options = {
day: "numeric",
month: "short",
year: "numeric",
};
console.log(userId)
wixUsersBackend.emailUser('checkIn', userId, {
variables: {
weight: $w("#currenWeight").value,
name: $w("#firstName").text,
fat: $w("#fatInpercentage").value,
date: $w("#checkInDate").value.toLocaleDateString("en-US",options),
time: $w("#time").value.substr(0, 5),
comments: $w("#howTheyfelt").value,
}
} )
.then( () => {
// do something after the email was sent successfully
wixLocation.to("/home");
// console.log("Email Sent")
} )
.catch( (err) => {
// handle error that prevented the email from being sent
console.log(err)
} );
}
);
} );
export function submitCheckinButton_click(event) {
// Add your code for this event here:
$w('#profile').setFieldValue('currentWeight', $w('#currenWeight').value);
$w('#profile').save()
console.log("Member profile weight updated!")
$w("#checkInsWrite").save();
}
The second approach is different ..
We need to get the user ID, name and email address before submission, or .. we can just create a reference field in the database that holds the member who submitted the data.
Step 1: Create a reference field in your collection to the members collection called "Members/PrivateMembersData", give the field ID the value 'owner'.
Step 2: Prepare the page by importing the necessary web modules.
import wixUsers from 'wix-users'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; // We'll use this to email the admin import {notifyAdmin} from 'backend/member.jsw';
Step 3: Decide which approach you want to use to submit the data, is it:
A) Using the dataset:
To use the dataset, make sure the dataset mode is set to "Write-Only", connect all the elements to the dataset and add this code to the page's onReady() function.
$w.onReady(() => { let variables = {}; // We'll need it later $w('#checkInsWrite').onReady(() => { $w('#checkInsWrite').onBeforeSave(() => { // First area - We'll run some code here later }) $w('#checkInsWrite').oAfterSave(async() => { // Second area - We'll run some code here later }) // Third area - We'll use it later // End of area 3 three }) })
Then we need to set the user ID as the owner of the submitted data - we can use the _owner field but it doesn't allow you to access the member data such as their name.
Place this code in the first specified area above, then save the details locally. This code block saves the user ID with the submitted form data and save variables for later use.
$w('#checkInsWrite').setFieldValue({ owner: wixUsers.currentUser.id }); $w('#profile').setFieldValue({ currentWeight: $w('#currenWeight').value }); // Now we'll use the variable object we declared in step 3-A const options = { day: "numeric", month: "short", year: "numeric" } variables.weight = $w("#currenWeight").value; variables.name = $w("#firstName").text; variables.fat = $w("#fatInpercentage").value; variables.date = $w("#checkInDate").value.toLocaleDateString("en-US",options); variables.time = $w("#time").value.substr(0, 5); variables.comments = $w("#howTheyfelt").value;
B) Wix Data API - Without a dataset.
This method uses wix data API to save the form to the collection.
To submit the form using this method, we'll need only one event handler, but we'll need to validate each element and take care of the uploaded images individually, I don't think you need this approach in your particular case.
Backend Page 'member.jsw'.
Step 4: Import wix users (backend module) to our web module.
import wixUsersBackend from 'wix-users-backend';
Step 5: Create a function on our web module on backend:
export function notifyAdmin(variables) { const admin_id = 'ff1990c7-eb13-481c-ae60-6fae9f4c6060'; let options = { variables: variables } return wixUsersBackend.emailUser('checkIn', admin_id, options) .then(() => { return 'success'; }).catch(err => { return { error: err } }) }
Frontend Page
Step 6: Save the other dataset values, and call the function to email the user:
Place this code inside the second area in Step 3: A.
$w('#profile').save(); /* Now send an Email to the administrator (You) , we'll use the custom function */ await notifyAdmin(variables).then((result) => { if (typeof result === 'string' && result === 'success') { wixLocation.to('/home'); } else { console.error(result.error); } })
Step 7: Trigger the save process.
We'll create an onClick event handler to trigger the saving process when clicked.
Place this code inside the third area in Step 3: A.
$w('#submitCheckinButton').onClick((event) => { $w('#checkInsWrite').save(); })
APIs used: page's onReady, dataset's onReady, dataset's onAfterSave and dataset's onBeforeSave event handlers and dataset's save.
Hope this helps~!
Ahmad
Looks like I didn't get it first, but now I do ..
I thought you want to email yourself (admin), to email a user we'll need to get his/her ID using their email address, then we can email the user, here's what I suggest:
Step1: Create a function on the backend on any web module, mine is member.jsw:
export function getUserIdByEmail(email) { return wixData.query('Members/PrivateMembersData') .eq('loginEmail', email) .find({suppressAuth: true}) .then((result) => { if (result.length > 0) { return result.items[0]._id; } else { return { error: 'not found' } } }).catch(err => { return { error: err } }) }
Step 2: Import the function into the page you want to send the email from:
import {getUserIdByEmail} from 'backend/member.jsw';
Step 3: Invoke the function to get the user ID.
$w("#checkInsWrite").onAfterSave(async () => { const options = { day: "numeric", month: "short", year: "numeric" } let userId = await getUserIdByEmail().then((result) => { if (typeof result === 'string') { return } else { console.error(result.error) return null; } }) if (userId) { wixUsersBackend.emailUser('checkIn', userId, { variables: { name: $w("#firstName").text, fat: $w("#fatInpercentage").value, date: $w("#checkInDate").value.toLocaleDateString("en-US",options), time: $w("#time").value.substr(0, 5), comments: $w("#howTheyfelt").value, } }) } })
If you need further explanation please let me know, also, feel free to reply back with any related questions.
Ahmad
OK so this is for the second scenario for me as admin to email a user? Can you write the one where by the user fills out a form and submit button emails just me the administrator?
my id in the private member data base collection is ff1990c7-eb13-481c-ae60-6fae9f4c6060 do i need to put that in the code?
@krays23 Yes it need to be included, but for your safety, I suggest that the ID and the whole process be on the backend for security reasons. I'll write a separate answer for the other scenario.
@Ahmad HI Ahmad firstly let me apologise for my disappearance. been mad here and only just got back on the job.
so the above first code is what i have forgotten is it obsolete?
the second code is for me the admin to send a triggered email a specified user? ( how does the specified user get selected?
how about the one for the logged in user to send me a triggered email?
The code below is from the form i want the user to fill out and then when they click submit the email always come to me the administrator
thanks again
Dan
import wixLocation from 'wix-location'; import wixData from 'wix-data'; import wixUsersBackend from 'wix-users-backend'; $w.onReady(function () { $w("#checkInsWrite").onAfterSave( () => { const userId = 'ff1990c7-eb13-481c-ae60-6fae9f4c6060'; const options = { day: "numeric", month: "short", year: "numeric", }; console.log(userId) wixUsersBackend.emailUser('checkIn', userId, { variables: { weight: $w("#currenWeight").value, name: $w("#firstName").text, fat: $w("#fatInpercentage").value, date: $w("#checkInDate").value.toLocaleDateString("en-US",options), time: $w("#time").value.substr(0, 5), comments: $w("#howTheyfelt").value, } } ) .then( () => { // do something after the email was sent successfully wixLocation.to("/home"); // console.log("Email Sent") } ) .catch( (err) => { // handle error that prevented the email from being sent console.log(err) } ); } ); } ); export function submitCheckinButton_click(event) { // Add your code for this event here: $w('#profile').setFieldValue('currentWeight', $w('#currenWeight').value); $w('#profile').save() console.log("Member profile weight updated!") $w("#checkInsWrite").save(); }