Advanced Gmail integration with powerful automation features.
bash
export GMAIL_CREDENTIALS_PATH="/path/to/credentials.json"
export GMAIL_TOKEN_PATH="/path/to/tokens.json"
Or place credentials in default locations:
~/.credentials/gmail-credentials.json~/.credentials/gmail-token.jsonpython
from gmail_enhanced import GmailClient
gmail = GmailClient()
# Simple email
gmail.send(
to="recipient@example.com",
subject="Hello",
body="Email content"
)
# With attachment
gmail.send(
to="recipient@example.com",
subject="Report",
body="Please find the report attached",
attachments=["report.pdf"]
)
python
# Complex queries
results = gmail.search(
query="from:boss@company.com",
label="INBOX",
after="2024/01/01",
has_attachments=True
)
# Search with OR
results = gmail.search_or([
"subject:urgent",
"label:important"
])
python
# Create label
label = gmail.create_label("Projects/Work/Q1", color="#4A90E2")
# Get label stats
stats = gmail.get_label_stats("INBOX")
# Apply labels
gmail.add_labels(["Label1", "Label2"], message_ids)
python
# Download attachments from search results
attachments = gmail.search_attachments(
query="subject:invoice",
save_dir="./downloads"
)
# Upload attachment
gmail.send(
to="recipient@example.com",
subject="File",
attachments=["/path/to/file.pdf"]
)
python
# Create rule
gmail.add_rule(
name="Categorize invoices",
query="subject:invoice has:attachment",
add_labels=["Processed/Invoices"]
)
# Run rules
gmail.process_rules()
MethodDescriptionsend(to, subject, body, attachments, cc, bcc)Send emailsearch(query, max_results, label)Search emailsget_message(msg_id, format)Get email detailsdelete_message(msg_id)Move to trasharchive_message(msg_id)Archive email
MethodDescriptioncreate_label(name, color)Create labelrename_label(old_name, new_name)Rename labeldelete_label(name)Delete labelget_labels()List all labelsget_label_stats(label)Get label statistics
MethodDescriptiondownload_attachment(msg_id, attachment_id, save_path)Download attachmentsearch_attachments(query, save_dir)Search and downloadget_attachment_info(msg_id)List attachments
MethodDescriptionadd_rule(name, query, actions)Create processing ruleprocess_rules()Run all rulescreate_template(name, subject, body)Create email templatesend_template(template_name, to, variables)Send using template
MethodDescriptionparse_email(msg_id)Extract structured dataextract_invoice(msg_id)Parse invoice dataextract_contacts(msg_id)Extract email addresses
text
Basic:
from:user@example.com
to:user@example.com
subject:keyword
"exact phrase"
Filters:
after:2024/01/01
before:2024/12/31
older_than:7d
newer_than:2h
Flags:
has:attachment
has:drive
is:unread
is:starred
is:important
Labels:
label:INBOX
label:Work
Combinations:
from:boss AND subject:urgent
(from:alice OR from:bob) AND is:unread
Common errors:
invalid_credentials: Re-authenticaterate_limit: Wait and retrynot_found: Message ID invalidpermission_denied: Check scopestext
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.labels
https://www.googleapis.com/auth/gmail.modify