Exemplo n.º 1
0
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
Exemplo n.º 2
0
class SESConnectionTest (unittest.TestCase):
    
    def get_suite_description(self):
        return 'SES connection test suite'

    def setUp(self):
        self.conn = SESConnection()
        self.test_email = '*****@*****.**'
    
    def test_1_connection(self):
        """ Tests insantiating basic connection """
        
        c = SESConnection()
        assert c
    
    def test_2_get_send_statistics(self):
        """ Tests retrieving send statistics """
        
        assert self.conn.get_send_statistics()
        
    def test_3_get_send_quota(self):
        """ Tests retrieving send quota """
        
        assert self.conn.get_send_quota()
        
    def test_4_get_verified_emails(self):
        """ Tests retrieving list of verified emails """
        
        assert self.conn.get_verified_emails()
        
    def test_5_verify_email_address(self):
        """ Tests verifying email address """
        
        assert self.conn.verify_email_address(email=self.test_email)
    
    def test_6_send_email(self):
        """ Tests sending an email """
        
        assert self.conn.send_email(source=self.test_email,
                     subject='Test',
                     message='Test Message',
                     to='self.test_email')
        
        # Email with cc and bcc
        assert self.conn.send_email(source=self.test_email, 
                         subject='Test', 
                         message='Test Message', 
                         to=[self.test_email], 
                         cc=[self.test_email],
                         bcc=[self.test_email])
                        
    def test_7_send_raw_email(self):
        """ Tests sending a raw email """
        
        assert self.conn.send_raw_email(source=self.test_email,
                         message=self._create_raw_email_message())
    def test_8_delete_verified_email(self):
        """ Tests deleting verified email """
        
        assert self.conn.delete_verified_email(email=self.test_email)
        
    def _create_raw_email_message(self):
        """ Creates a test mime-type email using native Email class """
        
        me = self.test_email
        you = self.test_email

        # Create message container - the correct MIME type is multipart/alternative.
        msg = MIMEMultipart('alternative')
        msg['From'] = me
        msg['To'] = you
        msg['Subject'] = "Link"

        # Create the body of the message (a plain-text and an HTML version).
        text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
        html = """\
        <html>
          <head></head>
          <body>
            <p>Hi!<br>
               How are you?<br>
               Here is the <a href="http://www.python.org">link</a> you wanted.
            </p>
          </body>
        </html>
        """

        # Record the MIME types of both parts - text/plain and text/html.
        part1 = MIMEText(text, 'plain')
        part2 = MIMEText(html, 'html')

        # Attach parts into message container.
        # According to RFC 2046, the last part of a multipart message, in this case
        # the HTML message, is best and preferred.
        msg.attach(part1)
        msg.attach(part2)
    
        return msg
Exemplo n.º 3
0
import os, pwd, grp
f=open('/tmp/.uwsgi.lock', 'w+')
f.close()
uid = pwd.getpwnam('oskar').pw_uid
gid = grp.getgrnam('oskar').gr_gid
os.chown('/tmp/.uwsgi.lock', uid, gid)

import ctypes
from ctypes import CDLL

pylibc = CDLL("/home/ella/Ella/ella/awsenckeys.so")
pylibc.awsakey.restype = ctypes.c_char_p
pylibc.awsskey.restype = ctypes.c_char_p

AWSAKEY = pylibc.awsakey()
AWSSKEY = pylibc.awsskey()

######################################################

conn = SESConnection(AWSAKEY, AWSSKEY)
data =  conn.get_send_statistics()
data =  data["GetSendStatisticsResponse"]["GetSendStatisticsResult"]
for i in data["SendDataPoints"]:
    print "Complaints: %s" % i["Complaints"]
    print "Timestamp: %s" % i["Timestamp"]
    print "DeliveryAttempts: %s" % i["DeliveryAttempts"]
    print "Bounces: %s" % i["Bounces"]
    print "Rejects: %s" % i["Rejects"]
				            
Exemplo n.º 4
0
import os, pwd, grp

f = open('/tmp/.uwsgi.lock', 'w+')
f.close()
uid = pwd.getpwnam('oskar').pw_uid
gid = grp.getgrnam('oskar').gr_gid
os.chown('/tmp/.uwsgi.lock', uid, gid)

import ctypes
from ctypes import CDLL

pylibc = CDLL("/home/ella/Ella/ella/awsenckeys.so")
pylibc.awsakey.restype = ctypes.c_char_p
pylibc.awsskey.restype = ctypes.c_char_p

AWSAKEY = pylibc.awsakey()
AWSSKEY = pylibc.awsskey()

######################################################

conn = SESConnection(AWSAKEY, AWSSKEY)
data = conn.get_send_statistics()
data = data["GetSendStatisticsResponse"]["GetSendStatisticsResult"]
for i in data["SendDataPoints"]:
    print "Complaints: %s" % i["Complaints"]
    print "Timestamp: %s" % i["Timestamp"]
    print "DeliveryAttempts: %s" % i["DeliveryAttempts"]
    print "Bounces: %s" % i["Bounces"]
    print "Rejects: %s" % i["Rejects"]