Exemple #1
0
def build_message(message_type, uid, message, flags=None):
    if message_type not in ['incoming', 'outgoing']:
        raise ValueError('Invalid message type: %s' % (message_type))

    headers = get_message_headers(message)
    text_body, html_body = get_message_text_and_html_part(message)

    subject = headers.get('Subject', None)
    sender = headers.get('From', None)
    recipient = headers.get('To', None)

    if sender:
        sender = Person.from_string(sender)

    if recipient:
        recipient = Person.from_string(recipient)

    date_sent = headers.get('Date', None)
    date_received = headers.get('X-Received', None)

    if date_received:
        date_received = date_received.split('\n')[-1].strip()

    read = None

    if flags:
        read = '\Seen' in flags

    spf_signature = headers.get('Received-SPF', None)
    dkim_signature = headers.get('DKIM-Signature', None)

    authentication_results = headers.get('Authentication-Results', None)
    valid_spf_signature = None
    valid_dkim_signature = None

    if authentication_results:
        valid_spf_signature = 'spf=pass' in authentication_results
        valid_dkim_signature = 'dkim=pass' in authentication_results

    kwargs = {'uid': uid, 'subject': subject, 'sender': sender,
              'recipient': recipient, 'headers': headers, 'read': read,
              'text_body': text_body, 'html_body': html_body}

    if message_type == 'incoming':
        cls = IncomingMessage
        kwargs['date_sent'] = date_sent
        kwargs['date_received'] = date_received
        kwargs['spf_signature'] = spf_signature
        kwargs['dkim_signature'] = dkim_signature
        kwargs['valid_spf_signature'] = valid_spf_signature
        kwargs['valid_dkim_signature'] = valid_dkim_signature
    elif message_type == 'outgoing':
        cls = OutgoingMessage
        kwargs['date_sent'] = date_sent

    message = cls(**kwargs)
    return message
    def test_person_from_string(self):
        values = (
            ('Komal Gupta <*****@*****.**>', ('Komal Gupta',
                                              '*****@*****.**')),
            ('Komal <*****@*****.**>', ('Komal', '*****@*****.**')),
            ('<*****@*****.**>', (None, '*****@*****.**')),
            ('komal Gupta          <*****@*****.**>', ('Komal Gupta',
                                                       '*****@*****.**')),
        )

        for string, expected in values:
            person = Person.from_string(string)

            self.assertEqual(person.email, expected[1])
            self.assertEqual(person.name, expected[0])
    def test_person_from_string(self):
        values = (
            ('Tomaz Muraus <*****@*****.**>', ('Tomaz Muraus',
                                               '*****@*****.**')),
            ('Tomaz <*****@*****.**>', ('Tomaz', '*****@*****.**')),
            ('<*****@*****.**>', (None, '*****@*****.**')),
            ('Tomaz Muraus          <*****@*****.**>', ('Tomaz Muraus',
                                                        '*****@*****.**')),
        )

        for string, expected in values:
            person = Person.from_string(string)

            self.assertEqual(person.email, expected[1])
            self.assertEqual(person.name, expected[0])
    def test_person_from_string(self):
        values = (
            ('Tomaz Muraus <*****@*****.**>',
             ('Tomaz Muraus', '*****@*****.**')),
            ('Tomaz <*****@*****.**>', ('Tomaz', '*****@*****.**')),
            ('<*****@*****.**>', (None, '*****@*****.**')),
            ('Tomaz Muraus          <*****@*****.**>',
            ('Tomaz Muraus', '*****@*****.**')),
        )

        for string, expected in values:
            person = Person.from_string(string)

            self.assertEqual(person.email, expected[1])
            self.assertEqual(person.name, expected[0])