New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

gmail-reader

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gmail-reader

A Gmail reader package for Google Workspace accounts with domain-wide delegation support

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Workspace Gmail Reader

A TypeScript library for reading emails from Google Workspace (formerly G Suite) Gmail accounts. Supports both OAuth2 authentication and domain-wide delegation with service accounts.

Features

  • 🔒 Secure authentication with OAuth2 and domain-wide delegation
  • 📧 Read emails with full content, headers, and attachments
  • 🔍 Advanced search and filtering capabilities
  • 📎 Download email attachments
  • 📝 Comprehensive TypeScript types
  • ✅ Production-ready with error handling
  • 🧪 Well-tested with Jest

Installation

npm install workspace-gmail-reader

Prerequisites

For OAuth2 Authentication

  • Create a Google Cloud Project
  • Enable the Gmail API
  • Create OAuth2 credentials (Client ID and Client Secret)
  • Set up OAuth consent screen
  • Obtain a refresh token

For Domain-Wide Delegation

  • Create a Google Cloud Project
  • Enable the Gmail API
  • Create a service account
  • Download the service account key file
  • Enable domain-wide delegation in your Google Workspace admin console
  • Grant necessary OAuth scopes to the service account

Usage

OAuth2 Authentication

import { createGmailClient, EmailQueryOptions } from 'workspace-gmail-reader';

// Create client configuration
const config = {
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
  redirectUri: 'your-redirect-uri' // Optional
};

// Create Gmail client
const gmailClient = createGmailClient(config, 'your-refresh-token');

// Authenticate and read emails
async function readEmails() {
  await gmailClient.authenticate();

  const options: EmailQueryOptions = {
    maxResults: 10,
    labelIds: ['INBOX'],
    includeSpamTrash: false
  };

  const result = await gmailClient.getEmailsFrom('sender@example.com', options);
  console.log(result.emails);
}

Domain-Wide Delegation

import { createDelegatedGmailClient } from 'workspace-gmail-reader';

// Create client configuration
const config = {
  clientId: 'your-service-account-email',
  clientSecret: '' // Not needed for domain-wide delegation
};

// Create Gmail client with domain-wide delegation
const gmailClient = createDelegatedGmailClient(
  config,
  'your-service-account-key-file-content',
  'user-to-impersonate@your-domain.com',
  ['https://www.googleapis.com/auth/gmail.readonly'] // Optional scopes
);

// Authenticate and read emails
async function readEmails() {
  await gmailClient.authenticate();
  const result = await gmailClient.searchEmails('has:attachment');
  console.log(result.emails);
}

Working with Attachments

// Get email with attachments
const email = await gmailClient.getEmail('email-id');

if (email.attachments) {
  for (const attachment of email.attachments) {
    const data = await gmailClient.getAttachment(email.id, attachment.attachmentId);
    // data is a Buffer containing the attachment content
    fs.writeFileSync(attachment.filename, data);
  }
}

API Reference

Classes

GmailClient

The main class for interacting with Gmail.

Factory Functions

createGmailClient(config, refreshToken?, options?)

Creates a new Gmail client for OAuth2 authentication.

createDelegatedGmailClient(config, keyFile, impersonateEmail, scopes?)

Creates a new Gmail client with domain-wide delegation.

Interfaces

GmailClientConfig

interface GmailClientConfig {
  clientId: string;
  clientSecret: string;
  redirectUri?: string;
}

EmailQueryOptions

interface EmailQueryOptions {
  maxResults?: number;
  labelIds?: string[];
  includeSpamTrash?: boolean;
  q?: string;
  pageToken?: string;
}

Email

interface Email {
  id: string;
  threadId?: string;
  subject: string;
  from: string;
  to: string;
  date: string;
  snippet: string;
  body?: {
    plain?: string;
    html?: string;
  };
  attachments?: EmailAttachment[];
  labels?: string[];
}

Examples

Check the examples directory for complete examples:

  • readFromEmail.ts: Example using OAuth2 authentication
  • domainWideDelegation.ts: Example using domain-wide delegation

Development

# Install dependencies
npm install

# Build the package
npm run build

# Run tests
npm test

# Run examples
npm run example
npm run example:delegated

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Keywords

gmail

FAQs

Package last updated on 20 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts