Dat
Dat

Reputation: 21

Email Name using Google App Script - Make a user name appear in the "From" section of the receivers email

When I compose a Gmail, my name appears to the receiver (e.g. Quoc Dat).

But when I use this code to send mails, not my name but only my email address appears to the receiver.

How can I fix this code so that my name appears to the receiver?

function onFormSubmit(){
   var sheet=SpreadsheetApp.getActiveSheet();
   var dataRange=sheet.getDataRange();
   var data=dataRange.getValues();
   for(var i=1;i<=data.length-1;++i){
      var row=data[i];
      if(row[3]!='a'){
         var address=row[2];
         var message="Hello";
         MailApp.sendEmail(address,"Hello",message);
         sheet.getRange(i+1,4).setValue("a");
         SpreadsheetApp.flush();
      }
   }
}

Upvotes: 2

Views: 381

Answers (1)

Alan Wells
Alan Wells

Reputation: 31310

You must use the name Advanced Parameter and the options

Currently:

MailApp.sendEmail(address,"Hello",message);

Must Be:

MailApp.sendEmail(address,"Hello",message,{name:'Your Name Here'});

Or:

var address = "[email protected]";
var message = "The body of email here";

var objectOfOptions = {//Object literal of advanced options
  name:"Your Name Here"
}

MailApp.sendEmail(address,"Hello",message,objectOfOptions);

Link to documentation: Apps Script Documentation - MailApp.sendEmail()

If your spreadsheet has the user name, you'll need to get that. If you don't have the user name associated with the email address, "MailApp" won't automatically get the user name.

Upvotes: 2

Related Questions