0

I want to send mail with an inline image instead of as an attachment through google sheet. Please help

the script is as below:

function emailSummary() {
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var sh = ss.getSheetByName("Hitesh")
    var file = DriveApp.getFileById('1T8QA_WsXQkZZGwmBSN13iPV7rM8xGG_GtYy6T1eug-c') 
    var gmail = '[email protected]';
    var html =  '<png>'

    MailApp.sendEmail("[email protected]", "sunject", "Dear Sir,\n\n Forwarding herewith Monthly Summaryw \n\n Regards \n Hitesh ", {
    //email address, subject, message

     name: 'Hitesh', //file name
     attachments: [file.getAs(MimeType.PDF)] //file to attach
    });
}

2 Answers 2

1

You can pass HTML to the .sendMail() function. This link to the official documentation includes an example for inline images!

// This code fetches the Google and YouTube logos, inlines them in an email
// and sends the email
function inlineImage() {
  var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
  var googleLogoBlob = UrlFetchApp
                         .fetch(googleLogoUrl)
                         .getBlob()
                         .setName("googleLogoBlob");

  //You can also get these images from your drive to attach them.

  var imgBlob = DriveApp.getFileById("<DRIVE_FILE_ID_OF_PICTURE>")
                     .getBlob()
                     .setName("imgBlob");


  MailApp.sendEmail({
    to: "[email protected]",
    subject: "Logos",
    htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
              "img from drive: <img src='cid:driveImg'>"
    inlineImages:
      {
        googleLogo: googleLogoBlob,
        driveImg: imgBlob
      }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Emailing Images from your Google Drive

You can use the htmlBody parameter in GmailApp.sendMail(). However, if you want to avoid having to store the image in a URL that is publicly accessible. You can do something like this.

This is a portion of my JavaScript:

 function sendImgMsg() {
    var fileId=$('#mediaSel').val();//This is the fileId where the image is store.  In my image converter script I keep all of this images in the same folder.
    google.script.run
    .withSuccessHandler(function(fObj){
      var msg=$('#emsg').val();//This is the contents of a textarea
      var hl='<p>' + msg + '</p><br /><strong>File Name:</strong> '+ fObj.name + '<img src="'+ fObj.uri +'" title="' + fObj.filetype + '" />';
      $('#email').css('display','none');
      google.script.run.sendImageMessage(hl);//This is the code that sends the email
    })
    .getSelectedFile(fileId);
  }

This is a portion of my html:

<div id="email">
   <textarea id="emsg" cols="40" rows="4"></textarea>
   <br /><input type="button" value="Send" onClick="sendImgMsg()" />
</div>

This is a portion of my code.gs:

function getSelectedFile(fileId){
  var file=DriveApp.getFileById(fileId);
  var dataURI=file.getBlob().getDataAsString();
  var s=dataURI.split(',')[0];
  var mediaType=s.slice(s.indexOf(':')+1,s.indexOf('/'));
  var fileType=s.slice(s.indexOf('/')+1,s.indexOf(';'));
  var fObj={name:file.getName(),uri:dataURI ,type:mediaType,filetype:fileType};
  return fObj;
}

function sendImageMessage(hl) {
  GmailApp.sendEmail('recipient', 'ImageInAnEmail', null ,{htmlBody: hl});
}

This is the code that converts external images to imageURI's:

function convImageUrl(url){
  var blob=UrlFetchApp.fetch(url).getBlob();
  var b64Url='data:' + blob.getContentType() + ';base64,' + Utilities.base64Encode(blob.getBytes());
  return b64Url;
}

The above is a part of a script that I use for converting images to imageURI's so that I can store and access them on my Google Drive.

2 Comments

This is interesting, but why not just .getBlob() on an image from Drive?
@Chris It's part of another script that I wrote and I just pieced the necessary sections together to share the information. And it was something new to me so there is probably a better way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.