Version 3 of Checking your IMAP mail with Expect

Updated 2003-07-05 21:14:52

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.

Enjoy the summer -- Tcl yourself today!

  #!/usr/bin/expect -f

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

  set server my.server.com
  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"
  }

  exit