How to Send Email Via Google App Script?

How to Send Email Via Google App Script?

Here I will illustrate how to send an email with Google App Script, I will create and share the function for sending emails.

In the function, First we enter the mail to whom you want to send the mail and then set the subject line, and body. To send an email we will use the SendEamil(recipient, subject, body, options) Method.

function send_email(){
   var to = '[email protected]';
   var subject_line = 'Testing MAIL'
   var body = 'Hello All';
GmailApp.sendEmail(to,subject_line,body);
}

In the next step, we will fetch the data from Google Sheets and send an email with it. Here, we create a function to send an email from the sheet. Here is the code that sends the mail from the Google sheet.

function send_email_from_google_sheet(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Send Email Data');
var data = sheet.getDataRange().getValues();

 for(var c=1; c<data.length; c++)
   {
    var thisRow = data[c];
      var to = thisRow[0];
      var this_subject = thisRow[1];
      var body = thisRow[2];
   GmailApp.sendEmail(to,this_subject,body);
   }
}
techfixxo Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *