Sometimes one needs to send an email with several files attached but the names of the files might not be known before execution. That makes it hard to automate such workload.
Here is one one way to achieve this with file-like data sources. All you need is:
- a working SMTP configuration
- and a file-like data source (local file system, S3, FTP, ..)
declare object filesToSendContent = array();
declare object filesToSendNames = array();
declare object filesToSendType = array();
loop on (select a.file, a.filePath from (exec "ds_file.getFiles"("pathAndPattern" => '*.csv'))as a) as cur
filesToSendNames = array_add(filesToSendNames,cur.filePath);
filesToSendContent = array_add(filesToSendContent,cur.file);
filesToSendType = array_add(filesToSendType,'text/csv');
"Recipients" => <MailRecipients>,
"Subject" => 'My test submission',
"Body" => 'Check the attachments.',
"AttachmentNames" => filesToSendNames,
"Attachments" => filesToSendContent,
"AttachmentMimeTypes" => filesToSendType
The procedural block retrieves all files found at the source with the schema pattern corresponding to the one provided (here: "pathAndPattern" => '*.csv'). Whilst iterating through them, 3 arrays are being created. One for
- the file names (which will be seen when the recipients get the emails)
- the file contents (aka the blobs)
- the files types (here hard coded to text/csv for all files)
You just have to provide a real list of recipients instead of <MailRecipients> and replace ds_file with the name of your data source which you want to crawl for files.
Please sign in to leave a comment.