mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-25 20:18:38 +02:00
Review feedback: error tolerance, logging, etc.
This commit is contained in:
parent
a314b905d3
commit
f0ecae08c5
3 changed files with 43 additions and 23 deletions
|
@ -6,6 +6,13 @@ from django.conf import settings
|
|||
from django.template.loader import get_template
|
||||
|
||||
|
||||
class EmailSendingError(RuntimeError):
|
||||
|
||||
"""Local error for handling all failures when sending email."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def send_templated_email(template_name: str, to_address: str, context={}):
|
||||
"""Send an email built from a template to one email address.
|
||||
|
||||
|
@ -16,19 +23,26 @@ def send_templated_email(template_name: str, to_address: str, context={}):
|
|||
template = get_template(template_name)
|
||||
email_body = template.render(context=context)
|
||||
|
||||
ses_client = boto3.client(
|
||||
"sesv2",
|
||||
region_name=settings.AWS_REGION,
|
||||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
||||
)
|
||||
ses_client.send_email(
|
||||
FromEmailAddress=settings.DEFAULT_FROM_EMAIL,
|
||||
Destination={"ToAddresses": [to_address]},
|
||||
Content={
|
||||
"Simple": {
|
||||
"Subject": {"Data": "Thank you for applying for a .gov domain"},
|
||||
"Body": {"Text": {"Data": email_body}},
|
||||
try:
|
||||
ses_client = boto3.client(
|
||||
"sesv2",
|
||||
region_name=settings.AWS_REGION,
|
||||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise EmailSendingError("Could not access the SES client.") from exc
|
||||
|
||||
try:
|
||||
ses_client.send_email(
|
||||
FromEmailAddress=settings.DEFAULT_FROM_EMAIL,
|
||||
Destination={"ToAddresses": [to_address]},
|
||||
Content={
|
||||
"Simple": {
|
||||
"Subject": {"Data": "Thank you for applying for a .gov domain"},
|
||||
"Body": {"Text": {"Data": email_body}},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
raise EmailSendingError("Could not send SES email.") from exc
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue