Skip to main content

Because scrapping Gmail data doesn't have to be a pain

Project description

Gmail Wrapper

CircleCI

Because scrapping Gmail data doesn't have to be a pain.

Installing

pip install gmail-wrapper

Developing

Create your venv

virtualenv .venv
source .venv/bin/activate

Then you can install the dependencies

pip install -e .

Testing

Simply run

make test

Basic Usage

  • Setup the client
from gmail_wrapper import GmailClient

email_account = "john.doe@gmail.com"
credentials_string = "{...}" # You must generate this on your Google account
scopes = [GmailClient.SCOPE_READONLY]
client = GmailClient(email_account, secrets_json_string=credentials_string, scopes=scopes)
  • Fetch messages
import sys

query = "filename:pdf label:friends" # Check Gmail query docs: https://support.google.com/mail/answer/7190
messages = client.get_messages(query=query, limit=10)
for message in messages:
    print("-- MESSAGE {} --".format(message.id))
    print("SUBJECT: {}".format(message.subject))
    print("DATE: {}".format(message.date))
    for attachment in message.attachments:
        print("\t-- ATTACHMENT {} --".format(attachment.id))
        print("\t\tFILENAME: {}".format(attachment.filename))
        print("\t\tDECODED SIZE: {}".format(sys.getsizeof(attachment.content)))
  • Modify message labels

If a single message:

message_id = "..."
message = client.get_message(message_id)
print(message.labels) # ["foo", "bar"]
message.modify(add_labels=["processed"], remove_labels=["foo"]) # Beware that you'll need proper scopes
print(message.labels) # ["bar", "processed"]

If multiple messages:

message_ids = ["...", "..."]
message = client.modify_multiple_messages(message_ids, ["processed"], remove_labels=["foo"])
  • Archive a message
message.archive() # Beware that you'll need proper scopes
  • Send message
content = '''
<html>
    <h1>Hey there</h1>
    <p>I am using gmail-wrapper lib!</p>
</html>
'''

message = client.send(
    subject="You will like it",
    html_content=content,
    to="thanos@loadsmart.com",
    cc=["iron.man@loadsmart.com", "spider.man@loadsmart.com"],
    bcc=["wolverine@loadsmart.com"]
) # Beware that you'll need proper scopes
print(message) # Gmail message: ABC123
  • Reply message
message_id = "..."
message = client.get_message(message_id)

reply = '''
I am out for vacation, will return <strong>Jan 14</strong>.

If it is really important, you can call me, but think twice.
'''
response = message.reply(reply)
  • Handle exceptions

Exceptions are part of every developer day-to-day. You may want to handle exceptions as follows:

from gmail_wrapper.exceptions import (
    MessageNotFoundError,
    AttachmentNotFoundError,
    GmailError,
)

try:
    do_something()
except MessageNotFoundError as e:
    print(f"There is no message! {e}")
except AttachmentNotFoundError as e:
    print(f"There is no attachment! {e}")
except GmailError as e:
    print(f"Google servers are burning! {e}")

Disclaimer

Gmail is a trademark of Google. This open-source project is not an official library nor has any endorsement of Google.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

gmail_wrapper-2.0.0-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gmail_wrapper-2.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for gmail_wrapper-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 590a74ce3a9e0217aa7f100efef2760123747c6e4152933388a5d41d512c818d
MD5 01ec469ef0ec9d315d854c725151f9b7
BLAKE2b-256 b38142a5314884f2e1b6a1fd519911656286edcb01cf00399f367028babfa45a

See more details on using hashes here.

Supported by

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