From b4014e83a9daf703bf028eb91a36d2e6c96717da Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 3 Oct 2024 01:48:19 +0200 Subject: [PATCH] Add a test tool to test smtp with oauth2 token --- test/other/test_smtp_oauth2.py | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 test/other/test_smtp_oauth2.py diff --git a/test/other/test_smtp_oauth2.py b/test/other/test_smtp_oauth2.py new file mode 100755 index 00000000000..3a1e0082656 --- /dev/null +++ b/test/other/test_smtp_oauth2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import smtplib +import base64 + +# Information connection +smtp_server = "smtp.office365.com" +smtp_port = 587 +access_token = "tokenhere" +email_from = "emailfrom@domain.com" +email_to = "test@example.com" +subject = "Test Email" +body = "This is a test email sent using OAuth2 and Office365." + +# Prepare the token for authentication +auth_string = f"user={email_from}\1auth=Bearer {access_token}\1\1" +auth_string = base64.b64encode(auth_string.encode()).decode() + +# Create connection SMTP +server = smtplib.SMTP(smtp_server, smtp_port) +server.ehlo() +server.starttls() +server.ehlo() + +try: + print (auth_string) + response = server.docmd("AUTH", "XOAUTH2 " + auth_string) + + # Check authentication + if response[0] != 235: + raise Exception(f"Authentication fails : {response[1].decode()}") + +except Exception as e: + print (f"Error : {e}") + +finally: + server.quit()