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 🙏

© 2026 – Pkg Stats / Ryan Hefner

gmail-archive

v0.0.2

Published

A simple CLI tool for saving emails matching a GMail query in maildir format.

Downloads

53

Readme

Save a GMail query as a local maildir

Overview

A simple CLI tool to save a GMail query as a local maildir.

There are two main use-cases:

  1. Archive a conversation for offline access or backup.
  2. Perform incremental backups of an open-ended query (e.g. all non-inbox emails)

Gmail-archive uses Google Auth 2.0 for authentication. On the first run, it will open your browser and ask for permission to give your gmail-archive instance read-only access to to your Gmail account. Access and refresh tokens are stored on disk, so this step isn't required on subsequent runs.

Install

npm install -g gmail-archive

This will make the gmail-archive command available on your PATH. Use gmail-archive --help to see the available actions and options.

Usage

Archive a conversation

For example, let's say you want to archive the thread with funny gifs in April 2017 under the ~/mail/gifs2017 directory.

gmail-archive init \
  --dir ~/mail/gifs2017 \
  --query "has:attachment subject:gif" \
  --before 2017-05-01 \
  --after 2017-04-01 \
  --client-id <google_client_id> \
  --client-secret <google_client_secret>

See below for how to obtain Google API credentials. This will open an OAUTH window in your default browser and cache your OAUTH tokens to disk

Then sync the maildir:

gmail-archive fetch --dir ~/mail/gifs2017 --verbose

The sync uses only atomic writes to the maildir and can be safely interrupted and resumed at any time. Subsequent calls to gmail-archive fetch will only download messages that haven't already been downloaded.

Incremental backups of the GMail's "All Mail" folder

Let's use the gmail-archive cli to create a monthly maildirs that syncs all archived (i.e., non-inbox emails) from your Gmail. This will result in, e.g., the following maildir hierarchy.

<root>/
  2014.01/
    cur/
    new/
    tmp/
  2014.02/
    cur/
    new/
    tmp/
  ...
  2018.02/
    cur/
    new/
    tmp/p

First configure the archive.

gmail-archive init \
  --dir ~/mail/archive \
  --query "-label:inbox" \
  --period month \
  --client-id <google_client_id> \
  --client-secret <google_client_secret>

We are now ready to sync the archive. The first run will fetch all matching messages, and subsequent runs will efficiently fetch only the new messages.

gmail-archive fetch --dir ~/mail/archive --verbose

Obtaining Google API credentials

You need to register gcal-to-org as an application yourself to obtain a Client ID and Client secret.

Go to the Google API Manager and create a new project under any name.

Within that project, enable the "GMail" API. There should be a searchbox where you can just enter those terms.

In the sidebar, select "Credentials" and then create a new "OAuth Client ID". The application type is "Other".

You’ll be prompted to create a OAuth consent screen first. Fill out that form however you like.

Finally you should have a Client ID and a Client secret. Provide these in the config below.

Notes

This tool is in the early stages and addresses my particular needs. However, it should be easy to adapt it to your use-case, and I would be happy to iterate on the design.

The fetch command's --concurrency option controls how many emails to download in parallel. The default value, 10, provides an order-of-magnitude speedup over IMAP sync tools like offlineimap.

Due to limitations of Gmail's API, entire emails must be stored in memory before writing them to disk. Large attachments can therefore cause the Node VM to run out of memory. Should you experience this, consider increasing the size of Node's old space.

export NODE_OPTIONS=--max_old_space_size=4096
gmail-archive [OPTIONS]

License

Copyright © 2017-2018 John Whitbeck

Distributed under the Apache License Version 2.0.