Search code examples
google-apps-scriptgoogle-apps

Send Google file to email


I have built a sidebar in Google App Scripts and on some action I would like to send a document to email and would like it to appear in email like when you share a document you get a kind of widget of the document with the file title some text a button etc.

Is it possible to attach a file like this and not as a PDF?

something like this


Solution

  • You can easily attach a Google Document or any Blob to an email as well as construct an email with information about the document including a link, using the following code:

    var myDocument = DocumentApp.openById(<DOC_ID>);
    var mySpreadsheet = SpreadsheetApp.openById(<SPREADSHEET_ID>);
    MailApp.sendEmail({
        to: "recipient@example.com",
        subject: "My Email",
        htmlBody: "Document Name: " + myDocument.getName() + ". " +
                  "Document Link: " + myDocument.getURL(),
        attachments: //Or just attach the Docs... If you don't want to, remove this option
          [
            myDocument,
            mySpreadsheet 
          ]
      });
    

    Edit: The htmlBody does, obviously, use HTML can be styled as you would a webpage. My example is very basic. There is no standard card or widget offered by Google. They offer the HTML implementation is to be extremely flexible so that we can create our own! There are many free email templates out there, I have used Litmus's free templates in the past.