'RESOLVED 'send an email or message from a site user to the business owner on site who isn't loggedIn tutorial

Hello can anyone suggest the right way of doing this when a site user submits a form to the specified business? I’


I’ve looked at the api but all seems a little confusing

Edit:

I have since used the corvid api for sending a triggered email however it works only if the site member is logged in?

Page code

import wixLocation from 'wix-location';
import wixUsers from 'wix-users';

export function submitMessage_click(event) {
//$w.onReady(() => {
 let userId = $w('#idFieldText').text; // get user ID
 let email = $w('#email').value // value for variable1
 let jobDesciption = $w('#messageTextBox').value
 let contactNumber = $w('#contactNumber').value

    wixUsers.emailUser("SD0gxMU", userId, {
 "variables": {
 "email": email,
 "jobDesciption": jobDesciption,
 "contactNumber": contactNumber
            }
        })
        .then(() => {
            console.log("Triggered email sent");
 //wixLocation.to(`/home`) // email has been sent
        })
        .catch((err) => {
            err = $w('#error').show()// there was an error sending the email
        });

}

Any idea’s for a site user to send a site member an email via a form when neither are logged in? can this be done? @givemeawhisky @yisrael-wix (Wix) @J.D. @Giri Zano@David - SKmedia @Yoav (Wix)

Ok so i figured it out myself eventually and its now “WORKING :)”
i hope this helps someone who’s having just as much trouble i was!

This code allows for site users browsing the site to message/email existing site members via a triggered email! who are either ‘LOGGED IN OR OUT’ (which you have to build yourself to get an id code from for the email)

This is how your Triggered email should look with all the essential variables in place

triggered email in dashboard

FRONTEND CODE

import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import { sendEmail } from 'backend/backendEmail';

$w.onReady(() => {

    $w("#dynamicDataset").onReady(() => {

 var currentItem = $w("#dynamicDataset").getCurrentItem();
        $w("#textTitle").text = "Send a message to - " + currentItem.traderBusinessName;

 
        $w("#dataset1").onAfterSave(() => {
 // $w('#uploadGif').hide('FadeOut');
 // let jobTitleVal = currentItem.traderBusinessName;

 let userId = currentItem._id;
 let recruiterEmail = currentItem.traderBusinessName;
 let emails = $w("#email").value;
 let firstName = $w("#firstName").value;
 let lastName = $w("#lastName").value;
 let contactNumber = $w("#contactNumber").value;
 let jobLocation = $w('#jobLocation').value;
 let jobDescription = $w('#messageTextBox').value;
 let jobId = $w('#jobId').text


            $w("#postingMailTo").show();
            $w("#postingMailTo").text = "Posting message to " + recruiterEmail;

            sendEmail(userId, emails, firstName, lastName, jobDescription, contactNumber, jobLocation, jobId, recruiterEmail)
                .then(sendEmailStat => {
 //$w("#successMessage").show();
                    $w("#successMessage").text = 'Email Status is  : ' + sendEmailStat;
                    console.log(sendEmailStat);
                })
                .catch(error => {
                    console.log(error);
                    $w("#error").show();
                    $w("#error").text = "There was an Error sending message! please try again later! " + error;
                });

        });

    });
})

BACKEND CODE

named: backendEmail.jsw

// Filename: backend/backendEmail.jsw (web modules need to have a .jsw extension)

import wixUsersBackend from 'wix-users-backend';

export function sendEmail(userId, emails, firstName, lastName, jobDescription, contactNumber, jobLocation, jobId, recruiterEmail) {
    wixUsersBackend.emailUser("SD0gxMU", userId, { // The Code in red is //from the triggered email you need to replace with your own!!
            variables: {
 "email": emails, // variables in the triggered email named the same, which //can be altered
 "firstName": firstName,
 "lastName": lastName,
 "jobDescription": jobDescription,
 "contactNumber": contactNumber,
 "jobLocation": jobLocation,
 "jobId": jobId,
 "companyName": recruiterEmail

            }
        })
        .then(() => {
 return "Email Sent Successfully" + userId;
        })
        .catch((err) => {
 return "Failed to send Email to recruiter due to error: " + err;

        });
}