Python's process of receiving mail using IMAP ... A depressing exploration __python

Source: Internet
Author: User
Tags imap rfc822

I'm just a python beginner ...

Want to use Python to do a very simple function of receiving mail, only to see the official Python doc (http://docs.python.org/2/library/imaplib.html) really very difficult to understand, after Google, explore, To sum up a little:

To receive mail using IMAP, of course, import imaplib.

Import Imaplib

Then follow the normal, set up link → Login

conn = Imaplib. IMAP4 ("imap.xxx.com", 143)

Conn.login ("UserName", "password")

Then I want to check the Inbox mail, how to do? To select a directory first, the Inbox default name is "INBOX", IMAP is to support the creation of folders, see other folders, if it is a new folder of their own, then the name will generally be "INBOX. New Folder", Different mailboxes may not represent the same way, if you don't know, then run conn.list () to view all the folders.

Conn.select ("INBOX")

After selecting, and then looking at the folder, note that the IMAP view is actually a search process, the original IMAP command is searched all (approximate), in Python:

Type, data = Conn.search (None, ' all ')

It then returns the number of all the messages in the Inbox, sorted in ascending order of receiving time, and finally the last one.

Search this is a lot of trouble, because the official document does not say the function of the second parameter how to use, so look for the following, you can fill in the command:

Http://www.afterlogic.com/mailbee-net/docs/MailBee.ImapMail.Imap.Search_overload_1.html

So if I want to find essh mail, use

Type, data = Conn.search (None, ' (SUBJECT "Essh")

Inside to use a bracket, represented is a query condition, you can specify multiple query criteria, such as from XXXX SUBJECT "AAA", note that the command to be covered with parentheses (painful attempt)

Search the first parameter is the meaning of the charset, and the none means the default ASCII,

Data gets an array of only one string element, containing many numbers, separated by a space

[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 '

So the last thing you want to get is:

Msglist = Data[0].split () Last
= Msglist[len (msglist)-1]

And then get that message back and use the FETCH function

Example:

Conn.fetch (Last, ' RFC822. SIZE Body[header. FIELDS (SUBJECT)])

But the return is a string of MIME-encoded Dongdong, do not understand, if it can be like eml that an operation of an e-mail.

The method is some, use the email library.

Import Email

Then you get the message format in RFC822, and then you convert it to a messages object with email.message_from_string. You can do it, (http://docs.python.org/2/library/ email.message.html)

Type,data=connect.fetch (Msglist[len (msglist)-1], ' (RFC822) ')
msg=email.message_from_string (data[0][1))
Content=msg.get_payload (Decode=true)

Finally content is the contents of the message, so that temporarily achieve my purpose, said here first.

http://zhiwei.li/text/2010/11/python%E9%80%9A%E8%BF%87imap%E5%8D%8F%E8%AE%AE%E6%90%9C%E7%B4%A2%E9%82%AE%E4%BB%B6/

http://code.activestate.com/recipes/52299-imap-mailwatcher-program-using-tkinter/

http://hg.python.org/cpython/file/2.7/Lib/imaplib.py

This article originates from: http://www.cnblogs.com/yhlx/archive/2013/03/22/2975817.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.