Skip to main content

A fastai-style Gmail API client with convenient read/write support

Project description

solvemail

A simple Gmail / Google Workspace email client built on the official Gmail API, using the fastai/fastcore coding style.

Install

pip install solvemail

Or for development:

pip install -e .

OAuth setup

For detailed instructions on setting up Google Cloud credentials, see ezgmail's excellent documentation.

In brief:

  1. Create an OAuth Client ID (Desktop app) in Google Cloud Console and enable the Gmail API
  2. Download the client secrets JSON as credentials.json
  3. Put credentials.json next to your script (or pass its path)

On first run, solvemail will open a browser to authorize and will save token.json.

Quick start

import solvemail

solvemail.init()  # reads credentials.json + token.json in cwd

# For multiple accounts, use separate token files:
# solvemail.init(token_path='work.json')    # first run opens browser to auth
# solvemail.init(token_path='personal.json') # switch account without re-auth

# Check which account you're using
solvemail.profile().email

# solvemail exports the key API functionality through wildcard import
from solvemail import *

# Search for threads
threads = search_threads('is:unread newer_than:7d', max_results=10)

# Get thread details
t = threads[0]
for m in t.msgs():
    print(m.frm, '|', m.subj)

# Read a message
m = t.msgs()[0]
m.subj, m.frm, m.snip, m.text()

# Send an email
send(to='someone@example.com', subj='Hello', body='Hi there!')

# Create and send a reply
draft = t.reply_draft(body='Thanks!')
draft.send()

Features

Searching

# Search threads (conversations)
search_threads('from:boss@company.com', max_results=20)

# Search individual messages
search_msgs('has:attachment filename:pdf', max_results=100)

Messages

m = msg(id)           # Fetch by id
m.subj, m.frm, m.to             # Headers
m.text(), m.html()              # Body content
m.mark_read(), m.mark_unread()  # Read status
m.star(), m.unstar()            # Starred
m.archive()                     # Remove from inbox
m.trash(), m.untrash()          # Trash
m.add_labels('MyLabel')         # Add labels
m.rm_labels('INBOX')            # Remove labels

Threads

t = thread(id)        # Fetch by id
t.msgs()                        # List messages
t[0], t[-1]                     # Index into messages
t.reply_draft(body='...')       # Create reply draft
t.reply(body='...')             # Send reply directly

# Batch fetch multiple threads efficiently (one HTTP call)
threads = search_threads('in:inbox', max_results=50)
threads = get_threads(threads)

Message display

Messages render nicely in Jupyter notebooks (quotes and signatures stripped automatically).

m = t[-1]
m.body()   # Cleaned text (no quotes/signatures)
m.html()   # HTML body (falls back to text wrapped in <pre>)

# View message with headers (as dict or plain text)
view_msg(m.id)                      # Returns dict with headers + body
view_msg(m.id, as_json=False)       # Returns formatted text

# View full thread
view_thread(t.id)                   # Dict of msgid -> msg dict
view_thread(t.id, as_json=False)    # Concatenated text with separators

Inbox helpers

view_inbox(max_msgs=20)             # Batch fetch inbox messages
view_inbox_threads(max_threads=20)  # Batch fetch inbox threads
view_inbox(unread=True)             # Only unread

Labels

labels()                        # List all labels
label('INBOX')                  # Get by name or id
find_labels('project')          # Search labels
create_label('My Label')        # Create new label

Drafts

drafts()                        # List drafts
create_draft(to='...', subj='...', body='...')
reply_to_thread(thread_id, body='...')

Bulk operations

# Batch modify labels (auto-chunks, no 1000 message limit)
ids = [m.id for m in search_msgs('in:inbox')]
batch_label(ids, add=['SPAM'], rm=['INBOX'])

# Trash multiple messages
trash_msgs(ids)

# Permanently delete (requires full mail scope)
batch_delete(ids)

Using the Gmail class directly

from solvemail import Gmail

g = Gmail(creds_path='credentials.json', token_path='token.json')
lbl = g.create_label('test-label')

m = g.send(to=g.profile().email, subj='hello', body='hi there')
m.add_labels(lbl)

lbl.delete()

Testing

Set these env vars to run e2e tests against a throwaway Gmail/Workspace account:

  • GMAILX_CREDS — path to credentials.json
  • GMAILX_TOKEN — path to token.json (will be created if missing)
  • GMAILX_E2E — set to 1 to enable e2e tests
pytest -q

Credits

Inspired by ezgmail by Al Sweigart — thanks Al for the great work! The ezgmail repo also has excellent documentation on setting up Gmail API credentials.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

solvemail-0.1.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

solvemail-0.1.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file solvemail-0.1.2.tar.gz.

File metadata

  • Download URL: solvemail-0.1.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for solvemail-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2a3d8ba8616dc971d87f4498cf6f4d43f4ebf1167bd49965e6810482fa4d1013
MD5 77b8d434206d654888b9a57284032432
BLAKE2b-256 d29c1094787afd77bd3ad471594a7bbee93fb43b38df7bf0f5a0389f6d1c5381

See more details on using hashes here.

File details

Details for the file solvemail-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: solvemail-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for solvemail-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc1529e28639c67b1741813da7c7635ecb057b29c036e0c0d5990812c1441939
MD5 e5ff6b00d19b99b628a0cdb0bd9c97d1
BLAKE2b-256 1cf20a8c22e74f16e3ee2ebfb530948365552fbf623c5d575220660db27ecdf6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page