views:

311

answers:

1

I use Gmail and an application that notifies me if I've received a new email, containing its title in a tooltip. (GmailNotifier with Miranda-IM) Most of the emails I receive are ones I don't want to read, and it's annoying having to login to Gmail on a slow connection just to delete said email. I believe plugin is closed source.

I've been (unsuccessfully) trying to write a script that will login and delete the 'top' email (the one most recently received). However this is not as easy I thought it would be.

I first tried using imaplib, but discovered that it doesn't contain any of the methods I hoped it would. It's a bit like the dbapi spec, containing only minimal functionality incase the imap spec is changed. I then tried reading the imap RFC (rfc3501). Halfway through it, I realized I didn't want to write an entire mail client, so decided to try using pop3 instead.

poplib is also minimal but seemingly has what I need. However pop3 doesn't appear to sort the messages in any order I'm familiar with. I have to either call top() or retr() on every single email to read the headers if I want to see the date received.

I could probably iterate through every single message header, searching for the most recent date, but that's ugly. I want to avoid parsing my entire mailbox if possible. I also don't want to 'pop' the mailbox and download any other messages.

It's been 6 hours now and I feel no closer to a solution than when I started. Am I overlooking something simple? Is there another library I could try? (I found a 'chilkat' one, but it's bloated to hell, and I was hoping to do this with the standard library)

+1  A: 
import poplib

#connect to server
mailserver = poplib.POP3_SSL('pop.gmail.com')
mailserver.user('recent:YOURUSERNAME') #use 'recent mode'
mailserver.pass_('YOURPASSWORD') #consider not storing in plaintext!

#newest email has the highest message number
numMessages = len(mailserver.list()[1])

#confirm this is the right one, can comment these out later
newestEmail = mailserver.retr(numMessages)
print newestEmail

#most servers will not delete until you quit
mailserver.dele(numMessages)
mailserver.quit()

I worked with the poplib recently, writing a very primitive email client. I tested this with my email server (not gmail) on some test emails and it seemed to work correctly. I would send yourself a few dummy emails to test it out first.

Caveats:

Hope this helps, it should be enough to get you going!

swanson
That's actually identical to what I had. But for some reason Gmail doesn't display my messages in chronological order.In my case, numMessages does give the last email in Gmails seemingly unordered list, but it's a random one from over a year agoI really appreciate the help. Perhaps the problem lies with Gmail?
Gary Oldfaber
After adding some primitive debug code, I've checked the header timestamps for every single message.My Gmail pop account apparently hasn't received any mail since 2007.I don't know how Google implement their pop3 system, but it appears to be bugged. There's no correlation between emails in my pop3 account and the web interface.It's as though the pop3 account contains half of them, and the web contains the other half.
Gary Oldfaber
Hi Gary,Try adding replacing your username with "recent:username" in the mailserver.user() line above. Also make sure your Gmail account is set to allow POP3 in the configuration settings.I do see what you mean about the 'random' message numbers, but if you read here (http://mail.google.com/support/bin/answer.py?answer=47948) you will see that you need to use "recent mode" so that all messages are available.I think if you don't use the recent mode, it is maybe returning the most recent unread message? That's just a hunch.
swanson
Anyways, using the code above I was able to delete the most recent messages from my Gmail account.
swanson
Perfect! Adding recent:username worked.
Gary Oldfaber