Google脚本,用于发送电子邮件草稿并标记以表格形式发送的电

时间:2017-08-24 15:58:45

标签: javascript google-sheets

这是我的第一篇文章,我是谷歌脚本和新手Javascript-er的新手。我正在努力解决我们房地产CRM的问题。我想做的是将我们的生日快乐草稿电子邮件(标记为“生日快乐”)从我们公司发送给我们每个客户的生日。我在C栏上有他们的生日,而D栏有一个公式来测试今天是否是他们的生日(TRUE / FALSE)。我将每天运行以下脚本。该脚本整体工作,并成功将电子邮件发送到B列中的电子邮件地址,主题位于A列;但是,我无法弄清楚如何在E列中为每个发送的生日电子邮件自动输入通知(带有日期)。

以下是我的Google工作表供参考:https://docs.google.com/spreadsheets/d/1iiY-3tZI1rGsND-3PhoN_g8hPPEZ5dlXQ4zOyyla1Tk/edit?usp=sharing

以下是我的代码,目前无法正常使用电子邮件通知:

function sendBirthdayEmails() {

  var threads = GmailApp.search('in:draft label:Happy-Birthday');

  if(threads.length == 0){
    MailApp.sendEmail("sam@masonrealestate.net", "Happy Birthday email ERROR!!", "There was an error sending the automatic Mason Real Estate Happy Birthday Email. No Draft was found with the label 'Happy-Birthday'. ERROR No. 1");
    return;
  }

  var spreadsheet = SpreadsheetApp.openById("1iiY-3tZI1rGsND-3PhoN_g8hPPEZ5dlXQ4zOyyla1Tk");  //*******NEED TO UPDATE FOR CRM - Data Sheet********* 
  var sheet = spreadsheet.getSheetByName('Birthday Emails')

  var range = sheet.getRange("J1"); 
  var dateString = new Date().toString();
  range.setValue(dateString);   // this makes all formulas recalculate

  var startRow = 2;  // First row of data to process
  var numRows = 1000;   // Number of rows to process
  // Fetch the range of cells A2:E1000
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();   

  var emailSentNotification = "Email Sent" & dateString;

  for (i in data) {
    var row = data[i];
    if( row[3] == true) {
      var emailAddress = row[1];  // Second column for email addresses
      var subject = row[0];       // First Column

      sheet.getRange(i+2,5).setValue(emailSentNotification);

      try {
        // finds the Happy Birthday draft email and sends it to the Client's email address and the subject includes their name.  
        for(var i = 0; i < threads.length; i++){
            var messages = threads[i].getMessages();
            for(var j = 0; j < messages.length; j++){
              var mssg = messages[j];
              if(mssg.isDraft()){
                mssg.forward(emailAddress, {
                  // cc: "",
                  bcc: "sam@masonrealestate.net",
                  subject: subject,
                  name: "Mason Real Estate Company"
                });
              }
            }
          //threads[i].removeLabel(labelHappy-Birthday);
        }

      } catch(errorDetails) {
        MailApp.sendEmail("sam@masonrealestate.net", "Happy Birthday email ERROR!!", "There was an error sending the automatic Mason Real Estate Happy Birthday Email. ERROR No. 2");
      }
    }
  }
}

0 个答案:

没有答案