Send a personnalised email from a Google Form - email

I'm starting a new project. I have to send a confirmation email when someone purchases a book with my Google Form. By now, I can send an email when you complete the form but I can't put the name of the buyer and the number of books in the email. F.ex : Hello "John", you gonna receive your "3" books in a week.
I've tried something like this :
function sendEmail(e) {
var formreponse = e.responses;
var itemreponse = formreponse.getItemResponses();
var number= itemreponse[5].getResponse(); //because it's in the 5th columns of the spread sheets
var name= itemreponse[3].getResponse();
And then I just write a simple code like this
var TextToSend = "blablabla" + name + " blabla" + number + ".";
GmailApp.sendEmail(emailTo, subject, TextToSend, options);
But I've got an error message:
Cannot read property 'responses' of undefined.
And I don't know why!
It's very frustrating for me not to be able to finish this simple task.

Issues:
When running this function manually, through the editor, no event object is passed, and so e is undefined. You should not run this function manually.
responses is not a valid Event property of Form submit triggers.
Solution:
Install an onFormSubmit trigger, either manually or programmatically, so that the function will run automatically when the Form is submitted, and e will be populated.
Change e.responses to e.response:
var formreponse = e.response;
Reference:
Event Objects > Google Forms events > Form submit

Related

Capture HTML Form Responses in Google Sheets and send a Confirmation Email to the Form Submitter

I want the script to send the created html email template email.html to the person who last submitted the form.
Below is the success message I get in the console:
Below is the actual email that is received by the person who submits the form:
The email.html is formatted correctly and appears perfectly when sent manually.
I hope this is reprex enough.
EDIT: Maybe it's easier if I include the code
function sendEmail () {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var data = ss.getRange(lr,3);
var email = data.getValue();
var message = HtmlService.createTemplateFromFile('email');
var subject = "Test Subject"
GmailApp.sendEmail(email,subject, message);
}
It's not clear why you are using HtmlService.createTemplateFromFile, but from the image it's clear there at least one error, the script misses two methods:
HtmlService.HtmlTemplate.evaluate() to evaluate the Templated HTML
HtmlService.HtmlOutput.getContent() to get the HTML from the HtmlService.HtmlOutput object returned by the previous method.
Another option that looks to be an error is the use of GmailApp.sendEmail(string,string,string) method, as the third parameter should be a string to be used as the email plain text content. If you want to pass HTML, instead use GmailApp.sendEmail(string,string,string, Object)
Related
Emailing Google Sheet range (with or without formatting) as a HTML table in a Gmail message
Sending an email in HTML and plain with a Gmail Apps Script
Google script inject html into html email
References
https://developers.google.com/apps-script/guides/html
https://developers.google.com/apps-script/guides/html/templates
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)

Lime Survey Validate Text Field Equal to Other

I'm asking to the user for double email check, so I have two text fields and I need to know how to validate that the second text field input be equal to the first one.
Maybe somekind of regular expression in the validation field?
Thanks
UPDATE
This basic script solved my need. Select "html source" on the Tiny editor of the second question, where you are asking again for the email and then add.
jQuery(document).ready(
function(){
var first_email = jQuery("#answer496577X323X3572"),
second_email = jQuery('#answer496577X323X3573');
first_email.on('focusout', function(){
email = first_email.val();
});
second_email.on('focusout', function(){
email2 = second_email.val();
if (email2 != email){
alert("Mail input doesn't match");
first_email.focus();
}
});
});
Change the "answer496577X323X3572" by the proper input ID of the fields.
Your system can be easily hijacked with deactivate the javascript. You don't validate via the server. And LimeSurvey can do both : show an error in javascript + validate in PHP (server).
If you use a multiple text question with EMAIL for code, EMAIL for subcode #1 and CONFIRM for #2 : put this in "Question validation equation" advanced settings.
EMAIL_EMAIL == EMAIL_CONFIRM.

How to access a data attribute value of a form input server side?

I am sending through a form object to Google Apps Script with:
var formObject = $("#my_form")[0];
google.script.run.processForm(formObject)
The form includes a file input and I have no problem retrieving this and the other input values on the server with something like:
function processForm(formObject) {
var user_name = formObject.userNameInputName;
}
The documentation is clear that GAS can send through a form providing it is the only parameter:
https://developers.google.com/apps-script/guides/html/reference/run#myFunction(...)
My question is:
How do I access a data attribute value of a form input server side?
And to answer this I need to ask a silly question:
What is a "form object"? What is it's structure, what does it "look like" - is it just a JavaScript object or something else? (if I console.log(formObject) client side it just displays the HTML). If I know this I figure I will know how to access the data attribute value correctly.
Edit:
I ended up just adding a hidden input field to the form and setting the val() of it via an on click event before submitting the form, then I could access the value server side with:
function processForm(formObject) {
var user_name = formObject.userNameInputName;
var was_a_data_attribute = formObject.myHiddenInputField;
}

Interaction between google mail and Google Sheets

I want to know if there is an application that can allow an interaction between Google Mail and a Google Sheet.
I explain myself :
I receive many e-mail from online forms and all the email are similar with the inormations about the customer I have to contact. I want to receive all the form into a Google Sheets that I can manage like a CRM.
Thank you for your help.
Regards,
Steve
There are services that can assist with this and you can use custom scripts also. It can also depend on what type of platform you're using for the contact form.
IFTTT | Zapier | Script
If the data is correctly formatted this one is an easy script to do exaclty that.
In google sheets, Tool >Script Editor >
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(' List');
var last = sheet.getLastRow();
for(var i=2;i<last+1;i++){}
var firstName = sheet.getRange(i, 1).getValue();
var email = sheet.getRange(i, 4).getValue(); // the second value is the column numbers , from left to right 1 to all
var message = 'Hola '+firstName+', message'; // Replace "Message" with your meesage and if you need to break lines use /n at the start of the new line
GmailApp.sendEmail(email, 'MPFC Schedule Assistant Bot',message);
}

Undefined e.values on google email notifications

I am using script editor in google spreadsheets to send an auto response email. However, I received Summary of failures notifications suggesting an invalid email: undefined (on the Mail.App line). I don't know how this error came about. Please help. Thanks so much.
function myFunction(e) {
var Nickname = e.values [2];
var email = e.values [10];
var subject = "Form Submitted"
var message =
"Thank you, " = + Nickname + ". Your response is accepted. You may want to check our website # iydsphilippines.weebly.com for registration payment details. Thanks and we hope to see you soon!";
MailApp.sendEmail(email, subject, message);
}
Here is a link to a copy of my spreadsheet.
<>
I'm no expert however I also had the same issue and by trial and error I have discovered what the problem is.
It seems that recently Google have "upgraded / changed" the way fields are evaluated.
If any fields are blank then problems arise and as your email field in column K (i.e. the 10th field counting the first as zero) and some of the others are blank it seems to get confused. Until Google fix this "undocumented feature" I have a workaround.
Move your Email column to nearer the beginning i.e. straight after the name for example but before any optional fields, then renumber it in the code and you should be OK.
Specifically in your example: Move Column K to be Column C and renumber "var email = e.values [10]" to be "var email = e.values [2]" That should do it.
Good Luck and let me know how you get on. From Lester in the UK