Checking email with Python

后端 未结 6 1502
心在旅途
心在旅途 2020-11-28 02:06

I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitorin

相关标签:
6条回答
  • 2020-11-28 02:32

    Gmail provides an atom feed for new email messages. You should be able to monitor this by authenticating with py cURL (or some other net library) and pulling down the feed. Making a GET request for each new message should mark it as read, so you won't have to keep track of which emails you've read.

    0 讨论(0)
  • 2020-11-28 02:32

    I found a pretty good snippet when I wanted to do this same thing (and the example uses gmail). Also check out the google search results on this.

    0 讨论(0)
  • 2020-11-28 02:39

    While not Python-specific, I've always loved procmail wherever I could install it...!

    Just use as some of your action lines for conditions of your choice | pathtoyourscript (vertical bar AKA pipe followed by the script you want to execute in those cases) and your mail gets piped, under the conditions of your choice, to the script of your choice, for it to do whatever it wants -- hard to think of a more general approach to "trigger actions of your choice upon receipt of mails that meet your specific conditions!! Of course there are no limits to how many conditions you can check, how many action lines a single condition can trigger (just enclose all the action lines you want in { } braces), etc, etc.

    0 讨论(0)
  • 2020-11-28 02:45

    I recently solved this problem by using procmail and python

    Read the documentation for procmail. You can tell it to send all incoming email to a python script like this in a special procmail config file

    :0:
    | ./scripts/ppm_processor.py
    

    Python has an "email" package available that can do anything you could possibly want to do with email. Read up on the following ones....

    from email.generator import Generator
    from email import Message
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    0 讨论(0)
  • 2020-11-28 02:49

    People seem to be pumped up about Lamson:

    https://github.com/zedshaw/lamson

    It's an SMTP server written entirely in Python. I'm sure you could leverage that to do everything you need - just forward the gmail messages to that SMTP server and then do what you will.

    However, I think it's probably easiest to do the ATOM feed recommendation above.

    EDIT: Lamson has been abandoned

    0 讨论(0)
  • 2020-11-28 02:53

    Gmail provides the ability to connect over POP, which you can turn on in the gmail settings panel. Python can make connections over POP pretty easily:

    import poplib
    from email import parser
    
    pop_conn = poplib.POP3_SSL('pop.gmail.com')
    pop_conn.user('username')
    pop_conn.pass_('password')
    #Get messages from server:
    messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    # Concat message pieces:
    messages = ["\n".join(mssg[1]) for mssg in messages]
    #Parse message intom an email object:
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    for message in messages:
        print message['subject']
    pop_conn.quit()
    

    You would just need to run this script as a cron job. Not sure what platform you're on so YMMV as to how that's done.

    0 讨论(0)
提交回复
热议问题