56 lines
1.5 KiB
Python
Executable File
56 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Exchange Web Services (EWS) Email Sender
|
|
Alternative à SMTP et Graph API
|
|
"""
|
|
|
|
import sys
|
|
try:
|
|
from exchangelib import Credentials, Account, Message, Mailbox, DELEGATE
|
|
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
|
|
HAS_EWS = True
|
|
except ImportError:
|
|
HAS_EWS = False
|
|
|
|
def send_via_ews(email, password, to_email, subject, body):
|
|
"""Send email via EWS"""
|
|
if not HAS_EWS:
|
|
return False, "exchangelib not installed"
|
|
|
|
try:
|
|
# Disable SSL verification for testing
|
|
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
|
|
|
|
credentials = Credentials(email, password)
|
|
account = Account(email, credentials=credentials, autodiscover=True, access_type=DELEGATE)
|
|
|
|
m = Message(
|
|
account=account,
|
|
subject=subject,
|
|
body=body,
|
|
to_recipients=[Mailbox(email_address=to_email)]
|
|
)
|
|
m.send()
|
|
return True, "OK"
|
|
except Exception as e:
|
|
return False, str(e)
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 5:
|
|
print("Usage: ews-sender.py <email> <password> <to> <subject> [body]")
|
|
sys.exit(1)
|
|
|
|
email = sys.argv[1]
|
|
password = sys.argv[2]
|
|
to_email = sys.argv[3]
|
|
subject = sys.argv[4]
|
|
body = sys.argv[5] if len(sys.argv) > 5 else "Test email via EWS"
|
|
|
|
success, result = send_via_ews(email, password, to_email, subject, body)
|
|
|
|
if success:
|
|
print("OK")
|
|
else:
|
|
print(f"ERROR:{result[:200]}")
|
|
sys.exit(1)
|