LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to delete emails based on subject? (https://www.linuxquestions.org/questions/linux-server-73/how-to-delete-emails-based-on-subject-4175544986/)

markkus619 06-10-2015 08:46 AM

How to delete emails based on subject?
 
Hi,

Theres a way to delete all emails based on subject on linux?

I need remove more than 100k os mails with subject:

Mail delivery failed: returning message to sender

pan64 06-10-2015 11:41 AM

what kind of mailer do you have? For example using thunderbird you can easily create a filter to do that.

markkus619 06-10-2015 12:15 PM

Hi

Thanks for attention

I need remove this via shell from the server...

I'm using something like:

find . -type f -exec grep -i "Mail delivery failed: returning message to sender" -exec rm -v {} \;
grep: Mail delivery failed: returning message to sender: Arquivo ou diretório não encontrado

find $1 . -type f -exec grep -i "Mail delivery failed: returning message to sender" {} \; | xargs rm -v $2
for RM in $(find . -type f -exec grep -i "Mail delivery failed: returning message to sender" {} \;); do rm -v $RM ; done



But i can't figure out how to remove the file. Just how to find the subject... :confused:

Habitual 06-10-2015 01:30 PM

Quote:

Originally Posted by markkus619 (Post 5375174)
I'm using something like:

Code:

find . -type f -exec grep -i "Mail delivery failed: returning message to sender" -exec rm -v {} \;
grep: Mail delivery failed: returning message to sender: Arquivo ou diretrio no encontrado

Code:

find $1 . -type f -exec grep -i "Mail delivery failed: returning message to sender" {} \; | xargs rm -v $2
for RM  in $(find . -type f -exec grep -i "Mail delivery failed: returning message to sender" {} \;); do rm -v $RM ;

done

Not gonna work, but go ahead and see.
IF you had thunderbird, you could "see" where in your Tbird setup these messages occur using:
Code:

find .thunderbird/ -type f -exec grep -il "Mail delivery failed: returning message to sender" {} \;
do NOT do any -exec on anything found since it will nuke the folder it finds in and not the actual message.

Thunderbird (all of mozilla-land, it seems) uses sqllite databases for message storage, so nuking them should be done from
inside the Thunderbird program.
Code:

file .thunderbird/uvpc4uva.default/global-messages-db.sqlite
.thunderbird/RanDOM.default/global-messages-db.sqlite: SQLite 3.x database, user version 30


schlabs 06-10-2015 06:04 PM

if i can think in high voice.... i think that in your place i do a script based on:
cd maildir/folder
grep -in "subject" * |grep -in "subject to delete" >a.txt

If all work fine you will get into "a.txt"the list of matching files.

My script can contain error there is not intended to copy/paste, is to give a idea about direction that i take in your place.

markkus619 06-11-2015 09:07 AM

Thanks!
 
Hi people.

Maybe i not expressing myself properly. I'm using centos with exim. No email client. Thanks for efforts.


Quote:

Originally Posted by schlabs (Post 5375329)
if i can think in high voice.... i think that in your place i do a script based on:
cd maildir/folder
grep -in "subject" * |grep -in "subject to delete" >a.txt

If all work fine you will get into "a.txt"the list of matching files.

My script can contain error there is not intended to copy/paste, is to give a idea about direction that i take in your place.



Hi schlabs

Thanks for help!

That was what i was looking for.

Now i can take the name of mailfile by the subject.

Soon as possible i'll make a script and post here.

I was a little confuse how to take the name of file by subject. But with your help now i can figure out how to. :D

Habitual 06-11-2015 10:28 AM

Quote:

Originally Posted by markkus619 (Post 5375608)
But with your help now i can figure out how to. :D

Don't have to.
To delete all queued messages containing a certain string in the body < for exim only

schlabs 06-12-2015 06:09 AM

Quote:

Originally Posted by markkus619 (Post 5375608)
Hi people.

Maybe i not expressing myself properly. I'm using centos with exim. No email client. Thanks for efforts.






Hi schlabs

Thanks for help!

That was what i was looking for.

Now i can take the name of mailfile by the subject.

Soon as possible i'll make a script and post here.

I was a little confuse how to take the name of file by subject. But with your help now i can figure out how to. :D

grep -in in human mode say: show me the name of the file and the line with matching string, and case insensitive. So with the first grep you will get all files with "subject" string, and will show the line of subject. So the first grep will show all emails subject and their filename.

The second grep make the real filter showing only the files that matching "spam to erase"or "mail returned" or etc.

After both grep you will get a list of filenames that meet your requirements. Some like :
mailfile12345:25:Subject: mail returned
or
mail_filename:number of line:matching text

A last step is redirect the screen to any file, so you will get a list of files to erase.

Actually i do some similar, i create a folder SPAM email. When the user put a email here i look (grep) email provider of spammer, ip provider of spammer, and domains involved. This automatically update the spam filter.

Best Regards

markkus619 06-16-2015 10:13 AM

Hi

I found a way with this command. To remove all in the current folder.

grep -in "Mail delivery failed: returning message to sender" * | grep -in "Mail delivery failed: returning message to sender" | rm -rf $(awk -F ":" '{print $2}')

Thanks for the help.

pan64 06-16-2015 10:28 AM

Sorry guy, do you mean the same grep two times and piped into an rm?
I would rather suggest you to look for another solution, this is not the right way.

markkus619 06-16-2015 11:01 AM

Quote:

Originally Posted by pan64 (Post 5378093)
Sorry guy, do you mean the same grep two times and piped into an rm?
I would rather suggest you to look for another solution, this is not the right way.

OK.

Any suggestion??

Later i'll try something...

average_user 06-16-2015 11:01 AM

I did something similar myself recently using procmail, one of the best mail filtering programs out there by looping through all e-mails and executing procmail on them and removing an e-mail afterwards like this:
Code:

#!/usr/bin/env sh

set -e

for dir in cur new; do
    find $INPUTMAILDIR/$dir -type f | while read file; do
        procmail < $file
        rm $file
    done
done

You need to add a correct rule to your ~/.procmailrc before running this script. The rule could look like this:
Code:

:0:
* ^Subject:.*Mail delivery failed: returning message to sender*
/dev/null

BTW, I also recommend procmail as an every-day filtering program for your e-mail. You can make it execute an arbitrary action based on e-mail sender, subject, body or attachments.


All times are GMT -5. The time now is 12:13 AM.