LoginSignup
1
1

More than 3 years have passed since last update.

【GAS】重要でないメールを1週間分まとめて受け取る

Posted at

なんで作ろうと思ったか

今すぐ見なくてもいい通知メールがかなりある。受信トレイが汚れるのでできれば週末に全部をまとめた一覧表をぱっと確認したくなったため。

UI

  • 今すぐ見なくていいメールをGmail側のフィルタでタグ付けかつ受信トレイスキップさせる
  • 週末にスキップされたメールの一覧メールが送られてくる メールの画像 無題.png

処理フロー

  1. 週に1度タグ付けされたメールの取得
  2. 一覧にしてメール

ソース

function combineBoringEmails() {
  //一週間の間のautoArchiveタグがついたメールの取得
  const boringEmails = GmailApp.search('label:autoArchive newer_than:7d');
  const messages = GmailApp.getMessagesForThreads(boringEmails);
  //メールの一覧作成。Html。メールのリンクはhttps://mail.google.com/mail/u/0/#all/ + そのメールのID
  const hyperLinks = messages.map(function(msg) {
    return makeHtmlHyperLink(msg[0].getSubject(), 'https://mail.google.com/mail/u/0/#all/' + msg[0].getId());
  })
  const htmlBody = makeHtmlList(hyperLinks);

  GmailApp.sendEmail('email@adress.gmail', 'メールまとめ', '', {htmlBody: htmlBody})
}

function makeHtmlHyperLink(str, url) {
  return '<a href="' + url + '">' + str + '</a>';
}

function makeHtmlList(arr) {
  return arr.reduce(function(acc, cur) {
    return acc + '<li>' + cur + '</li>';
  }, '<ol>') + '</ol>';
}

これを1週間トリガーで実行

あとがき

Gmail側のフィルタ例
from:(information OR noreply OR no-reply OR info@ OR notification)
重要なメールも混ざるので程々に。タグ付はGmail側でできるので便利。
スレッドがあるメールはうまく動かない。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1