Add a test tool to test smtp with oauth2 token
This commit is contained in:
parent
c955ab06e7
commit
b4014e83a9
1 changed files with 36 additions and 0 deletions
36
test/other/test_smtp_oauth2.py
Executable file
36
test/other/test_smtp_oauth2.py
Executable file
|
|
@ -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()
|
||||
Loading…
Reference in a new issue