ken
ken

Reputation: 41

Sending Google Doc as HTML in email body using Google Apps Script while preserving format

Using the script below on a Google Doc, I'm trying to send the document as HTML in an email body. It's converting the document correctly (when I check the exported document via the url) and sending the email with the same content, but it loses the following formatting at some point: font format (e.g., size, color) and table format (e.g., borders, background color)

function sendGoogleDocAsHTML(){
  var id = DocumentApp.getActiveDocument().getId() ;
  var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
  var param = {
method      : "get",
headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url, param);
  var email = <EMAIL>;
  var subject = <SUBJECT>;
  GmailApp.sendEmail(email, subject,"", {htmlBody:html});
}

How do I preserve the format in the email?

Upvotes: 3

Views: 3977

Answers (1)

Adharsha Neel
Adharsha Neel

Reputation: 504

This worked fine for me. Hope this may help

  function sendGoogleDocAsHTML(){
  var id = DocumentApp.getActiveDocument().getId() ;
  var url = "https://docs.google.com/document/d/"+id+"/export?format=html"
  var param = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url, param);
  var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" +
                                            "To: [email protected]\r\n" +
                                            "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
                                            html+"\r\n\r\n");
   var message = Gmail.newMessage();
   message.raw = raw;
   var sentMsg = Gmail.Users.Messages.send(message, 'me');
}

Upvotes: 8

Related Questions