Tutorial to Send Email from Google Apps Script – Hello everyone, In the previous two tutorials about Google Apps I have covered inserting Google script on web page and creating dynamic drop down in Google Forms. Today I am going to show a simple way to send emails from Google Scripts. There are many classes that can be used to send emails from scripts like GmailApp, MailApp. Using GmailApp class allows you to get access to your Gmail account where MailApp class is created just to send emails. So in this tutorial I will cover MailApp Class and a simple working example to send email from Google Scripts.
Send Email from Google Scripts – MailApp example
In this tutorial I have created a form in Google script and on successful registration of the user, an email is sent to the registered email id. Since focus of this tutorial is to send email from Google scripts, I am skipping the part to design and create form in Google scripts. That I will cover in the next tutorial on Google Apps Scripting. So to start my Form looks like as shown below:
Now main task is to generate an email and send confirmation of the account created to the registered email account. For that you need to include the following code in your button handler event:
As you can see the code which is very simple, I am using MailApp class and calling sendEmail function of that class. There are different parameters to set:
- to: email id of the receiver, In the code above i have set it to emailname variable that holds the current user email id. You can use any email id per your need
- subject: Email subject
- htmlBody: this field is used to set the content of the email and as you can see i have used html tags to design look of the email.
You can also use the following code to send email using MailApp class
MailApp.sendEmail(emailAddress, subject, message);
where all parameters in the function are variables which you can set as per your need.
Comment below if you face any issue running the code..