Sending an email notification without attachments

Now, before I start, I have looked on the Forums at other threads and not being fluent in Java I am struggling a bit. I’m looking for some advice to send an email when the user doesn’t upload an attachment. I have got it working, so that when the user does upload an attachment I get a document link through, however when an attachment isn’t present in the form (as it isn’t required) no email comes through.

The front end works fine. A user is able to upload all appropriate details and it is successfully saved to the database.

The details are as follows:

Dataset ID: #dataset1

Datasbase name: “Enquiries”

Database columns, left to right: Title - Name - Email Address - Telephone Number - Your message to us - Company - Attachment

I will include an image of the code below, due to company privacy I’ve had to blank out the const recipient email address, but let’s call it joebloggs@email.com if it makes a difference.


Like I have previously stated, I have looked at the other threads on the forum and been through the wix code resources to try find a solution, but unless I’m missing it (not being amazingly code savy) I cannot find a solution.

Many thanks,

Hi Nathan,

The email is not sent because documentUrl is probably undefined.

Change the following line to solve the issue:

const matches = item.attachment.match(convertRegex) || "NOLINK";

Hi, Ido.

I’ve just added the || “NOLINK”; and it hasn’t solved the problem.

The email is still received if an attachment is uploaded, but if one isn’t uploaded we still do not get a notification.

Many thanks,

Hi Nathan,

Try this:

function sendFormData() {
 const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
 const item = $w("#dataset1").getCurrentItem();
 //Instead of "fieldName", add here the relevant field name to which
 //the upload button is connected to.   
 let documentUrl;
 if ($w("#uploadButton5").value.length > 0) {
 const matches = item.attachment.match(convertRegex);
        documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
    }
 const subject = `New Submission by ${$w("#input6").value}`;
 const body = `Comments: ${$w("#textBox2").value}
    \rEmail: ${$w("#input7").value}
    \rCompany: ${$w("#input9").value}
    \rPhonenumber: ${$w("#input8").value}
    \rFile: ${documentUrl}`;

 const recipient = "info@jupiterunderfloorheating.com";

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

Ido,

This has worked perfectly. Thank you for your support on this matter.

All the best,

Nathan

Hello Ido,

I do beg your pardon in reviving an old thread, however this code that was working now sends through an email stating the file is undefined when one is very clearly uploaded. The file can be access in the database on Wix.com

I find that when I change this

if ($w(“#uploadButton5”).value.length > 0)

to

if ($w(“#uploadButton5”).value !== 0)

I can get an email with the document Url through, however it blocks email notifications coming through if there is no file attached. Is it possible for you to advise on the matter?

Sorry for the thread revive once again, and many thanks in advance as always.

Nathan

Hi, I am having the same issue as nathan. I thought I managed to get around it with an IF Else statement but it reversed the issue. Emails without attachments went through and attachments failed. Any update on this would be greatly appreciated as its currently crippling for a client.

Hi Nathan,

I’m not sure if you’re still having this issue, although I managed to resolve it with the below code. Let me know if this works for you.

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);
}