sendEmail function: How to show the account name as sender not the email prefix

Tags: , ,



I’m sending emails using sendEmail function from google sheets and the emails arrive showing the sender as the prefix of my email address (before the @ sign) instead of my gsuite account name, as they do when sending an email manually. Is there a way to change the from field?

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet1 = ss.getSheetByName('Sheet1');
  var sheet2 = ss.getSheetByName('Sheet2');
  var subject = sheet2.getRange(2, 1).getValue();
  var n = sheet1.getLastRow();
  for (var i = 2; i < n + 1; i++) {
    var emailAddress = sheet1.getRange(i, 2).getValue();
    var name = sheet1.getRange(i, 1).getValue();
    var message = sheet2.getRange(2, 2).getValue();

    message = message.replace("<name>", name);
    MailApp.sendEmail(emailAddress, subject, message);
  }

Answer

When you want to set From of the header from From: ###@gmail.com to From: sample name <###@gmail.com> using MailApp.sendEmail, how about modifying as follows?

From:

MailApp.sendEmail(emailAddress, subject, message);

To:

MailApp.sendEmail(emailAddress, subject, message, {name: "sample name"});

or

MailApp.sendEmail({to: emailAddress,subject: subject,body: message,name: "sample name"});

Note:

  • This modification supposes that your script except for MailApp.sendEmail(emailAddress, subject, message) works.
  • If I misunderstood your goal, can you provide the result value you expect? By this, I would like to confirm it.

Reference:



Source: stackoverflow