Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: IMAP Problems

Reply
Thread Tools

Re: IMAP Problems

 
 
MRAB
Guest
Posts: n/a
 
      07-03-2010
Paul Jefferson wrote:
> Hi,
> I'm trying to write a simple script which displays the basic details
> of a person's mailbox. My problem is that it causes all the messages
> to be marked as read on the server, which is not what I'm after, and
> I also can't get the imap.sort command to work properly (currently
> commented out as I replaced it with a imap.search to get the thing
> working.
> These are probably very simple things, but I've not tried this library
> before so am a bit stuck so any help wwould be very gratefully
> received.
> Thanks,
> Paul
>
> Code:
>
> # -*- coding: cp1252 -*-
> import imaplib,email
>
> # you want to connect to a server; specify which server
> server= imaplib.IMAP4_SSL('imap.googlemail.com
> <http://imap.googlemail.com>')
> # after connecting, tell the server who you are
> server.login('x...
> <http://groups.google.com/groups/unlock?_done=/group/comp.lang.python/browse_thread/thread/024b81e0ea199177&msg=c9ac781cea58a990>@gmail.com
> <http://gmail.com>', 'xxxxxxx')
> # this will show you a list of available folders
> # possibly your Inbox is called INBOX, but check the list of mailboxes
> code, mailboxen= server.list()
> print mailboxen
> # if it's called INBOX, then…
> server.select("INBOX")
>
> typ, data = server.search(None, 'ALL')
> #typ, data = server.sort("Date","UTF-8", 'ALL')
> print len(data[0].split())
> for num in data[0].split():
> typ, data = server.fetch(num, '(RFC822)')
> #print 'Message %s\n%s\n' % (num, data[0][1])
> msg = email.message_from_string(data[0][1])
> print msg["From"]
> print msg["Subject"]
> print msg["Date"]
> print "_______________________________"
>
> server.close()
> server.logout()
>

You might want to read what it says here:

http://tools.ietf.org/html/rfc2060.html#page-41

If you can use '(BODY[])' instead of '(RFC822)' then you could use
'(BODY.PEEK[])'.

Alternatively, try:

server.store(num, '-FLAGS', r'\Seen')

to mark it as unread after fetching.
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      07-04-2010

> I'm trying to write a simple script which displays the basic details
> of a person's mailbox. My problem is that it causes all the messages
> to be marked as read on the server,
>
> code, mailboxen= server.list()
> print mailboxen
> # if it's called INBOX, then…
> server.select("INBOX")


You probably want to try examine() instead of select(). That opens
the mailbox in a read-only mode which and should avoid changing any
flag values.

From RFC3501:

The EXAMINE command is identical to SELECT and returns the same
output; however, the selected mailbox is identified as
read-only. No changes to the permanent state of the mailbox,
including per-user state, are permitted; in particular, EXAMINE
MUST NOT cause messages to lose the \Recent flag.

--
Grant

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Support for IMAP IDLE in net/imap Abhishiv Saxena Ruby 4 07-04-2009 04:07 PM
imap and Tmail. Getting whole email with imap Adam Akhtar Ruby 1 12-15-2008 10:55 PM
net/imap - imap.search "TO" can't find email jasonnaylor Ruby 1 04-16-2008 04:05 AM
Writing IMAP->GMail proxy... Where to go for IMAP RFC help? Jon Fi Ruby 4 10-21-2006 09:00 PM
Curier-IMAP and imap.create() Henrik Ormåsen Ruby 0 08-19-2006 06:29 AM



Advertisments