Attaching optional files to the email Module

I currently have the following setup:

When a Form is submitted on my CMS, the input is sent to make via a webhook. Then I filter through all the fields from the webhook data and ensure that the file is not empty (as it is optional and will be empty when no file is uploaded) When it is not empty I save the data and file name in a var. I repeat this for every file (3). After that I fetch all created variables and would now like to attach every file to an email. The problem is that I do not know which of these vars exist and the email module throws an error when an empty value is added.

So my question:
How can I only attach files to the email that are NOT null or empty. I would need to have conditional attachments for emails and I just cant seem to wrap my head around that :slight_smile:

Any help is greatly appreciated!

Welcome to the Make community!

Please provide the example output bundle of the Get Multiple Variable module by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A. Upload as Text File

Save each bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

B. Insert as Formatted Code Block

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles.
These are the two ways to format text so that it won’t be modified by the forum:

  • Method 1: Type code block manually

    Add three backticks ``` before and after the content/bundle, like this:

    ```
    content goes here
    ```

  • Method 2. Highlight and click the format button in the editor

Providing the input/output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

This will allow others to better assist you. Thanks!

Thanks for your reply!
Sure, here is the output bundle:

[
    {
        "file1": "IMTBuffer(3855359, binary, b48a8ee89615415674006729e95b8c7d5dbc8dfa): 255044462d312e360d25e2e3cfd30d0a323738352030206f626a0d3c3c2f46696c7465722f466c6174654465636f64652f4669727374203134312f4c656e67746820323735372f4e2031382f547970652f4f626a53746d3e3e73747265616d0d0a68deec",
        "file2": "IMTBuffer(3855359, binary, b48a8ee89615415674006729e95b8c7d5dbc8dfa): 255044462d312e360d25e2e3cfd30d0a323738352030206f626a0d3c3c2f46696c7465722f466c6174654465636f64652f4669727374203134312f4c656e67746820323735372f4e2031382f547970652f4f626a53746d3e3e73747265616d0d0a68deec",
        "file3": "IMTBuffer(3855359, binary, b48a8ee89615415674006729e95b8c7d5dbc8dfa): 255044462d312e360d25e2e3cfd30d0a323738352030206f626a0d3c3c2f46696c7465722f466c6174654465636f64652f4669727374203134312f4c656e67746820323735372f4e2031382f547970652f4f626a53746d3e3e73747265616d0d0a68deec"
    }
]

This would be an example of all documents being present.

But a more realistic case would be that only two out of three files are present. In my current setup that would mean that the variable for the file would not be created and can therefore not be fetched later on in the flow, right?

Thank you!

You can add the files variable to an empty array, then remove null values.

e.g.:

{{ remove(add(emptyarray; 214.file1; 214.file2; 214.file3); null) }}

Screenshot_2024-08-21_150827

Output

(if there are only two)

Screenshot_2024-08-21_150824

Hope this helps! Let me know if there are any further questions or issues.

— @samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

That already works like a charm!
However one thing still confuses me:

The E-Mail module expects file name and data for every attachment. So now i tried the following:

Create a file_name var for each file and fetch it in the same module as the data vars. Now i have to combine the name and data into an entry, so the email module can map that. So i thought of using a two dimensional array, so each entry has name and data. But then I found the Array aggregators and this seemed like the way to do it. But at the same time not because it does not know which name goes to what data, and it does not create an array but returns only one entry that can consist of multiple values.

Now I am just scratching my head on how to do that, Maybe creating an array using the json module? but how would I append the items to this?

This is the new setup that obviously does not work :wink:

Here is the output bundle of the get multiple vars, now with the name vars added:

[
    {
        "file1": "IMTBuffer(3855359, binary, b48a8ee89615415674006729e95b8c7d5dbc8dfa): 255044462d312e360d25e2e3cfd30d0a323738352030206f626a0d3c3c2f46696c7465722f466c6174654465636f64652f4669727374203134312f4c656e67746820323735372f4e2031382f547970652f4f626a53746d3e3e73747265616d0d0a68deec",
        "file2": "IMTBuffer(3855359, binary, b48a8ee89615415674006729e95b8c7d5dbc8dfa): 255044462d312e360d25e2e3cfd30d0a323738352030206f626a0d3c3c2f46696c7465722f466c6174654465636f64652f4669727374203134312f4c656e67746820323735372f4e2031382f547970652f4f626a53746d3e3e73747265616d0d0a68deec",
        "file3": "null",
        "file1_name": "file.pdf",
        "file2_name": "file.pdf"
    }
]

Maybe you could give me a hint on how to solve this

David

I have a quick update on how far I got with some playing around:

I have now created a JSON Structure that reflects the data in a way I can work with.

So now each file name and file data are combined into a single collection like this:

[
    {
        "File1": {
            "Data": "/9j/4QAYRXASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QMdaHR0cDov3QxB5Y8HA9RDg5PovoY8EGKKKD6KiRIkqV6Fx5SVweKhDlEeLly/QQ4WC4SzXGaOooMGVKhBgwY8MXg4HD6CHB9C5cuX6UjGEGLiXA9CRhioxjHmuH0SB",
            "Name": "file.jpeg"
        },
        "File2": {
            "Data": null,
            "Name": null
        },
        "File3": {
            "Data": null,
            "Name": null
        }
    }
]

This JSON is then parsed and fed into an interator. At the moment i just manually create items in the iterator and add the collection from the parsed JSON as the value.

Next I feed this value to the aggregator. This is where the next issue is. I feed a collection into the iterator, so it should iterate over every item and each iteam consist of a collection with Name and Data fields. However, the aggregator only receives a “value” field from the iterator.

I thought that the “value” field is just a collection where I can select specific fields. I tried about every combination to get the specific fields of the collection, “get”, “map”, and even the dot notation way. But I just can’t seem to get the Name and Data separated.

This is the output bundle of the iterator (I currently only iterate over one item)

[
    {
        "Data": "/9j/4QAYRXhpZgAASUkqPVfAkg9FUCC4K9Lw+u5fF+lJXpfoXDhlfqHh9deiuB+jfqAv6M+l4uXCX6k9Ty8nofpH1TlfoPJxXB9OpUD6u+ivRcGXL4qJEjxcSPpP1l8P0qlwYx+oOQ9L6XliyuK9IwfSkYS4yvQP65+ocPB9Ih+gMIR4fQep9b639Gel+ocPB9L//Z",
        "Name": "file.jpeg",
        "__IMTINDEX__": 1,
        "__IMTLENGTH__": 1
    }
]

So i see an array containing all the iteration items and each item contains multiple fields.

This is what everything looks like now:

Is that a viable option or am I completely missing the target?

All the best,
David

@samliew Maybe I should have mentioned you in the answer :wink:

1 Like