谷歌脚本自动转发谷歌收件箱

时间:2018-05-08 18:11:19

标签: google-apps-script gmail google-inbox

我将此代码用于Gmail,但现在我希望为Google Inbox执行类似操作。问题是Google Inbox不支持标签,因此我必须提出另一种解决方案。也许以某种方式每隔5分钟检查一次容器内的电子邮件然后转发它们?还有其他建议吗?

function autoForward() {
  var label = 'ForwardThis';
  var recipient = 'domain@forward.domain.com';
  //  if the script runs every 5 minutes; change otherwise
  var interval = 5;          
  var date = new Date();
  var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
  var threads = GmailApp.search('label:' + label + ' after:' + timeFrom);
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    var from = messages[i].getFrom();
    var subject = messages[i].getSubject();
    var to = messages[i].getTo();
    var attachment = messages[i].getAttachments();
    var body = messages[i].getBody();
    for (var j = 0; j < messages.length; j++) {
      var emailoptions = (
        "---------- Forwarded message ----------" + '<br>' +
        'From: ' + from + "<'" + from.replace(/^.+<([^>]+)>$/, "$1") + "'>" + '<br>' +
        'Date: ' + date + '<br>' +
        'Subject: ' + subject + '<br>' +
        'To: ' + to + "" + '<br>' + '<br>' + '<br>'
      );
      messages[j].forward(recipient,{htmlBody: body + emailoptions, Attachment: attachment});
    }
  }
}

0 个答案:

没有答案