1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gmail に届いたメールを1日1回Discordに送る

Posted at

毎日深夜に昨日届いたメールを全部Discordに転送する

1. Discord のウェブフックを作成する

step1.jpg
step2.jpg
step3.jpg
step4.jpg

2. Google App Scripts のプロジェクトを作成する

https://script.google.com/home
にアクセスします

step5.jpg

以下のスクリプトを入力します

function main() {
  const yesterdaysEmails = getYesterdaysEmails(EMAIL_FROM); // Discordに転送したいメールの送り元アドレス
  const webhook_url = DISCORD_WEBHOOK_URL; // DiscordのウェブフックURL
  yesterdaysEmails.forEach(email => {
    sendToDiscord(webhook_url, email.getPlainBody());
  });
}

function getYesterdaysEmails(from){
  const today = new Date();
  const yesterday = new Date();
  yesterday.setDate(today.getDate() - 1);

  const threads = GmailApp.search(`from:${from} after:${yesterday.getFullYear()}/${yesterday.getMonth() + 1}/${yesterday.getDate()} before:${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`);
  const msgs = GmailApp.getMessagesForThreads(threads);
  
  const ret = [];
  for(let i = 0; i < msgs.length; i++) {
    for(let j = 0; j < msgs[i].length; j++) {
      ret.push(msgs[i][j]);
    }
  }
  return ret;
}

function sendToDiscord(webhook_url, text) {
  const data = {
    content: text,
  };
  const res = UrlFetchApp.fetch(webhook_url, {
    method: "post",
    headers: { 'Content-Type': 'application/json' },
    payload: JSON.stringify(data),
  });
}

step6.jpg

3. 実行テストする

step7.jpg

実行テストして昨日の分のメールがDiscordに投稿されればOK

4. 1日1回実行するトリガーをセットする

スクリーンショット 2020-05-03 21.34.14.png

スクリーンショット 2020-05-03 21.35.35.png

スクリーンショット 2020-05-03 21.36.17.png

おわり

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?