Help! How to email trigger on new web app form submission?

589 views
Skip to first unread message

NMG MDO

unread,
Apr 5, 2023, 1:34:32 AM4/5/23
to Google Apps Script Community
Hi Team,
I've created web app form with the help of apps script. I want when new responses added in sheet so should be email send to user.
toemail :
ccemail:

Both fields are on my sheet, please find below my script


function sendmail() {

  //SpreadsheetApp.getActiveSpreadsheet().getRangeByName("Emails").activate();
   //var names = ss.getSheetByName("Emails");
  //var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const ss  = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('Responses')//rename sheet name per your file;
  //var to = SpreadsheetApp.getActiveSpreadsheet();
  var lr = ws.getLastRow();

  var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1,1).getValue();
  //Logger.log(templateText);
  var i = lr;
  for (lr;i<=lr;i++){
    var byname = ws.getRange(i,2).getValue();
    var bymail = ws.getRange(i,4).getValue();
    var dept = ws.getRange(i,5).getValue();
    var priority = ws.getRange(i,12).getValue();
    var message = ws.getRange(i,9).getValue();
    var pdate = ws.getRange(i,10).getValue();
    var rdate = ws.getRange(i,11).getValue();
    var raisedName = ws.getRange(i,7).getValue();
    var helpingDep = ws.getRange(i,6).getValue();
    var acceptanceLink = ws.getRange(i,13).getValue();
    var currentEmail = ws.getRange(i,14).getValue();
    var helpingDept = ws.getRange(i,6).getValue();
    var ccmail = ws.getRange(i,15).getValue();


    var messageBody = templateText.replace("{byname}",byname).replace("{bymail}",bymail).replace("{dept}",dept).replace("{priority}",priority).replace("{rdate}",rdate).replace("{message}",message).replace("{pdate}",pdate).replace("{raisedName}",raisedName).replace("{helpingDep}",helpingDep).replace("{acceptanceLink}",acceptanceLink).replace("{helpingDept}",helpingDept);
    var subjectLine = "HIT Ticket Raised From  " + byname ;
    MailApp.sendEmail(currentEmailsubjectLine,messageBody,{
      cc : ccmail
    });
    

  }
  
}

Brett Grear

unread,
Apr 5, 2023, 6:46:40 AM4/5/23
to Google Apps Script Community
Can you not send the email on formsubmit using the form data instead of the spreadsheet?
Alternatively, you could have a time trigger to periodically check whether it needs to send an email.  You would need a new column called SENT or something to mark as true when the email was sent to avoid duplicates.

Posting via phone so apologies for spelling and formatting 

NMG MDO

unread,
Apr 5, 2023, 8:35:08 AM4/5/23
to Google Apps Script Community
Thank you! Brett Grear for your valueble reply.
I could not type script as per your suggestion. 
can you please help me in it? if I give you my both script HTML, Code then could you help me?

Brett Grear

unread,
Apr 5, 2023, 6:03:33 PM4/5/23
to Google Apps Script Community
I can try

AutoBi Tech

unread,
Apr 6, 2023, 12:02:16 PM4/6/23
to Google Apps Script Community
Hi, I'm giving you editor link of my project.. Where you can see my both script file HTML, apps script or email script.
please find below my project link:-

Nerio Villalobos

unread,
Apr 11, 2023, 4:19:48 AM4/11/23
to google-apps-sc...@googlegroups.com
You can add the email notification functionality to your script by using a trigger that runs when a new form response is submitted. Here's an example modified script that should accomplish this:

function sendmail(e) {
  // Get the submitted form data
  var submittedData = e.namedValues;
 
  // Get the email addresses and message from the submitted data
  var toEmail = submittedData['toemail'][0];
  var ccEmail = submittedData['ccemail'][0];
  var message = 'This is the email message for the form submission.';
 
  // Send the email
  MailApp.sendEmail({
    to: toEmail,
    cc: ccEmail,
    subject: 'New form submission',
    body: message
  });
}

You'll also need to create a trigger to run the sendmail() function when a new form response is submitted. To do this, follow these steps:

Open your Google Sheet that's connected to your Google Form.
Click on the "Tools" menu and select "Script editor".
In the script editor, click on the "Edit" menu and select "Current project's triggers".
Click the "Add trigger" button in the bottom right corner of the dialog.
Choose "sendmail" as the function to run.
Choose "From spreadsheet" as the event source.
Choose "On form submit" as the event type.
Click "Save" to create the trigger.
This will run the sendmail() function every time a new form submission is received, and it will send an email to the specified recipients. Make sure to update the email addresses and message in the script to match your specific needs.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6295ea07-dcd2-4e12-967a-2ec9dd3df090n%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Reply all
Reply to author
Forward
0 new messages