0
votes

So basically, My father asked me to create a script to find emails with a "Drive" label and save their contents to his drive. (If your asking why, its so he can email himself a couple of PDF's and have the script automatically put them together in his drive)

And I've gotten the whole thing done except for one thing: Can't figure out how to save the attachments. In the processDocument method I loop through the attachments and try to do:

doc.appendParagraph(the attachment)

Is there any proper way of adding the attachments using the processDocument() method?

Link to Code: http://pastebin.com/RwFNs232

Thanks.

2
It's a good idea to post the code you are using in order to get better answers! Are you using Gmail service to read messages? - nanndoj
nanndoj, Yes i am using gmail service. Im doing it by searching all threads having attachments and the label drive. - 086

2 Answers

1
votes

You can use DriveApp to save the attachment blob in a separated file:

var fileBlob = "your blob" // GmailAttachment.copyBlob() method;
var file = DriveApp.createFile(blob);

And then write in your doc the link to the attachment

doc.appendParagraph(file.getUrl());

Note:

You may want to write the attachment content in your document. In this case you can write directly to the doc without create a file in your Drive. But it is dangerous because you can't get some blob's content as a well formated string.

doc.appendParagraph("attachment content"); //GmailAttachment.getDataAsString() 

I think it will solve your issue.

0
votes

As @nanndoj said, you should use the blob file structure to input the files. Additional information about blobs should be asked as a separate question.

However, the attachment should not just be appended to the doc of the email. Instead, you should include the blob reference in the call of the sendEmail function, as seen in Google's documentation here.

Example:

sendEmail(recipient, subject, body, options)

MailApp.sendEmail('[email protected]', 'Attachment example', 'Two files are attached.', {
     name: 'Automatic Emailer Script',
     attachments: [file.getAs(MimeType.PDF), blob]
 });