Hi, Here is the code:
// Page Code
import { sendEmail } from ‘backend/sendEmail’ ;
import wixWindow from ‘wix-window’ ;
import wixUsers from ‘wix-users’ ;
$w . onReady ( function () {
wixUsers . currentUser . getEmail ()
. then ( ( email ) => {
$w ( ‘#fromEmail’ ). value = email ;
})
});
export function btnSend_click ( event , $w ) {
console . log ( $w ( “#toEmail” ). value + $w ( “#subject” ). value + $w ( “#fromEmail” ). value + $w ( “#emailContent” ). value );
if ( $w ( ‘#toEmail’ ). validity . valid === false )
return ;
if ( $w ( ‘#fromEmail’ ). validity . valid === false )
return ;
if ( $w ( ‘#subject’ ). validity . valid === false )
return ;
if ( $w ( ‘#emailContent’ ). validity . valid === false )
return ;
sendEmail (
$w ( "#toEmail" ). value ,
$w ( "#subject" ). value ,
$w ( "#fromEmail" ). value ,
$w ( "#emailContent" ). value )
. then (( res ) => {
let dataObj = {
status : “success” ,
email : $w ( ‘#toEmail’ ). value
};
console . log ( dataObj );
//wixWindow.openLightbox(“Message”, dataObj);
//$w(“#toEmail”).value = “”;
$w ( “#subject” ). value = “” ;
//$w(“#fromEmail”).value = “”;
$w ( “#emailContent” ). value = “” ;
//$w(“#toEmail”).resetValidityIndication();
$w ( “#subject” ). resetValidityIndication ();
//$w(“#fromEmail”).resetValidityIndication();
$w ( “#emailContent” ). resetValidityIndication ();
})
. catch (( error ) => {
console . log ( error );
let dataObj = {
status : “fail” ,
email : $w ( ‘#toEmail’ ). value
};
console . log ( dataObj );
//wixWindow.openLightbox(“Message”, dataObj);
})
}
// sendGrid.js
import { fetch } from ‘wix-fetch’ ;
const key = " 'SG… I used my API Key here" ;
export function sendWithService ( sender , recipient , subject , body ) {
const url = “[Link here - 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 ());
}
//sendEmail.jsw
import { sendWithService } from ‘backend/sendGrid’ ;
export function sendEmail ( recipient , subject , sender , body ) {
return sendWithService ( sender , recipient , subject , body );
}
Error: Mentioned in description
Please check this & let me know if I did something wrong.