Search code examples
google-apps-scriptgmail

Script - GmailApp.SendEmail with InlineImages from Google Drive


Hy, im trying to make a script to send inline images from an alias gmail account. I found most of the code in GmailApp-s documentation, but now im stuck at the last step... Plese plese help, how do i get this part to work?

Thank you!

var html =  
'<body>' + 
  '<img src='cid:image'>' +
'</body>' 

    function testGmailApp() {  
var ImageBlob = DriveApp
                      .getFileById('0Bx4vy5p9TA6bekY3Q2ZNdzViVkE')
                      .getBlob()
                      .setName("ImageBlob");
      GmailApp.sendEmail(
'[email protected]', 
'test GmailApp', 
'test',{
htmlBody: html, 
inlineImages: {image: ImageBlob}
      }); 

}


Solution

  • Here is how you insert an inline image to your email. You can find an example of this in the documentation for MailApp. In this particular case of sending an inline image, the syntax for both functions remains the same.

    As mentioned in the documentation, the first thing step to insert the inline image is to insert an img tag with src =" cid:"Name of the image blob in inlineimages object here" The html string will look like this:

    var html =  
    '<body>' + 
      '<h2> Test <img src = "cid:image"> </h2><br />' +
    '</body>'
    

    The remaining function remains the same.
    Note: src points to cid: image, the name of the key in the inline image object.

    Final code:

    var html =  
        '<body>' + 
          '<h2> Test <img src = "cid:image"> </h2><br />' +
        '</body>'
    
        function testGmailApp() {  
    var ImageBlob = DriveApp
                          .getFileById('0Bx4vy5p9TA6bekY3Q2ZNdzViVkE')
                          .getBlob()
                          .setName("ImageBlob");
          GmailApp.sendEmail(
    '[email protected]', 
    'test GmailApp', 
    'test',{
    htmlBody: html, 
    inlineImages: {image: ImageBlob}
          }); 
    }