Tuesday, 1 September 2009

IMAP PUSH GMail Python Script (for Mac, but easily changable)


So, you need a python script to do something when you get an email (from GMail, for example, but other services that support IMAP4 will work as well). I came across this post when finding out how to do this. It pointed me in the right direction; I didn't have to do much initial thinking/goggling.

A quick explanation as to how the script works:
  1. Connect using IMAP to imap.gmail.com
  2. Send login credentials (encrypted with SSL, don't worry)
  3. Select 'INBOX'
  4. Send IDLE command and wait for mail...
  5. When a message gets to Google's servers, they terminate the IDLE connection. This then signals to our script that a message has been received (or we've timed out)
  6. The script then checks to see if there is new, unseen mail, and, if so, uses growlnotify to let us know the sender and the subject. (This is the bit that can be replaced to do whatever you want it to do. You can get the message body if you want, times, dates, etc... and do whatever with it)
  7. IDLE command is sent again (i.e. back to 4.)
Instead of a growl notification, you could use pySerial to send something to an Arduino and light something, spin something, etc... Feel free to change the code and do whatever to it.

There are two bits of code you'll need: imaplib2.py and my IMAPPush.py

I hosted my IMAPPush.py on Google Sites because I couldn't host it here. All you need to do is put the files in the same directory, cd to said directory and execute:

python IMAPPush.py USERNAME [PASSWORD]

The PASSWORD is not required -- you'll be prompted for it if it is omitted.

To find out more about IMAP IDLE, see it's RFC (RFC 2177) here.

There's no point me explaining how the code works here; if you want to understand how the code works, I've put a lot of comments in the code. That's the best place for explaining the code, after all.

Here are the links to the code again:
  1. http://sites.google.com/site/hmmtheresanidea/files/IMAPPush.py?attredirects=0
  2. http://www.cs.usyd.edu.au/%7Epiers/python/imaplib.html
Any comments, suggestions, etc... please let me know below. Thanks for reading!

UPDATE: links fixed, and code is now licensed.

47 comments:

  1. Thanks for publishing this code!
    One of the problems I'm having with it is that there is no response timeout checking.
    So if the remote imap server reboots the script does not try to re login. Any ideas on how to fix this?

    ReplyDelete
  2. Hi Chris,

    What license is this released under? Would really appreciate it if you could add some sort of contact email address so I can get in touch :-)

    Cheers,

    Pete

    ReplyDelete
  3. Hi Peter,

    It wasn't before, but I've updated the code so it's now under a MIT/X11 license -- basically, do what you want with it!

    I don't really know if this is a good choice for a license, but it seemed to be the most flexible one I came across. It seems pretty compatible with others as well.

    What are you doing, or planning to do, with it? I'm curious. It's good to see that it may be of use to someone!


    Thanks for commenting,

    All the best,


    Chris

    P.S. I have just added an email address to my profile.

    ReplyDelete
  4. I'm using this to get emails from a GMail account and deliver it to trac using email2trac. Thanks a lot!

    ReplyDelete
  5. Great! Glad it was of some use!

    ReplyDelete
  6. This is a beautiful example of IMAP IDLE using Python. This is going to be a lot of help to me while trying to work things out, all those wonderful comments! *drools*

    ReplyDelete
  7. Holy crap, this is awesome. I have implemented this into a script I have been using for years and it is working great. My problem is I can't seem to get the thread to die when not running interactively. In your code, the script is run interactively and pressing q followed by enter kills the thread and then exits the script.

    My script does not run interactively and when I send sigterm to it with kill {pid} the thread is never notified of the sigterm and it never exits! How can I handle this situation to get everything to exit when I request it to?

    ReplyDelete
  8. I was excited to uncover this website. I wanted to thank you for your time for this wonderful read!!
    I definitely enjoyed every little bit of it and I have you
    saved to fav to look at new stuff in your blog.

    ReplyDelete
  9. I was excited to uncover this website. I wanted to thank you for your time for this wonderful read!!
    I definitely enjoyed every little bit of it and I have you saved to fav to look at
    new stuff in your blog.

    ReplyDelete
  10. Specific i could jeopardize me, and most previous marketing, 2018 - livery for treating viagra online.

    Newark condos generic viagra telaflora. Ha come Viagra
    do to your blood pressure.

    ReplyDelete
  11. Each phone includes an Analog Telephony Adapter (ATA), so only regular modems are requured
    that hzve an Ethernet jack. They also offer discounted rates
    for those who need to surf the web for longer durations as
    well as for night packages. Alternatively, users will be able too back upp their information on i - Cloud.

    ReplyDelete
  12. However, what are you going to do if you live
    in a rural region and don't have a fast internet connection or anyy connection att all.

    Like Verizon, Sprint does not provide nationwide coverage of
    Wi - Max; instead, they offer 3G mobile broadband services
    for cities lacking Wi - Maax access. Plus e - Readers have
    doubled as web browsing devices giving sers to have access to
    many oof the most important information from the one device.

    ReplyDelete
  13. Before going to see a dealer, it iss also important to do
    yoour research oon the price. Behause the signal has to travel so much farther, to a satellite in geostationary orbit from the subscriber and then back to a service
    provider who then routes it back to the subscriber, there is
    often a delay which can maoe thhe service less effective when bing used for certain purposes.
    Wiith this remarkably amazing technology you can make unlimitd affordable calls internationally.

    ReplyDelete
  14. Howw can you be sure to find the best possible broadband servixe for your postcode.
    Cabl internet is readily aailable if you already have a pay TV subscription, and you should
    count yourself fortunate if you live in one of the pivileged areas where fiber
    optic internet is available. Sureillance has also increased thanks to the internet.

    ReplyDelete
  15. Most service provideers in the developoed countries
    are either already providing a Broadband service up to 24Mbps, or will be
    in tthe near future, and it will be a version of ADSL2+ that they are using.

    The internet iis a great tool to learn what evbents take place each
    year and each month in your new city. Plus e - Readers hhave doubled as web browsing devices giving
    users to have access tto many of the most important information from the onne device.

    ReplyDelete
  16. There are even internet connected devices now tbat stream video annd TV
    services to the home entertainment center. The internet is a great tool to learn what events take place each year and each month in your
    new city. Once you are satisfied, bbuy yourself a prepaid starter kit, with sim and data.

    ReplyDelete
  17. Although for many people a credit or debit card may be the best option, the prepaid card is also a viable option for some.
    They also offer discounted rates for those who neeed to surf the web
    for longer durations ass well as for night packages. You cannot evenn escape references to broadband in thee news media,
    with the government recently announcing plans to get
    90 percent off homes and businesses in the UK connected to super-fast broadbaand
    by 2015and manyy communities inn rural areas that telecoim companies such
    as BT and Virgin Media will not be installing fibre optic cabling declaring their intent to fund their own network upgrades.

    ReplyDelete
  18. As the fourth week oof the New Year hovers in the imminent
    future, most people have already kicked their New Year's promises and resolutions to the wayside.
    Prepaid travel cards are a great option for those wishing to wiithdraw and spend money abroad.
    There are many considerations that need to be taken into account
    before you staqrt trading.

    ReplyDelete
  19. There is a rise in shopping among people who may be having a glass of wine andd then decide to purchase something that
    they may not even remember doing later. Make sure that they have backup equipment suchh as multiple transit providers and DHCP servers.

    Metro ethernet is simple to use and is relatively easy to install.

    ReplyDelete
  20. It's going to be finish of mine day, however before finish I am reading this wonderful piece of writing to improve my experience.

    ReplyDelete
  21. Today, I went to the beachfront with my kids.
    I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She put the
    shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but I had to
    tell someone!

    ReplyDelete
  22. Good post however , I was wondering if you could write a litte
    more on this topic? I'd be very thakful if you could elaborate a little bit further.
    Thank you!

    ReplyDelete
  23. Paragraph writing is also a fun, if you know afterward you can write if not it
    is complex to write.

    ReplyDelete
  24. Good day! I could have sworn I've visited this website before but after browsing through many of the pposts I realized it's
    new to me. Anyhow, I'm definitely pleased I found it and I'll be book-marking it and checking back
    regularly!

    ReplyDelete
  25. I visit daily some blogs and sites to read posts, but this webpage offers feature based posts.

    ReplyDelete
  26. It is the best time to make some plans for the long run and it's
    time to be happy. I have read this put up and if I may just I wish
    to recommend you some fascinating things or suggestions.
    Perhaps you could write next articles referring to this article.
    I want to read more things approximately it!

    ReplyDelete
  27. Pretty component of content. I simply stumbled upon your site and iin accession capital to asxert that I acquire inn fact loved account your blog posts.
    Any way I'll be subscribing in your feeds or even I success yoou get right of entry too
    persistently quickly.

    ReplyDelete
  28. Terrific post however , I was wondering if you could write a
    litte more on this subject? I'd be very thankfu iif you could elaborate a little bit further.
    Bless you!

    ReplyDelete
  29. With havin so much written content do you ever run into any
    problems of plagorism or copyright infringement? My site has a lot of unique content I've either written myself
    or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you
    know any methods to help reduce content from being ripped off?
    I'd truly appreciate it.

    ReplyDelete
  30. Nice post. I was checking constantly this blog and I'm impressed!
    Very useful ijformation particularly tthe last part :) I care
    for such info a lot. I was seeking this particular information forr a
    very long time. Thank you and good luck.

    ReplyDelete
  31. Highly descriptive article, I enjoyed that a lot.
    Will there be a part 2?

    ReplyDelete
  32. Thaks for your personal marvelous posting!
    I certainly enjoyed rewding it, you might be a great author.I will be
    sure to bookmark your blog and definitely will come
    back later in life. I wnt to encourage youu to ultimatey continue your great work, have a nice morning!

    ReplyDelete
  33. It's very easy to find out any topic on web as compared to books, as I found
    this article at this site.

    ReplyDelete
  34. What you said was actually very reasonable. However, what about this?

    what if you added a little content? I mean, I don't wish to tell you
    how to run your blog, however suppose you
    added something that grabbed folk's attention? I mean "IMAP PUSH GMail Python Script (for Mac, but easily changable)" is a little vanilla.
    You might look at Yahoo's front page and note how they create post
    titles to grab viewers to click. You might try adding a video or a picture or two to get
    readers interested about everything've got to say.
    In my opinion, it could make your website a little bit more interesting.

    ReplyDelete
  35. Hi there! I could havfe ssorn I've been to this website before but after reading through ssome of the ppst
    I reealized it's new to me. Nonetheless, I'm definitely delighted I foumd it and I'll be book-marking
    and checking back often!

    ReplyDelete
  36. I'm really enjoying the theme/design of our weblog. Do you ever run nto any
    internet browser comnpatibility problems? A number of my blog readers have complained about my website not working correctly in Explorer but looks great in Safari.
    Do you have any sughestions to helpp fiix this problem?

    ReplyDelete
  37. Magnificent site. Lots oof helpful info here. I'm
    sending itt to a few friends ans also sharing in delicious.
    And obviously, thanks to your sweat!

    ReplyDelete
  38. I got this website from my pal who told me on the topic of this web site and now
    this time I am browsing this site and reading very informative posts at
    this time. http://www.laimaimai.net.cn/comment/html/?123189.html

    ReplyDelete
  39. I got this website from my pal who told me on the topic of this web site and now this time I
    am browsing this site and reading very informative posts
    at this time. http://www.laimaimai.net.cn/comment/html/?123189.html

    ReplyDelete
  40. It could be that they see the most visitors, are the most delicate, are the
    hardest to maintain, or any combination of these factors.

    ReplyDelete
  41. It could be that they see the most visitors, are the most delicate, are the hardest to maintain, or
    any combination of these factors.

    ReplyDelete
  42. Useful info. Lucky me I discovered your weeb site
    accidentally, and I'm shocked why this accident didn't
    took place in advance! I bookmarked it.

    ReplyDelete
  43. Very nice post. I simply stummbled upon your blog and wanted to
    mention that I have truly enjoyed browsing your weblog posts.
    In any case I'll be subscribing on your rss feed and I'm hoping you write again soon!

    ReplyDelete
  44. Hi there, this weekend is nice in support of me, because this moment i
    am reading this wobderful educational piece of writing here at my house.

    ReplyDelete
  45. I've been exploring for a little bit for any high quality articles or weblog posts
    in this sort of arra . Exploring in Yahoo I eventually stumbled upon this web site.
    Reading this info So i am glad to exhibit that I
    have a very excellent uncanny feeling I came upon exactly what I needed.
    I most unquestionably will make sure to do nnot fazil to remember this web site and provides it a look regularly.

    ReplyDelete
  46. Deposits are instant, whilst withdrawals may well take additional than 10 days to arrive.

    ReplyDelete
  47. On the web sports betting was initial approved by legislators in July 2020.

    ReplyDelete