forked from mirror/django-mailbox
Adding get_text_body convenience method.
This commit is contained in:
parent
0a283e9ade
commit
afa82b861c
4 changed files with 58 additions and 1 deletions
|
|
@ -320,6 +320,21 @@ class Message(models.Model):
|
|||
)
|
||||
)
|
||||
|
||||
def get_text_body(self):
|
||||
def get_body_from_message(message):
|
||||
body = ''
|
||||
if message.is_multipart():
|
||||
for part in message.get_payload():
|
||||
if part.get('content-type', '').split(';')[0] == 'text/plain':
|
||||
body = body + get_body_from_message(part)
|
||||
else:
|
||||
body = body + message.get_payload()
|
||||
return body
|
||||
|
||||
return get_body_from_message(
|
||||
self.get_email_object()
|
||||
)
|
||||
|
||||
def get_email_object(self):
|
||||
return email.message_from_string(self.body)
|
||||
|
||||
|
|
|
|||
|
|
@ -63,3 +63,17 @@ class TestProcessMessage(TestCase):
|
|||
os.path.basename(attachment.document.name),
|
||||
'heart.png',
|
||||
)
|
||||
|
||||
def test_message_get_text_body(self):
|
||||
message = self._get_email_object('multipart_text.eml')
|
||||
|
||||
mailbox = Mailbox.objects.create()
|
||||
msg = mailbox.process_incoming_message(message)
|
||||
|
||||
expected_results = 'Hello there!'
|
||||
actual_results = msg.get_text_body().strip()
|
||||
|
||||
self.assertEquals(
|
||||
expected_results,
|
||||
actual_results,
|
||||
)
|
||||
|
|
|
|||
28
django_mailbox/tests/multipart_text.eml
Normal file
28
django_mailbox/tests/multipart_text.eml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Return-path: <test@adamcoddington.net>
|
||||
Delivery-date: Thu, 17 Jan 2013 19:26:25 +0000
|
||||
Received: by mail-vc0-f174.google.com with SMTP id n11so944457vch.33
|
||||
for <ya@dumayju.net>; Thu, 17 Jan 2013 11:26:20 -0800 (PST)
|
||||
X-Received: by 10.220.226.68 with SMTP id iv4mr332182vcb.31.1358450780039;
|
||||
Thu, 17 Jan 2013 11:26:20 -0800 (PST)
|
||||
MIME-Version: 1.0
|
||||
Received: by 10.221.0.211 with HTTP; Thu, 17 Jan 2013 11:25:59 -0800 (PST)
|
||||
X-Originating-IP: [67.131.102.78]
|
||||
From: Adam Coddington <test@adamcoddington.net>
|
||||
Date: Thu, 17 Jan 2013 11:25:59 -0800
|
||||
Message-ID: <CAMdmm+hK=swkM_SJ3EfoAO-AAE1Rr8Nyd5-OU3BHG6_1u5AdUw@mail.gmail.com>
|
||||
Subject: Test Multipart Text E-mail
|
||||
To: test@latestrevision.net
|
||||
Content-Type: multipart/alternative; boundary=14dae9cdc8cb30695204d380f8a2
|
||||
X-Gm-Message-State: ALoCoQm0/CGO1NxYZBBXjGN7748Ohv5P068bWQiELfbo+g19i80yvx1xi0th+N28ElGN1oS06SuR
|
||||
|
||||
--14dae9cdc8cb30695204d380f8a2
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
|
||||
Hello there!
|
||||
|
||||
--14dae9cdc8cb30695204d380f8a2
|
||||
Content-Type: text/html; charset=UTF-8
|
||||
|
||||
<div dir="ltr">Hello there!</div>
|
||||
|
||||
--14dae9cdc8cb30695204d380f8a2--
|
||||
Loading…
Add table
Add a link
Reference in a new issue