Skip to content
Advertisement

How to find uid of existing python email object

I have been reading through this document. Most of the document is based on finding an email’s uid. From the article:

"The way this works is pretty simple: use the uid function, and pass in the string of the command in as the first argument. The rest behaves exactly the same.

result, data = mail.uid('search', None, "ALL") # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]

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', [], {}) 

using the python “email” library I can select a record from a django queryset and turn it into an email object:

qs = Message.objects.filter("my criteria")
first = qs[0]
one = first.get_email_object() // one is an email object

Does the existing data in the db have a uid, and if so how can I grab it.

Advertisement

Answer

The strict answer to your question is “no”. The document you quote is about looping through an IMAP folder (in this case, a Gmail account), which will certainly get a unique ID (uid) from the server which tracks the unique message ID for each Email message.

Because you are constructing a mail message object using Django, you won’t have such a UID. The “ID” field you do get from django.db.models.fields.AutoField is the sequential auto-increment ID that the Gmail/IMAP web page you quote says is “unacceptable”.

You may want to look at the “uuid” library (http://docs.python.org/2/library/uuid.html) to generate unique ID values for your messages, but unless you also store those in your database, you’ll be re-generating them over and over.

If you care to share more exact information about what you’re trying to build (a web-based Email reader, perhaps?) then we as a community might have some better ideas for you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement