bash脚本从特定地址(pop3帐户)删除邮件 [英] bash script delete mails from specific address (pop3 account)

查看:51
本文介绍了bash脚本从特定地址(pop3帐户)删除邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要清除来自特定地址的邮件中的邮箱

我有成千上万的消息,我想在bash脚本中执行此操作,并不时运行它(从不同的地址接收垃圾邮件,不幸的是,我的垃圾邮件过滤器"对它们的影响很小)

解决方案

要通过命令行与邮件服务器交互,可以使用 telnet openssl ./p>

您可以使用以下命令连接到pop服务器(以gmail为例.您必须查找电子邮件主机pop3地址和套接字.):

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

由于此命令是交互式的,因此会要求提供用户名,密码和一系列命令.

expect 是一个可以自动与交互式命令进行交互的工具.基本语法如下:期望"somestring"动作->如果我们监视的程序显示"somestring",则我们执行该动作.

这是一个脚本,该脚本将删除您的电子邮件地址中显示的所有消息:

 #!/usr/bin/expect#如果scrpit失败,您可以修改超时设置超时1#我们的连接变量设置IP"pop.gmail.com"将插座设置为"995"设置用户"user.name"设置通行证密码"#我们在此处设置要删除邮件的地址.转义特殊的正则表达式字符,例如点.设置target_address"mail \ .example @ gmail \ .com"#我们启动要与之交互的子流程生成openssl s_client -connect $ ip:$ socket -quiet#如果连接正常,我们尝试登录期望-re".OK.*" {发送用户$ user \ r"}期望-re".OK.*" {发送"pass $ pass \ r"}#如果登录正常,我们将尝试计算服务器上的消息数#您将获得以下输出:#+确定NB_MSG TOTAL_SIZE期望-re".OK.*" {发送"stat \ r"}#如果stat命令一切正常...期望-re".OK.*" {#我们从stat命令的输出中提取邮件数设置mail_count [lindex [split [lindex [split $ expect_out(buffer)\ n] 1]"] 1]#我们遍历每封电子邮件...为{set i 1} {$ i< = $ mail_count} {incr i 1} {#我们检索电子邮件的标题发送前$ i 0 \ r"#如果标题包含收件人:$ target_address"(或收件人:< $ target_address>"或收件人:联系人姓名< $ target_address>" ...)#要根据发件人进行过滤,请将正则表达式更改为"\ nFrom:..."期望-re"\ nTo:\ [^ \ n \] * $ target_address" {#我们删除电子邮件发送"dele $ i \ r"}}}期望默认 

您可能需要更改您的电子邮件帐户设置,以允许使用外部程序

I want to clean my mailbox from mails from specific address

I have thousands of messages, I want to do this in bash script, and run it from time to time (a receive SPAM from different addresses, and unfortunately my "spam filters" have only small effect on them)

解决方案

To interract with a mail server through command line, you could use either telnet or openssl.

You can connect to your pop server using the following command (I've taken gmail as an example. You'll have to look for your email host pop3 address and socket.) :

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

As this command is interractive, it will ask for a username, a password and a serie of commands.

expect is a tool that can automate interaction with interactive commands. The basic syntax is as follow : expect "somestring" action -> If the program we monitor displays "somestring", we execute the action.

Here is a script that would delete all the messages present on your email address :

#!/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\.example@gmail\.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

You might need to alter your email account settings to allow the use of external programs

这篇关于bash脚本从特定地址(pop3帐户)删除邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆