How to send email with multiple attachments from Zoho Creator application

Thought LogikBlogs, Deluge, Zoho CreatorLeave a Comment

We have come across different scenarios where we have deal with more than one attachments to be included as part of sendmail task in Zoho creator application. One of the example is, A bespoke enquiry management application which has capability to send out emails to the enquiries from the app as part that process the person dealing with enquire will attach multiple documents, forms to fill to the email's attachment file upload subform. Unfortunately Zoho creator doesn't support dynamic file names in the "Attachments" parameter in send email task. So we had to get creative and play with limitations.

In the below example we have function to send email, which will be called upon submission of email form, we send the record ID as an input to the function call. In the function we fetch the details of the email using the record id, then get the file names of the attachment in the email and the attachment count.

Attachment count is then used to create the variables to hold the attachment file name. Please note Zoho creator doesn't support yet passing a list directly to "Attachments" parameter in "sendmail" task, otherwise we can avoid those if else statements and support unlimited attachments. Because of this limitation below script we are adding first 7 files to the attachment of the send email. Considering the fact that 98% of the emails will not have more than 5 attachments per email. Of-course you can add more variables and get the addition file names if there is a requirement.

void Email(int emailID)
{
	fet = Email_Form[ID == emailID];
	attachmentCount = 0;
	attachement =[];
	if (fet !=null) {
		filenames = fet.Attachments.File_upload;
	}
	
	if (filenames != null) {
		for each  attachment in fet.Attachments
		{
			//attachement.add("file:" + filenames.get(recCount));
			attachmentCount = attachmentCount + 1;
		}
	}
	
	if (attachmentCount = 1){
		file1 = filenames.get(attachmentCount-1);
	}
	else if ( attachmentCount = 2) 
    {
		file1 = filenames.get(attachmentCount-2);
		file2 = filenames.get(attachmentCount-1);
    }
	else if ( attachmentCount = 3) 
    {
		file1 = filenames.get(attachmentCount-3);
		file2 = filenames.get(attachmentCount-2);
		file3 = filenames.get(attachmentCount-1);
    }
	else if ( attachmentCount = 4) 
    {
		file1 = filenames.get(attachmentCount-4);
		file2 = filenames.get(attachmentCount-3);
		file3 = filenames.get(attachmentCount-2);
		file4 = filenames.get(attachmentCount-1);
    }
	else if ( attachmentCount = 5) 
    {
		file1 = filenames.get(attachmentCount-5);
		file2 = filenames.get(attachmentCount-4);
		file3 = filenames.get(attachmentCount-3);
		file4 = filenames.get(attachmentCount-2);
		file5 = filenames.get(attachmentCount-1);
    }
	else if ( attachmentCount = 6) 
    {
		file1 = filenames.get(attachmentCount-6);
		file2 = filenames.get(attachmentCount-5);
		file3 = filenames.get(attachmentCount-4);
		file4 = filenames.get(attachmentCount-3);
		file5 = filenames.get(attachmentCount-2);
		file6 = filenames.get(attachmentCount-1);
    }
	else if ( attachmentCount = 7) 
    {
		file1 = filenames.get(attachmentCount-7);
		file2 = filenames.get(attachmentCount-6);
		file3 = filenames.get(attachmentCount-5);
		file4 = filenames.get(attachmentCount-4);
		file5 = filenames.get(attachmentCount-3);
		file6 = filenames.get(attachmentCount-2);
		file7 = filenames.get(attachmentCount-1);
    }
	
    sendmail
    [ 
	    from : zoho.loginuserid
	    to : fet.To
	    subject: fet.Subject_field 
	    message : "<div>" + fet.Message + "<br></div>" 
		Attachments: file:file1, file:file2, file:file3, file:file4, file:file5, file:file6, file:file7
    ]
}

If you have better idea or have any improvements on the method described? Let me know in the comments!

Pro Tip:

Null Checks!!!

Every programmer must include sufficient null checks to avoid errors caused by null values during real world runtime. Why go through the headache of fixing it when you can avoid it? When scripting, make it a habit to ask yourself this question – will this value ever be null? If it’s a yes, that’s a place for a null check!

Leave a Reply

Your email address will not be published. Required fields are marked *