Esempio n. 1
0
def send_response(recipient, from_address,
                  subject, body_text, body_html,
                  replyid, attachments):
    '''
    Send email back to the user that sent feedback.
    On error, raises exception.
    `replyid` may be None. `attachments` may be None.
    '''

    # TODO: Use SMTP rather than SES if we have attachments SES or SMTP (get@ style).
    # DISABLING ATTACHMENTS
    if attachments:
        return

    extra_headers = {'Reply-To': from_address}

    if replyid:
        extra_headers['In-Reply-To'] = replyid
        extra_headers['References'] = replyid

    body = []
    if body_text:
        body.append(('plain', body_text))
    if body_html:
        body.append(('html', body_html))

    raw_email = sendmail.create_raw_email(recipient,
                                          from_address,
                                          subject,
                                          body,
                                          attachments,
                                          extra_headers)

    if not raw_email:
        return

    # SES has a send size limit, above which it'll reject the email. If our raw_email is
    # larger than that, we'll discard the HTML version.
    if len(raw_email) > _SES_EMAIL_SIZE_LIMIT:
        body.pop() # discard HTML
        raw_email = sendmail.create_raw_email(recipient,
                                              from_address,
                                              subject,
                                              body,
                                              attachments,
                                              extra_headers)

    if not raw_email:
        return

    try:
        sendmail.send_raw_email_amazonses(raw_email, from_address, recipient)
    except:
        raise
Esempio n. 2
0
def send_test_email(recipient,
                    from_address,
                    subject,
                    body,
                    attachments=None,
                    extra_headers=None):
    '''
    Used for debugging purposes to send an email that's approximately like
    a response email.
    '''
    raw = sendmail.create_raw_email(recipient, from_address, subject, body,
                                    attachments, extra_headers)
    if not raw:
        print('create_raw_email failed')
        return False

    raw = _dkim_sign_email(raw)

    # Throws exception on error
    if not sendmail.send_raw_email_smtp(raw, from_address, recipient):
        print('send_raw_email_smtp failed')
        return False

    print('Email sent')
    return True
Esempio n. 3
0
def send(recipients, from_address,
         subject, body_text, body_html,
         replyid=None):
    '''
    Send email via SMTP. Throws `smtplib.SMTPException` on error.
    `recipients` may be an array of address or a single address string.
    '''

    reply_to_header = {'In-Reply-To': replyid,
                       'References': replyid} if replyid else None

    body = []
    if body_text:
        body.append(('plain', body_text))
    if body_html:
        body.append(('html', body_html))

    raw_email = sendmail.create_raw_email(recipients,
                                          from_address,
                                          subject,
                                          body,
                                          None,
                                          reply_to_header)

    smtp_server = smtplib.SMTP_SSL(config['smtpServer'], config['smtpPort'])
    smtp_server.login(config['emailUsername'], config['emailPassword'])

    sendmail.send_raw_email_smtp(raw_email,
                                 from_address,
                                 recipients,
                                 smtp_server)
def forward_to_administrator(email_type, email_string):
    '''
    `email_type` should be something like "Complaint".
    '''

    if settings.ADMIN_FORWARD_ADDRESSES:
        raw = sendmail.create_raw_email(settings.ADMIN_FORWARD_ADDRESSES,
                                        settings.RESPONSE_FROM_ADDR,
                                        '%s %s' % (settings.ADMIN_FORWARD_SUBJECT_TAG, email_type),
                                        email_string)
        if not raw:
            print('create_raw_email failed')
            return False

        raw = _dkim_sign_email(raw)

        # Throws exception on error
        if not sendmail.send_raw_email_smtp(raw,
                                            settings.RESPONSE_FROM_ADDR,
                                            settings.ADMIN_FORWARD_ADDRESSES):
            print('send_raw_email_smtp failed')
            return False

        print('Email sent')
        return True
Esempio n. 5
0
def send_response(recipient, from_address, subject, body_text, body_html,
                  replyid, attachments):
    '''
    Send email back to the user that sent feedback.
    On error, raises exception.
    `replyid` may be None. `attachments` may be None.
    '''

    # TODO: Use SMTP rather than SES if we have attachments SES or SMTP (get@ style).
    # DISABLING ATTACHMENTS
    if attachments:
        return

    extra_headers = {'Reply-To': from_address}

    if replyid:
        extra_headers['In-Reply-To'] = replyid
        extra_headers['References'] = replyid

    body = []
    if body_text:
        body.append(('plain', body_text))
    if body_html:
        body.append(('html', body_html))

    raw_email = sendmail.create_raw_email(recipient, from_address, subject,
                                          body, attachments, extra_headers)

    if not raw_email:
        return

    try:
        sendmail.send_raw_email_amazonses(raw_email, from_address)
    except:
        raise
Esempio n. 6
0
def forward_to_administrator(email_type, email_string):
    '''
    `email_type` should be something like "Complaint".
    '''

    if settings.ADMIN_FORWARD_ADDRESSES:
        raw = sendmail.create_raw_email(settings.ADMIN_FORWARD_ADDRESSES,
                                        settings.RESPONSE_FROM_ADDR,
                                        '%s %s' % (settings.ADMIN_FORWARD_SUBJECT_TAG, email_type),
                                        email_string)
        if not raw:
            print('create_raw_email failed')
            return False

        raw = _dkim_sign_email(raw)

        # Throws exception on error
        if not sendmail.send_raw_email_smtp(raw,
                                            settings.RESPONSE_FROM_ADDR,
                                            settings.ADMIN_FORWARD_ADDRESSES):
            print('send_raw_email_smtp failed')
            return False

        print('Email sent')
        return True
Esempio n. 7
0
def send(recipients, from_address,
         subject, body_text, body_html,
         replyid=None):
    '''
    Send email via SMTP. Throws `smtplib.SMTPException` on error.
    `recipients` may be an array of address or a single address string.
    '''

    reply_to_header = {'In-Reply-To': replyid,
                       'References': replyid} if replyid else None

    body = []
    if body_text:
        body.append(('plain', body_text))
    if body_html:
        body.append(('html', body_html))

    raw_email = sendmail.create_raw_email(recipients,
                                          from_address,
                                          subject,
                                          body,
                                          None,
                                          reply_to_header)

    smtp_server = smtplib.SMTP_SSL(config['smtpServer'], config['smtpPort'])
    smtp_server.login(config['emailUsername'], config['emailPassword'])

    sendmail.send_raw_email_smtp(raw_email,
                                 from_address,
                                 recipients,
                                 smtp_server)
Esempio n. 8
0
def send_test_email(recipient, from_address, subject, body,
                    attachments=None, extra_headers=None):
    '''
    Used for debugging purposes to send an email that's approximately like
    a response email.
    '''
    raw = sendmail.create_raw_email(recipient, from_address, subject, body,
                                    attachments, extra_headers)
    if not raw:
        print('create_raw_email failed')
        return False

    raw = _dkim_sign_email(raw)

    # Throws exception on error
    if not sendmail.send_raw_email_smtp(raw, from_address, recipient):
        print('send_raw_email_smtp failed')
        return False

    print('Email sent')
    return True
Esempio n. 9
0
def send_response(recipient, from_address,
                  subject, body_text, body_html,
                  replyid, attachments):
    '''
    Send email back to the user that sent feedback.
    On error, raises exception.
    `replyid` may be None. `attachments` may be None.
    '''

    # TODO: Use SMTP rather than SES if we have attachments SES or SMTP (get@ style).
    # DISABLING ATTACHMENTS
    if attachments:
        return

    extra_headers = {'Reply-To': from_address}

    if replyid:
        extra_headers['In-Reply-To'] = replyid
        extra_headers['References'] = replyid

    body = []
    if body_text:
        body.append(('plain', body_text))
    if body_html:
        body.append(('html', body_html))

    raw_email = sendmail.create_raw_email(recipient,
                                          from_address,
                                          subject,
                                          body,
                                          attachments,
                                          extra_headers)

    if not raw_email:
        return

    try:
        sendmail.send_raw_email_amazonses(raw_email, from_address)
    except:
        raise
Esempio n. 10
0
    def process_email(self, email_string):
        '''
        Processes the given email and sends a response.
        Returns True if successful, False or exception otherwise.
        '''

        self._email_string = email_string

        if not self._parse_email(email_string):
            return False

        # Look up all config entries matching the requested address.
        request_conf = [
            item for item in self._conf
            if item['email_addr'] == self.requested_addr
        ]

        # If we didn't find anything for that address, exit.
        if not request_conf:
            logger.info('fail: invalid requested address: %s',
                        self.requested_addr)
            return False

        # Check if the user is (or should be) blacklisted
        if not self._check_blacklist():
            logger.info('fail: blacklist')
            return False

        # Process each config entry found the for the requested address separately.
        # Don't fail out early, since the other email send method has a chance
        # to succeed even if one fails. (I.e., SMTP will succeed even if there's
        # a SES service problem.)
        full_success = True
        exception_to_raise = None
        for conf in request_conf:
            attachments = None
            if conf['attachments']:
                attachments = []
                for attachment_info in conf['attachments']:
                    bucketname, bucket_filename, attachment_filename = attachment_info
                    attachments.append((aws_helpers.get_s3_attachment(
                        settings.ATTACHMENT_CACHE_DIR, bucketname,
                        bucket_filename), attachment_filename))

            extra_headers = {
                'Reply-To': self.requested_addr,
                'Auto-Submitted': 'auto-replied'
            }

            if self._requester_msgid:
                extra_headers['In-Reply-To'] = self._requester_msgid
                extra_headers['References'] = self._requester_msgid

            raw_response = sendmail.create_raw_email(self._requester_addr,
                                                     self._response_from_addr,
                                                     self._subject,
                                                     conf['body'], attachments,
                                                     extra_headers)

            if not raw_response:
                full_success = False
                continue

            if conf.get('send_method', '').upper() == 'SES':
                # If sending via SES, we'll use its DKIM facility -- so don't do it here.
                try:
                    if not sendmail.send_raw_email_amazonses(
                            raw_response, self._response_from_addr):
                        return False
                except BotoServerError as ex:
                    if ex.error_message == 'Address blacklisted.':
                        logger.critical(
                            'fail: requester address blacklisted by SES')
                    else:
                        exception_to_raise = ex

                    full_success = False
                    continue
            else:
                raw_response = _dkim_sign_email(raw_response)

                if not sendmail.send_raw_email_smtp(
                        raw_response,
                        settings.COMPLAINTS_ADDRESS,  # will be Return-Path
                        self._requester_addr):
                    full_success = False
                    continue

        if exception_to_raise:
            raise exception_to_raise
        return full_success
Esempio n. 11
0
    def process_email(self, email_string):
        '''
        Processes the given email and sends a response.
        Returns True if successful, False or exception otherwise.
        '''

        self._email_string = email_string

        if not self._parse_email(email_string):
            return False

        # Look up all config entries matching the requested address.
        request_conf = [item for item in self._conf if item['email_addr'] == self.requested_addr]

        # If we didn't find anything for that address, exit.
        if not request_conf:
            logger.info('fail: invalid requested address: %s', self.requested_addr)
            return False

        # Check if the user is (or should be) blacklisted
        if not self._check_blacklist():
            logger.info('fail: blacklist')
            return False

        # Process each config entry found the for the requested address separately.
        # Don't fail out early, since the other email send method has a chance
        # to succeed even if one fails. (I.e., SMTP will succeed even if there's
        # a SES service problem.)
        full_success = True
        exception_to_raise = None
        for conf in request_conf:
            attachments = None
            if conf['attachments']:
                attachments = []
                for attachment_info in conf['attachments']:
                    bucketname, bucket_filename, attachment_filename = attachment_info
                    attachments.append((aws_helpers.get_s3_attachment(settings.ATTACHMENT_CACHE_DIR,
                                                                      bucketname,
                                                                      bucket_filename),
                                        attachment_filename))

            extra_headers = {'Reply-To': self.requested_addr}

            if self._requester_msgid:
                extra_headers['In-Reply-To'] = self._requester_msgid
                extra_headers['References'] = self._requester_msgid

            raw_response = sendmail.create_raw_email(self._requester_addr,
                                                     self._response_from_addr,
                                                     self._subject,
                                                     conf['body'],
                                                     attachments,
                                                     extra_headers)

            if not raw_response:
                full_success = False
                continue

            if conf.get('send_method', '').upper() == 'SES':
                # If sending via SES, we'll use its DKIM facility -- so don't do it here.
                try:
                    if not sendmail.send_raw_email_amazonses(raw_response,
                                                             self._response_from_addr):
                        return False
                except BotoServerError as ex:
                    if ex.error_message == 'Address blacklisted.':
                        logger.critical('fail: requester address blacklisted by SES')
                    else:
                        exception_to_raise = ex

                    full_success = False
                    continue
            else:
                raw_response = _dkim_sign_email(raw_response)

                if not sendmail.send_raw_email_smtp(raw_response,
                                                    settings.COMPLAINTS_ADDRESS,  # will be Return-Path
                                                    self._requester_addr):
                    full_success = False
                    continue

        if exception_to_raise:
            raise exception_to_raise
        return full_success
    email_body += get_exception_info()
    email_body += '\n\n'
    email_body += 'SES quota info\n----------------------\n' + get_ses_quota()
    email_body += '\n\n'
    email_body += 'Instance info\n----------------------\n' + get_instance_info(
    )
    email_body += '\n\n'
    email_body += 'Logwatch Basic\n----------------------\n' + logwatch_basic

    email_body += 'Instance-specific stats'
    email_body += '\n\n'
    email_body += loginfo

    email_body += '</pre>'

    subject = '%s Stats' % (settings.STATS_SUBJECT_TAG, )

    raw_email = sendmail.create_raw_email(
        settings.STATS_RECIPIENT_ADDRESS, settings.STATS_SENDER_ADDRESS,
        subject, [['plain', email_body], ['html', email_body]])

    if not raw_email:
        sys.exit(1)

    if not sendmail.send_raw_email_smtp(raw_email,
                                        settings.STATS_SENDER_ADDRESS,
                                        settings.STATS_RECIPIENT_ADDRESS):
        sys.exit(1)

    sys.exit(0)
Esempio n. 13
0
    email_body += 'Postfix queue counts\n----------------------\n' + queue_check
    email_body += '\n\n'
    email_body += get_send_info()
    email_body += '\n\n'
    email_body += get_exception_info()
    email_body += '\n\n'
    email_body += 'SES quota info\n----------------------\n' + get_ses_quota()
    email_body += '\n\n'
    email_body += 'Instance info\n----------------------\n' + get_instance_info()
    email_body += '\n\n'
    email_body += 'Logwatch Basic\n----------------------\n' + logwatch_basic

    email_body += '</pre>'

    subject = '%s Stats' % (settings.STATS_SUBJECT_TAG,)

    raw_email = sendmail.create_raw_email(settings.STATS_RECIPIENT_ADDRESS,
                                          settings.STATS_SENDER_ADDRESS,
                                          subject,
                                          [['plain', email_body], ['html', email_body]])

    if not raw_email:
        sys.exit(1)

    if not sendmail.send_raw_email_smtp(raw_email,
                                        settings.STATS_SENDER_ADDRESS,
                                        settings.STATS_RECIPIENT_ADDRESS):
        sys.exit(1)

    sys.exit(0)