Tuesday, April 28, 2020

Sending Automated Emails using Google Apps Script

Sending Automated Emails using Google Apps Script is easy. we have a good MailApp class that is used to send any mail in any way. it has 5 methods as per google documentation. the Five methods based on what parameter we are passing in methods. below are the methods.

  1. sendEmail(message)  --> [it is void type]
  2. getRemainingDailyQuota() -->   [it is integer type,we can return integer]
  3. sendEmail(recipient, subject, body, options) --> [it is void type]
  4. sendEmail(to, replyTo, subject, body) -->    [it is void type]
  5. sendEmail(recipient, subject, body)     ---> [it is void type]

1. First Create a Sheet list like below image

creates the spreadsheet with 3 columns as shown below image.
and declare the values in the spreadsheet to send the mail. and column one is the First Name, Column Two is Email and Column Three is Mail Subject.

Sending Automated Emails using Google Apps Script

2. Go To Script Editor and write the code

  1. you need to open the Script Editor:  SpreadSheet --> Tools --> Script Editor and write the Google App Script as shown below image.  The SpreadSheet has 3 columns.
        • Column One =row[0]
        • Column Two =row[1]
        • Column Three =row[2]
  2.  Reach row we have declared with different names as shown below and iterate each email and send the email to the recipient. in Spreadsheet has 3 emails.
      • row[0] =firstname
      • row[1]=emailId
      • row[2]=subject 
      • getActiveSheet(): it is used to get the content of the current active spreadsheet
      • getDataRange(): This method used to get the range of corresponding to the dimension in which data is present

Sending Automated Emails using Google Apps Script

3. Below is the Google Apps Script Source Code

function sendAutomatedEmails()
        var sSheet = SpreadsheetApp.getActiveSheet();
        var rangeValue = sSheet.getDataRange();
        var data = rangeValue.getValues();
        for (var i = 1; i < data.length; i++)
            (function(val) {
                var row = data[i];
                var firstname = row[0];
                var emailId = row[1];
                var subject = row[2];
                var message = "Dear" + firstname + "\n\n" + subject;
                MailApp.sendEmail(emailId, subject, message);

4. The Final Result of Automated Email Using Google App Script

The FinalOut looks like the below image. we get the email to respected email which we have declared in the spreadsheet

Sending Automated Emails using Google Apps Script

No comments: