urma/app/helpers.py
2023-01-15 20:50:57 +00:00

52 lines
1.2 KiB
Python

import os
import smtplib
import ssl
from config import Config
from email.message import EmailMessage
from config import Config
from log import log
from typing import Any, List
def index_ojects_by_parameter(object_list: List, param: Any):
"""
Create a dictionary from passed list where each list entry is keyed
by passed param
n
"""
results = {}
for obj in object_list:
results[getattr(obj, param)] = obj
return results
def send_mail(to_addr, from_addr, subj, body):
# From https://docs.python.org/3/library/email.examples.html
# Create a text/plain message
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subj
msg['From'] = from_addr
msg['To'] = to_addr
# Send the message via SMTP server.
context = ssl.create_default_context()
try:
s = smtplib.SMTP(host=Config.MAIL_SERVER, port=Config.MAIL_PORT)
if Config.MAIL_USE_TLS:
s.starttls(context=context)
if Config.MAIL_USERNAME and Config.MAIL_PASSWORD:
s.login(Config.MAIL_USERNAME, Config.MAIL_PASSWORD)
s.send_message(msg)
except Exception as e:
print(e)
finally:
s.quit()