Reads a specified email through POP3 protocol

Source: Internet
Author: User
Tags timedelta


To read a specified email through the POP3 protocol, you must first install the maillib library. Assume that I want to obtain the first URL of each email sent by Douban in a week. The Python code import poplib import maillib from datetime import datetime, timedelta www.2cto.com def email_filter (sender, body): target = "http: //" sender, body = sender [1], body. split ("\ n") if sender = "webmaster@douban.com": for line in body: if target in line: return line. strip () def read_email (email, password, host, port = 110, days = 0): conn = poplib. POP3 (host, port) # conn. set_debuglevel (1) # output debugging information conn. user (email) conn. pass _ (password) links = [] nr = conn. stat () [0] # obtain the number of mails for I in range (nr, 0,-1): server_msg, body, octets = conn. retr (I) msg = maillib. message. from_string ("\ n ". join (body) today = datetime. now (). replace (hour = 0, minute = 0, second = 0, microsecond = 0) if msg. date <today-timedelta (days = days): break www.2cto.com link = email_filter (msg. sender, msg. body) if link: links. append (link) return links if _ name _ = "_ main _": links = read_email ("me@126.com", "pass", host = "pop.126.com ", days = 7) for link in links: print link author ryan. liu

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.