问题
I have an email interface client, and I am using IMAP for my requests. I want to be able to show, in real-time, basic email data, for a list view.
As in, for example, the GMail list view. For that, I need to do an IMAP request to obtain the subject of all emails, the date of all emails, etc. This works so far.
The problem is, I want to also show the first characters of the body text. If I use the BODYSTRUCTURE
call to obtain the index of the text/HTML
part it takes too long (for emails with thousands of characters it might take well over a second per email, while using only the subject/date/etc calls takes about 0.02 seconds max.
I tried using the BODY[INDEX]<0.XYZ>
bytes where XYZ
is the number of the first bytes we want to obtain, but to my dismay, it takes as long as using the BODY[INDEX]
call. Sometimes even more.
Is there another way to obtain the first text characters, but in a quick manner? If I want to list 300 emails on my interface I cannot afford to spend 1 second per email just to obtain the first text characters.
I'm using Python with imaplib for this, even though probably not relevant.
回答1:
there exists a raw fetch command defined in IMAP RFC.
FETCH 2 (BODY[]<0.size>)
for example if you want to fetch first 100 bytes of mail then you can fire the command as
FETCH 2 (BODY[]<0.100>)
回答2:
If you really want to fetch the beginning of the first textual part of a message, you will have to parse the BODYSTRUCTURE. After you obtain the part ID of the desired textual part, use the BODY[number]<0.size>
syntax.
The suggestion given in the other answer will fail on multipart messages (like if you have a text/plain
and text/html
, which is most common format today.
来源:https://stackoverflow.com/questions/15792128/obtain-partial-imap-text-part