GAS Help: Copying attachment from Gmail into Gdrive - doesn't copy attachments

Hey everyone.

Iโ€™m working on a script that will intercept emails, copy some details into a Gsheet, and copy the attachment into the Gdrive (while also putting a filepath into a cell for the row created per email).

I have two problems that Iโ€™m hoping someone can shed some light on


Problem 1: The copied attachment into the Gdrive isnโ€™t the actual attachment

The problem Iโ€™m having is that the actual file created isnโ€™t a copy of the attachment file, itโ€™s a 15kb PDF with the following contents:


Hereโ€™s a closer look: 3X_a_9_a94c9d40324171de8ff4efeec5808e2676e48968.png

Hereโ€™s the script accomplishing the file copy

3X_2_a_2ac27bf5482ad5543c3bbf5e409af185b987a520.png

Everything in here works, except for attachment.copyBlob() - thatโ€™s creating the small bit of text you see in the images above.

How can I make the copy an actual copy of the attachment?


Problem 2: I canโ€™t get the attachment name to return out of the attachments loop

You can see from the script fragment above that the attachment stuff is inside a loop;

  • I need to get the name of the attachment (which is the name of the file created in the Gdrive) addended to the newFilePath.

But when I tried return newFilePath, it validated but itโ€™s not actually returning a value; the filepath thatโ€™s reported is missing the file name.

The code is built for multiple attachments, but I really only need it for one. In this scenario, there will only always be one attachment - but I canโ€™t find anything that shows how to code for a single thing, itโ€™s all for multiple (which makes sense).

So how can I get the filename out of the attachment loop?


Thanks to you all! 3X_d_5_d51363a862e7ab883241c312ac5d7f271579cdd3.gif

2 7 438
7 REPLIES 7

I think you need to getMimeType() in your ForEach() loop and use that type for the new blob().

@GreenFlux I originally had that in there, pulling the PDF type from the attachment and setting that to the mime type - but it produced the same result. It was my thought that if I didnโ€™t specify the mimeType it would figure it out itself.

Maybe this for Problem #2?

3X_2_6_2683505a8c8924d8f9b7dea5cc3a90ea1a93595a.png

Indeed you were correct @Marc_Dillon thanks!

Still having a problem with actually copying the content of the pdf.

When I investigate whatโ€™s inside the attachmentโ€ฆ Iโ€™m finding that itโ€™s actually empty.

3X_4_b_4b47e3ee74272018f80c0354bea14f894b72cb28.png

I have no idea what Iโ€™m doing wrong. attachment.copyBlob() is the only thing that people are putting out there in the internet, thereโ€™s no other methods or anything that I can find.

Yet in my case, it doesnโ€™t actually pull the attachment.

I must be missing something.

Thanks for the info, the problem Iโ€™m facing wonโ€™t be solved by the link youโ€™ve suggested.

  • Everything I find online gives the same exact solution;
    • Eventually you get down to a loop through the attachments, and you then attachment.copyBlob()

My problem is that itโ€™s not copying the blob - itโ€™s actually causing some sort of error within the PDF file itself, some kind of error or something.

Iโ€™m pretty much abandoning this for now, as Iโ€™ve managed to extract the data from the attachment using attachment.getPlainBody() - and thatโ€™s good enough for now.

But sure would be nice, and my client is expecting, the file from the gmails to be entered into the Gdrive.

Top Labels in this Space