Skip to content

Commit d8b5374

Browse files
committed
Temporary Email Address 🦋
1 parent 0ab7f93 commit d8b5374

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## 🗑 Disposable Email Address
2+
3+
Create a second Gmail address and turn it into a temporary and disposable email address.
4+
5+
[Temporary Gmail Address](https://www.labnol.org/internet/disposable-email-address/28072/)
6+
7+
> This software is provided "as-is," without any express or implied warranty.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"timeZone": "Asia/Kolkata",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8",
6+
"oauthScopes": [
7+
"https://www.googleapis.com/auth/gmail.send",
8+
"https://www.googleapis.com/auth/gmail.modify",
9+
"https://www.googleapis.com/auth/script.scriptapp"
10+
]
11+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)