Zoho Creator: Sendmail with Many Attachments
In my Zoho Creator app I’ve created a form that includes a subform for users to upload attachments. This subform allows users to upload zero, one or more attachments without limitation. This is all great until you need to generate an email using the Zoho Creator sendmail task and attach all of these uploads to the email. Unfortunately, the sendmail task does not support the expressions that would be needed to attach an unknown number of files within an email. I had to get creative.
I discovered that the sendmail task will accept a list of file variables even if some of the variables are empty. This means I could determine a maximum number of attachments to include in an email, create variables for that many attachments, and write my sendmail task to include those variables as attachments. I determined that 10 attachments would be plenty for my users, but you could create as many variables to hold your attachments as needed for your application.
It’s not pretty, but it works:
//Create a collection of all attachments
all_attachments = input.Attachment_Subform.Attachment_Field.getall();
//Assign the first 10 attachments to a variable
attachment1 = all_attachments.get(0);
attachment2 = all_attachments.get(1);
attachment3 = all_attachments.get(2);
attachment4 = all_attachments.get(3);
attachment5 = all_attachments.get(4);
attachment6 = all_attachments.get(5);
attachment7 = all_attachments.get(6);
attachment8 = all_attachments.get(7);
attachment9 = all_attachments.get(8);
attachment10 = all_attachments.get(9);
//Include each variable in the attachments section of the sendmail task
sendmail
[
from : "me@email.com"
to : "you@email.com"
subject : "Email Subject"
message : "Email Body"
attachments : file: attachment1, file: attachment2, file: attachment3, file: attachment4, file: attachment5, file: attachment6, file: attachment7, file: attachment8, file: attachment9, file: attachment10
]
Do you have a better work or have any improvements on the method described? Let me know!