google-apps-scriptblobgmail-apiadd-onutility

How to convert email as a blob in Gmail Addon Using Card Services


My aim to convert an email's content, with attachments, into a binary large object (blob) format within the Gmail add-on. I'm getting the body content using the below function but I'm not able to convert it into blob format.

I referred to the following documents but I don't know which function I have to use here for blob format.

  1. Blob
  2. GmailApp
function loadAddon(event){
    var accessToken = event.gmail.accessToken;
    var messageId = event.gmail.messageId;
    GmailApp.setCurrentMessageAccessToken(accessToken);
    var mailMessage = GmailApp.getMessageById(messageId);
    var emailContent = mailMessage.getBody();
    console.log("Email Content: " + emailContent);
}

Can anyone guide me on this?


Solution

  • In order to create a blob of the eml format, please modify the script as follows.

    Modified script:

    function loadAddon(event) {
      var accessToken = event.gmail.accessToken;
      var messageId = event.gmail.messageId;
      GmailApp.setCurrentMessageAccessToken(accessToken);
      var mailMessage = GmailApp.getMessageById(messageId);
      var emailContent = mailMessage.getBody();
      console.log("Email Content: " + emailContent);
    
      var blob = Utilities.newBlob(mailMessage.getRawContent(), "message/rfc822", "sampleFilename.eml");
    }
    

    Reference: