1
votes

First time posting, so please be gentle. I'm using bits of code from: https://www.splitbrain.org/blog/2017-01/30-save_gmail_attachments_to_google_drive https://ctrlq.org/code/19053-send-to-google-drive

I'm trying to save PDF attachments from Gmail directly to Google drive. I'm getting an Invalid Argument error when trying to save the file in the last line of code below. I have a loop to go through message threads and then within that loop, this is the code that is saving the attachments:

var message = threads[x].getMessages()[0];
var saveFolder = getFolder(driveFolderID,message);//this is valid folder
var att = message.getAttachments();
Logger.log(att[0].getContentType());  //this returns 'pdf'
file = saveFolder.createFile(att[0]);

Anyone know what I'm doing wrong, or where to look next?

Thanks

1
"some string data" most certainly is not the appropriate content for a PDF - just naming the file with an extension does not make it that kind of file. You might consider casting explicitly from the class GmailAttachment to the actual bytes of the attachment (getBytes) or to the Blob type via getBlob. Or if you've tried that already, make sure you're asking a good question. - tehhowch
Thanks I've updated the post. The line of code I had in there was to just test the folder was correct but that wasn't obvious. - Joe D
Try following this related SO post and tutorial about attachments in Gmail Message and Extract it to a Google Drive folder. This will help you save attachments to Google Drive. - Mr.Rebot
Thanks. Yes, I've seen those and my code is based on your first link. He has the same line of code: file = folder.createFile(att[z]). I've no idea why it doesn't work for me. - Joe D
You still haven't given an idea of what the actual attachments you are trying to save to drive are. Does this fail with every attachment? Specific types? Specific ones from a specific person? What have you tried to resolve this already? - tehhowch

1 Answers

1
votes

Ok. So I managed to figure this out so just updating. Apparently the contentType 'pdf' was causing the problem. If I do a att[0].setContentType('application/pdf') before trying to save the file, then the code works as expected.