Delete mails by subject using mailx ?

Delete mails by subject using mailx ?

am 28.02.2006 13:46:24 von materik

I want to delete mail by subject , i.e. "delete all mails from my
mailbox having subject xyz"

I want to run this using a crontab scheduled script.


How can I do this ? Im using mailx and Solaris 8.


TIA Materik

Re: Delete mails by subject using mailx ?

am 28.02.2006 23:34:28 von Garen Erdoisa

materik wrote:
> I want to delete mail by subject , i.e. "delete all mails from my
> mailbox having subject xyz"
>
> I want to run this using a crontab scheduled script.
>
>
> How can I do this ? Im using mailx and Solaris 8.
>
>
> TIA Materik
>

You could do this using a combination of formail and procmail

Cron job: to run the script at say 4:23am

23 4 * * * customscript

-=-=-=-=-
In the custom script have something like this which directs formail to
parse the messages on stdin expecting mail box format and to feed each
message separately to it's own procmail process so that the subject
headers can be checked separately. The procmail process would need to
have a custom.rc file to use in this case.
---------
#!/bin/bash

if ! [ -f /path/to/oldmailbox.lock ]; then
touch /path/to/oldmailbox.lock
else
echo "oldmailbox is locked. Script aborted"
exit 1;
fi

# process the oldmailbox messages into newmailbox using procmail to
# delete messages containing xyz in the subject header
cat /path/to/oldmailbox |formail -s procmail /path/to/custom.rc

# replace the oldmailbox with newmailbox.
mv newmailbox oldmailbox

# remove the lockfile
rm /path/to/oldmailbox.lock

-=-=-=-=-
In the procmail custom.rc file
---------

NEWMAILBOX="/home/${LOGNAME}/mail/newmailbox"

# delete messages containing xyz in the subject
# or you could just move them to a different folder.
:0
* ^Subject:.*xyz.*$
/dev/null

# save all messages not containing xyz in the subject to the
# newmailbox folder
:0:
${NEWMAILBOX}


Hope this helps.
Garen