Google Apps Script – Send HTML Email from Spreadsheet Using Different Alias

google-apps-emailgoogle-apps-script

I've been frolicking around sending emails from Google Spreadsheet.

The sent email has two requirements to fill:

  • It needs to be send from my work-alias
  • It needs to be as HTML

I can do the former with:

GmailApp.sendEmail(to,subject,body,{from:alias,replayTo:alias})

Given a proper definition of 'alias', that works very well.

I can do the latter with:

MailApp.sendEmail(to,subject,"",{htmlBody:body})

But, perhaps you guessed it:

  • GmailApp doesn't seem to support the HTMLBody option,
  • MailApp doesn't seem to be able to send e-mail from an alias.

Did I do something wrong? Did I search the wrong place? Is there a way around this?

Best Answer

Both API's, GmailApp and MailApp, have the possibility to send a HTML body via the advanced options. I used below code and was able to send HTML formatted text through both of them.

Code

// global var 
var html =  
    '<body>' + 
      '<h2> Test </h2><br />' +
        '<p> Greetings Earthling </p>' +
    '</body>'    

function testGmailApp() {  
  GmailApp.sendEmail(
    'your@emailaddress.com',         // recipient
    'test GmailApp',                 // subject 
    'test', {                        // body
      htmlBody: html                 // advanced options
    }
  ); 
}

function testMailApp() {
  MailApp.sendEmail(
    'your@emailaddress.com',         // recipient
    'test MailApp',                  // subject 
    'test', {                        // body
      htmlBody: html                 // advanced options
    }
  ); 
}

Screenshot

enter image description here

Reference