Changeset - 9cb4b622c930
[Not reviewed]
default
0 1 0
Dennis Fink - 3 years ago 2022-07-19 11:54:17
dennis.fink@c3l.lu
Use context manager with stmp connection
1 file changed with 12 insertions and 12 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
@@ -73,28 +73,28 @@ def post_toot(toot):
 
        )
 
        api.status_post(toot, visibility="unlisted")
 

	
 

	
 
def post_email(subject, body):
 
    if "EMAIL_PASS" in current_app.config:
 
        smtp_conn = smtplib.SMTP(
 
            current_app.config["EMAIL_HOST"], port=current_app.config["EMAIL_PORT"]
 
        )
 
        ssl_context = ssl.create_default_context()
 
        smtp_conn.starttls(context=ssl_context)
 
        smtp_conn.login(
 
            current_app.config["EMAIL_USER"], current_app.config["EMAIL_PASS"]
 
        )
 

	
 
        msg = email.message.EmailMessage(policy=email.policy.default)
 
        msg["To"] = current_app.config["EMAIL_ANNOUNCE_ADDRESS"]
 
        msg["From"] = "spaceapibot <{email}>".format(
 
            email=current_app.config["EMAIL_USER"]
 
        )
 

	
 
        email_user = current_app.config["EMAIL_USER"]
 
        msg["From"] = "spaceapibot <{email}>".format(email=email_user)
 

	
 
        msg["Subject"] = subject
 
        msg.set_content(body)
 
        smtp_conn.send_message(msg)
 

	
 
        with smtplib.SMTP(
 
            current_app.config["EMAIL_HOST"], port=current_app.config["EMAIL_PORT"]
 
        ) as smtp:
 
            smtp.starttls(context=ssl.create_default_context())
 
            smtp.login(email_user, current_app.config["EMAIL_PASS"])
 
            smtp.send_message(msg)
 

	
 

	
 
class Singleton:
 
    def __new__(cls, *args, **kwargs):
 
        key = str(hash(cls))
 

	
0 comments (0 inline, 0 general)