2016-11-02 38 views
0

我要清理的郵件我的郵箱,從特定地址bash腳本刪除特定的地址(POP3帳戶)的郵件

我有上千封郵件,我想這樣做在bash腳本,並從時間運行它時間(從不同地址接收垃圾郵件,不幸的是我的「垃圾郵件過濾器」對他們只有很小的影響)

+0

什麼是您的電子郵件地址主機?根據它的安全標準,你可以使用'telnet'或'openssl'來刪除你的郵件 – Aserre

+0

我使用的是poczta.o2.pl 據我所知這只是與openssl –

回答

0

要通過命令行與郵件服務器進行交互,可以使用telnetopenssl

您可以使用以下命令連接到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

一起工作謝謝,但我需要刪除特定地址的電子郵件,而不是所有電子郵件,這是我最大的問題 –