2016-02-17 17:48:23 +00:00
|
|
|
import boto3
|
2016-03-02 09:33:20 +00:00
|
|
|
from flask import current_app
|
|
|
|
|
from monotonic import monotonic
|
2016-04-06 16:34:45 +01:00
|
|
|
from app.clients import STATISTICS_DELIVERED, STATISTICS_FAILURE
|
2016-02-17 17:48:23 +00:00
|
|
|
from app.clients.email import (EmailClientException, EmailClient)
|
|
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
ses_response_map = {
|
|
|
|
|
'Bounce': {
|
|
|
|
|
"message": 'Bounced',
|
|
|
|
|
"success": False,
|
2016-04-08 16:13:10 +01:00
|
|
|
"notification_status": 'failed',
|
2016-04-06 16:34:45 +01:00
|
|
|
"notification_statistics_status": STATISTICS_FAILURE
|
|
|
|
|
},
|
|
|
|
|
'Delivery': {
|
|
|
|
|
"message": 'Delivered',
|
|
|
|
|
"success": True,
|
|
|
|
|
"notification_status": 'delivered',
|
|
|
|
|
"notification_statistics_status": STATISTICS_DELIVERED
|
|
|
|
|
},
|
|
|
|
|
'Complaint': {
|
|
|
|
|
"message": 'Complaint',
|
2016-04-08 16:13:10 +01:00
|
|
|
"success": True,
|
|
|
|
|
"notification_status": 'delivered',
|
|
|
|
|
"notification_statistics_status": STATISTICS_DELIVERED
|
2016-04-06 16:34:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_aws_responses(status):
|
|
|
|
|
return ses_response_map[status]
|
2016-03-10 17:29:17 +00:00
|
|
|
|
2016-02-17 17:48:23 +00:00
|
|
|
|
|
|
|
|
class AwsSesClientException(EmailClientException):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AwsSesClient(EmailClient):
|
|
|
|
|
'''
|
|
|
|
|
Amazon SES email client.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
def init_app(self, region, *args, **kwargs):
|
|
|
|
|
self._client = boto3.client('ses', region_name=region)
|
|
|
|
|
super(AwsSesClient, self).__init__(*args, **kwargs)
|
2016-02-25 11:23:04 +00:00
|
|
|
self.name = 'ses'
|
|
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
|
return self.name
|
2016-02-17 17:48:23 +00:00
|
|
|
|
|
|
|
|
def send_email(self,
|
|
|
|
|
source,
|
|
|
|
|
to_addresses,
|
|
|
|
|
subject,
|
|
|
|
|
body,
|
2016-03-18 11:47:01 +00:00
|
|
|
html_body='',
|
2016-02-17 17:48:23 +00:00
|
|
|
reply_to_addresses=None):
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(to_addresses, str):
|
|
|
|
|
to_addresses = [to_addresses]
|
|
|
|
|
if reply_to_addresses and isinstance(reply_to_addresses, str):
|
|
|
|
|
reply_to_addresses = [reply_to_addresses]
|
|
|
|
|
elif reply_to_addresses is None:
|
|
|
|
|
reply_to_addresses = []
|
|
|
|
|
|
2016-03-18 11:47:01 +00:00
|
|
|
body = {
|
|
|
|
|
'Text': {'Data': body}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if html_body:
|
|
|
|
|
body.update({
|
|
|
|
|
'Html': {'Data': html_body}
|
|
|
|
|
})
|
|
|
|
|
|
2016-03-02 09:33:20 +00:00
|
|
|
start_time = monotonic()
|
2016-02-17 17:48:23 +00:00
|
|
|
response = self._client.send_email(
|
|
|
|
|
Source=source,
|
|
|
|
|
Destination={
|
|
|
|
|
'ToAddresses': to_addresses,
|
|
|
|
|
'CcAddresses': [],
|
|
|
|
|
'BccAddresses': []
|
|
|
|
|
},
|
|
|
|
|
Message={
|
|
|
|
|
'Subject': {
|
|
|
|
|
'Data': subject,
|
|
|
|
|
},
|
2016-03-18 11:47:01 +00:00
|
|
|
'Body': body
|
2016-02-17 17:48:23 +00:00
|
|
|
},
|
|
|
|
|
ReplyToAddresses=reply_to_addresses)
|
2016-03-02 09:33:20 +00:00
|
|
|
elapsed_time = monotonic() - start_time
|
|
|
|
|
current_app.logger.info("AWS SES request finished in {}".format(elapsed_time))
|
2016-02-17 17:48:23 +00:00
|
|
|
return response['MessageId']
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# TODO logging exceptions
|
|
|
|
|
raise AwsSesClientException(str(e))
|