2016-11-02 10 views
0
私は特定のアドレスからのメールからの私のメールボックスをきれいにしたい

bashスクリプトは、特定のアドレス(POP3アカウント)からのメールを削除

私は、数千のメッセージは、私はbashスクリプトでこれを行う、とした時間からそれを実行する必要があり

+0

の使用を許可するようにあなたの電子メールアカウントの設定を変更する必要がある場合がありますアドレスホスト?それが持っているセキュリティ標準に応じて、あなたはあなたのメールを削除するために 'telnet'か' openssl'のどちらかを使うことができます – Aserre

+0

私はpcsz.o2.plを使っています 私はこれがopensslとしか働いていないことを知っています –

答えて

0

メールサーバにコマンドラインで干渉させるには、telnetまたはopensslのいずれかを使用できます。

あなたは、次のコマンドを使用して、POPサーバーに接続することができます(私は例として、Gmailに撮影したあなたの電子メールのホストPOP3アドレスとソケットを探すために必要があります。):

openssl s_client -connect pop.gmail.com:995 -quiet 

として、このコマンドは干渉的で、ユーザー名、パスワード、およびコマンドのセリフを要求します。

expectは、対話型コマンドとのやりとりを自動化するツールです。 expect "somestring" action - >監視しているプログラムが "somestring"と表示された場合、そのアクションを実行します。ここで

は、あなたのメールアドレスに存在するすべてのメッセージを削除するスクリプトです:

#!/usr/bin/expect 

#you can modify the timeout if the scrpit fails 
set timeout 1 

#our connection variables 
set ip "pop.gmail.com" 
set socket "995" 
set user "user.name" 
set pass "password" 

#we set the address we want to remove mails from here. Escape special regex characters such as dots. 
set target_address "mail\[email protected]\.com" 

#we launch the subprocess we want to interact with 
spawn openssl s_client -connect $ip:$socket -quiet 

#if connection went all right, we try to login 
expect -re ".OK.*" {send "user $user\r"} 
expect -re ".OK.*" {send "pass $pass\r"} 

#if login went alright, we try to count the messages on the server 
#you will get the following output : 
#+OK NB_MSG TOTAL_SIZE 
expect -re ".OK.*" {send "stat\r"} 

#if the stat command went allright ... 
expect -re ".OK.*" { 
     #we extract the number of mail from the output of the stat command 
     set mail_count [lindex [split [lindex [split $expect_out(buffer) \n] 1] " "] 1] 

     #we iterate through every email... 
     for {set i 1} {$i <= $mail_count} {incr i 1} { 
      #we retrieve the header of the email 
      send "top $i 0\r" 

      #if the header contains "To: $target_address" (or "To: <$target_address>" or "To: Contact Name <$target_address>" ...) 
      #to filter according to the sender, change the regex to "\nFrom: ..." 
      expect -re "\nTo: \[^\n\]*$target_address" { 
        #we delete the email 
        send "dele $i\r" 
      } 
     } 
} 
expect default 

あなたはあなたの電子メールが何であるかを外部プログラム

+0

ありがとう、私は特定のアドレスから電子メールを削除する必要がありますが、すべて削除する必要はありません。ここに私の最大の問題があります –

関連する問題