1

I'm trying to attach a Google Docs file to an email using Google App Scripts. I have a folder with the file inside and the idea is to get the file from there and send it by email using also an email address that is saved in a google sheet.

I founded and used this:

function emailDocTestasDocx() {
  var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
  var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
  var doc = UrlFetchApp.fetch(url);
  var me = Session.getEffectiveUser().getEmail();
  MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]});
}

It works but the final attachment is an html that redirects me to the doc inside the folder. I don't want that. I want to send the attachment but I didn't found a way to to this yet.

Any ideas? Thanks!

Pipa
  • 11
  • 3
  • Google Docs are not something you can open outside of cloud so you cannot attach them with the mail. You can either attach pdf or the docx using export. – Karan Oct 28 '21 at 17:35
  • Does this answer your question? [How to Attach Google Drive File with sendMail Function in Apps Script?](https://stackoverflow.com/questions/48831263/how-to-attach-google-drive-file-with-sendmail-function-in-apps-script) – Kos Oct 28 '21 at 19:24

1 Answers1

0

As what @Karan have mentioned on the comments, Google Docs are not something you can open outside of the cloud (it is always being opened via docs.google.com website via a link) so you cannot attach them as a file on the mail. You can either attach the document as PDF or export the Docs file first as a document with .docx extension & then save the file on your Google Drive:

Sample tweaked script that will send attach the Google doc file as PDF:

function emailDocTestasDocx() {
  var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
  var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
  var doc = DriveApp.getFileById(id); //used ID to get the file saved in your Drive using DriveApp.getFileById() method
  var me = Session.getEffectiveUser().getEmail();
  MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]}); //attach the file via email
}

Sample Result

enter image description here

You may also check the answers from these similar posts:

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
SputnikDrunk2
  • 3,398
  • 1
  • 5
  • 17