Thursday 12 March 2015

Zimbra script to delete Bulk emails with subject



Senario : A user has sent an email by mistake to a group to which the email was originally not intended to.

The following script will identify the email id from zimbra mysql and delete it

Note: the script should be run as zimbra user




#!/bin/bash
# This script will delete emails from user mail box using subject
#
# File should contain the user email address from which the mail has to be deleted. One email address per line
#
# Usage ./deletemail.sh {filename}
#
# SCRIPT SHOULD BE RUN AS ZIMBRA USER
#

FILENAME=$1
count=0
#READING THE MAIL ADDRESS FROM FILE LINE BY LINE
while read mailid
do

#Subject of the email
subject="Work From Home Policy"

#getting the mboxstore id to search for the email in mysql
mboxid=$(zmprov getMailboxInfo $mailid | grep mailboxId | awk {'print $2'}|awk '{ print substr( $0, length($0) - 1, length($0) ) }')
echo Searching $mailid for message

# GETTING THE MESSAGE ID FROM MYSQL
msgid=$(/opt/zimbra/bin/mysql mboxgroup"$mboxid" -e "select id from mail_item where mailbox_id='$mboxid' and subject like '%$subject%'" | awk {'print $1'} | grep -v "id")
echo $msgid
echo Removing the message $msgid from $mailid
# DELETING THE MESSAGE FROM THE MAILBOX
/opt/zimbra/bin/zmmailbox -z -m $mailid dm $msgid
echo Done.
done < $FILENAME

3 comments:

  1. Thank you for sharing this information. I find this information is easy to understand and very useful. Thumbs up!


    Melbourne App Developer

    ReplyDelete
  2. Thank you for sharing this information. I find this information is easy to understand and very useful. Thumbs up!

    Melbourne App Developer

    ReplyDelete
  3. how can i narrow down search by adding date?

    ReplyDelete

High Availability with IREDMAIL Integrated with Active Directory

This is step by step guide for Centos 7. Server1 will be the Active node and Server2 will be the failover node. After failover when ...