Email¶
The EmailConnector provides email-based communication using standard SMTP/IMAP (zero external dependencies) with an optional Gmail API backend for Google Workspace environments.
See
examples/alarm_to_workorder/for a workflow that includes anotify_technicianstep.
Prerequisites¶
SMTP/IMAP (default)¶
- SMTP server hostname and port (e.g.
smtp.gmail.com:465) - IMAP server hostname and port (e.g.
imap.gmail.com:993) — for receiving - Email credentials (username + password or app-specific password)
Gmail API (optional)¶
- A Google Cloud project with Gmail API enabled
- OAuth 2.0 credentials JSON file (Desktop app type)
- Install the Gmail extra:
pip install machina-ai[gmail]
Installation¶
# SMTP/IMAP — no extra dependencies (uses Python stdlib)
pip install machina-ai
# Gmail API backend
pip install machina-ai[gmail]
Configuration¶
Capabilities¶
| Capability | Method | Description |
|---|---|---|
send_message |
send_message(to, text, subject=...) |
Send an email |
receive_message |
listen(handler) |
Poll IMAP inbox for new messages |
Usage with Agent¶
from machina import Agent
from machina.connectors import Email
email = Email(
smtp_host="smtp.example.com",
imap_host="imap.example.com",
username="agent@example.com",
password="${EMAIL_PASSWORD}",
)
agent = Agent(
llm="openai:gpt-4o",
connectors=[email],
)
await agent.start()
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
smtp_host |
str |
"" |
SMTP server hostname |
smtp_port |
int |
465 |
SMTP server port |
imap_host |
str |
"" |
IMAP server hostname (for receiving) |
imap_port |
int |
993 |
IMAP server port |
username |
str |
"" |
Email account username |
password |
str |
"" |
Email account password |
use_tls |
bool |
True |
Use TLS/SSL (True=SMTP_SSL, False=STARTTLS) |
from_address |
str |
username | Sender address for outgoing mail |
gmail_credentials_file |
str \| None |
None |
Path to Gmail OAuth credentials (activates Gmail API mode) |
poll_interval |
int |
30 |
Seconds between inbox polls |
Known Limitations¶
- HTML email rendering is not supported (plain text only).
- Attachments (e.g. PDF work orders) are not yet supported.
- Gmail API mode requires a one-time OAuth browser flow on first use.
- IMAP polling is not real-time — use
poll_intervalto control frequency.
API Reference¶
EmailConnector ¶
EmailConnector(*, smtp_host: str = '', smtp_port: int = 465, imap_host: str = '', imap_port: int = 993, username: str = '', password: str = '', use_tls: bool = True, from_address: str = '', gmail_credentials_file: str | None = None, poll_interval: int = 30)
Connector for email via SMTP/IMAP or Gmail API.
Uses the Python standard library (smtplib + imaplib) by
default — no extra dependencies required. For Google Workspace
environments, pass gmail_credentials_file to use the Gmail API
backend instead (requires pip install machina-ai[gmail]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smtp_host
|
str
|
SMTP server hostname. |
''
|
smtp_port
|
int
|
SMTP server port (default 465 for SSL). |
465
|
imap_host
|
str
|
IMAP server hostname for receiving mail. |
''
|
imap_port
|
int
|
IMAP server port (default 993 for SSL). |
993
|
username
|
str
|
Email account username (usually the email address). |
''
|
password
|
str
|
Email account password or app-specific password. |
''
|
use_tls
|
bool
|
Whether to use TLS/SSL (default |
True
|
from_address
|
str
|
The |
''
|
gmail_credentials_file
|
str | None
|
Path to a Gmail API OAuth credentials JSON file. When set, SMTP/IMAP are ignored and the Gmail API is used instead. |
None
|
poll_interval
|
int
|
Seconds between IMAP inbox polls (default 30). |
30
|
Example
send_message
async
¶
Send an email message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to
|
str
|
Recipient email address. |
required |
text
|
str
|
Message body (plain text). |
required |
subject
|
str
|
Email subject line. |
'Machina Notification'
|
Raises:
| Type | Description |
|---|---|
ConnectorError
|
If not connected or sending fails. |
listen
async
¶
Poll the IMAP inbox for new messages and dispatch to handler.
For Gmail API mode, polls using the Gmail API users.messages.list
endpoint. For SMTP/IMAP mode, connects to the IMAP server and
polls for UNSEEN messages.
This is a blocking call that polls until cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
MessageHandler
|
Async callback that receives an :class: |
required |