Skip to content

Commit

Permalink
api: use SES for transactional emails
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Jul 13, 2022
1 parent 18557d7 commit dfe941e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
13 changes: 10 additions & 3 deletions mpcontribs-api/mpcontribs/api/__init__.py
Expand Up @@ -33,7 +33,7 @@
invalidChars = set(punctuation.replace("*", ""))
invalidChars.add(" ")
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")
sns_client = boto3.client("sns")
ses_client = boto3.client("ses")

# NOTE not including Size below (special for arrays)
FILTERS = {
Expand Down Expand Up @@ -99,8 +99,15 @@ def valid_dict(dct):
remap(dct, visit=visit, enter=enter)


def send_email(to, subject, template):
sns_client.publish(TopicArn=to, Message=template, Subject=subject)
def send_email(to, subject, html):
ses_client.send_email(
Source=current_app.config["MAIL_DEFAULT_SENDER"],
Destination={"ToAddresses": [to]},
Message={
"Subject": {"Data": subject},
"Body": {"Html": {"Data": html}}
}
)


def get_collections(db):
Expand Down
25 changes: 6 additions & 19 deletions mpcontribs-api/mpcontribs/api/projects/document.py
Expand Up @@ -18,7 +18,7 @@
StringField, BooleanField, DictField, URLField, EmailField,
FloatField, IntField, EmbeddedDocumentListField, EmbeddedDocumentField
)
from mpcontribs.api import send_email, sns_client, valid_key, valid_dict, delimiter, enter
from mpcontribs.api import send_email, valid_key, valid_dict, delimiter, enter

PROVIDERS = {"github", "google", "facebook", "microsoft", "amazon"}
MAX_COLUMNS = 50
Expand Down Expand Up @@ -196,15 +196,7 @@ def post_save(cls, sender, document, **kwargs):
html = render_template(
"admin_email.html", doc=doc_yaml, link=link, hours=hours
)
send_email(admin_topic, subject, html)
resp = sns_client.create_topic(
Name=f"mpcontribs_{document.name}",
Attributes={"DisplayName": f"MPContribs {document.title}"},
)
endpoint = document.owner.split(":", 1)[1]
sns_client.subscribe(
TopicArn=resp["TopicArn"], Protocol="email", Endpoint=endpoint
)
send_email(admin_email, subject, html)
else:
delta_set, delta_unset = document._delta()

Expand All @@ -219,10 +211,8 @@ def post_save(cls, sender, document, **kwargs):
host=portal,
project=document.name
)
topic_arn = ":".join(
admin_topic.split(":")[:-1] + ["mpcontribs_" + document.name]
)
send_email(topic_arn, subject, html)
owner_email = document.owner.split(":", 1)[1]
send_email(owner_email, subject, html)

if "columns" in delta_set or "columns" in delta_unset or (
not delta_set and not delta_unset
Expand Down Expand Up @@ -307,11 +297,8 @@ def post_delete(cls, sender, document, **kwargs):
"owner_email.html", approved=False,
admin_email=admin_email, project=document.name
)
topic_arn = ":".join(
admin_topic.split(":")[:-1] + ["mpcontribs_" + document.name]
)
send_email(topic_arn, subject, html)
sns_client.delete_topic(TopicArn=topic_arn)
owner_email = document.owner.split(":", 1)[1]
send_email(owner_email, subject, html)


register_field(ProviderEmailField, ProviderEmail, available_params=(params.LengthParam,))
Expand Down

0 comments on commit dfe941e

Please sign in to comment.