Gmail Service

The Gmail service allows you to send emails, search messages, and manage your inbox programmatically.

Getting Started

python
from gspace import GSpace

gspace = GSpace.from_oauth(
    credentials_file="credentials.json",
    scopes=["gmail"]
)

gmail = gspace.gmail()

Sending Emails

Simple Email

python
gmail.send_simple_email(
    to="recipient@example.com",
    subject="Hello from Gspace",
    body="This is a test email sent using Gspace."
)

Email with Attachments

python
gmail.send_email(
    to=["user1@example.com", "user2@example.com"],
    subject="Important Documents",
    body="Please find the documents attached.",
    attachments=["/path/to/file1.pdf", "/path/to/file2.docx"],
    cc="manager@example.com",
    bcc="archive@example.com"
)

Searching Messages

python
messages = gmail.search_messages(
    query="from:important@company.com subject:urgent",
    max_results=50
)

for message in messages:
    print(message.get('subject'))

Reading Messages

python
# Get a specific message
message = gmail.get_message(message_id)

# List recent messages
messages = gmail.list_messages(max_results=10)

Managing Labels

python
# List all labels
labels = gmail.list_labels()

# Create a label
label = gmail.create_label(
    name="Important",
    label_list_visibility="labelShow"
)

# Add label to message
gmail.add_label(message_id, label_id)

Drafts

python
# Create a draft
draft = gmail.create_draft(
    to="recipient@example.com",
    subject="Draft Email",
    body="This is a draft."
)

# Send a draft
gmail.send_draft(draft_id)