add email sending

This commit is contained in:
pstruebi
2022-06-14 16:04:50 +02:00
parent d104227a75
commit e287a09e1b
2 changed files with 47 additions and 21 deletions

View File

@@ -63,6 +63,18 @@ def get_access_token():
return result
def execute_get_request(token: dict, endpoint:str):
return requests.get( # Use token to call downstream service
endpoint,
headers={'Authorization': 'Bearer ' + token['access_token']},).json()
def execute_post_request(token, endpoint, data):
return requests.post(
endpoint,
json=data,
#data=data,
headers={'Authorization': 'Bearer ' + token['access_token']})
def execute_patch_request(token, endpoint, data):
return requests.patch(
endpoint,
@@ -70,17 +82,39 @@ def execute_patch_request(token, endpoint, data):
#data=data,
headers={'Authorization': 'Bearer ' + token['access_token']},).json()
def send_mail(to, subject, content, user_id=USER_ID):
token=get_access_token()
with open("templates/auth/activate.html") as f:
content=f.read()
mail = {
"message": {
"subject": subject,
"body": {
"contentType": "html",
"content": content
},
"toRecipients": [
{
"emailAddress": {
"address": to
}
}
],
},
"saveToSentItems": "false"
}
return execute_post_request(token, "https://graph.microsoft.com/v1.0/users/" + user_id + "/sendMail", mail)
def update_calendar_event(event_id, data, user_id=USER_ID):
token = get_access_token()
endpoint= "https://graph.microsoft.com/v1.0/users/" + user_id + f"/calendar/events/{event_id}"
return execute_patch_request(token, endpoint, data)
def execute_get_request(token: dict, endpoint:str):
return requests.get( # Use token to call downstream service
endpoint,
headers={'Authorization': 'Bearer ' + token['access_token']},).json()
def execute_user_request(token, endpoint, user_id=USER_ID):
return execute_get_request(token, "https://graph.microsoft.com/v1.0/users/" + user_id + f"/{endpoint}")
@@ -155,7 +189,7 @@ if __name__ == "__main__":
parser.add_argument('--show_event_entries', help='Show available event fields', action='store_true')
parser.add_argument('--show_events', help='Show available events for selected calendar', action='store_true')
parser.add_argument('--update_test_event', help='Execute a patch request for testing', action='store_true')
parser.add_argument('--send_test_email', help='send a test email', action='store_true')
args = parser.parse_args()
@@ -204,11 +238,16 @@ if __name__ == "__main__":
data["attendees"][0]["emailAddress"]["name"] = "Patrick S."
data["attendees"][0]["emailAddress"]["address"] = "struebin.patrick@gmail.com"
#Test2
event_id = "AAMkADY0MDg1MTVjLTg5ZjItNGQxYS04MGQ3LWY2NjJmYjM0YmZhOQBGAAAAAADXD7SdVoWYQI4RYXbBumMEBwAf_ngZxs71RonY3GuLL8TVAADHFw_OAAAf_ngZxs71RonY3GuLL8TVAADQqYjcAAA%3D"
ret = get_calendar_event(event_id)
#ret = update_calendar_event(token, event_id, data)
ret = update_calendar_event(token, event_id, data)
print(json.dumps(ret, indent=2))
if args.send_test_email:
with open("templates/auth/activate.html") as f:
content=f.read()
print(send_mail("struebin.patrick@gmail.com", "test test", content))
print("Done.")

View File

@@ -1,13 +0,0 @@
from flask.ext.mail import Message
from app import app, mail
def send_email(to, subject, template):
msg = Message(
subject,
recipients=[to],
html=template,
sender=app.config['MAIL_DEFAULT_SENDER']
)
mail.send(msg)