Exemplo n.º 1
0
def process_ses_smtp_results(self, response):
    try:
        ses_message = json.loads(response['Message'])

        notification_type = ses_message['notificationType']
        headers = ses_message['mail']['commonHeaders']
        source = ses_message['mail']['source']
        recipients = ses_message['mail']['destination']

        if notification_type == 'Bounce':
            notification_type = determine_notification_bounce_type(
                notification_type, ses_message)

        aws_response_dict = get_aws_responses(notification_type)

        notification_status = aws_response_dict['notification_status']

        try:
            # Get service based on SMTP name
            service = services_dao.dao_services_by_partial_smtp_name(
                source.split("@")[-1])

            # Create a sent notification based on details from the payload
            template = templates_dao.dao_get_template_by_id(
                current_app.config['SMTP_TEMPLATE_ID'])

            for recipient in recipients:

                message = "".join((
                    'A message was sent from: \n',  # noqa: E126
                    source,
                    '\n\n to: \n',
                    recipient,
                    '\n\n on: \n',
                    headers["date"],
                    '\n\n with the subject: \n',
                    headers["subject"]))

                notification = process_notifications.persist_notification(
                    template_id=template.id,
                    template_version=template.version,
                    recipient=recipient,
                    service=service,
                    personalisation={
                        'subject': headers["subject"],
                        'message': message
                    },
                    notification_type=EMAIL_TYPE,
                    api_key_id=None,
                    key_type=KEY_TYPE_NORMAL,
                    reply_to_text=recipient,
                    created_at=headers["date"],
                    status=notification_status,
                    reference=ses_message['mail']['messageId'])

                if notification_type == 'Complaint':
                    _check_and_queue_complaint_callback_task(
                        *handle_smtp_complaint(ses_message))
                else:
                    _check_and_queue_callback_task(notification)

        except NoResultFound:
            reference = ses_message['mail']['messageId']
            current_app.logger.warning(
                "SMTP service not found for reference: {} (update to {})".
                format(reference, notification_status))
            return

        statsd_client.incr('callback.ses-smtp.{}'.format(notification_status))

        return True

    except Retry:
        raise

    except Exception as e:
        current_app.logger.exception(
            'Error processing SES SMTP results: {}'.format(type(e)))
        self.retry(queue=QueueNames.RETRY)
Exemplo n.º 2
0
def test_dao_services_by_partial_smtp_name(notify_db_session):
    create_service(service_name="SMTP CHAMP", smtp_user="******")
    services_from_db = dao_services_by_partial_smtp_name("smtp")
    assert services_from_db.name == "SMTP CHAMP"