1

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]})
}
  • Please add an [mcve] (add the code of a complete but minimal function that reproduces the problem) – Rubén Jun 20 '20 at 17:59
  • 1
    @Rubén, I have added code snippets now. Hope you find it useful. – Narendra Rana Jun 20 '20 at 19:29
  • How are you calling the function (from the script editor, a custom menu, ...)? Are you signed-in into multiple accounts? Have you already tried using Chrome in incognito mode with all the extensions disabled? – Rubén Jun 21 '20 at 16:51

2 Answers2

0

This doesn't return the file URL

DriveApp.getFileById('<Google File ID>')

that explains why the third code example fails

Try this

function sendAttachment(){
   var file = DriveApp.getFileById('<Google File ID>');
   var fileBlob = file.getBlob();
   GmailApp.sendEmail(
      <recipient email>, 
      'MAIL with ATTACHMENT', 
      'Hi, \nPlease see mail with file from Drive as attachment', 
      {attachments:[fileBlob]}
   )
}

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • The code is fine. Even with the revised code given by you, I am getting the same error "The feature you are attempting to use has been disabled by your domain administrator." . Need to know where I can enable this permission. – Narendra Rana Jun 21 '20 at 11:18
0

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.