App Script Check G-mail

function main() {

  var mailCondsunread = ('in:anywhere -{in:drafts} -{in:sent}');
  // var mailCondsunread = ('is:unread');

  forward(mailCondsunread);
  
}

function forward(mailConds) {
  var myThreads = GmailApp.search(mailConds);

  for(var i=0; i<myThreads.length; i++){
    var myMsgs = GmailApp.getMessagesForThread(myThreads[i]);

    for (var j=0; j<myMsgs.length; j++) {
      forwardMailNotice(myMsgs[j]);
      console.log(myMsgs[j]);
    }

    myThreads[i].markRead();
  }
}

function forwardMailNotice(lineToken, mailMsg) {

       let unreadEmailMessage = "";

       const emailFrom = mailMsg.getFrom();
       unreadEmailMessage += "\n📥From : " + emailFrom;

      const emailDate = Utilities.formatDate(mailMsg.getDate(), "GMT+0700", "dd/MM/yyyy HH:mm")
       unreadEmailMessage += "\n📅From : " + emailDate;
  
       unreadEmailMessage += "\n📧To : Banchong@pcru";

       const emailSubject = mailMsg.getSubject();
       unreadEmailMessage += "\n\n🏷️Subject : " + emailSubject;


      const emailContents = mailMsg.getPlainBody().replace(/[^{][^}]*}/ig, "").replace(/(\r\n|\n|\r)+/g, "\n").replace(/( )+/g," ");

      unreadEmailMessage += "\n\n📋Contents : " + emailContents;

     if (unreadEmailMessage.length > 800) {
        unreadEmailMessage = unreadEmailMessage.slice(0, 800) + "...";
     }

      sendMessageToLineNotify(unreadEmailMessage)

    }
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Loading

Rate this post

Tags: