I am trying to add all contents of 2 different folders in google drive to my GmailApp so that email contains all the files from both folders.
Sample Code: it throws an error Exception:
Invalid argument: attachment at sendEmail(Code:180:20)
var contents = DriveApp.getFolderById('***').getFiles();
var contentspdf = DriveApp.getFolderById('***').getFiles();
var attach = [];
while(contents.hasNext()){
var file = contents.next();
if (file.getMimeType() == "application/pdf"){
attach.push(file);
}
}
var attachtwo = [];
while(contentspdf.hasNext()){
var files = contentspdf.next();
if (files.getMimeType() == "application/pdf"){
attachtwo.push(files);
}
}
GmailApp.sendEmail(currentEmail, Subject, messageBody,{
attachments: [attach, attachtwo,],
name: 'Test'
});
You can use Spread syntax (...) to put all the attachments into the attachments
argument:
attachments: [...attach, ...attachtwo]
Solution:
var contents = DriveApp.getFolderById('***').getFiles();
var contentspdf = DriveApp.getFolderById('***').getFiles();
var attach = [];
while(contents.hasNext()){
var file = contents.next();
if (file.getMimeType() == "application/pdf"){
attach.push(file);
}
}
var attachtwo = [];
while(contentspdf.hasNext()){
var files = contentspdf.next();
if (files.getMimeType() == "application/pdf"){
attachtwo.push(files);
}
}
GmailApp.sendEmail(currentEmail, Subject, messageBody,{
attachments: [...attach, ...attachtwo],
name: 'Test'
});
Please make sure the V8 Runtime environment is enabled.