Home > Net >  Using Gmail Alias to Send HTML
Using Gmail Alias to Send HTML

Time:07-24

I am using the following code to send a Google sheet-derived HTML table and some text via gmail. I can successfully send it from my 'default' subscriber account, but not from a functioning alias account.

I want to send it from that alias.

I know to implement GmailApp.getAliases();, I know what number the alias I want to use is, I just cannot get the HTML output into the email.

The closest I have come is just "object."

Any advice, suggested code, very welcome. Thank you!

 function getEmailHtml(appearanceData){
  var htmlTemplate = HtmlService.createTemplateFromFile("Template.html");
  htmlTemplate.appearance = appearanceData;
  var htmlBody = htmlTemplate.evaluate().getContent();
  return htmlBody;}

function sendEmail() {
  var appearanceData = getData();
  var body = getEmailText(appearanceData);
  var htmlBody = getEmailHtml(appearanceData);


MailApp.sendEmail({
    to: "[email protected]",
    cc: "[email protected]",
    subject: "XXX XXX XXX",
    body: body,
    htmlBody: htmlBody
      });

  function getEmailText(appearanceData) {
  var text = "";
  appearanceData.forEach(function(appearance) {
  text = text   appearance.client   "\n"   appearance.court   "\n"   appearance.date   "\n"   appearance.time   "\n"   appearance.charges   "\n"   appearance.notes  "\n"   appearance.phone  "\n-----------------------\n\n";
  });
  return text;
}

/**
 * @OnlyCurrentDoc
 */
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var startRow = 2;  
  var startCol = 2; 
  var numRows = sheet.getLastRow()-1;  
  var numCols = 10; 


  sheet.getRange(startRow, startCol, numRows, numCols);

  var values = SpreadsheetApp.getActive().getSheetByName("Settings").getRange(startRow, startCol, numRows, numCols).getDisplayValues();
  values.shift(); //remove headers
  var appearances = [];
  values.forEach(function(value) {
    var appearance = {};
    appearance.client = value[0];
    appearance.court = value[1];
    appearance.date = value[2];
    appearance.time = value[3];
    appearance.charges = value[4];
    appearance.notes = value[5];
    appearance.phone = value[6];
    appearances.push(appearance);

     
  
})
Logger.log(JSON.stringify(appearances));
  return appearances;

}}

CodePudding user response:

Have you tried using the GmailApp class instead of the MailApp class?

I think you can use something like this:

GmailApp.sendEmail(emailAddress, 'This is the subject of the email', 
                                 'Please see the body of the email.', {
    to: "[email protected]",
    cc: "[email protected]",
    subject: "XXX XXX XXX",
    body: body,
    from: aliases[0]
    name: "Sender Name", 
    htmlBody: htmlBody
      });

I took as base the samples on the Google Documentation for getAliases() and sendEmail(recipient, subject, body, options).

The issue was fixed with this change:

GmailApp.sendEmail(
      "[email protected]",
      "Outlook",
       body,{
            cc: "[email protected]",
            from: "[email protected]",
            htmlBody: htmlBody,
          }
        );
  • Related