整理所有'unread = true'的邮件

时间:2014-06-13 08:20:13

标签: javascript google-apps-script

您好我需要整理未在Gmail中读取的邮件。但是当我添加' isUnread'时,我无法提取正文,发件人和附件属性。

 var firstThread = GmailApp.getInboxThreads(0,1)[0];
 var unread = firstThread.isUnread();
 var message = unread.getMessages()[0];

如果我这样做它可行,但我没有得到未读消息。

var firstThread = GmailApp.getInboxThreads(0,1)[0];
   var message = firstThread.getMessages()[0];

任何提示?

3 个答案:

答案 0 :(得分:1)

也许只是一个有序的问题,你可以试试

var firstThread = GmailApp.getInboxThreads(0,1)[0];
 var messages = firstThread.getMessages();
 var unread = [];
 for (var i=0; i<messages.length; i++){
  if (messages[i].isUnread()) unread.push(messages[i]);
 }

答案 1 :(得分:0)

sailens方法按预期工作,搜索也是获得您想要的简单方法。 以下2个小函数演示:

function getUnreadMessages() {
 var unreadThreads = GmailApp.getInboxThreads(0,100);
  if(unreadThreads.length==0){Logger.log('no unread messages');return};
  var unreads = [];unreads.push('\n\n');
  for(var n in unreadThreads){
    if(unreadThreads[n].isUnread()){unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n')};
  }

  Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}


function searchUnreads() {
  var unreadThreads = GmailApp.search('label:unread');
  if(unreadThreads.length==0){Logger.log('no unread messages');return};
  var unreads = [];unreads.push('\n\n');
  for(var n in unreadThreads){
    unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n');
  }
  Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}

更新: 从线程中的每条消息中获取一些细节的版本,构建另一个易于使用的函数

function getUnreadMessages() {
 var unreadThreads = GmailApp.getInboxThreads(0,100);
  if(unreadThreads.length==0){Logger.log('no unread messages');return};
  var unreads = [];unreads.push('\n\n');
  for(var n in unreadThreads){
    if(unreadThreads[n].isUnread()){unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessages()[n].getDate()
                                                 .getMessageCount()+' message(s)\n');getDetails(unreadThreads[n])};
  }

  Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}


function searchUnreads() {
  var unreadThreads = GmailApp.search('label:unread');
  if(unreadThreads.length==0){Logger.log('no unread messages');return};
  var unreads = [];unreads.push('\n\n');
  for(var n in unreadThreads){
    unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n');
  }
  Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}
function getDetails(thread){
  for(var n=0;n<thread.getMessageCount();n++){
    Logger.log(thread[n].getMessages()[n].getBody()+' on '+thread[n].getMessages()[n].getDate())
    Logger.log(thread[n].getMessages()[n].getAttachments());
  }
}

答案 2 :(得分:0)

我得到了它的工作和简单,因为它可能是:

function myFunction() {

  var firstThread = GmailApp.getInboxThreads(0,1)[0];
 var message = firstThread.getMessages()[0];
  var unread = message.isUnread();


  var sender = message.getFrom();
  var body = "Detta mail är ursprungligen skickat från" + " " + sender + '\n' + message.getBody();
  var subject = message.getSubject();
  var attachment = message.getAttachments();
    if (unread == true){

    GmailApp.sendEmail("user.name@compay.com", subject, "",  {htmlBody: body, attachments: attachment});
  }