npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gmail-getter

v2.2.0

Published

A simple tool that gets emails from the Gmail API

Downloads

17,535

Readme

gmail-getter

About package

A simple Node.js client that gets messages from Gmail API via http requests & OAuth.

Contributions are very welcome!

Installation

npm install gmail-getter

Requirements

To log into Gmail API you need to obtain credentials: Client ID, Client Secret, Refresh token. These credentails will let you get an Access token, which is required in further requests (like getting list of emails or a single email and etc.).

Steps to go:

  1. Create a project in Google Cloud Console.
  2. Create OAuth credentials in API & Services section (preferably, select Desktop app there if you need it for automated tests) and then download it.
  3. Enable Gmail API.
  4. Obtain a Refresh token.

To get a Refresh token - simply execute a command in a project root:

npx gmail-getter get-refresh-token

(or anywhere else if you've got the package installed globally)

get-refresh-token

You must put credentials.json file (p.2) in a place where you execute the command.

[!WARNING]
Credentials file name is name-sensitive - rename it to credentials.json if it's named different.

[!IMPORTANT]
Refresh token expires after some time, especially if you don't use it for a while. Keep that in mind, since you'd need to generate a new one from time to time.

Usage

Import

Import whatever you need from the package:

// ES6+
import {getAccessToken, checkInbox} from 'gmail-getter'

// CommonJS
const {getAccessToken, checkInbox} = require('gmail-getter')

or the whole package:

// ES6+
import gmail from 'gmail-getter'

// CommonJS
const gmail = require('gmail-getter')

Get Access token

You need to get an Access token before you execute other requests:

import {getAccessToken} from 'gmail-getter'

const accessToken = await getAccessToken(
  'your-client-id',
  'your-client-secret', 
  'your-refresh-token'
)

It'd probably be a good idea to get it on the earlier stages of a test run (global setup?) and store it as an environment variable.

Check inbox

Use a polling function that returns an email:

const email = await checkInbox({
  token: 'your-access-token', 
  query: 'from:your-email-username subject:Your-Email-Subject'
})

Options available:

  • token is an access token
  • timeout (optional) sets maximum execution time for the function
  • step (optional) sets timeout between retries to fetch an email
  • all (optional) says whether to find a single email or all mathing the query criteria
  • query (optional) lets you filter out emails you want to find, you can find more about queries there
  • logs (optional) turns on or off polling logs of attempts to get emails

Parse HTML from email

If your email contains html content - you can parse it and even render it in the browser with a browser automation tool (like Playwright):

const html = parseHtmlFromEmail(email)
  • email is an email that you fetch with checkInbox function

If you're maintaining an automated test - this might be the best option, as you can verify contents of the email and click any links inside of it.

Complete flow example

And that's how you can get an email you need. It'd probably be good to wrap the logic into a method and then call it from wherever you need (tests?).

import {getAccessToken, checkInbox} from 'gmail-getter'

const accessToken = await getAccessToken(
  'your-client-id', 
  'your-client-secret', 
  'your-refresh-token'
)

const email = await checkInbox({
  token: accessToken,
  query: 'your-query'
})