Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
Go Back   Zimbra :: Forums > Zimbra Collaboration Suite > Administrators

Welcome to the Zimbra :: Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-2010, 04:56 PM
Active Member
 
Posts: 37
Default Contrib: How to delete emails based on subject script

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.

Code:
#!/bin/bash
# rm_mssg.sh user subject

if [ -z "$2" ]; then
 echo "usage:  rm_message.sh <fromuser_name> <subject>"
 echo "  Rummages through all mail boxes looking for messages from <fromuser_name> with subject=<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
__________________
Production: 5.0.18_GA_3011.UBUNTU8 FOSS
Ubuntu 8.04 LTS

Testing: 7.1.4_GA_2555.UBUNTU10_64 NETWORK edition
Ubuntu 10.04.4 LTS

Last edited by pacsteel; 01-20-2010 at 04:58 PM.. Reason: attribution
Reply With Quote
  #2 (permalink)  
Old 01-21-2010, 05:42 AM
Moderator
 
Posts: 1,554
Default

handy. you may want to put it on your sandbox too so people could find it searching the wiki
Reply With Quote
  #3 (permalink)  
Old 03-30-2010, 11:03 AM
Junior Member
 
Posts: 6
Default

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.
Reply With Quote
  #4 (permalink)  
Old 03-30-2010, 11:08 AM
Moderator
 
Posts: 7,928
Default

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 ?
__________________
Reply With Quote
  #5 (permalink)  
Old 03-30-2010, 11:27 AM
Junior Member
 
Posts: 6
Default

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.
Reply With Quote
  #6 (permalink)  
Old 03-30-2010, 11:40 AM
Junior Member
 
Posts: 6
Default

No it wasn't just for my email account it was for 100+ accounts.
Reply With Quote
  #7 (permalink)  
Old 03-30-2010, 11:57 AM
Moderator
 
Posts: 1,554
Default

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
Reply With Quote
  #8 (permalink)  
Old 03-30-2010, 12:00 PM
Moderator
 
Posts: 1,554
Default

yah i think so, try doing this

change this line

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

Code:
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

blog.zimbra.com




 

SEO by vBSEO ©2011, Crawlability, Inc.