Sending a confirmation email after a google form is submitted - google-apps-script

I have looked at many resources to figure out what is wrong with my script but nothing is working. I am trying to send a confirmation email after a google form has been submitted. It seems like my script is not calling the email address correctly from the form.
My original script came from http://acrl.ala.org/techconnect/?p=2343 and I modified it for my form.
This is what it looked like:
function swykemailconfirm(e) {
var userEmail = e.values[10]; //email from column K
var firstName = e.values[2]; //first name from column C
var lastName = e.values[1]; //last name from column B
var test = e.values[4]; //test name from column E
MailApp.sendEmail(userEmail,
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
I am getting an email with this error message after doing a test form submission:
Can not find method (class) sendEmail (string, string). (Line 6, file "Code")
After searching here Google Forms Confirmation Script for a way to edit my script, I used one of the suggestions and changed my code to the following:
function swykemailconfirm(e) {
var userEmail = e.values["E-mail"][0];
var firstName = e.values[2];
var lastName = e.values[1];
var test = e.values[4];
MailApp.sendEmail(userEmail,
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
I am getting an email with this error message after doing a test form submission:
TypeError: Can not read property "0" from undefined. (Line 2, file "Code")
Any help is much appreciated!

Your original function (top) will work properly with one small modification.
Google Docs is giving you the error message "Cannot find method (class) sendEmail (string, string). (Line 6, file "Code")" because the sendEmail method requires three arguments to passed: recipient (string), subject (string), body (string). Currently, you are only passing two arguments: recipient and body. See documentation here
Modify your function to the following and everything should work!
function swykemailconfirm(e) {
var userEmail = e.values[10]; //email from column K
var firstName = e.values[2]; //first name from column C
var lastName = e.values[1]; //last name from column B
var test = e.values[4]; //test name from column E
MailApp.sendEmail(userEmail,
"Registration Confirmation Subject Line",
"Thank you " +firstName + lastName + "for signing up to take the " + test + "Show What You Know test. " +
"Make sure you see Ms. May to get your pass. " +
"See you on Thursday in room 32 at 3:30." +
"The Math Department");
}
Hope that helps!

Related

I would like to populate an email message with the contents of a specific, static, cell in sheets

I am sending a confirmation email when someone signs up for an appointment on a Google form. I would like to have the email message be populated with the contents of a specific cell. This means I would be able to type an email message into cell A2 and when someone fills out a form, it will send that message to them as the confirmation email.
I have two different email messages I am using, confirmation email and reminder email. I would like both codes to populate the same message.
function confirmationEmail(e) {
var userName = e.values[2];
var userEmail = e.values[3] + ";user#gmail.com";
var date = e.values[4];
var studentName = e.values[1];
var body1 = "Hello " + userName + ",";
var body2 = studentName + " has been registered for a physical at High School for the following date/time:";
var body3 = "Please park in the large student parking lot on the east side of the High School.";
var body4 = "Thanks!";
var signature = "Name" + "\n" + "High School" + "\n" + "user#gmail.com" + "\n" + "***-***-****";
var file1 = DriveApp.getFilebyId("1WDxic1meHzEGSjybJ2SS1h2MqGAhIAK4");
var file2 = DriveApp.getFilebyId("1v4GQAP8PkTPQRPoYIdsEOMBr2ZvRO1eocsqixyZ42gA");
GmailApp.sendEmail(userEmail,
"Registration",
body1 + "\n\n" + body2 +"\n\n" + date + "\n\n" + body3 + "\n\n" + body4 + "\n\n" + signature,
{attachments:[file1, file2]})
}
This code works perfectly already, however, I have some co-workers even less savvy than I. It would work best if they could just fill in the cell with the contents of the message to be able to send this out. So ideally, "body3" would be written into a cell within sheets and populated into the email.
So I guess you would want something like this:
var body3 = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();
You should probably include some conditional statements in the code to prevent the sending of the email in the event that cell 'A2' is blank and include an alert to inform the user that it needs to be filled in.
Perhaps something like this:
if(body1 && body2 && body3){
//send email
}else{
SpreadsheetApp.getUi().alert('Invalid or Missing Content');
}
Truthy

Custom Email based on Cell values in google sheets

I need help while building a Request Approval Flow in google Forms/Sheets
I have a data response sheet similar to like below, Column A to J headers are
"Timestamp" "EmailAddress" "Name" "Targets" "Account#" "Reason" "Access(Days)" "Approver" "Approved" "CaseID"
I have already setup a form submit email trigger to Approval body through formMule AddOn, Now I want to trigger email when Approval body approve or disapprove the request in Data response sheet.
Everytime when anyone select "Y" or "N" to column "I", script should suppose to trigger an email based on the data present in that row.
I am not an expert but tried to do it with following script and unfortunately not getting desired outcome, I set it up Current project trigger to OnEdit
function sendNotification1(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses 1");
if(e.range.getColumn() == 9 && e.value == "Y")
{
var name = e.range.offset(0,-6).getValue();
var comment = e.range.offset(0,-3).getValue();
var email = e.range.offset(0,-7).getValue();
var case1 = e.range.offset(0,1).getValue();
var approver1 = e.range.offset(0,-1).getValue();
var subject = "Request has been Approved";
var body = "Hi " + name + ", your Change Request number " + case1 + " has been approved by " + approver1 + " with following comments: " + "\n\r" + comment;
MailApp.sendEmail(email, subject, body);
}
if(e.range.getColumn() == 9 && e.value == "N")
{
var name = e.range.offset(0,-6).getValue();
var comment = e.range.offset(0,-3).getValue();
var email = e.range.offset(0,-7).getValue();
var case1 = e.range.offset(0,1).getValue();
var approver1 = e.range.offset(0,-1).getValue();
var subject = "Request has been Disapproved";
var body = "Hi " + name + ", your Change Request number " + case1 + " has been Dis-Approved by " + approver1 + " with following comments: " + "\n\r" + comment;
MailApp.sendEmail(email, subject, body);
}
}
StackDriver logging showed the following error, but couldn't identify where the issue exist while referring the cell addresses.
2018-09-14 08:19:06.336 PKT
The coordinates or dimensions of the range are invalid. at sendNotification1(Code:6)
I am able to trigger an email on any edit event with following code, but no luck with conditional edit event trigger.
function sendNotification1(e) {
MailApp.sendEmail("myemail#companygmail.com", "Sample subject", "Sample body");
}
While debugging the code, I can see the following error
TypeError: Cannot read property "range" from undefined. (line 6, file "Code")
Any help will be highly appreciated
You are likely running into the issue of the code not knowing what range you are referring to, because you never tell it where to look.
Add the following to the top of the function:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("YOUR SHEET NAME");
And it should work perfectly.
EDIT: Here's the email I sent to myself when I tested it.

Adding multiple attachments to email

I have a script which generates a document, saves as a PDF and attaches to an email. In addition to this attachment, I'm trying to add a second attachment which is an existing PDF saved on google drive (terms). Why is the following code not attaching the second document?
if (email_status == "YES" ) {
//send a pdf copy to customer
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var terms = DriveApp.getFileById('file ID');
var message = "Hi " + usernamefordoctitle + "!, please kindly find your invoice attached.\nMany Thanks!\nMe";
var emailAdd = sheet.getRange("D2").getValue()
var emailTo = emailAdd; // add customer email here
var subject = "Invoice for " + usernamefordoctitle + " from ME" + " - Invoice Number : " + newInvNumber;
var attach = {fileName:"INVOICE " + newInvNumber + " " + usernamefordoctitle + '.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach, terms.next()]});
ss.toast("70%: emailed customer");
Utilities.sleep(sleepINT);
}
You have
terms = DriveApp.getFileById('file ID');
followed by
terms.next()
This is incorrect, because getFileById gets you one specific file with the given Id. You were thinking of other methods like getFilesByName which return a file iterator. The name of the method is a clue to what it returns: getFile versus getFiles.
So, simply attaching
{attachments:[attach, terms]}
will work. You may also want to specify a MimeType like
terms.getAs(MimeType.PDF)
so that, e.g., you send a Google Doc as a PDF.

Google script - Can't attach a PNG file from Google Drive to form email

I've setup a trigger with the following code to send out an email everytime an employee submits a request. What this basically does is send out a brochure (in PNG format and stored in Google Drive). Somehow, the code does not work. Spent almost the entire day scouring the net for answers, but none helped. I tried the DriveApp.getFileByID method and it worked, but that was just for one file. The attachment depends on the product that the employee selects on the Google form, and that is dynamic.
Any help is much appreciated. Thanks!
function SendBrochure(e) {
try {
var email = e.values[1];
var subject = "ABC Co - Information you requested";
var msgcc = "test#gmail.com";
var aliases = GmailApp.getAliases();
var cxnm = e.values[2] + " " + e.values[3] + " " + e.values[4];
var msgl1 = "Greetings from ABC Co, " + cxnm + ".";
var emailText = "<b>Greetings from ABC Co, " + cxnm + ". </b> <p>With reference to your conversation
with us and your request for information, please find attached herein the same.</p> <p> Should you
require further assistance, please contact the undersigned, or refer to the brochure for pertinent
site contact details.</p> <p>We appreciate your interest and look forward to your visit soon.</p>
<p> Thanks and regards, <br><b>ABC Co</b><br>Tel: +1 202 555 1212<br>Web:
www.abc.com </p>";
var brochurename = e.values[5]; //gets file name from form (already filled in - for e.g f_1.png)
var brochure1 = DriveApp.getFilesByName(brochurename);
GmailApp.sendEmail(email, subject, msgl1, {
name: 'ABC Co',
attachments: [brochure1.next()],
htmlBody: emailText,
from: aliases[1]
});
GmailApp.sendEmail(msgcc, "Email sent!", "Your email request to " + email + " has been completed successfully at " + e.values[0], {
'from': aliases[1]
});
} catch (e) {
Logger.log(e.toString());
}
}
try getting the image as a blob with the right mime type, see [doc here][1],
in your code it would become :
...
attachments: [brochure1.next().getAs('image/png')],
...
PS : didn't have the opportunity to test this, please let us know it it does work. [1]: https://developers.google.com/apps-script/reference/drive/file?hl=fr-FR#getAs%28String%29
EDIT : here is a new version of your code that works, just change the image file names to suit your case.
I wrote a test function to be able to test without form submission.
function test(){
var par = {};
par['values']={};
par.values[0]=new Date();
par.values[1]='xxxxxxx#gmail.com';
par.values[2]='xxxxx';
par.values[3]='yyyyy';
par.values[4]='zzzzz';
par.values[5]='LOGO.jpg';// just for test I took a file I had in my drive
SendBrochure(par);
}
function SendBrochure(e) {
Logger.clear();
var email = e.values[1];
var subject = "ABC Co - Information you requested";
var msgcc = "yyyyyyyy#gmail.com";
var aliases = GmailApp.getAliases();
var cxnm = e.values[2] + " " + e.values[3] + " " + e.values[4];
var msgl1 = "Greetings from ABC Co, " + cxnm + ".";
var emailText = "<b>Greetings from ABC Co, " + cxnm + ". </b> <p>With reference to your conversation "+
"with us and your request for information, please find attached herein the same.</p> <p> Should you "+
"require further assistance, please contact the undersigned, or refer to the brochure for pertinent "+
"site contact details.</p> <p>We appreciate your interest and look forward to your visit soon.</p>"+
"<p> Thanks and regards, <br><b>ABC Co</b><br>Tel: +1 202 555 1212<br>Web: www.abc.com </p>";
var brochurename = e.values[5]; //gets file name from form (already filled in - for e.g f_1.png)
var brochure = false;
Logger.log('brochurename = '+brochurename)
var brochure1 = DriveApp.getFilesByName(brochurename);
if(brochure1.hasNext()){
var brochure = brochure1.next().getBlob();// get the blob
Logger.log('brochure name = '+brochure.getName())
}else{
Logger.log("didn't find a file with name = "+brochurename)
}
Logger.log('email data = '+email+' '+subject+' '+ msgl1)
if(brochure){
GmailApp.sendEmail(email, subject, msgl1,{attachments:[brochure], htmlBody:emailText});
GmailApp.sendEmail(msgcc, "Email sent!", "Your email request to " + email + " has been completed successfully at " + e.values[0])
}
GmailApp.sendEmail(msgcc, "Email not sent!", "Your email request to " + email + " has not been sent because of an invalid attachment\nSee Log below : \n\n"+Logger.getLog());
}

Creating an Auto Response using Google Forms

I am trying to create an auto response back to the user after they have filled in a Google Form. So far i have the following code which i have adapted from another user
function emailconfirm(e) {
var userEmail = e.values[3]; //email from column D
var firstName = e.values[2]; //first name from column C
var StoreNo = e.values[4]; //last name from column E
var StoreName = e.values[5]; //test name from column F
MailApp.sendEmail(userEmail,
"Corporate Order Form Submitted",
"Thank you " +firstName + ", your Order Form for " +StoreNo +StoreName + "has been submitted. " +
"This has been forwarded to the Team for approval. " +
"Regards ");
I am getting to the following message when I debug the code:
TypeError: Cannot read property "values" from undefined. (line 2, file "Code")
Any ideas where I am going wrong?
When you're entering that function in the debugger, you've got no event parameter e to give it. The error message isn't lying to you.
See How can I test a trigger function in GAS?.