js-gmail-api
v1.0.1
Published
Javascript functions to make easy the integration with the gmail api from google.
Downloads
8
Maintainers
Readme
js-gmail-api
Javascript functions to make easy the integration with the gmail api from google.
Table of Contents
Installing
Using npm:
$ npm install js-gmail-api
Using yarn:
$ yarn add js-gmail-api
Import
Once the package is installed, you can import the library using import
or require
approach:
import jsGmailAPI from "js-gmail-api";
OR:
const jsGmailAPI = require("js-gmail-api");
Functions
Note: Every functions takes gmail
as the first parameter.
import { google } from "googleapis";
const auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
async function main(auth) {
const gmail = google.gmail({ version: "v1", auth });
/** ... */
}
main(auth);
getAllEmails
returns an array of all emails in your inbox, in an object containing the id of each email.
JavaScript
import { getAllEmails } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails = await getAllEmails(gmail);
emails.forEach((email) => console.log(email.id));
}
TypeScript
import { getAllEmails } from "js-gmail-api";
import type { Messages } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails: Messages = await getAllEmails(gmail);
emails.forEach((email) => console.log(email.id));
}
getUnreadEmails
has the same return as getAllEmails
but containing only unread emails.
JavaScript
import { getUnreadEmails } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails = await getUnreadEmails(gmail);
emails.forEach((email) => console.log(email.id));
}
TypeScript
import { getUnreadEmails } from "js-gmail-api";
import type { Messages, Message } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails: Messages = await getUnreadEmails(gmail);
emails.forEach((email: Message) => console.log(email.id));
}
getMessageById
returns an array of object containing .
JavaScript
import { getAllEmails } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails = await getAllEmails(gmail);
emails.forEach((email) => console.log(email.id));
}
TypeScript
import { getAllEmails } from "js-gmail-api";
import type { Messages } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails: Messages = await getAllEmails(gmail);
emails.forEach((email) => console.log(email.id));
}
getMessageById
returns an array of object containing .
JavaScript
import { getAllEmails } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails = await getAllEmails(gmail);
emails.forEach(async (email) => {
const message = await getFormattedMessageById(gmail, email.id);
console.log(message);
});
}
TypeScript
import { getAllEmails, getFormattedMessageById } from "js-gmail-api";
import type { Messages, FormattedMessage } from "js-gmail-api";
async function main(auth) {
/** ... */
const emails: Messages = await getAllEmails(gmail);
emails.forEach(async (email) => {
const message: FormattedMessage = await getFormattedMessageById(
gmail,
email.id
);
console.log(message);
});
}
Example
If you are not familiarized with the gmail api, see the gmail api quickstart to get the initialization and authentication snippet.
import {
getUserInfo,
getFormattedMessageById,
getUnreadEmails,
markAsRead,
} from "js-gmail-api";
import { google } from "googleapis";
const auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
const gmail = google.gmail({ version: "v1", auth }); // returns an object
const user = await getUserInfo(gmail); // { emailAddress, messagesTotal }
console.log(">_ You are logged as", user.emailAddress);
const unreadEmails = await getUnreadEmails(gmail); // [{ id }]
// Check if there is any unread email
if (unreadEmails && unreadEmails.length > 0) {
console.log(">_ There is no unread emails in your inbox.");
} else {
console.log(`>_ There is ${unreadEmails.length} unread emails in your inbox`);
}
// Print all unread emails and mark as read
unreadEmails.forEach((email) => {
getFormattedMessageById(gmail, email.id)
.then((res) => res.data)
.then((message) => {
console.log(message); // message: { from, body }
markAsRead(gmail, email.id);
})
.catch(console.error);
});