bashemailalpine-linuxmutt

Delete mail from bash command line


I'm trying to find a way to delete a Gmail email using bash command line.

I tried to find something with mutt or alpine but I did not found a way to do it without launching the client. The purpose is to delete the first email with one command line.


Solution

  • I had few issue so I prefered imap connection this is how I did it and I empty all the mailbox as I don't need an email in once my robot read them. Thanks Aserre for your help :

    #!/usr/bin/expect
    
    set timeout 1
    
    set ip "imap.gmail.com"
    set socket "993"
    set user "myusername"
    set pass "mypassword"
    
    spawn openssl s_client -connect $ip:$socket -crlf
    
    expect -re ".OK.*" {send "01 LOGIN $user $pass \r"}
    expect -re "01 OK.*" {send "02 SELECT INBOX\r"}
    
    expect -re "02 OK.*" {send "03 STORE 1:* +FLAGS (\\Deleted)\r"}
    expect -re "03 OK.*" {send "04 EXPUNGE\r"}
    expect -re "04 OK.*" {send "05 LOGOUT\r"}
    

    Bye