diff --git a/lib/email_api/emailer.py b/lib/email_api/emailer.py index b53292c2a..f5ccbc918 100644 --- a/lib/email_api/emailer.py +++ b/lib/email_api/emailer.py @@ -1,6 +1,7 @@ import time from typing import List from pathlib import Path +import ssl from smtplib import SMTP_SSL import logging @@ -44,9 +45,15 @@ def send_emails(self, emails: List[EmailParams]): """ logger.debug("connecting to SMTP server") - + context = ssl.create_default_context() + context.options |= ( + ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 + ) # Disable TLS 1.0 and 1.1 with SMTP_SSL( - self.smtp_account.server, self.smtp_account.port, timeout=60 + self.smtp_account.server, + self.smtp_account.port, + context=context, + timeout=60, ) as server: server.ehlo() logger.info("SMTP server connection established") diff --git a/lib/workflows/send_error_vm_email.py b/lib/workflows/send_error_vm_email.py new file mode 100644 index 000000000..a9c47b86c --- /dev/null +++ b/lib/workflows/send_error_vm_email.py @@ -0,0 +1,49 @@ +from openstack_query import ServerQuery +from email_api.emailer import Emailer +from structs.email.email_params import EmailParams +from structs.email.email_template_details import EmailTemplateDetails +from structs.email.smtp_account import SMTPAccount + +sq = ServerQuery() +sq.select(["name", "id", "status", "updated_at"]) +sq.where(preset="younger_than", prop="updated_at", days=60) +sq.where(preset="equal_to", prop="status", value="ACTIVE") +sq.sort_by([("server_name", "ascending")]) +sq.run("prod-vgc59244", as_admin=False, all_projects=False) +sq.append_from("projects", cloud_account="prod", props=["project_id", "project_name"]) + +uq = sq.then("users") +uq.group_by("user_email") +uq.run("prod") + +print(uq.to_string()) +# uq.to_csv("./") + +Emailer( + SMTPAccount( + username="", + password="", + smtp_auth=False, + server="mail.gridpp.rl.ac.uk", + port=465, + secure=False, + ) +).send_emails( + [ + EmailParams( + subject="test email", + email_to=["anish.mudaraddi@stfc.ac.uk"], + email_from="anish.mudaraddi@stfc.ac.uk", + email_templates=[ + EmailTemplateDetails( + template_name="test", + template_params={ + "username": "anish", + "test_message": "test that email works", + }, + ), + EmailTemplateDetails(template_name="footer"), + ], + ) + ] +)