Exemplo n.º 1
0
def send_emails(args, config):
    template = config.get('template', 'default')
    if template not in EMAIL_TEMPLATES:
        log.error(
            'No handler for mail template "%s" found, skip sending emails',
            template)
        return

    if 'emails' not in config:
        log.error('No emails found in config, skip sending emails')
        return

    handler = EMAIL_TEMPLATES[template]
    msg = handler(args, config)
    msg['From'] = '*****@*****.**'
    msg['To'] = ','.join(config['emails'])
    if 'subject' in config:
        msg['Subject'] = config['subject']
    else:
        msg['Subject'] = 'Test results for %s (%s)' % (args.timestamp,
                                                       config['name'])

    conn = SESConnection(
        #credentials!
    )
    conn.send_raw_email(msg.as_string())
    log.debug('Sent "%s" mail for test %s to %s', template, config['name'],
              config['emails'])
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
 def process_message(self, peer, mailfrom, rcpttos, data):
     # Print the request information
     sys.stdout.write(u"Receiving message from: %s\n" % str(peer))
     sys.stdout.write(u"Message addressed from: %s\n" % str(mailfrom))
     sys.stdout.write(u"Message addressed to: %s\n" % str(rcpttos))
     sys.stdout.write(u"Message length: %s\n" % str(len(data)))
     sys.stdout.flush()
     
     # Parse the message and remove headers that are offensive to SES
     msg = Parser().parsestr(data)
     if 'Errors-To' in msg:
         del msg['Errors-To']
     
     # Send out via SES
     try:
         connection = SESConnection(aws_access_key_id=self.aws_key,
             aws_secret_access_key=self.aws_secret)
         connection.send_raw_email(msg.as_string(), source=mailfrom)
     except BotoServerError, err:
         sys.stderr.write(str(err))
         sys.stdout.flush()
Exemplo n.º 4
0
    def process_message(self, peer, mailfrom, rcpttos, data):
        # Print the request information
        sys.stdout.write(u"Receiving message from: %s\n" % str(peer))
        sys.stdout.write(u"Message addressed from: %s\n" % str(mailfrom))
        sys.stdout.write(u"Message addressed to: %s\n" % str(rcpttos))
        sys.stdout.write(u"Message length: %s\n" % str(len(data)))
        sys.stdout.flush()

        # Parse the message and remove headers that are offensive to SES
        msg = Parser().parsestr(data)
        if 'Errors-To' in msg:
            del msg['Errors-To']

        # Send out via SES
        try:
            connection = SESConnection(aws_access_key_id=self.aws_key,
                                       aws_secret_access_key=self.aws_secret)
            connection.send_raw_email(msg.as_string(), source=mailfrom)
        except BotoServerError, err:
            sys.stderr.write(str(err))
            sys.stdout.flush()
Exemplo n.º 5
0
def send_raw_email_amazonses(raw_email,
                             from_address,
                             recipient=None,
                             aws_key=None,
                             aws_secret_key=None):
    '''
    Send the raw email via Amazon SES.
    If the credential arguments are None, boto will attempt to retrieve the
    values from environment variables and config files.
    recipient seems to be redudant with the values in the raw email headers.
    '''

    conn = SESConnection(aws_key, aws_secret_key)

    if isinstance(recipient, str) or isinstance(recipient, unicode):
        recipient = [recipient]

    conn.send_raw_email(raw_email, source=from_address, destinations=recipient)

    # Getting an error when we try to call this. See:
    # http://code.google.com/p/boto/issues/detail?id=518
    #conn.close()

    return True
Exemplo n.º 6
0
def send_raw_email_amazonses(raw_email,
                             from_address,
                             recipient=None,
                             aws_key=None,
                             aws_secret_key=None):
    '''
    Send the raw email via Amazon SES.
    If the credential arguments are None, boto will attempt to retrieve the
    values from environment variables and config files.
    recipient seems to be redudant with the values in the raw email headers.
    '''

    conn = SESConnection(aws_key, aws_secret_key)

    if isinstance(recipient, str) or isinstance(recipient, unicode):
        recipient = [recipient]

    conn.send_raw_email(raw_email, source=from_address, destinations=recipient)

    # Getting an error when we try to call this. See:
    # http://code.google.com/p/boto/issues/detail?id=518
    #conn.close()

    return True
Exemplo n.º 7
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.º 8
0
def send(fromAddr, rcptTo, data):
	conn = SESConnection(aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
	conn.send_raw_email(data, destinations=rcptTo)
Exemplo n.º 9
0
def send(fromAddr, rcptTo, data):
    conn = SESConnection(aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                         aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
    conn.send_raw_email(data, destinations=rcptTo)