Example #1
0
def draft_send_api():
    if request.content_type == "message/rfc822":
        msg = create_draft_from_mime(g.namespace.account, request.data,
                                     g.db_session)
        validate_draft_recipients(msg)
        resp = send_raw_mime(g.namespace.account, g.db_session, msg)
        return resp

    data = request.get_json(force=True)
    draft_public_id = data.get('draft_id')
    if draft_public_id is not None:
        draft = get_draft(draft_public_id, data.get('version'), g.namespace.id,
                          g.db_session)
        schedule_action('delete_draft',
                        draft,
                        draft.namespace.id,
                        g.db_session,
                        inbox_uid=draft.inbox_uid,
                        message_id_header=draft.message_id_header)
    else:
        draft = create_draft(data, g.namespace, g.db_session, syncback=False)

    validate_draft_recipients(draft)
    resp = send_draft(g.namespace.account, draft, g.db_session)
    return resp
Example #2
0
def draft_send_api():
    data = request.get_json(force=True)
    draft_public_id = data.get('draft_id')
    if draft_public_id is not None:
        draft = get_draft(draft_public_id, data.get('version'), g.namespace.id,
                          g.db_session)
        if not any((draft.to_addr, draft.cc_addr, draft.bcc_addr)):
            raise InputError('No recipients specified')
        validate_draft_recipients(draft)
        resp = send_draft(g.namespace.account, draft, g.db_session,
                          schedule_remote_delete=True)
    else:
        to = get_recipients(data.get('to'), 'to', validate_emails=True)
        cc = get_recipients(data.get('cc'), 'cc', validate_emails=True)
        bcc = get_recipients(data.get('bcc'), 'bcc', validate_emails=True)
        if not any((to, cc, bcc)):
            raise InputError('No recipients specified')
        subject = data.get('subject')
        body = data.get('body')
        tags = get_tags(data.get('tags'), g.namespace.id, g.db_session)
        files = get_attachments(data.get('file_ids'), g.namespace.id,
                                g.db_session)
        replyto_thread = get_thread(data.get('thread_id'), g.namespace.id,
                                    g.db_session)

        draft = sendmail.create_draft(g.db_session, g.namespace.account,
                                      to, subject, body, files, cc, bcc,
                                      tags, replyto_thread, syncback=False)
        resp = send_draft(g.namespace.account, draft, g.db_session,
                          schedule_remote_delete=False)

    return resp
Example #3
0
def draft_send_api():
    data = request.get_json(force=True)
    draft_public_id = data.get('draft_id')
    if draft_public_id is not None:
        draft = get_draft(draft_public_id, data.get('version'), g.namespace.id,
                          g.db_session)
        validate_draft_recipients(draft)
        resp = send_draft(g.namespace.account, draft, g.db_session,
                          schedule_remote_delete=True)
    else:
        draft = create_draft(data, g.namespace, g.db_session, syncback=False)
        validate_draft_recipients(draft)
        resp = send_draft(g.namespace.account, draft, g.db_session,
                          schedule_remote_delete=False)
        if resp.status_code == 200:
            # At this point, the message has been successfully sent. If there's
            # any sort of error in committing the updated state, don't allow it
            # to cause the request to fail. Otherwise a client may think their
            # message hasn't been sent, when it fact it has.
            try:
                g.db_session.add(draft)
                g.db_session.commit()
            except Exception as exc:
                g.log.critical('Error committing draft after successful send',
                               exc_info=True, error=exc)
    return resp
Example #4
0
def draft_send_api():
    data = request.get_json(force=True)
    draft_public_id = data.get('draft_id')
    if draft_public_id is not None:
        draft = get_draft(draft_public_id, data.get('version'), g.namespace.id,
                          g.db_session)
        if not any((draft.to_addr, draft.cc_addr, draft.bcc_addr)):
            raise InputError('No recipients specified')
        validate_draft_recipients(draft)
        resp = send_draft(g.namespace.account,
                          draft,
                          g.db_session,
                          schedule_remote_delete=True)
    else:
        to = get_recipients(data.get('to'), 'to', validate_emails=True)
        cc = get_recipients(data.get('cc'), 'cc', validate_emails=True)
        bcc = get_recipients(data.get('bcc'), 'bcc', validate_emails=True)
        if not any((to, cc, bcc)):
            raise InputError('No recipients specified')
        subject = data.get('subject')
        body = data.get('body')
        tags = get_tags(data.get('tags'), g.namespace.id, g.db_session)
        files = get_attachments(data.get('file_ids'), g.namespace.id,
                                g.db_session)
        replyto_thread = get_thread(data.get('thread_id'), g.namespace.id,
                                    g.db_session)

        draft = sendmail.create_draft(g.db_session,
                                      g.namespace.account,
                                      to,
                                      subject,
                                      body,
                                      files,
                                      cc,
                                      bcc,
                                      tags,
                                      replyto_thread,
                                      syncback=False)
        resp = send_draft(g.namespace.account,
                          draft,
                          g.db_session,
                          schedule_remote_delete=False)

    return resp
Example #5
0
def draft_send_api():
    if request.content_type == "message/rfc822":
        msg = create_draft_from_mime(g.namespace.account, request.data,
                                     g.db_session)
        validate_draft_recipients(msg)
        resp = send_raw_mime(g.namespace.account, g.db_session, msg)
        return resp

    data = request.get_json(force=True)
    draft_public_id = data.get('draft_id')
    if draft_public_id is not None:
        draft = get_draft(draft_public_id, data.get('version'), g.namespace.id,
                          g.db_session)
        schedule_action('delete_draft', draft, draft.namespace.id,
                        g.db_session, inbox_uid=draft.inbox_uid,
                        message_id_header=draft.message_id_header)
    else:
        draft = create_draft(data, g.namespace, g.db_session, syncback=False)

    validate_draft_recipients(draft)
    resp = send_draft(g.namespace.account, draft, g.db_session)
    return resp
Example #6
0
def draft_send_api():
    data = request.get_json(force=True)
    if data.get('draft_id') is None:
        if data.get('to') is None:
            return err(400, 'Must specify either draft id + version or '
                       'message recipients.')
    else:
        if data.get('version') is None:
            return err(400, 'Must specify version to send')

    draft_public_id = data.get('draft_id')
    version = data.get('version')
    if draft_public_id is not None:
        try:
            valid_public_id(draft_public_id)
            draft = g.db_session.query(Message).filter(
                Message.public_id == draft_public_id).one()
        except InputError:
            return err(400, 'Invalid public id {}'.format(draft_public_id))
        except NoResultFound:
            return err(404, 'No draft found with id {}'.
                       format(draft_public_id))

        if draft.namespace != g.namespace:
            return err(404, 'No draft found with id {}'.
                       format(draft_public_id))

        if draft.is_sent or not draft.is_draft:
            return err(400, 'Message with id {} is not a draft'.
                       format(draft_public_id))

        if not draft.to_addr:
            return err(400, "No 'to:' recipients specified")

        if draft.version != version:
            return err(
                409, 'Draft {0}.{1} has already been updated to version {2}'.
                format(draft_public_id, version, draft.version))

        validate_draft_recipients(draft)

        try:
            schedule_action('send_draft', draft, g.namespace.id, g.db_session)
        except ActionError as e:
            return err(e.error, str(e))
    else:
        to = get_recipients(data.get('to'), 'to', validate_emails=True)
        cc = get_recipients(data.get('cc'), 'cc', validate_emails=True)
        bcc = get_recipients(data.get('bcc'), 'bcc', validate_emails=True)
        subject = data.get('subject')
        body = data.get('body')
        try:
            tags = get_tags(data.get('tags'), g.namespace.id, g.db_session)
            files = get_attachments(data.get('file_ids'), g.namespace.id,
                                    g.db_session)
            replyto_thread = get_thread(data.get('thread_id'),
                                        g.namespace.id, g.db_session)
        except InputError as e:
            return err(404, e.message)

        try:
            draft = sendmail.create_draft(g.db_session, g.namespace.account,
                                          to, subject, body, files, cc, bcc,
                                          tags, replyto_thread, syncback=False)
            schedule_action('send_directly', draft, g.namespace.id,
                            g.db_session)
        except ActionError as e:
            return err(e.error, str(e))

    draft.state = 'sending'
    return g.encoder.jsonify(draft)
Example #7
0
def draft_send_api():
    data = request.get_json(force=True)
    if data.get('draft_id') is None:
        if data.get('to') is None:
            return err(
                400, 'Must specify either draft id + version or '
                'message recipients.')
    else:
        if data.get('version') is None:
            return err(400, 'Must specify version to send')

    draft_public_id = data.get('draft_id')
    version = data.get('version')
    if draft_public_id is not None:
        try:
            valid_public_id(draft_public_id)
            draft = g.db_session.query(Message).filter(
                Message.public_id == draft_public_id).one()
        except InputError:
            return err(400, 'Invalid public id {}'.format(draft_public_id))
        except NoResultFound:
            return err(404,
                       'No draft found with id {}'.format(draft_public_id))

        if draft.namespace != g.namespace:
            return err(404,
                       'No draft found with id {}'.format(draft_public_id))

        if draft.is_sent or not draft.is_draft:
            return err(
                400,
                'Message with id {} is not a draft'.format(draft_public_id))

        if not draft.to_addr:
            return err(400, "No 'to:' recipients specified")

        if draft.version != version:
            return err(
                409,
                'Draft {0}.{1} has already been updated to version {2}'.format(
                    draft_public_id, version, draft.version))

        validate_draft_recipients(draft)

        try:
            schedule_action('send_draft', draft, g.namespace.id, g.db_session)
        except ActionError as e:
            return err(e.error, str(e))
    else:
        to = get_recipients(data.get('to'), 'to', validate_emails=True)
        cc = get_recipients(data.get('cc'), 'cc', validate_emails=True)
        bcc = get_recipients(data.get('bcc'), 'bcc', validate_emails=True)
        subject = data.get('subject')
        body = data.get('body')
        try:
            tags = get_tags(data.get('tags'), g.namespace.id, g.db_session)
            files = get_attachments(data.get('file_ids'), g.namespace.id,
                                    g.db_session)
            replyto_thread = get_thread(data.get('thread_id'), g.namespace.id,
                                        g.db_session)
        except InputError as e:
            return err(404, e.message)

        try:
            draft = sendmail.create_draft(g.db_session,
                                          g.namespace.account,
                                          to,
                                          subject,
                                          body,
                                          files,
                                          cc,
                                          bcc,
                                          tags,
                                          replyto_thread,
                                          syncback=False)
            schedule_action('send_directly', draft, g.namespace.id,
                            g.db_session)
        except ActionError as e:
            return err(e.error, str(e))

    draft.state = 'sending'
    return g.encoder.jsonify(draft)