Skip to content

Commit

Permalink
Added a new label option which allows to search for emails outside th…
Browse files Browse the repository at this point in the history
…e inbox (spam, trash etc) - Closes #65.

Added verbose message when not enough arguments were provided to init.js.
Upgraded to latest googleapis version.
  • Loading branch information
levz0r committed Sep 10, 2021
1 parent 99ac9fb commit b893abd
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 57 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Congratulations! `gmail-tester` is ready to use.
- `include_body`: boolean. Set to `true` to fetch decoded email bodies.
- `before`: Date. Filter messages received _after_ the specified date.
- `after`: Date. Filter messages received _before_ the specified date.
- `label`: String. The default label is 'INBOX', but can be changed to 'SPAM', 'TRASH' or a custom label. For a full list of built-in labels, see https://developers.google.com/gmail/api/guides/labels?hl=en

**Returns:**
An array of `email` objects with the following fields:<br>
Expand Down Expand Up @@ -123,6 +124,8 @@ _Some senders will send you `text/html` content, the others will send you `plain
- `after`: Date. Filter messages received _before_ the specified date.
- `wait_time_sec`: Integer. Interval between inbox checks (in seconds). _Default: 30 seconds_.
- `max_wait_time_sec`: Integer. Maximum wait time (in seconds). When reached and the email was not found, the script exits. _Default: 60 seconds_.
- `label`: String. The default label is 'INBOX', but can be changed to 'SPAM', 'TRASH' or a custom label. For a full list of built-in labels, see https://developers.google.com/gmail/api/guides/labels?hl=en


**Returns:**
An array of `email` objects with the following fields:<br>
Expand Down
3 changes: 2 additions & 1 deletion gmail-tester.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module "gmail-tester" {
after?: Date;
wait_time_sec?: number;
max_wait_time_sec?: number;
label: string;
}

export interface GetMessagesOptions {
Expand Down Expand Up @@ -46,4 +47,4 @@ declare module "gmail-tester" {
credentials_json: string,
token_path: string
): Promise<void>;
}
}
7 changes: 5 additions & 2 deletions gmail-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async function _get_recent_email(credentials_json, token_path, options = {}) {
const gmail_emails = await gmail.get_recent_email(
gmail_client,
oAuth2Client,
query
query,
options.label
);
for (const gmail_email of gmail_emails) {
const email = {
Expand Down Expand Up @@ -146,6 +147,7 @@ async function __check_inbox(credentials_json, token_path, options = {}) {
* @param {Date} [options.after] - Date. Filter messages received _before_ the specified date.
* @param {number} [options.wait_time_sec] - Interval between inbox checks (in seconds). Default: 30 seconds.
* @param {number} [options.max_wait_time_sec] - Maximum wait time (in seconds). When reached and the email was not found, the script exits. Default: 60 seconds.
* @param {string} [options.label] - String. The default label is 'INBOX', but can be changed to 'SPAM', 'TRASH' or a custom label. For a full list of built-in labels, see https://developers.google.com/gmail/api/guides/labels?hl=en
*/
async function check_inbox(
credentials_json,
Expand All @@ -156,7 +158,8 @@ async function check_inbox(
to: undefined,
wait_time_sec: 30,
max_wait_time_sec: 30,
include_body: false
include_body: false,
label: "INBOX"
}
) {
if (typeof options !== "object") {
Expand Down
14 changes: 7 additions & 7 deletions gmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function get_new_token(oAuth2Client, token_path) {
return new Promise((resolve, reject) => {
rl.question("Enter the code from that page here: ", async code => {
rl.close();
oAuth2Client.getToken(code, function(err, token) {
oAuth2Client.getToken(code, function (err, token) {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -82,7 +82,7 @@ async function list_labels(gmail, oauth2Client) {
userId: "me",
auth: oauth2Client
},
function(err, res) {
function (err, res) {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -115,7 +115,7 @@ async function list_messages(gmail, oauth2Client, query, labelIds) {
auth: oauth2Client,
labelIds: labelIds
},
async function(err, res) {
async function (err, res) {
if (err) {
reject(err);
} else {
Expand All @@ -131,7 +131,7 @@ async function list_messages(gmail, oauth2Client, query, labelIds) {
labelIds: labelIds,
pageToken: nextPageToken
},
function(err, res) {
function (err, res) {
if (err) {
reject(err);
} else {
Expand All @@ -158,10 +158,10 @@ async function list_messages(gmail, oauth2Client, query, labelIds) {
* @param {google.auth.OAuth2} oauth2Client An authorized OAuth2 client.
* @param {String} query String used to filter the Messages listed.
*/
async function get_recent_email(gmail, oauth2Client, query = "") {
async function get_recent_email(gmail, oauth2Client, query = "", label = "INBOX") {
try {
const labels = await list_labels(gmail, oauth2Client);
const inbox_label_id = [labels.find(l => l.name === "INBOX").id];
const inbox_label_id = [labels.find(l => l.name === label).id];
const messages = await list_messages(
gmail,
oauth2Client,
Expand All @@ -179,7 +179,7 @@ async function get_recent_email(gmail, oauth2Client, query = "") {
id: message.id,
format: "full"
},
function(err, res) {
function (err, res) {
if (err) {
reject(err);
} else {
Expand Down
4 changes: 4 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const gmail = require("./gmail-tester");

(async () => {
if (process.argv.length < 5) {
console.error(`Usage: init.js <path-to-credentials.json> <path-to-token.json> <target-email>`)
process.exit(1)
}
await gmail.check_inbox(process.argv[2], process.argv[3], {
subject: "",
from: "",
Expand Down
139 changes: 94 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmail-tester",
"version": "1.3.2",
"version": "1.3.3",
"description": "A simple NodeJS gmail client which checks the inbox for specific message existance",
"main": "gmail-tester.js",
"types": "gmail-tester.d.ts",
Expand All @@ -16,7 +16,7 @@
"author": "Lev Gelfenbuim",
"license": "MIT",
"dependencies": {
"googleapis": "^66.0.0"
"googleapis": "^85.0.0"
},
"homepage": "https://github.com/levz0r/gmail-tester"
}

0 comments on commit b893abd

Please sign in to comment.