Condition Email with or without attachment

Hi Scott ,

Thank you for your valuable input. I managed to remedy the issue by doing as you suggested and logging all variable values. This gave me insight into what was failing and below is the working solution. Thank you greatly for your assistance in this! I’ll leave this here in case someone has a similar issue :slight_smile:

import {sendEmail, sendEmailWithSender} from 'backend/email';

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

function sendFormData() {
const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
const item = $w('#QuoteSubmissiondataset').getCurrentItem();
let documentUrl = "";
if (item.fileAttachment === undefined) {
  documentUrl = "No Attachment";
} else {
const matches = item.fileAttachment.match(convertRegex);
documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
}
const subject = `Online Enquiry - ${$w("#CustomerName").value}`;
const body = `Name: ${$w("#CustomerName").value}
\rEmail: ${$w("#CustomerEmail").value}
\rPhone Number: ${$w("#CustomerPhoneNumber").value}
\rEnquiry: ${$w("#CustomerEnquiry").value}
\rFile: ${documentUrl}`;
const sender = $w("#CustomerEmail").value;

sendEmailWithSender(subject, body, sender);
}