automatic email import from mailserver

Hello group
is there a way to automatically import emails form an IMAP mailserver?
Sometimes, on the road, I’d like to send me notes.
Regards,
Vinz

Right now DEVONthink Pro Office is only able to import messages/mailboxes from email clients or from Unix mailboxes (.mbox files). But we’ll consider this for future releases.

Thanks Christian for your reply.

Because there seems to be no solution at the moment, I made a tiny python-script this afternoon. I tested it with my gmail account. It works.

I’m now able to send quick notes to my email-account and they are automatically imported to DEVONthink.

Maybe there is someone who can help me with this.
Vinz

It does:

  • Connect to my IMAP server and download all emails with “Note …” in subject.
  • Store them as textfiles to the DEVONthink Inbox for further handling.
  • Subject is the filename. Content is the content.

But be aware:

  • It re-downloads emails, that already have been downloaded.
  • It overwrites files with same name in Inbox.
  • There is no error handling.
  • There are names, passwords and absolute path in the script.
  • This is for testing only.
# Python
import imaplib
import email
import datetime
import os

# = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# This program connects to your IMAP-server and downloads
# emails with subject beginning with "Note " to DEVONthink.
#
# Disclaimer: This is for amusement only. Never execute
# this on any system!
# 2011-11-13 Vinz


# connect to imap server inbox
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('your.name@gmail.com', 'your_password')
mail.select("inbox")

# get my notes an save to DEVONthink
notes = get_my_notes(mail)
save_notes(notes)


# = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# function to get email plaintext
def get_first_text_block(email_message_instance):
    maintype = email_message_instance.get_content_maintype()
    if maintype == 'multipart':
        for payload in email_message_instance.get_payload():
            if payload.get_content_maintype() == 'text':
                return payload.get_payload()
    elif maintype == 'text':
        return email_message_instance.get_payload()

# = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# function to get emails: Subject beginning with "Note " and maximum 30 days old
def get_my_notes(mail_instance):
	date = (datetime.date.today() - datetime.timedelta(30)).strftime("%d-%b-%Y")
	result, data_mails = mail_instance.uid('search', None, '(SENTSINCE {date} HEADER Subject "Note *")'.format(date=date))
	email_uids = data_mails[0].split()
	ret_val = []
	for email_uid in email_uids:
		result, data_onemail = mail_instance.uid('fetch', email_uid, '(RFC822)')
		raw_email = data_onemail[0][1]
		email_message = email.message_from_string(raw_email)
		email_subject = email_message['Subject']
		email_text = get_first_text_block(email_message)
		ret_val += [[email_subject, email_text]]
	return ret_val

# = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# function to save my notes to DEVONthink
def save_notes(my_notes):
	for note in my_notes:
		subject = note[0]
		content = note[1]
		inbox_path = os.path.join(os.environ['HOME'], "Library/Application Support/DEVONthink Pro 2/Inbox/")
		if not os.path.exists(inbox_path):
			print "Can't find DEVONthink INBOX. Check path in script."
			return
		filename = os.path.join(inbox_path, subject + ".txt")
		f = open(filename, 'w+')
		f.write(content)
		f.close()
		print "Saved new note: " + subject

Edit:
2011-11-13 17:45: Better creation of path to DEVONthink Inbox.

You might try to post this in the Scripting board to get more feedback.

thanks for the advice.

I started an new thread there: Script for automatic email import from mail server

Vinz

Yes,

Please improve the email import / archive functionality.

Dan