def get_ses_quota(): ''' Returns the simple Amazon SES quota info, in text. ''' # Open the connection. Uses creds from boto conf or env vars. conn = SESConnection() quota = conn.get_send_quota() conn.close() return json.dumps(quota, indent=2)
class AmazonSEService(EmailService): def __init__(self, *args, **kwargs): """ Initializes the Amazon SES email service. """ self.connection = None self.id = settings.EMAIL_SERVICES_CLIENT_ID self.key = settings.EMAIL_SERVICES_CLIENT_KEY def open(self): """ Creates the connection that will interact with the Amazon API using Boto. """ if self.connection: return self.connection = SESConnection(aws_access_key_id=self.id, aws_secret_access_key=self.key) def close(self): """ Creates the connection that will interact with the Amazon API using Boto. """ if not self.connection: return self.connection.close() self.connection = None def send_messages(self, email_messages): """ Sends one or more email messages using throught amazon SES using boto. """ if not self.connection: self.open() for message in email_messages: self.connection.send_raw_email( source=message.from_email, destinations=message.recipients(), raw_message=message.message().as_string())
def get_ses_send_stats(): """ Fetches the Amazon SES, which includes info about bounces and complaints. Processes that data, returns some text suitable for the email. """ # Open the connection. Uses creds from boto conf or env vars. conn = SESConnection() stats = conn.get_send_statistics() conn.close() one_day_ago = datetime.datetime.now() - datetime.timedelta(1) one_week_ago = datetime.datetime.now() - datetime.timedelta(7) one_day_ago_counter = collections.Counter() one_week_ago_counter = collections.Counter() two_weeks_ago_counter = collections.Counter() for dp in stats['GetSendStatisticsResponse']['GetSendStatisticsResult']['SendDataPoints']: dt = datetime.datetime.strptime(str(dp['Timestamp']).translate(None, ':-'), "%Y%m%dT%H%M%SZ") dp_count = {k: int(v) for k, v in dp.items() if v.isdigit()} if dt > one_day_ago: one_day_ago_counter.update(dp_count) if dt > one_week_ago: one_week_ago_counter.update(dp_count) else: two_weeks_ago_counter.update(dp_count) res = 'SES Send Stats\n=====================================' for title, data in (('Last Day', one_day_ago_counter), ('Last Week', one_week_ago_counter), ('Two Weeks Ago', two_weeks_ago_counter)): res += '\n%s\n---------------------------------' % (title) for i, k in enumerate(('DeliveryAttempts', 'Bounces', 'Complaints', 'Rejects')): res += '\n%16s: %5s' % (k, data[k]) if i != 0: res += ' (%d%%)' % (100 * data[k] / data['DeliveryAttempts']) return res