Getting only new mail from an IMAP server
You want to use the UniqueId (UID) for the messages. This is specifically why it was created.
You will want to keep track of the last UID requested, and then, to request all new messages you use the message set "[UID]:*", where [UID] is the actual UID value.
For example, lets say the last message feteched had a unique id of "123456". You would fetch
123456:*
Then, discard the first returned message.
UIDs are 'supposed' to be stable across sessions, and never change, and always increase in value. The catch to verify this, is to check the UIDValidity when you select the folder. If the UIDValidity number hasn't changed, then the UIDs should still be valid across sessions.
Here are the relevant parts from the RFC:
2.3.1.1. Unique Identifier (UID) Message Attribute
A 32-bit value assigned to each message, which when used with the
unique identifier validity value (see below) forms a 64-bit value
that MUST NOT refer to any other message in the mailbox or any
subsequent mailbox with the same name forever. Unique identifiers
are assigned in a strictly ascending fashion in the mailbox; as each
message is added to the mailbox it is assigned a higher UID than the
message(s) which were added previously. Unlike message sequence
numbers, unique identifiers are not necessarily contiguous.
The unique identifier of a message MUST NOT change during the
session, and SHOULD NOT change between sessions. Any change of
unique identifiers between sessions MUST be detectable using the
UIDVALIDITY mechanism discussed below. Persistent unique identifiers
are required for a client to resynchronize its state from a previous
session with the server (e.g., disconnected or offline access
clients); this is discussed further in [IMAP-DISC].
Note: The next unique identifier value is intended to
provide a means for a client to determine whether any
messages have been delivered to the mailbox since the
previous time it checked this value.
Here is the link with more info:
http://www.faqs.org/rfcs/rfc3501.html
What I would do, is also keep track of the InternalDate of the messages downloaded. This way, if you ever lose UID sync, you can at least iterate through the messages, and find the last one you downloaded, based upon the InternalDate of the message.
Get only NEW Emails imaplib and python
Something like this will do the trick.
conn = imaplib.IMAP4_SSL(imap_server)
try:
(retcode, capabilities) = conn.login(imap_user, imap_password)
except:
print sys.exc_info()[1]
sys.exit(1)
conn.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
for num in messages[0].split(' '):
print 'Processing :', message
typ, data = conn.fetch(num,'(RFC822)')
msg = email.message_from_string(data[0][1])
typ, data = conn.store(num,'-FLAGS','\\Seen')
if ret == 'OK':
print data,'\n',30*'-'
print msg
conn.close()
There's also a duplicate question here - Find new messages added to an imap mailbox since I last checked with python imaplib2?
Two useful functions for you to retrieve the body and attachments of the new message you detected (reference: How to fetch an email body using imaplib in python?) -
def getMsgs(servername="myimapserverfqdn"):
usernm = getpass.getuser()
passwd = getpass.getpass()
subject = 'Your SSL Certificate'
conn = imaplib.IMAP4_SSL(servername)
conn.login(usernm,passwd)
conn.select('Inbox')
typ, data = conn.search(None,'(UNSEEN SUBJECT "%s")' % subject)
for num in data[0].split():
typ, data = conn.fetch(num,'(RFC822)')
msg = email.message_from_string(data[0][1])
typ, data = conn.store(num,'-FLAGS','\\Seen')
yield msg
def getAttachment(msg,check):
for part in msg.walk():
if part.get_content_type() == 'application/octet-stream':
if check(part.get_filename()):
return part.get_payload(decode=1)
PS: If you pass by in 2020 after python 2.7 death: replace email.message_from_string(data[0][1])
with email.message_from_bytes(data[0][1])
How to find only new messages when using PHP's IMAP functions for a POP3 mailbox?
Well found this..
Basically, pop3 protocol doesn't support this function. However, you
can implement this with message-id. Message-Id is an unique identifier of email on
POP3 server. Your application can get message-id of a specified email by GetMsgID
method of POPMAIN object.Firstly, your application should record message-id of email retrieved to a local
message-id list. Next time before you retrieve email, compare local message-id
with remote message-id. If this message-id exists in your
local message-id list, then it is old, otherwise it is new.
Although I'm pretty sure I read before that not all clients will returns the message_id... anyone know if this is correct?
How to download only new emails from imap?
The javadoc helps:
IMAPFolder provides the methods:
getMessagesByUID(long start, long end) and
getUID(Message message)
With getUID() you can get the UID of the last message you already have downloaded. With getMessagesByUID you can define this last message you have downloaded as start-range and look with the method getUIDNext() to find the last message which would be the end of the range.
Related Topics
Difference Between Plugins and Ruby Gems
How to Call an Older Version of a Gem from the Commandline
Given the Session Key and Secret, How to Decrypt Rails Cookies
Writing Over Previously Output Lines in the Command Prompt with Ruby
Hot Deploy on Heroku with No Downtime
Rake Assets:Precompile Attempting to Connect to Database
How to or Should I Find an Object by the Object_Id Attribute in Ruby
Dynamic Rails Routes Based on Database Models
Rails: Plus Sign in Get-Request Replaced by Space
Hashes of Hashes Idiom in Ruby
How to Measure the Size of a Ruby Object
Ruby: Extracting Words from String
Is There a Simple Way to Get Image Dimensions in Ruby
Difference Between Kernel#Yield_Self, Yield(Self), Kernel#Then and Object#Tap in Ruby