使用Google表格和AppScript将HTML电子邮件发送给多个收件人 [英] Sending HTML email to multiple recipients using Google Sheet and AppScript

查看:181
本文介绍了使用Google表格和AppScript将HTML电子邮件发送给多个收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本,可以帮助我将批量HTML电子邮件发送给多个收件人. 在我的Google表格文件中,列1包含电子邮件地址,列2包含收件人的姓名. 我要实现并遇到的另一件事是,我想用收件人的姓名填充每封电子邮件.例如,我正在将电子邮件发送到xxx@domain.com,我希望该电子邮件以尊敬的xxx"开头,以此类推,以此类推.

这怎么办?

我的代码如下:

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

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

      var currentEmail = ss.getRange(i, 1).getValue();
      var subjectLine = "Test";
      var htmlOutput = HtmlService.createHtmlOutputFromFile('email'); 
      var email = htmlOutput.getContent();


      MailApp.sendEmail( currentEmail, "test", email, { htmlBody: email } ) 


    }      
}  

谢谢

解决方案

在电子邮件正文中添加占位符并将其替换为您要使用的名称怎么办?

例如,HTML电子邮件正文模板可能类似于:

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

然后稍微更改原始脚本:

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 });

    }  
}

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. The one more thing i am trying to achieve and got stuck at is that i want to populate each email with the name of the recipient. For example , i am sending email to xxx@domain.com , i would like the email to start with "Dear xxx" and so on for all recipients.

How can this be done?

My code is as follows:

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

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

      var currentEmail = ss.getRange(i, 1).getValue();
      var subjectLine = "Test";
      var htmlOutput = HtmlService.createHtmlOutputFromFile('email'); 
      var email = htmlOutput.getContent();


      MailApp.sendEmail( currentEmail, "test", email, { htmlBody: email } ) 


    }      
}  

Thank you

解决方案

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>
   <base target="_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 });

    }  
}

这篇关于使用Google表格和AppScript将HTML电子邮件发送给多个收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆