Example #1
0
def add_message_to_database(message):
    """
    Add the message to the database if it is unique according to its Message-ID field.

    Returns a set of contacts who are recipients of the message.
    """
    message_id = message.get('Message-ID')
    matching_messages = Mail.objects.filter(message_id__exact=message_id)
    contact_set = set()
    if len(matching_messages) == 0:
        m = Mail()
        m.sender = get_or_add_contact(*email.utils.parseaddr(message.get('from')))
        m.subject = message.get('Subject')
        m.date = datetime_from_email_date(message.get('Date'))
        m.message_id = get_if_present(message, 'Message-ID')
        m.thread_index = get_if_present(message, 'Thread-Index')
        m.in_reply_to = get_if_present(message, 'In-Reply-To')
        m.references = get_if_present(message, 'References')
        (m.content_type, m.body) = get_body(message)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            m.save()
        contacts = add_contacts_to_mail(m.to, message.get_all('to'))
        contact_set |= contacts
        contacts = add_contacts_to_mail(m.cc, message.get_all('cc'))
        contact_set |= contacts
        add_tags_to_mail(m)
        for hook in settings.MAILSHARE_NEW_EMAIL_HOOKS:
            hook(m)
    return contact_set