![見出し画像](https://assets.st-note.com/production/uploads/images/66584753/rectangle_large_type_2_cc91f94a7e2dcc8fd7a166c38051a4e6.png?width=800)
はじめてのGAS(8)Gmailで条件を指定してメールを取得する
メール取得
Google Apps Script(GAS)を使い、Gmail内からキーワード条件で検索してメールを取得する方法を紹介します。
function getMail() {
const query = "subject:(お問い合わせ)";
const Threads = GmailApp.search(query, 0, 10);
const MessagesForThreads = GmailApp.getMessagesForThreads(Threads);
console.log("●検索クエリー: " + query);
console.log("●対象スレッド数: " + Threads.length);
for (const Thread of MessagesForThreads) {
console.log("○スレッド内のメール数:" + Thread.length);
for (const Message of Thread) {
const strDate = Utilities.formatDate(Message.getDate(), "JST", "yyyy/MM/dd HH:mm:ss")
const Subject = Message.getSubject();
const PlainBody = Message.getPlainBody();
console.log("受信日時:" + strDate);
console.log("メール件名:" + Subject);
console.log("メール本文:" + PlainBody);
console.log("==================");
}
}
}
![](https://assets.st-note.com/img/1638030201409-V5HFHx9g4R.png?width=800)
メールの検索条件
ここから先は
2,155字
/
4画像
¥ 500
この記事が気に入ったらサポートをしてみませんか?