# Gmail Wrapper
[![CircleCI](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master.svg?style=svg&circle-token=110f54407b50c79865fe1f9b4352e213bc68504b)](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master)
Because scrapping Gmail data doesn't have to be a [pain](https://googleapis.github.io/google-api-python-client/docs/dyn/gmail_v1.html).
## Installing
```sh
pip install gmail-wrapper
```
## Developing
Create your [venv](https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments)
```sh
virtualenv .venv
source .venv/bin/activate
```
Then you can install the dependencies
```sh
pip install -e .
```
## Testing
Simply run
```sh
make test
```
## Basic Usage
- Setup the client
```python
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
```python
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:
```python
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:
```python
message_ids = ["...", "..."]
message = client.modify_multiple_messages(message_ids, ["processed"], remove_labels=["foo"])
```
- Archive a message
```python
message.archive() # Beware that you'll need proper scopes
```
- Send message
```python
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
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/loadsmart/gmail-wrapper",
"name": "gmail-wrapper",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "gmail",
"author": "Loadsmart Inc.",
"author_email": "engineering@loadsmart.com",
"download_url": "",
"platform": null,
"description": "# Gmail Wrapper\n\n[![CircleCI](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master.svg?style=svg&circle-token=110f54407b50c79865fe1f9b4352e213bc68504b)](https://circleci.com/gh/loadsmart/gmail-wrapper/tree/master)\n\nBecause scrapping Gmail data doesn't have to be a [pain](https://googleapis.github.io/google-api-python-client/docs/dyn/gmail_v1.html).\n\n## Installing\n\n```sh\npip install gmail-wrapper\n```\n\n## Developing\n\nCreate your [venv](https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments)\n\n```sh\nvirtualenv .venv\nsource .venv/bin/activate\n```\n\nThen you can install the dependencies\n\n```sh\npip install -e .\n```\n\n## Testing\n\nSimply run\n\n```sh\nmake test\n```\n\n## Basic Usage\n\n- Setup the client\n\n```python\nfrom gmail_wrapper import GmailClient\n\nemail_account = \"john.doe@gmail.com\"\ncredentials_string = \"{...}\" # You must generate this on your Google account\nscopes = [GmailClient.SCOPE_READONLY]\nclient = GmailClient(email_account, secrets_json_string=credentials_string, scopes=scopes)\n```\n\n- Fetch messages\n\n```python\nimport sys\n\nquery = \"filename:pdf label:friends\" # Check Gmail query docs: https://support.google.com/mail/answer/7190\nmessages = client.get_messages(query=query, limit=10)\nfor message in messages:\n print(\"-- MESSAGE {} --\".format(message.id))\n print(\"SUBJECT: {}\".format(message.subject))\n print(\"DATE: {}\".format(message.date))\n for attachment in message.attachments:\n print(\"\\t-- ATTACHMENT {} --\".format(attachment.id))\n print(\"\\t\\tFILENAME: {}\".format(attachment.filename))\n print(\"\\t\\tDECODED SIZE: {}\".format(sys.getsizeof(attachment.content)))\n```\n\n- Modify message labels\n\nIf a single message:\n\n```python\nmessage_id = \"...\"\nmessage = client.get_message(message_id)\nprint(message.labels) # [\"foo\", \"bar\"]\nmessage.modify(add_labels=[\"processed\"], remove_labels=[\"foo\"]) # Beware that you'll need proper scopes\nprint(message.labels) # [\"bar\", \"processed\"]\n```\n\nIf multiple messages:\n\n```python\nmessage_ids = [\"...\", \"...\"]\nmessage = client.modify_multiple_messages(message_ids, [\"processed\"], remove_labels=[\"foo\"])\n```\n\n- Archive a message\n\n```python\nmessage.archive() # Beware that you'll need proper scopes\n```\n\n- Send message\n\n```python\ncontent = '''\n<html>\n <h1>Hey there</h1>\n <p>I am using gmail-wrapper lib!</p>\n</html>\n'''\n\nmessage = client.send(\n subject=\"You will like it\",\n html_content=content,\n to=\"thanos@loadsmart.com\",\n cc=[\"iron.man@loadsmart.com\", \"spider.man@loadsmart.com\"],\n bcc=[\"wolverine@loadsmart.com\"]\n) # Beware that you'll need proper scopes\nprint(message) # Gmail message: ABC123\n```\n\n- Reply message\n\n```python\nmessage_id = \"...\"\nmessage = client.get_message(message_id)\n\nreply = '''\nI am out for vacation, will return <strong>Jan 14</strong>.\n\nIf it is really important, you can call me, but think twice.\n'''\nresponse = message.reply(reply)\n```\n\n- Handle exceptions\n\nExceptions are part of every developer day-to-day. You may want to handle exceptions as follows:\n\n```python\nfrom gmail_wrapper.exceptions import (\n MessageNotFoundError,\n AttachmentNotFoundError,\n GmailError,\n)\n\ntry:\n do_something()\nexcept MessageNotFoundError as e:\n print(f\"There is no message! {e}\")\nexcept AttachmentNotFoundError as e:\n print(f\"There is no attachment! {e}\")\nexcept GmailError as e:\n print(f\"Google servers are burning! {e}\")\n```\n\n## Disclaimer\n\nGmail is a trademark of Google. This open-source project is **not** an official library nor has any endorsement of Google.\n",
"bugtrack_url": null,
"license": "",
"summary": "Because scrapping Gmail data doesn't have to be a pain",
"version": "2.0.0",
"project_urls": {
"Homepage": "https://github.com/loadsmart/gmail-wrapper"
},
"split_keywords": [
"gmail"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b38142a5314884f2e1b6a1fd519911656286edcb01cf00399f367028babfa45a",
"md5": "01ec469ef0ec9d315d854c725151f9b7",
"sha256": "590a74ce3a9e0217aa7f100efef2760123747c6e4152933388a5d41d512c818d"
},
"downloads": -1,
"filename": "gmail_wrapper-2.0.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "01ec469ef0ec9d315d854c725151f9b7",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 7470,
"upload_time": "2024-01-04T20:47:58",
"upload_time_iso_8601": "2024-01-04T20:47:58.643446Z",
"url": "https://files.pythonhosted.org/packages/b3/81/42a5314884f2e1b6a1fd519911656286edcb01cf00399f367028babfa45a/gmail_wrapper-2.0.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-04 20:47:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "loadsmart",
"github_project": "gmail-wrapper",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"circle": true,
"requirements": [],
"tox": true,
"lcname": "gmail-wrapper"
}