Read, search, and send emails via EWS (Exchange Web Services) for Microsoft Exchange On-Premise.
Uses HTTPS (port 443) + NTLM authentication — works even when IMAP/SMTP ports are firewalled.
Config is stored at ~/.config/ews-exchange/.env.
pip install requests requests_ntlm
# Required
EWS_URL=https://mail.your-company.com/EWS/Exchange.asmx
EWS_DOMAIN=YOUR_AD_DOMAIN
EWS_USER=you@your-company.com
EWS_PASS=your_password
EWS_FROM=you@your-company.com
# Optional
EWS_VERIFY_SSL=false # Set to false for self-signed certs (default: false)
Important for Exchange On-Premise:
EWS_DOMAIN is your Active Directory domain (e.g. UCH), NOT the email domainEWS_VERIFY_SSL=falsehttps://autodiscover.your-company.com/Autodiscover/Autodiscover.xmlAdd prefixed variables for additional accounts:
# Work account (WORK_ prefix)
WORK_EWS_URL=https://mail.company.com/EWS/Exchange.asmx
WORK_EWS_DOMAIN=COMPANY
WORK_EWS_USER=me@company.com
WORK_EWS_PASS=password
WORK_EWS_FROM=me@company.com
Use --account before any command:
python scripts/ews.py --account work check
Test EWS connection and authentication.
python scripts/ews.py [--account <name>] test
Check latest emails.
python scripts/ews.py [--account <name>] check [--limit 10] [--folder inbox] [--recent 2h] [--unread]
Options:
--limit : Max results (default: 10)--folder : Mailbox folder (default: inbox)--recent : Only show emails from last X time (e.g. 30m, 2h, 7d)--unread: Only show unread emailsFetch full email content by index number (from check output) or UID.
python scripts/ews.py [--account <name>] fetch <index_or_uid> [--folder inbox]
Options:
--folder : Mailbox (default: inbox)Send email via EWS.
python scripts/ews.py [--account <name>] send --to <email> --subject <text> [options]
Required:
--to : Recipient (comma-separated for multiple)--subject : Email subjectOptional:
--body : Plain text body--html: Send body as HTML--body-file : Read body from file--cc : CC recipients--bcc : BCC recipients--attach : Attachments (comma-separated)--from : Override default senderExamples:
# Simple text email
python scripts/ews.py send --to recipient@example.com --subject "Hello" --body "World"
# HTML email
python scripts/ews.py send --to recipient@example.com --subject "Newsletter" --html --body "<h1>Welcome</h1>"
# Email with attachment
python scripts/ews.py send --to recipient@example.com --subject "Report" --body "Please find attached" --attach report.pdf
Search emails with filters.
python scripts/ews.py [--account <name>] search [options]
Options:
--unseen: Only unread messages--from : From address contains--subject : Subject contains--recent : From last X time (e.g. 30m, 2h, 7d)--limit : Max results (default: 20)--folder : Mailbox to search (default: inbox)List all mailbox folders.
python scripts/ews.py [--account <name>] folders
List all configured email accounts.
python scripts/ews.py list-accounts
| Folder ID | Name |
|---|---|
| ----------- | ------ |
| inbox | Inbox |
| sentitems | Sent Items |
| drafts | Drafts |
| deleteditems | Deleted Items |
| junkemail | Junk Email |
| outbox | Outbox |
| archiveinbox | Archive Inbox |
Authentication failed (401):
EWS_DOMAIN is your AD domain (e.g. UCH), not email domainuser@domain.com or just usernameHTTP 500 / Invalid Request:
RequestServerVersionSSL/TLS errors:
EWS_VERIFY_SSL=false for self-signed certificates (common on-prem)Connection timeout:
telnet mail.company.com 443共 1 个版本