Search code examples
google-apps-scriptgoogle-drive-apigmailgoogle-oauthgoogle-workspace

Unable to send excel attachment by Gmail using Google Script


I am using Google Apps Script (*.gs) to send mail via Gmail. Mails are working file and even attachments using images available in public URL are working fine. I am using

var file = DriveApp.getFileById("Google file ID") 

to get the file and attach. I am getting the error

The feature you are attempting to use has been disabled by your domain administrator.

I am the admin and want to understand exactly where do I enable this feature. Can someone guide me please.

Addtionally, I checked my Google script Code's project properties and found that the following 4 OAuth Scopes are required by the script: Here are the scopes and next to them the response when I try accessing these in my browser.

  1. https://mail.google.com/
    -- Able to access mails.
  2. https://www.googleapis.com/auth/drive
    -- The text "drive" appears in the top of the browser
  3. https://www.googleapis.com/auth/script.external_request
    -- The page gives error "Not Found. Error 404"
  4. https://www.googleapis.com/auth/spreadsheets.currentonly
    -- The page gives error "Not Found. Error 404"

Here are 3 scenarios I have tried. the 3rd one fails.

  1. Send mail without any attachment: WORKS PROPERLY.
function SimpleMail(){
  GmailApp.sendEmail(<recipient email>, 'MAIL without any ATTACHMENT', 
 'Hi, \nPlease see mail without any attachment' )  
}
  1. Send mail with attachment from a public URL: WORKS FINE with Attached image.
function sendAttachment(){
        var attachmentUlr = "<URL of an Image on a public facing website>" ;
        var fileBlob = UrlFetchApp
          .fetch(attachmentUlr)
          .getBlob()
          .setName("fileBlob"+'.jpg');  
        GmailApp.sendEmail(<recipient email>, 'MAIL with ATTACHMENT', 
 'Hi, \nPlease see mail with image from a website as attachment', {attachments:[fileBlob]})
}
  1. With attachment from my Google Drive URL: FAILS,
function sendAttachment(){
        var attachmentUlr = DriveApp.getFileById('<Google File ID>');
        var fileBlob = UrlFetchApp
            .fetch(attachmentUlr)
            .getBlob()
            .setName("fileBlob"+'.xlsx');  
        GmailApp.sendEmail(<recipient email>, 'MAIL with ATTACHMENT', 
 'Hi, \nPlease see mail with file from Drive as attachment', {attachments:[fileBlob]})
}

Solution

  • The issue got resolved once I enabled Drive SDK API as an admin. Got to admin panel > Apps > G Suite > Settings for Drive and Docs > Features And Applications. Enable / Check on the box - "Allow users to access Google Drive with the Drive SDK API".

    Once this setup was enabled, my code was able to pick the file from google drive for attachment.