Jordzzz
Jordzzz

Reputation: 11

Embed images from business Google Drive to Gmail

I've tried searching for ways to do this, but I don't think I know what I'm searching for. I have a script that should get the ID of an image in a Google Drive of a business account, and embed this in an html that will be sent via email. The script looks like this (I just simplified it):

function sendEmail() {

    var imageID = '1pjb-Ap3XoI7fETZOPQaCNb3t8VzANR2C'
    var body ='<div><img src="https://drive.google.com/uc?export=view&id=' + imageID + '" width="900"/></div>'
    
    
    GmailApp.sendEmail(
      '[email protected]',
      'Test',
      body,
      {
      htmlBody: body,
      }
    );
    }

However, the output doesn't display the image, which looks like this: enter image description here

If I put the image in a public google drive folder, and use its ID for the img src, the email properly shows the embedded image.

We can't however have the images residing in a public/personal Google Drive due to security reasons. Are there ways in which we can still embed images in an html body that will be used by Apps Script to send emails without breaking the images?

We're developing the Apps Script with an account in our business domain though, so I'm not sure what permissions we're missing.

Upvotes: 0

Views: 474

Answers (1)

Wicket
Wicket

Reputation: 38160

You have two options, share only the image with anyone with the link or attach the image and embed it into the message body.

For the first option, the simplest way might be to have a Shared Drive with a folder shared with anyone with the link and put there the images that will be embed on the emails.

For the second option, the options parameter should include a inlineImages property having an object with a property for each image :

function sendEmail() {

    var imageID = '1pjb-Ap3XoI7fETZOPQaCNb3t8VzANR2C';
    var imageBlob = DriveApp.getFileById(imageID).getBlob();

    var body ='<div><img src="cid:image" width="900"/></div>'
    
    
    GmailApp.sendEmail(
      '[email protected]',
      'Test',
      body,
      {
      htmlBody: body,
      inlineImages: {image: imageBlob}
      }
    );
    }

Related

Upvotes: 1

Related Questions