Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
Expose MessageId in Email backend (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoacierno authored May 5, 2024
1 parent b367423 commit 6b06136
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pythonit_toolkit/emails/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def send_email(
to: str,
variables: Optional[Dict[str, str]] = None,
reply_to: List[str] = None
):
) -> str:
pass
5 changes: 4 additions & 1 deletion pythonit_toolkit/emails/backends/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, List, Optional
from uuid import uuid4

from pythonit_toolkit.emails.templates import EmailTemplate

Expand All @@ -18,7 +19,7 @@ def send_email(
to: str,
variables: Optional[Dict[str, str]] = None,
reply_to: List[str] = None,
):
) -> str:
reply_to = reply_to or []

print("=== Email sending ===")
Expand All @@ -29,3 +30,5 @@ def send_email(
print(f"Variables: {str(variables)}")
print(f"Reply to: {str(reply_to)}")
print("=== End Email sending ===")

return f'messageid-{uuid4()}'
5 changes: 3 additions & 2 deletions pythonit_toolkit/emails/backends/ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ def send_email(
to: str,
variables: Optional[Dict[str, str]] = None,
reply_to: List[str] = None,
):
) -> str:
reply_to = reply_to or []

variables = self.encode_vars({"subject": subject, **(variables or {})})
self.ses.send_templated_email(
response = self.ses.send_templated_email(
Source=from_,
Destination={"ToAddresses": [to]},
Template=f"pythonit-{self.environment}-{template}",
TemplateData=json.dumps(variables),
ReplyToAddresses=reply_to,
ConfigurationSetName='primary',
)
return response['MessageId']

def encode_vars(self, variables: dict[str, Any]) -> dict[str, Any]:
vars = dict()
Expand Down
7 changes: 6 additions & 1 deletion tests/emails/backends/test_ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
@test("send email via ses")
async def _():
with patch("pythonit_toolkit.emails.backends.ses.boto3") as mock_boto:
SESEmailBackend("production").send_email(
mock_boto.client.return_value.send_templated_email.return_value = {
'MessageId': 'msg-id-123'
}
message_id = SESEmailBackend("production").send_email(
template=EmailTemplate.RESET_PASSWORD,
subject="Subject",
from_="[email protected]",
to="[email protected]",
variables={"a": "b", "c": "d"},
)

assert message_id == 'msg-id-123'

mock_boto.client.return_value.send_templated_email.assert_called_once_with(
Source="[email protected]",
Destination={"ToAddresses": ["[email protected]"]},
Expand Down

0 comments on commit 6b06136

Please sign in to comment.