Python mail basics and python Basics

Source: Internet
Author: User

Python mail basics and python Basics

Python is not very convenient to operate emails. To tell the truth, it is not very thorough. I want to summarize what I have encountered this time.

The email includes the imap, pop, and imap protocols. This time, we use the imap4 protocol and the mail class,

The Code mainly references

The main code is as follows:

 

Initialize and define the email server

self.IMAP_SERVER='imap.gmail.com'        self.IMAP_PORT=993        self.M = None        self.response        self.mailboxes = [] 

 

Log on and select mailbox:

self.M = imaplib.IMAP4_SSL(self.IMAP_SERVER, self.IMAP_PORrc, self.response = self.M.login(username, password)tye,data = m.M.select() 

 

Email search:

ret, msgnums = m.M.search(None, 'BODY', datapath)

 

Get email information:

status, response = self.M.fetch(id,"(RFC822)")mailText = response[0][1]mail_message = email.message_from_string(mailText)subject = unicode(email.Header.make_header(email.Header.decode_header(mail_message['subject'])))#print "subject_________:" +subjectmail_from = email.utils.parseaddr(mail_message["from"])[1]mail_to = email.utils.parseaddr(mail_message["to"])[1]time = mail_message['Date']print '['+mail_message['Date']+']'+'\n'+'From:'+mail_from+ ' To:'+mail_to+'\n'+'Subject:'+subject+'\n'return self.get_first_text_block(mail_message), subject, mail_from, time

 

Maintype = email_message_instance.get_content_maintype () indicates the type of content returned from the email. If it is text, it is better to process it. If it is multipart, You have to traverse email_message_instance to process it based on different types.

 

Email. message_from_string (mailText) returns a struct containing the basic information of the email.

 

The problem with character string encoding is a pain point in the mail. After all, the mail formats are different, some are unicode, some are UTF-8, some are gb2312, and there are attachments, images, and other formats,
Of course, only the text is processed this time, and there is no need to process attachments and images. I convert all characters to unicode for processing.

 

When processing strings, chardet can be used to determine the string type. When reading and writing files, codecs can be used to specify the character set type for reading and writing.

 

In addition, post the python mail processing articles worth reference;
Http://www.programcreek.com/python/example/58598/email.message.as_string (some examples of mail processing)
Https://tools.ietf.org/html/rfc3501#section-6.4.4 (reference for email search)

 

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.