Shiver
Shiver

Reputation: 3

Sending Google Forms responses to specified e-mail address on Submit

I am trying to setup Google Form in a way that once Form Submiter hits "Submit" button the responses are not only populated in dedicated spreadsheet but also sent via e-mail to particular mailing group

Probably the best way would be to use Google Scripts and Triggers, including "onsubmit", "Mailapp.sendmail" functions but my struggles here are: a) to get the responses directly from just submitted form b) to change "send from" e-mail address (that would send an e-mail to mailing group) to submiter's e-mail c) connect the form responses with mailapp.sendmail function

I have tried something like this but it holds the issues mentioned above:

function OnSubmit(e) {  var formResponses = 
FormApp.getActiveForm().getResponses();
Logger.log(formResponses.length);
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
Logger.log('Last response to the question "%s" was "%s"',
           itemResponse.getItem().getTitle(),
           itemResponse.getResponse());
MailApp.sendEmail ("[email protected]", "Form Submited " + Date.now(), 
FormApp.getActiveForm().getResponses());
}}

I really appreciate all your help! Because I am total amateur in this one.

Upvotes: 0

Views: 1622

Answers (1)

St3ph
St3ph

Reputation: 2290

a) to get answer in an email you can do that :

function sendAnswers(e) {
  var items = e.response.getItemResponses();
  var emailRespondent = e.response.getRespondentEmail();
  var html = "";
    for(var i = 0; i< items.length; i++) {
    var item = items[i];
    html += "<b>"+item.getItem().getTitle()+"</b> : "+ item.getResponse() + "<br>";

  }
  MailApp.sendEmail("EMAIL_RECIPIENT", "Form answers", "", {htmlBody:html})
}

b) it is not possible to change the 'To' of email.

c) Yes we use the MailApp function.

=> do not forget to create a trigger to have the function run on onFormSubmit.

=> in emailRespondent you have email of person that submit the form if you click to collect email of respondent. if you don't do that comment the line i.e.

  // var emailRespondent = e.response.getRespondentEmail();

Stéphane

Upvotes: 1

Related Questions