TheEfreet
TheEfreet

Reputation: 3

Google Form mail notification script. Emails always have "me" as sender

I have tried to use the search function, but did not find a solution for my problem, or I do not understand enough yet.

I have written a script for google forms, that sends an automatic email to two email addresses, when an user submits the form. I have build in some information that should show in the email subject and puts the input from the forms into the email, with some simple HTML formatting.

My problem is, that the emails always have my Email address as the sender (the account I used for creating the form) I would like to have the senders email, the one that is submitting the form. How is that possible?

Here is the code:

    function sendFormByEmail(e) 
{    
  // Sent to Email address
  var email1 = "[email protected]";
  var email2 = "[email protected]"; 


  // Variables

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
  var txt = "";
  var subject = "Example text: ";

  // Var e = form input as array.
  // Loop through the array.

  for(var i in headers){
    txt += "<b>" + headers[i] + '</b>' + ': '+ 
e.namedValues[headers[i]].toString() + '<br><br>';    
  }

  // Insert variables from the spreadsheet into the subject.
  // Create email subject
  subject += "Example text " + e.namedValues[headers[2]].toString();

  // Send the email
  MailApp.sendEmail(email1, subject, "", {htmlBody:txt});
  MailApp.sendEmail(email2, subject, "", {htmlBody:txt}); 

}

Upvotes: 0

Views: 106

Answers (1)

shabnam bharmal
shabnam bharmal

Reputation: 584

It is not possible to send the mail as the user who submitted the form as the script is running from the user account and the mailapp will send the mail from that account only. you can change the display name according to the user name who submiited the form by the parameter name. Also you can change the email to [email protected] by adding the parameter noReply in mailApp syntax. However you cannot change it to the user who submitted the form

You can refer this documentation for the above parameters : https://developers.google.com/apps-script/reference/mail/mail-app

Hope this could help

Upvotes: 1

Related Questions