如何通过别名发送带有附件的批量 gmail 电子邮件

How to send bulk gmail emails with attachments via aliases

我想使用电子表格发送带有唯一附件的批量 google 邮件。在这个 spreadsheet 中,我已经放置了电子邮件地址、内容模板和相应的附件 url。我有下面的代码,它帮助我从我的收件箱成功地发送了带有独特附件的电子邮件。

但是,我想更改此代码,以便从我已经在 Gmail 设置中设置的别名(例如:alias@gmail.com)发送电子邮件,请告诉我我应该对我的 gmail 收件箱设置做哪些更改才能从别名发送电子邮件。如果有人能告诉我如何更改下面的代码来执行此操作,我将不胜感激。提前致谢!

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var numRows = 3;
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[2];
    var subject = "Attachments";
    var message = row[4];
    var fileId = row[3].split("/")[5];
    var emailSent = row[0];
    try {
      var file = DriveApp.getFileById(fileId);
      if (emailSent != EMAIL_SENT) {
        GmailApp.sendEmail(emailAddress, subject, message, { attachments: [file.getBlob()] });
        sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
        // SpreadsheetApp.flush(); // I think that this might not be required to be used.
      }
    } catch(e) {
      console.log("File of " + row[3] + "cannot be retrieved. Error message is " + e.message);
    }
  }
}

GmailApp 在文档中对此提供支持

sendEmail函数:

https://developers.google.com/apps-script/reference/gmail/gmail-app#sendemailrecipient,-subject,-body,-options

getAliases函数:

https://developers.google.com/apps-script/reference/gmail/gmail-app#getaliases

您需要确保您拥有 GmailApp 可以读取的别名,因此请尝试上面的这个片段 link 以查看您拥有的别名:

// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
  GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
  GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}

您需要准确选择这些值之一才能使用 {'from': alias} 参数

修改脚本

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var numRows = 3;
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[2];
    var subject = "Attachments";
    var message = row[4];
    var fileId = row[3].split("/")[5];
    var emailSent = row[0];
    try {
      var file = DriveApp.getFileById(fileId);
      if (emailSent != EMAIL_SENT) {
        GmailApp.sendEmail(emailAddress, subject, message, { attachments: [file.getBlob()] , 'from': 'alias@gmail.com'});
        sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
        // SpreadsheetApp.flush(); // I think that this might not be required to be used.
      }
    } catch(e) {
      console.log("File of " + row[3] + "cannot be retrieved. Error message is " + e.message);
    }
  }
}

参考资料