Condition Email with or without attachment

Hi I am trying to send emails using sendGrid regardless whether they have an attachment or not. Unfortunately the below method does not work and am currently perplexed. If someone could point me in the right direction I would be greatly appreciative!


function sendFormData() {
const convertRegex = new RegExp(/wix:document:\/\/v1\/([^\/]+)\/(.*)$/);
const item = $w('#dataset').getCurrentItem();
//Instead of "fieldName", add here the relevant field name to which
//the upload button is connected to. 
let documentUrl;
if ($w("#Attachment").value.length > 0) {
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 recipient = $w("#CustomerEmail").value;

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

Edit: Just a side note I have also tried the below without any luck
if ($w(“#Attachment”).value !== 0)

1 Like

Hi, your question in quite general.
What’s the problem exactly? Is it with getting the attachment or with sending an email?
Did you try sending a simple email and it worked?

Hi Ohad,

Sorry I forgot that part. Using the .length > 0 method it sends with the /rFile: feild as “undefined” if there is an attachment and sends through without attachment successfully. Although if !== 0 method is used it sends successfully with the attachment URL if a file is uploaded but it fails with a match error and doesnt send submissions without an attachment.

I hope this clears it up. Thank you for your time!

Please note that the File array returned from the uploadButton has a name attribute that you should use: https://www.wix.com/code/reference/$w.UploadButton.html#File

Mohamed:

Just to repeat Ohad’s question.

What are you actually trying to do?

So for example what is the expected outcome of this code:

if ($w("#Attachment").value.length > 0) {
  const matches = item.fileAttachment.match(convertRegex);
  documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`; }

There doesn’t appear to be any relationship between $w(‘#Attachment’) and documentURL. An explanation along with the page link might be helpful :-).

Thanks

Hi stcroppe,

The expected outcome of the statement mentioned is if there is a file uploaded " if ($w(" #Attachment ").value.length > 0) " then “matches” finds the string required for the document attached in the dataset then applies it to " [docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]};](http://docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;) " resulting in a document URL to be attached to the email.

This was working but now returns “File Undefined” where the URL is meant to be.

Edit: Sorry page link is scsengineering.com.au

So Mohamed:

First of all the upload button you have is called $w(‘#FileAttachment’) - which you are using in your code.

Now is your assertion that because you have connected your dataset to the $w(‘#FileAttachment’) upload button somehow then the current item from your data set will be dynamically updated whenever you make a file selection using the upload button?

If so I think that is probably a bad assumption. If you want to guarantee that the current item has been updated with an element value you need to save the item first which will have the effect of wix-dataset reading the element values and updating the data collection and the dataset data copy.

What you should be doing is using the value of the element directly then you will get the effect you are looking for.

So NOT this

if ($w("#FileAttachment").value !== 0) {
    const matches = item.fileAttachment.match(convertRegex);
    documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
}

You should be doing this:

if ($w("#FileAttachment").value.length > 0) {
    // The upload button returns an array and so could return 
    // multiple files selections but we will use the first one
    // Note: you might want to add a lightbox dialogue to alert 
    // the user if they select more than one file which one they 
    // are going to be sending
    const fileName = $w("#FileAttachment").value[0];
    const matches = fileName.match(convertRegex);
    documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
}

Think of the dataset as read only, only containing information that is in the data collection. So if you haven’t put something in the data collection via a dataset save() or new() then if the data in any bound element changes these changes probably won’t be in your copy of the current item.

Make sense?
Steve

1 Like

Hi Steve,

Thank you for your time and the break down and yes you are definitely correct I should have been referencing the array, although the change you recommended still returned " File: undefined " when a pdf was attached. Debugging shows that the file is being saved. Please see the attached screenshot of the debug in verbose from after the file upload.

PS: I really appreciate the help!

OK I have a couple more suggestions.

  1. Sprinkle some console.log statements around your code to check on the value of key variables etc. Just to make sure the values you are expecting are actually being set correctly.
    So add a console.log($w(“#FileAttachment”)); just before you test for the value (this will show you the whole element object and its contents - you could also use this console.log($w(“#FileAttachment”).value);).
    Add console.log(matches); immediately before you assign to documentUrl and add console.log(documentUrl); after you assign to documentUrl.
    This will show you what you are generating as the code executes. Of course you can add other log statements but this will help you narrow down the problem.
  2. Change the code you have where you are attaching the file. Basically if there is no file to attach then just don’t attach one. So instead of this
const body = `Name: ${$w("#CustomerName").value}
  \rEmail: ${$w("#CustomerEmail").value}
  \rPhone Number: ${$w("#CustomerPhoneNumber").value}
  \rEnquiry: ${$w("#CustomerEnquiry").value}
  \rFile: ${documentUrl}`;

do this

let body = `Name: ${$w("#CustomerName").value}
  \rEmail: ${$w("#CustomerEmail").value}
  \rPhone Number: ${$w("#CustomerPhoneNumber").value}
  \rEnquiry: ${$w("#CustomerEnquiry").value}`;
  if (documentUrl) {
    body += `\rFile: ${documentUrl}`;
  }

This will prevent null being used in the body string which is likely where one of your problem is coming from.

Share the output if you are still having problems

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

Hi Mohamed:

Good to know.
Can you mark this as Solved and highlight the best answer for others to find?
Cheers
Steve

Hi, buddys!

I’m going through the same problem … I can not send the url of the attachment to the email.

It always shows that the file was not attached.

I used the idea of ​​Mohamed, but it did not work.

I noticed that it calls two types of files for the sendEmail and sendEmailWithSender pages.

Follow the print of my code so that you can help me in some way if possible.