I want to send 1 general email and 1 conditional email (based on whether the response to a particular question was 'yes' or 'no') upon form submit. I have no issues with the general email being sent, but when I select the 'yes' option in the Google Form, the conditional email isn't sending. Here is the code I have so far (and I have also set up the relevant triggers for sendEmail and sendConditionalEmail). Any advice is appreciated!
function sendEmail(e) {
var html = HtmlService.createTemplateFromFile("GeneralEmail.html");
var htmlText = html.evaluate().getContent();
var emailTo = e.response.getRespondentEmail();
Logger.log(emailTo);
var subjectofEmail = "TESTING INFORMATION (BRJ)";
var textBody = "This email requires HTML support";
var options = {htmlBody: htmlText};
if(emailTo !== undefined){
MailApp.sendEmail(emailTo, subjectofEmail, textBody, options);
}
}
function sendConditionalEmail(e){
var Conditionalhtml = HtmlService.createTemplateFromFile("ConditionalEmail.html");
var ConditionalhtmlText = Conditionalhtml.evaluate().getContent();
var subjectOfEmail = "Guided Testing (BRJ)";
var textBody = "This email requires HTML support";
var interested = objectifyingFormforInterested();
var emailTo = e.response.getRespondentEmail();
Logger.log(interested);
if (interested == "Yes") {
MailApp.sendEmail( emailTo, subjectOfEmail,'', {
htmlBody: ConditionalhtmlText,
cc: 'xxx@gmail.com'});
}
}
function objectifyingFormforInterested(myForm){
var myForm = FormApp.openById("13o_WK3G8m6PeopBrI2y6p6SrNp-cGFs3vgpRdqotISY");
var formResponses = myForm.getResponses();
Logger.log(formResponses);
var currentResponse = formResponses[formResponses.length-1];
var responseArray = currentResponse.getItemResponses();
Logger.log(responseArray);
var form= {};
form.user = currentResponse.getRespondentEmail();
form.timestamp = currentResponse.getTimestamp();
form.formName = myForm.getTitle();
var response = responseArray[responseArray.length-1].getResponse();
Logger.log(response);
return response;
}
function camelize(str) {
str = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()@\+\?><\[\]\+]/g, '')
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
function sendConditionalEmail(e) {
var Conditionalhtml = HtmlService.createTemplateFromFile("ConditionalEmail.html");
var ConditionalhtmlText = Conditionalhtml.evaluate().getContent();
var subjectOfEmail = "Guided Testing (BRJ)"
var interested = objectifyingFormforInterested();//You are not passing the MyForm parameter to this function
var emailTo = e.response.getRespondentEmail();
if (interested == "Yes") {
MailApp.sendEmail(emailTo, subjectOfEmail, null, {
htmlBody: ConditionalhtmlText,
cc: 'xxx@gmail.com'
});
}
}
function objectifyingFormforInterested(myForm) {
var myForm = FormApp.openById("13o_WK3G8m6PeopBrI2y6p6SrNp-cGFs3vgpRdqotISY");
var formResponses = myForm.getResponses();
var currentResponse = formResponses[formResponses.length - 1];
var responseArray = currentResponse.getItemResponses();
var form = {};
form.user = currentResponse.getRespondentEmail();
form.timestamp = currentResponse.getTimestamp();
form.formName = myForm.getTitle();
var response = responseArray[responseArray.length - 1].getResponse();
return response;
}