Checking your IMAP mail with Expect

Rohan Pall, on a nice Saturday afternoon on July 5th 2003 decided to play with Expect and automate checking his IMAP mail account.

I based this script on Glenn Jackman's POP3 mail checking Expect script [L1 ]. I just loaded Expect up for the first time 2 days ago, so you can figure that it is dead simple to use, once you know some Tcl.

Now I just have to tell cron to run this script every so often, and I need to modify this script to pop up a box when I have new mail. Or maybe use something like TkDesk, or some other desktop agent. I've got so many desktop tclets that something to tie it together would r0x0r.

Enjoy the summer -- Tcl yourself today!

Make sure the script is not readable by anyone else, your PASSWORD is in it!

  #!/usr/bin/expect -f

  #log_user 0    ;# hide interaction, i.e. do not display to stdout
  set timeout 10
  match_max 100000

  set server
  set port 143
  set user {dude}
  set pass {32k$12!_a}

  spawn telnet $server $port
  expect {
    timeout {puts stdout "timeout while connecting to $server"; exit 1}
    "* OK"

  send "a001 login $user $pass\r"
  expect {
    timeout {puts stdout "timed out after login"; exit 1}
    "a001 NO" {puts stdout "bad login"; exit 1}
    "a001 OK"

  send "a002 examine inbox\r"
  expect {
    timeout {puts stdout "timed out after examining inbox"; exit 1}
    "a002 NO" {puts stdout "could not examine inbox"; exit 1}
    "a002 OK"

  #parray expect_out

  set buffer_to_parse $expect_out(buffer)
  regexp {([0-9]+) RECENT} $buffer_to_parse -> new_msgs
  puts "new: $new_msgs"

  send "a003 logout\r"
  expect {
    timeout {puts stdout "timed out after logout"; exit 1}
    "a003 NO" {puts stdout "could not logout"; exit 1}
    "a003 OK"


CL, intent on demonstrating that many people who think they need Expect do not need Expect, feels motivation to code a pure-Tcl example that almost as easily achieves the same effect--but not enough motivation yet to turn away from other projects.

schlenk It is quite simply to do it with the rudimentary IMAP4 client inside the Tcllib CVS tree.

DL Yes, CL and schlenk are correct but not all is lost. Drop an 'interact' in the middle of the script and it becomes a cute script for experimenting with an IMAP server interactively. I do that a lot! As far as solving the original problem (automating checking of mail account), I use tkbiff.