Exemplo n.º 1
0
def test_save_draft_syncback(db, config, message):
    from inbox.server.actions.gmail import remote_save_draft
    from inbox.server.sendmail.base import all_recipients
    from inbox.server.sendmail.message import create_email, SenderInfo
    from inbox.server.models.tables.imap import ImapAccount

    account = db.session.query(ImapAccount).get(ACCOUNT_ID)
    sender_info = SenderInfo(name=account.full_name,
                             email=account.email_address)
    to, subject, body = message
    email = create_email(sender_info, all_recipients(to), subject, body, None)
    flags = [u'\\Draft']
    date = datetime.utcnow()

    remote_save_draft(ACCOUNT_ID, account.drafts_folder.name,
                      email.to_string(), flags=flags, date=date)

    with crispin_client(account.id, account.provider) as c:
        criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)]

        c.conn.select_folder(account.drafts_folder.name, readonly=False)

        inbox_uids = c.conn.search(criteria)
        assert inbox_uids, 'Message missing from Drafts folder'

        c.conn.delete_messages(inbox_uids)
        c.conn.expunge()
Exemplo n.º 2
0
def test_save_draft_syncback(db, config, message):
    from inbox.server.actions.gmail import remote_save_draft
    from inbox.server.sendmail.base import all_recipients
    from inbox.server.sendmail.message import create_email, SenderInfo
    from inbox.server.models.tables.imap import ImapAccount

    account = db.session.query(ImapAccount).get(ACCOUNT_ID)
    sender_info = SenderInfo(name=account.full_name,
                             email=account.email_address)
    to, subject, body = message
    email = create_email(sender_info, all_recipients(to), subject, body, None)
    flags = [u'\\Draft']
    date = datetime.utcnow()

    remote_save_draft(ACCOUNT_ID,
                      account.drafts_folder.name,
                      email.to_string(),
                      flags=flags,
                      date=date)

    with crispin_client(account.id, account.provider) as c:
        criteria = ['NOT DELETED', 'SUBJECT "{0}"'.format(subject)]

        c.conn.select_folder(account.drafts_folder.name, readonly=False)

        inbox_uids = c.conn.search(criteria)
        assert inbox_uids, 'Message missing from Drafts folder'

        c.conn.delete_messages(inbox_uids)
        c.conn.expunge()
Exemplo n.º 3
0
def send(db_session, account, draft_public_id):
    """ Send the draft with public_id = draft_public_id. """
    sendmail_client = GmailSMTPClient(account.id, account.namespace)

    try:
        draft = db_session.query(SpoolMessage).filter(
            SpoolMessage.public_id == draft_public_id).one()
    except NoResultFound:
        log.info('NoResultFound for draft with public_id {0}'.format(
            draft_public_id))
        raise SendMailException('No draft with public_id {0} found'.format(
            draft_public_id))
    except MultipleResultsFound:
        log.info('MultipleResultsFound for draft with public_id {0}'.format(
            draft_public_id))
        raise SendMailException('Multiple drafts with public_id {0} found'.
                                format(draft_public_id))

    assert draft.is_draft and not draft.is_sent

    if not draft.to_addr:
        raise SendMailException("No 'To:' recipients specified!")

    assert len(draft.imapuids) == 1

    concat = lambda xlist: [u'{0} <{1}>'.format(e[0], e[1]) for e in xlist]
    recipients = all_recipients(concat(draft.to_addr), concat(draft.cc_addr),
                                concat(draft.bcc_addr))
    attachments = None

    if not draft.replyto_thread:
        return sendmail_client.send_new(db_session, draft.imapuids[0],
                                        recipients, draft.subject,
                                        draft.sanitized_body,
                                        attachments)
    else:
        assert isinstance(draft.replyto_thread, DraftThread)
        thread = draft.replyto_thread.thread

        message_id = draft.replyto_thread.message_id
        if thread.messages[0].id != message_id:
            raise SendMailException(
                'Can only send a reply to the latest message in thread!')

        thread_subject = thread.subject
        # The first message is the latest message we have for this thread
        message_id_header = thread.messages[0].message_id_header
        # The references are JWZ compliant
        references = thread.messages[0].references
        body = thread.messages[0].prettified_body

        # Encapsulate the attributes of the message to reply to,
        # needed to set the right headers, subject on the reply.
        replyto_attrs = ReplyToAttrs(subject=thread_subject,
                                     message_id_header=message_id_header,
                                     references=references,
                                     body=body)

        return sendmail_client.send_reply(db_session, draft.imapuids[0],
                                          replyto_attrs, recipients,
                                          draft.subject, draft.sanitized_body,
                                          attachments)
Exemplo n.º 4
0
def send(db_session, account, draft_public_id):
    """ Send the draft with public_id = draft_public_id. """
    log = get_logger(account.id, 'drafts')
    sendmail_client = GmailSMTPClient(account.id, account.namespace)

    try:
        draft = db_session.query(SpoolMessage).filter(
            SpoolMessage.public_id == draft_public_id).one()
    except NoResultFound:
        log.info('NoResultFound for draft with public_id {0}'.format(
            draft_public_id))
        raise SendMailException(
            'No draft with public_id {0} found'.format(draft_public_id))
    except MultipleResultsFound:
        log.info('MultipleResultsFound for draft with public_id {0}'.format(
            draft_public_id))
        raise SendMailException(
            'Multiple drafts with public_id {0} found'.format(draft_public_id))

    assert draft.is_draft and not draft.is_sent

    if not draft.to_addr:
        raise SendMailException("No 'To:' recipients specified!")

    assert len(draft.imapuids) == 1

    concat = lambda xlist: [u'{0} <{1}>'.format(e[0], e[1]) for e in xlist]
    recipients = all_recipients(concat(draft.to_addr), concat(draft.cc_addr),
                                concat(draft.bcc_addr))
    attachments = None

    if not draft.replyto_thread:
        return sendmail_client.send_new(db_session, draft.imapuids[0],
                                        recipients, draft.subject,
                                        draft.sanitized_body, attachments)
    else:
        assert isinstance(draft.replyto_thread, DraftThread)
        thread = draft.replyto_thread.thread

        message_id = draft.replyto_thread.message_id
        if thread.messages[0].id != message_id:
            raise SendMailException(
                'Can only send a reply to the latest message in thread!')

        thread_subject = thread.subject
        # The first message is the latest message we have for this thread
        message_id_header = thread.messages[0].message_id_header
        # The references are JWZ compliant
        references = thread.messages[0].references
        body = thread.messages[0].prettified_body

        # Encapsulate the attributes of the message to reply to,
        # needed to set the right headers, subject on the reply.
        replyto_attrs = ReplyToAttrs(subject=thread_subject,
                                     message_id_header=message_id_header,
                                     references=references,
                                     body=body)

        return sendmail_client.send_reply(db_session, draft.imapuids[0],
                                          replyto_attrs, recipients,
                                          draft.subject, draft.sanitized_body,
                                          attachments)