Skip to content

Commit

Permalink
introduce a new module to manage email messages with ses - #75
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard357 committed Apr 9, 2023
1 parent 885c4ef commit 5def6ca
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lambdas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

from .account import Account, State
from .email import Email
from .events import Events
from .key_value_store import KeyValueStore
from .logger import setup_logging, trap_exception, LOGGING_FORMAT
Expand All @@ -25,6 +26,7 @@
from .worker import Worker

__all__ = ['Account',
'Email',
'Events',
'KeyValueStore',
'LOGGING_FORMAT',
Expand Down
39 changes: 39 additions & 0 deletions lambdas/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""
Copyright Reply.com or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

from boto3.session import Session
import logging
import os


class Email:

@classmethod
def send(cls, recipients, subject, text, session=None):
logging.info("Sending an email")
session = session or Session()
ses = session.client('ses')
try:
ses.send_email(
Source=os.environ['ORIGIN_EMAIL_RECIPIENT'],
Destination=dict(ToAddresses=recipients),
Message=dict(Subject=dict(Data=subject, Charset='UTF-8'),
Body=dict(Text=dict(Data=text, Charset='UTF-8')))
)
return '[OK]'
except ses.exceptions.MessageRejected as exception:
logging.exception(exception)
36 changes: 36 additions & 0 deletions tests/test_lambda_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
"""
Copyright Reply.com or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import boto3
from unittest.mock import patch
from moto import mock_ses
import os
import pytest

from lambdas import Email

pytestmark = pytest.mark.wip


@pytest.mark.unit_tests
@patch.dict(os.environ, dict(ORIGIN_EMAIL_RECIPIENT='alice@example.com'))
@mock_ses
def test_send():
ses = boto3.client('ses')
ses.verify_email_identity(EmailAddress='alice@example.com')
parameters = dict(recipients=['bob@example.com'], subject='my subject', text='my message')
assert Email.send(**parameters) == '[OK]'

0 comments on commit 5def6ca

Please sign in to comment.