0

I'm working with a django app called django-mailbox (http://django-mailbox.readthedocs.org/en/latest/index.html) the purpose of which is to consume emails.

The app creates a "Message" model that looks like:

u'django_mailbox.message': {
        'Meta': {'object_name': 'Message'},
        'body': ('django.db.models.fields.TextField', [], {}),
        'encoded': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
        'from_header': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
        u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
        'in_reply_to': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'replies'", 'null': 'True', 'to': u"orm['django_mailbox.Message']"}),
        'mailbox': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'messages'", 'to': u"orm['django_mailbox.Mailbox']"}),
        'message_id': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
        'outgoing': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
        'processed': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
        'read': ('django.db.models.fields.DateTimeField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}),
        'subject': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
        'to_header': ('django.db.models.fields.TextField', [], {})

I am trying to extract the email text from various messages. Using the database API in the I have run the following:

>>> from django_mailbox.models import Message
>>> o = Message.objects.filter(in_reply_to_id__gt=0)
>>> n = o.values('body')
>>> n
[{'body': u'RGVsaXZlcmVkLVRvOiByb2JiaW5zYWxpc0BnbWFpbC5jb20KUmVjZWl2.......

Obviously the body is encoded somehow. How can I decode this and find the actual text in the emails?

1 Answer 1

2

According to the docs:

Email message bodies are base-64 encoded when stored in the database.

So you can obtain the message contents by calling base64.b64decode from the base64 module.

The e-mail text that you've mentioned so far is:

>>> s = "RGVsaXZlcmVkLVRvOiByb2JiaW5zYWxpc0BnbWFpbC5jb20KUmVjZWl2"
>>> import base64
>>> base64.b64decode(s)
'Delivered-To: <some email address>\nReceiv'

I have removed the e-mail address from the decoded output so it's not included in search results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.