Skip to content

EmailService

Generic IMAP/SMTP email via OAuth2. Prefer OutlookService for Microsoft 365 mailboxes over Graph when you want Graph API instead of IMAP/SMTP.

Credentials

CredentialWhere to get it
email_addressThe mailbox address you will send/receive as (often via EMAIL_ADDRESS)
OAuth2 tokensProvider-specific OAuth app (for example Google Cloud Console for Gmail, or Microsoft Entra app registration for Outlook/Exchange IMAP)

Configure IMAP/SMTP host settings for your provider.

Usage

python
from elsai_cloud.email_services import EmailService, AuthType
import os

service = EmailService(
    email_address=os.getenv("EMAIL_ADDRESS"),
    auth_type=AuthType.OAUTH2,
)

Send a plain-text email:

python
service.send_email(
    to_email="recipient@example.com",
    subject="Test Email",
    body="This is a test email from the elsai Cloud Connector.",
)

Send with CC, BCC, and attachments:

python
service.send_email(
    to_email="recipient@example.com",
    subject="Email with Attachments",
    body="This email contains multiple attachments.",
    cc=["cc1@example.com"],
    bcc=["bcc1@example.com"],
    attachments=["/path/to/attachment1.pdf", "/path/to/image.png"],
)

List recent emails:

python
emails = service.list_emails(limit=5)
for mail in emails:
    print(f"[{mail['index']}] {mail['subject']} | {mail['from']} | UID={mail['uid']}")

Read a specific email and download attachments:

python
email_details = service.get_email(uid=emails[0]["uid"], download_dir="./downloads")
print(f"Subject: {email_details['subject']}")
print(f"Body: {email_details['body']}")

if email_details.get("attachments"):
    for att in email_details["attachments"]:
        print(f"Attachment saved to: {att.get('saved_to')}")

Reply to an email:

python
service.reply(
    uid=emails[0]["uid"],
    reply_text="Thank you for your email.",
)

# With attachment
service.reply(
    uid=emails[0]["uid"],
    reply_text="Please find the document attached.",
    attachments=["/path/to/document.pdf"],
)

Forward an email:

python
service.forward(
    uid=emails[0]["uid"],
    to_email="forwarded@example.com",
)

# With CC, BCC, and attachment
service.forward(
    uid=emails[0]["uid"],
    to_email="forwarded@example.com",
    cc=["manager@example.com"],
    bcc=["archive@example.com"],
    attachments=["/path/to/document.pdf"],
)

See also

Copyright © 2026 elsai foundry.