Using sendGrid send multiple files

I checked this forum
https://www.wix.com/corvid/forum/community-discussion/include-images-documents-in-form-submission-email

It’s works.
But when only 1 image sends, it does not work.

Please help me.

import {sendEmail} from 'backend/email';

$w.onReady(function () {
  $w("#dataset1").onAfterSave(sendFormData);
});

// wix:document://v1 -> docs.wixstatic.com/ugd

function sendFormData() {
try{

 
 const item = $w("#dataset1").getCurrentItem();
 const wiximageurl = "https://static.wixstatic.com/media/";
 let imagelocal = "";
      imagelocal = item.image.replace('image://v1/', '');
      imagelocal = imagelocal.substr(imagelocal.lastIndexOf('/')+1);
 const imageUrl = wiximageurl + imagelocal;

 let whoimagelocal = "";
     whoimagelocal = item.whoimage.replace('image://v1/', '');
      whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf('/')+1);
 const whoimageUrl = wiximageurl + whoimagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
    \name: ${$w("#input1").value}
    \mail: ${$w("#input2").value}
    \birthday: ${$w("#datePicker1").value}
    \image: ${imageUrl}

    \onother: ${$w("#checkbox1").value}
    \name2: ${$w("#input3").value}
    \relationship: ${$w("#input4").value}
    \birthday2: ${$w("#datePicker2").value}
    \image2: ${whoimageUrl}

    \comment: ${$w("#richTextBox1").value}
        `;

    sendEmail(subject, body)
     .then(response => console.log(response));

}catch(e){
 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
    \name: ${$w("#input1").value}
    \mail: ${$w("#input2").value}
    \birthday: ${$w("#datePicker1").value}
 

    \onother: ${$w("#checkbox1").value}
    \name2: ${$w("#input3").value}
    \relationship: ${$w("#input4").value}
    \birthday2: ${$w("#datePicker2").value}

    \comment: ${$w("#richTextBox1").value}
        `;

 
 

 
  sendEmail(subject, body)
    .then(response => console.log(response)); 
}
}

someone help me

anyone help me

I think the simplest solution here will be to use if/else

try{
 const item = $w("#dataset1").getCurrentItem();
 const wiximageurl = "https://static.wixstatic.com/media/";
 let imagelocal = "";
 if(item.image && item.whoimage) {
            imagelocal = item.image.replace('image://v1/', '');
            imagelocal = imagelocal.substr(imagelocal.lastIndexOf('/')+1);
 const imageUrl = wiximageurl + imagelocal;

 let whoimagelocal = "";
            whoimagelocal = item.whoimage.replace('image://v1/', '');
            whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf('/')+1);
 const whoimageUrl = wiximageurl + whoimagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}
            \image: ${imageUrl}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}
            \image2: ${whoimageUrl}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        } else if(item.image && !item.whoimage) {
            imagelocal = item.image.replace('image://v1/', '');
            imagelocal = imagelocal.substr(imagelocal.lastIndexOf('/')+1);
 const imageUrl = wiximageurl + imagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}
            \image: ${imageUrl}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        } else if(!item.image && item.whoimage) {
 let whoimagelocal = "";
            whoimagelocal = item.whoimage.replace('image://v1/', '');
            whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf('/')+1);
 const whoimageUrl = wiximageurl + whoimagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}
            \image2: ${whoimageUrl}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        }
    }

Hi shan,
Thank you for your replay.
but it’s not work.

This errs

Error parsing web-module 'public/pages/vxzvz.js': Missing catch or finally clause (35:0) while parsing file: public/pages/vxzvz.js

I wrote this code:

try{
 const item = $w("#dataset1").getCurrentItem();
 const wiximageurl = "https://static.wixstatic.com/media/";
 let imagelocal = "";
 if(item.image && item.whoimage) {
            imagelocal = item.image.replace('image://v1/', '');
            imagelocal = imagelocal.substr(imagelocal.lastIndexOf('/')+1);
 const imageUrl = wiximageurl + imagelocal;

 let whoimagelocal = "";
            whoimagelocal = item.whoimage.replace('image://v1/', '');
            whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf('/')+1);
 const whoimageUrl = wiximageurl + whoimagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}
            \image: ${imageUrl}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}
            \image2: ${whoimageUrl}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        } else if(item.image && !item.whoimage) {
            imagelocal = item.image.replace('image://v1/', '');
            imagelocal = imagelocal.substr(imagelocal.lastIndexOf('/')+1);
 const imageUrl = wiximageurl + imagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}
            \image: ${imageUrl}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        } else if(!item.image && item.whoimage) {
 let whoimagelocal = "";
            whoimagelocal = item.whoimage.replace('image://v1/', '');
            whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf('/')+1);
 const whoimageUrl = wiximageurl + whoimagelocal;

 const subject = `From website: ${$w("#input1").value}`;
 const body = `plan: ${$w("#dropdown1").value}
            \name: ${$w("#input1").value}
            \mail: ${$w("#input2").value}
            \birthday: ${$w("#datePicker1").value}

            \onother: ${$w("#checkbox1").value}
            \name2: ${$w("#input3").value}
            \relationship: ${$w("#input4").value}
            \birthday2: ${$w("#datePicker2").value}
            \image2: ${whoimageUrl}

            \comment: ${$w("#richTextBox1").value}`;

            sendEmail(subject, body)
            .then(response => console.log(response));
        }
    }

@nanami You should have only replaced the code inside try and left the catch() statement where it was

@shantanukumar847 Amazing!!! it’s work!!! thank you so much.
I wrote all codes for the same case.

import {sendEmail} from ‘backend/email’;

$w.onReady( function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});

// wix:document://v1 → docs.wixstatic.com/ugd

function sendFormData() {
try {

const item = $w(“#dataset1”).getCurrentItem();
const wiximageurl = “https://static.wixstatic.com/media/”;
let imagelocal = “”;

if(item.image && item.whoimage ) {

imagelocal = item.image.replace(‘image://v1/’, ‘’);
imagelocal = imagelocal.substr(imagelocal.lastIndexOf(‘/’)+1);
const imageUrl = wiximageurl + imagelocal;

let whoimagelocal = “”;
whoimagelocal = item.whoimage.replace(‘image://v1/’, ‘’);
whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf(‘/’)+1);
const whoimageUrl = wiximageurl + whoimagelocal;

const subject = From website: ${$w("#input1").value};
const body = `plan: ${$w(“#dropdown1”).value}
\name: ${$w(“#input1”).value}
\mail: ${$w(“#input2”).value}
\birthday: ${$w(“#datePicker1”).value}
\image: ${imageUrl}

\onother: ${$w(“#checkbox1”).value}
\name2: ${$w(“#input3”).value}
\relationship: ${$w(“#input4”).value}
\birthday2: ${$w(“#datePicker2”).value}
\image2: ${whoimageUrl}

\comment: ${$w(“#richTextBox1”).value}
`;

sendEmail(subject, body)
.then(response => console.log(response));

} else if(item.image && !item.whoimage ) {
imagelocal = item.image.replace(‘image://v1/’, ‘’);
imagelocal = imagelocal.substr(imagelocal.lastIndexOf(‘/’)+1);
const imageUrl = wiximageurl + imagelocal;

const subject = From website: ${$w("#input1").value};
const body = `plan: ${$w(“#dropdown1”).value}
\name: ${$w(“#input1”).value}
\mail: ${$w(“#input2”).value}
\birthday: ${$w(“#datePicker1”).value}
\image: ${imageUrl}

\onother: ${$w(“#checkbox1”).value}
\name2: ${$w(“#input3”).value}
\relationship: ${$w(“#input4”).value}
\birthday2: ${$w(“#datePicker2”).value}

\comment: ${$w(“#richTextBox1”).value}`;

sendEmail(subject, body)
.then(response => console.log(response));
} else if(!item.image && item.whoimage ) {
let whoimagelocal = “”;
whoimagelocal = item.whoimage.replace(‘image://v1/’, ‘’);
whoimagelocal = whoimagelocal.substr(whoimagelocal.lastIndexOf(‘/’)+1);
const whoimageUrl = wiximageurl + whoimagelocal;

const subject = From website: ${$w("#input1").value};
const body = `plan: ${$w(“#dropdown1”).value}
\name: ${$w(“#input1”).value}
\mail: ${$w(“#input2”).value}
\birthday: ${$w(“#datePicker1”).value}

\onother: ${$w(“#checkbox1”).value}
\name2: ${$w(“#input3”).value}
\relationship: ${$w(“#input4”).value}
\birthday2: ${$w(“#datePicker2”).value}
\image2: ${whoimageUrl}

\comment: ${$w(“#richTextBox1”).value}`;

sendEmail(subject, body)
.then(response => console.log(response));
}
}

} catch (e){
const subject = From website: ${$w("#input1").value};
const body = `plan: ${$w(“#dropdown1”).value}
\name: ${$w(“#input1”).value}
\mail: ${$w(“#input2”).value}
\birthday: ${$w(“#datePicker1”).value}

\onother: ${$w(“#checkbox1”).value}
\name2: ${$w(“#input3”).value}
\relationship: ${$w(“#input4”).value}
\birthday2: ${$w(“#datePicker2”).value}

\comment: ${$w(“#richTextBox1”).value}
`;

sendEmail(subject, body)
.then(response => console.log(response));
}
}