|
| 1 | +/** |
| 2 | + * Disposable Gmail Address |
| 3 | + * ======================== |
| 4 | + * |
| 5 | + * Written by Amit Agarwal |
| 6 | + |
| 7 | + * Web: https://www.labnol.org |
| 8 | + * Twitter: @labnol |
| 9 | + * |
| 10 | + * Under MIT License |
| 11 | + */ |
| 12 | + |
| 13 | +const RECIPIENT = '[email protected]'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Run a trigger every 15 minutes that checks for |
| 17 | + * new emails in the Gmail inbox folder |
| 18 | + */ |
| 19 | +const initialize = () => { |
| 20 | + ScriptApp.getProjectTriggers().forEach((trigger) => { |
| 21 | + ScriptApp.deleteTrigger(trigger); |
| 22 | + }); |
| 23 | + ScriptApp.newTrigger('checkTemporaryInbox').timeBased().everyMinutes(5).create(); |
| 24 | +}; |
| 25 | + |
| 26 | +/** Check if an email message should be forward from the |
| 27 | + * temporary inbox to the main Gmail inbox based on the |
| 28 | + * date in the TO field of the incoming message |
| 29 | + */ |
| 30 | +const isAllowed_ = (email = '') => { |
| 31 | + const [, mm, dd, yyyy] = email.match(/\+(\d{2})(\d{2})(\d{4})?@/) || []; |
| 32 | + if (mm) { |
| 33 | + const now = new Date(); |
| 34 | + const date = new Date([yyyy || now.getFullYear(), mm, dd].join('/')); |
| 35 | + date.setHours(23); |
| 36 | + date.setMinutes(59); |
| 37 | + return date > now; |
| 38 | + } |
| 39 | + return false; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Fetch the 10 most recent threads from Gmail inbox, |
| 44 | + * parse the To field of each message and either forward it |
| 45 | + * or archive the emssage |
| 46 | + */ |
| 47 | +const checkTemporaryInbox = () => { |
| 48 | + GmailApp.getInboxThreads(0, 10).forEach((thread) => { |
| 49 | + thread.getMessages().forEach((message) => { |
| 50 | + if (isAllowed_(message.getTo())) { |
| 51 | + message.forward(RECIPIENT); |
| 52 | + } |
| 53 | + }); |
| 54 | + thread.moveToArchive(); |
| 55 | + }); |
| 56 | +}; |
0 commit comments