1

I need to delete all emails from a specific source. I can list all of the emails with the f source@emailaddress.com but I'm having to dump it to something like awk and back into mail... or copy all of the email header numbers individually.

Is there a faster way or a way integrated into the program that will do it automatically?

2 Answers2

0

The procmail package includes a handy tool called formail which allows the user to iterate over individual messages of a traditional mbox-style mailbox. My approach to tasks like this has been to split the mbox into individual messages, use standard UNIX tools (grep, rm) to process the messages, and then re-assemble the mailbox. Naturally, this needs to be done with the sendmail process stopped, so that no new mail arrives while you're working. Only a brief downtime is necessary.

You don't say which BSD you're on, but I'll assume FreeBSD. I will also assume that you're using bash for your shell, and that you have root access to the box, or at least sufficient sudo privileges to start/stop the sendmail instance on your machine, and to write directly to your mailbox /var/mail/$USER.

Install the procmail package and read the formail man page.

$ sudo pkg install procmail
$ man formail

Make a clean workspace:

$ mkdir ~/work
$ cd ~/work

Create a simple script and make it executable:

$ cat << EOF > cat-msg.sh
#!/bin/sh

cat > msg-$FILENO.txt
EOF
$ chmod 755 cat-msg.sh

And another:

$ cat << EOF > split-mbox.sh 
#!/bin/sh

export FILENO="00000"
formail -s cat-msg.sh < "$@"
EOF
$ chmod 755 split-mbox.sh

Stop the mailserver:

$ sudo service sendmail stop

Split your mbox into multiple files in the current directory:

./split-mbox.sh /var/mail/$USER

That command reads your mailbox, and creates files msg-#####.txt in the current directory. It does not write to or delete your /var/mail/$USER mailbox, so no harm done.

Using grep, identify the msg-*.txt files you want to delete, and examine a few of them to ensure that you are certain your regexp correctly targets only the messages you want to delete. It is okay to use multiple regexp passes, but you must be sure that each regexp does not have any false positives.

$ grep -lF '^From: spammer@example.com' msg*.txt
$ (use less to spot-check a few or all of them)

If the message files identified by grep are indeed messages you want to delete, then:

$ grep -lF '^From: spammer@example.com' msg*.txt | xargs rm -v

If there are multiple regexps you want to use to find the messages to delete, you can go back and grep again with a different regexp and spot-check those hits, etc.

Once you've successfully deleted all the offending messages, backup your mailbox, write the remaining msg*.txt files to your mailbox, and re-start the mailserver:

$ cp -vp /var/mail/$USER mbox-$USER
$ cat msg*.txt > /var/mail/$USER
$ sudo service sendmail start

If you find these tools handy, and wish to keep them for future use, then:

$ sudo mv -vi split-mbox.sh cat-msg.sh /usr/local/bin/
Jim L.
  • 929
0

I think we're making this a lot more complicated than it needs to be; at least when it comes to OpenBSD 7.2 which is the version of OpenBSD I use and the system where I ran the following command in mail(1) to delete email from a specific address:

d source@emailaddress.com

Non-interactively:

echo "d source@emailaddress.com" | mail -f