Skip to content

Commit

Permalink
email-ssl-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-mudaraddi committed Jul 11, 2024
1 parent 671b76b commit 486b96f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/email_api/emailer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
from typing import List
from pathlib import Path
import ssl
from smtplib import SMTP_SSL
import logging

Expand Down Expand Up @@ -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")
Expand Down
49 changes: 49 additions & 0 deletions lib/workflows/send_error_vm_email.py
Original file line number Diff line number Diff line change
@@ -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=["[email protected]"],
email_from="[email protected]",
email_templates=[
EmailTemplateDetails(
template_name="test",
template_params={
"username": "anish",
"test_message": "test that email works",
},
),
EmailTemplateDetails(template_name="footer"),
],
)
]
)

0 comments on commit 486b96f

Please sign in to comment.