Created
June 30, 2014 03:55
-
-
Save usk81/df2c6c8d578fc5fd6a85 to your computer and use it in GitHub Desktop.
[GMail] ラベルのない受信メールの送信アドレスと受信回数を教えてくれる処理作った。 ref: http://qiita.com/tienlen/items/6b90932cc2cc72c7f143
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 他の処理でも使ってるものは定数にしている | |
// set your email address. | |
var YOUR_EMAIL_ADDRESS = "あなたのGmailアドレス"; | |
// string for regular expression check | |
var EMAIL_REGEX = /[a-zA-Z0-9\._\-]+@[a-zA-Z0-9\.\-]+\.[a-z\.A-Z]+/g; | |
function getNoLabelSenders() { | |
var query = 'has:nouserlabels'; | |
var threadAll = 0; | |
var offset = 0; | |
var limit = 500; | |
var mailAddress = getEmailAddress(); | |
var AddressList = {}; | |
var hasMore = true; | |
while (hasMore) { | |
var threads = GmailApp.search(query, offset, limit); | |
if (threads.length < limit) { | |
hasMore = false; | |
} | |
offset = offset + limit; | |
for (var i = 0; i < threads.length; i++) { | |
var thread = threads[i]; | |
var firstMessage = thread.getMessages()[0]; | |
var sender = firstMessage.getFrom().match(EMAIL_REGEX)[0]; | |
if (sender && sender != mailAddress) { | |
if (AddressList[sender] != undefined) { | |
AddressList[sender] += 1; | |
} else { | |
AddressList[sender] = 1; | |
} | |
} | |
// Logger.log(sender); | |
} | |
// GmailApp.searchだと1回で500件までしか取れないので、 | |
// whileで回して全件取るようにしてますが、 | |
// 数が多いとタイムアウトとかで怒られるので、 | |
// その場合は強制的にhasMore = falseにしてください。 | |
// hasMore = false; | |
} | |
// Logger.log(AddressList); | |
if (AddressList) { | |
var message = "These messages don't have label \n\n"; | |
for (key in AddressList) { | |
message = message + key + ': ' + AddressList[key] + " messages\n"; | |
} | |
GmailApp.sendEmail(mailAddress, '[Report] No label massages', message); | |
} | |
} | |
function getEmailAddress() { | |
// Sessionだと認証を求められる代わりに定数や変数で指定しなくてもOK | |
// 嫌だったら YOUR_EMAIL_ADDRESSの方を使ってください | |
return Session.getEffectiveUser().getEmail(); | |
// return YOUR_EMAIL_ADDRESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment