Skip to content Skip to sidebar Skip to footer

Sending Html Email To Multiple Recipients Using Google Sheet And Appscript

I created a script that helps me send bulk HTML emails to multiple recipients. In my Google Sheet file Col 1 contains the email address and Col 2 has the Name of the recipient. Th

Solution 1:

You can use indexOf to find the @ character and split the array into a substring to only get the characters positioned before @. Then, you can just add it to your htmlBody parameter. For example:

function sendEmails() {
  varss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
  varlr= ss.getLastRow(); 
  varlc= ss.getLastColumn();

  for (vari=2;i<=lr;i++){

      varcurrentEmail= ss.getRange(i, 1).getValue();
      varsubjectLine="Test";
      varhtmlOutput= HtmlService.createHtmlOutputFromFile('email'); 
      varemail= htmlOutput.getContent();

      //My changes start herevarindex= currentEmail.indexOf('@');
      varname= currentEmail.substr(0, index); //chars from position 0 to indexvarhead='<html><body>Dear ' + name + ': <br />';

      MailApp.sendEmail( currentEmail, "test", email, { htmlBody: head + email } ) //You can add + '</body></html>' at the end if it's necessary.


    }      
}  

Solution 2:

What about adding a placeholder in the email body and replacing it with the name you want to use?

For example, the HTML email body template could be something like:

<!DOCTYPE html><html><head><basetarget="_top"></head><body><h1>Dear {0},</h1><p>Let's try Apps Script!</p></body></html>

Then changing your original script a bit:

function sendEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
  var lr = ss.getLastRow(); 

  for (var i=2; i<=lr; i++){

    var userEmail = ss.getRange('A'+i).getValue();
    var userName = ss.getRange('B'+i).getValue();
    var subject = 'Test Personalized Mass Email';
    var htmlOutput = HtmlService.createHtmlOutputFromFile('SampleEmail'); 
    var email = htmlOutput.getContent();
    email = email.replace('{0}', userName);

    MailApp.sendEmail(userEmail, subject, email, { htmlBody: email });

    }  
}

Post a Comment for "Sending Html Email To Multiple Recipients Using Google Sheet And Appscript"