Google Script - 转发邮件

时间:2013-07-16 13:33:22

标签: google-apps-script gmail

我可以向你征求意见吗?我有一个Google脚本可以执行此操作: 1)检查是否有任何标有星号的会话 2)如果是,请取消选择,然后将此对话转发到该地址

效果很好,但我需要轻松皮条客。而不是转发,我需要将消息发送到地址,该地址将成为报告的主题,并且发现消息正文的消息正文在链接到线程发现的消息下找到,并且锦上添花会有已在消息中引用附件。

    function TJ() {
  var threads = GmailApp.search('is:starred');
  for (var h = 0; h < threads.length; h++) {
    var messages = threads[h].getMessages();
    for (var i = 0; i < messages.length; i++) {
      if (messages[i].isStarred())
      {
        Logger.log(messages[i].getSubject());
        messages[i].unstar();
        messages[i].forward("--email-address--", {
          cc: "",
          bcc: ""
          });     
      }
  }
}
}

THX!

3 个答案:

答案 0 :(得分:3)

这很完美!

function AddSM2GTDApp() {
      var threads = GmailApp.search('is:starred');
      for (var h = 0; h < threads.length; h++) {
        var messages = threads[h].getMessages();
        for (var i = 0; i < messages.length; i++) {
          if (messages[i].isStarred())
          {
            Logger.log(messages[i].getSubject());
            var subject = messages[i].getSubject();
            Logger.log(messages[i].getBody());
            var body = messages[i].getBody();
            Logger.log(messages[i].getId());
            var id = messages[i].getId();
            var label = GmailApp.getUserLabelByName("Add2GTDApp");
            threads[h].addLabel(label);
            messages[i].unstar();
            MailApp.sendEmail({
         to: "your@email.com",
         subject: subject,
         htmlBody: "https://mail.google.com/mail/u/0/#inbox/"+id+"<br>--------------------------------------------------------------------------------------<br>"+body+"<br>--------------------------------------------------------------------------------------<br>Mail to Nirvana Script ",
       }); 
          }
      }
    }
    }

答案 1 :(得分:0)

请尝试以下代码,您可以添加5分钟时间驱动事件触发器来运行脚本

function MailForward() {

try{
var glabel = createLabel_("Mail-Forwarded");
var rtm_email = 'abc@abc.com';
var from_email = Session.getActiveUser().getEmail();
var threads =  GmailApp.search('in:inbox is:unread newer_than:1d');
var mForward = 0;

for (var i=0;i<threads.length;i++) {

  var messages=threads[i].getMessages();
  for (var m = 0; m < messages.length; m++){
    if (messages[m].isUnread()){

    mForward = 0;  
    var mlabels = threads[i].getLabels();
    for (var j = 0; j < mlabels.length; j++) {
      Logger.log(mlabels[j].getName());

      if (mlabels[j].getName() === "Mail-Forwarded") {
      mForward = 1;  
      }
    }

    if (mForward===0) {

    //   Logger.log(messages.length)
    //   Logger.log(messages[m].getFrom());
    var from = messages[m].getFrom();

    //Logger.log(messages[m].getDate());
    var date = messages[m].getDate();

    // Logger.log(messages[m].getSubject());
    var subject = messages[m].getSubject();

    //  Logger.log(messages[m].getTo());
    var to = messages[m].getTo();

    var body = messages[m].getBody();
    var attachment = messages[m].getAttachments();

    var emailoptions = ("---------- Forwarded message ----------" +'<br>'+'From: '+from+ "<'" + from.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+ 'Date: '+date+'<br>'+ 'Subject: ' +subject+'<br>'+ 
                    'To: ' +to+ "<'" + to.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+'<br>'+'<br>');

    messages[m].forward(rtm_email,{htmlBody: emailoptions + body , Attachment: attachment});

    glabel.addToThread(threads[i]);
    Logger.log(glabel.getName());
    messages[m].markRead();

    mForward = 1;

      }
    }
  }
}
} catch(e) {

 MailApp.sendEmail("abc@abc.com", "Exception found in Sript", e );
  Logger.log(e);

}
}

答案 2 :(得分:0)

function autoForwardZoom() {
  var s = "";
  var m = "";
  var c = 0;
  var a = "";
  var f = "";
  var label = 'IT/Zoom';
  var subject = 'Disconnected';
  var from = 'no-reply@domain.us';
  var threads = GmailApp.search("from:" + from + " label:" + label + " is:unread subject:(" + subject + ")", 0, 1);
  for (var i = 0; i < threads.length; i++) {
    var message = threads[i].getMessages();
    c = threads[i].getMessageCount() - 1;
    m = threads[i].getMessages()[c];
    b = m.getBody();
    s = m.getSubject();
    a = m.getAttachments();

    var returnData = [];
    var html = HtmlService.createTemplateFromFile('Temp');

    html.data = returnData;
    var template = html.evaluate().getContent();

    m.forward("youremail@gmail.com", {
      replyTo: "myemail@gmail.com",
      subject: s,
      htmlBody: template + "<br>" + b,
      attachments: a
    });
    GmailApp.markThreadsRead(threads);
  }

}

我的HTML模板,

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<div>Dear Team</div>
<div>&nbsp;</div>
<div>Please kindly check zoom controller issue</div>
<div>&nbsp;</div>
<div>Regards</div>
<div><i>please reply this email with word "SOLVED" to earn credits, once you has solved this issue</i></div>
<div>&nbsp;</div>
<div style='text-align: center;'>---------- Forwarded message from Zoom ----------</div>
  </body>
</html>