Contrib: How to delete emails based on subject script

Discuss your pilot or production implementation with other Zimbra admins or our engineers.
pacsteel
Posts: 37
Joined: Fri Sep 12, 2014 10:17 pm

Contrib: How to delete emails based on subject script

Postby pacsteel » Wed Jan 20, 2010 6:56 pm

This may be an old thread, but I ran into this same problem today so I thought I'd share the script I used in case someone else stumbles across this thread via a search engine like I did.
I had a user who sent about 50mb of photographs to every one of the users in her address book. Multiple times if they were in multiple distribution lists. Since I have multiple remote sites that use my Zimbra server via a couple shared DSL lines, this clobbered all the remote locations.
I took examples from a couple of the sources linked earlier in the thread (bdial's psudocode and King0770's script), and combined them. It runs about 35 accounts per minute on my system, so it wouldn't scale too well for larger installations, but it is still way faster than doing it by hand.
The expected inputs are the from_username and subject. The script steps through each account on the server and deletes any messages it finds from the user provided and with the subject provided.
I ran: ./rm_mssg.sh Smart_User "(no subject)"

Smart_User isn't a big fan of filling in the subject when she sends emails so there was some collateral damage, but I didn't care at that point.


#!/bin/bash

# rm_mssg.sh user subject
if [ -z "$2" ]; then

echo "usage: rm_message.sh "

echo " Rummages through all mail boxes looking for messages from with subject="

echo " Example: ./rm_mssg.sh butthead_user rems "

echo " Put double-quotes around multi-word subjects"

exit 0

else

addr=$1

subject=$2
for usractnopac in `zmprov gaa`

do

echo "Searching $usractnopac for Subject: $subject From: $addr"

for msg in `zmmailbox -z -m "$usractnopac" s -l 999 -t message "from:$addr subject:$subject"|awk '{ if (NR!=1) {print}}' |

grep -v -e Id -e "-" -e "^$" | awk '{ print $2 }'`

do

echo "Removing "$msg""

zmmailbox -z -m $usractnopac dm $msg

done

done

fi



bdial
Elite member
Elite member
Posts: 1633
Joined: Fri Sep 12, 2014 10:39 pm

Contrib: How to delete emails based on subject script

Postby bdial » Thu Jan 21, 2010 7:42 am

handy. you may want to put it on your sandbox too so people could find it searching the wiki
stealthguitar
Posts: 6
Joined: Sat Sep 13, 2014 1:01 am

Contrib: How to delete emails based on subject script

Postby stealthguitar » Tue Mar 30, 2010 1:03 pm

Used this to remove a sensitive email from my mail store BUT it failed to remove the messages from the trash....does this make sense? I would like to run this script again as it is very powerful but am not a coder and can't figure out how to tweak it to delete the message when it's in the trash.
uxbod
Ambassador
Ambassador
Posts: 7811
Joined: Fri Sep 12, 2014 10:21 pm

Contrib: How to delete emails based on subject script

Postby uxbod » Tue Mar 30, 2010 1:08 pm

Welcome to the forums :)
Silly question but was this in your mailstore; so could you not right click and empty. Or do you mean across the entire mailstore ?
stealthguitar
Posts: 6
Joined: Sat Sep 13, 2014 1:01 am

Contrib: How to delete emails based on subject script

Postby stealthguitar » Tue Mar 30, 2010 1:27 pm

I'm not sure of your question. The message was addressed to "EVERYONE" Someone replied to EVERYONE at our company and my boss asked if there was a way to delete that specific message from the sender quickly. So I found that script and ran it.
I ran the script above as is...I did need change the line

for usractnopac in `zmprov gaa`

to

for usractnopac in `zmprov -l gaa` or it wouldn't run.
stealthguitar
Posts: 6
Joined: Sat Sep 13, 2014 1:01 am

Contrib: How to delete emails based on subject script

Postby stealthguitar » Tue Mar 30, 2010 1:40 pm

No it wasn't just for my email account it was for 100+ accounts.
bdial
Elite member
Elite member
Posts: 1633
Joined: Fri Sep 12, 2014 10:39 pm

Contrib: How to delete emails based on subject script

Postby bdial » Tue Mar 30, 2010 1:57 pm

i bet it has somethign to do with the fact that searching in trash & junk is disabled by default on most accounts and requires explicitely telling it to in advanced search
bdial
Elite member
Elite member
Posts: 1633
Joined: Fri Sep 12, 2014 10:39 pm

Contrib: How to delete emails based on subject script

Postby bdial » Tue Mar 30, 2010 2:00 pm

yah i think so, try doing this
change this line


for msg in `zmmailbox -z -m "$usractnopac" s -l 999 -t message "from:$addr subject:$subject"|awk '{ if (NR!=1) {print}}' |


to


for msg in `zmmailbox -z -m "$usractnopac" s -l 999 -t message "is:anywhere from:$addr subject:$subject"|awk '{ if (NR!=1) {print}}' |


the is:anywhere says to search junk & trash as well
owls
Posts: 2
Joined: Sat Sep 13, 2014 3:19 am

Contrib: How to delete emails based on subject script

Postby owls » Tue Dec 17, 2013 1:07 pm

Can I use a regex for the $subject value? I'm picking up some emails that I don't want to delete. My search for info about this has come up empty...
Thanks for any clarification.
Owls

Return to “Administrators”

Who is online

Users browsing this forum: No registered users and 12 guests