Search code examples
javascriptgoogle-apps-scriptweb-applicationsgoogle-drive-apihtml-email

Add pics from Google Drive to html email body


Is it possible to add a png / gif file from the google disk to the email htmlbody generated by the google script? I have the code as below, the document is publicly shared, Drive API and Gmail API are enabled. Is this a problem sharing the file from the disk or an error in my code, please help

function sendaEmail(){
  var templ = HtmlService
  .createTemplateFromFile('template');
  
  var message = templ.evaluate().getContent();
  
  MailApp.sendEmail({
    to: 'email@gmail.com',
    subject: "subject",
    htmlBody: message
  });
}
<!DOCTYPE XHTML 1.0 Transitional//EN">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body style="margin: 0; padding: 0;">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td>
          <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
            <tr>
              <td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0;">
                <img src= <?= DriveApp.getFileById("1tKxUgeX_B27vnpNNpKy1jF_0zOxrBhmL").getAs("image/gif");?> alt="Creating Email" width="300" height="230" style="display: block;" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

Solution

  • yuri's answer is correct but I found a much simpler way. First you change the files sharing settings to 'Anyone with the link' then pull out the file ID and paste here: https://drive.google.com/uc?id=FILEID. And place the link to html code.