delete_gmail_inbox.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/expect
  2. #you can modify the timeout if the script fails
  3. set timeout 1
  4. #our connection variables
  5. set ip "pop.gmail.com"
  6. set socket "995"
  7. set user "flowfactoryautomation@gmail.com"
  8. set pass "auto1234AUTO1234"
  9. #we set the address we want to remove mails from here. Escape special regex characters such as dots.
  10. set target_address "mail\.example@gmail\.com"
  11. #we launch the subprocess we want to interact with
  12. spawn openssl s_client -connect $ip:$socket -quiet
  13. #if connection went all right, we try to login
  14. expect -re ".OK.*" {send "user $user\r"}
  15. expect -re ".OK.*" {send "pass $pass\r"}
  16. #if login went alright, we try to count the messages on the server
  17. #you will get the following output :
  18. #+OK NB_MSG TOTAL_SIZE
  19. expect -re ".OK.*" {send "stat\r"}
  20. #if the stat command went alright ...
  21. expect -re ".OK.*" {
  22. #we extract the number of mail from the output of the stat command
  23. set mail_count [lindex [split [lindex [split $expect_out(buffer) \n] 1] " "] 1]
  24. #we iterate through every email...
  25. for {set i 1} {$i <= $mail_count} {incr i 1} {
  26. #we retrieve the header of the email
  27. send "top $i 0\r"
  28. #if the header contains "To: $target_address" (or "To: <$target_address>" or "To: Contact Name <$target_address>" ...)
  29. #to filter according to the sender, change the regex to "\nFrom: ..."
  30. expect -re "\nTo: \[^\n\]*$target_address" {
  31. #we delete the email
  32. send "dele $i\r"
  33. }
  34. }
  35. }
  36. expect default