@
you're reading...
Scripting, Zimbra

Delete emails on Zimbra based on Subject

This is just a modification of what I have found from this link. Have added ability to loop through a mailbox when it finds multiple messages matching the subject defined.

#!/bin/bash

EMAIL_ADDRESSES=temp_email
SUBJECT="Employee Compensation Letter"

echo "Subject is: $SUBJECT"
echo ""
cat $EMAIL_ADDRESSES|while read mailid; do
#echo $mailid
#getting the mboxstore id to search for the email in mysql
mboxid=$(zmprov getMailboxInfo $mailid | grep mailboxId | awk {'print $2'})
mboxgrp=$(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_COUNT=0
while [ $MSGID_COUNT -eq 0 ]; do
if [ `/opt/zimbra/bin/mysql mboxgroup"$mboxgrp" -e "select id from mail_item where mailbox_id='$mboxid' and subject like '%$SUBJECT%'" | awk {'print $1'} | grep -v "id" |wc -l` -gt 1 ]; then
#print msg ids
/opt/zimbra/bin/mysql mboxgroup"$mboxgrp" -e "select id from mail_item where mailbox_id='$mboxid' and subject like '%$SUBJECT%'" | awk {'print $1'} | grep -v "id"
##
msgid=$(/opt/zimbra/bin/mysql mboxgroup"$mboxgrp" -e "select id from mail_item where mailbox_id='$mboxid' and subject like '%$SUBJECT%'" | awk {'print $1'} | grep -v "id"|tail -1)
#if [ ! -z $msgid ]; then
echo "Removing the message $msgid"
/opt/zimbra/bin/zmmailbox -z -m $mailid dm $msgid
#else
# echo "No message to delete from $mailid"
#fi
else
msgid=$(/opt/zimbra/bin/mysql mboxgroup"$mboxgrp" -e "select id from mail_item where mailbox_id='$mboxid' and subject like '%$SUBJECT%'" | awk {'print $1'} | grep -v "id"|tail -1)
if [ ! -z $msgid ]; then
echo "Removing the message $msgid"
/opt/zimbra/bin/zmmailbox -z -m $mailid dm $msgid
else
echo "No message to delete"
fi
MSGID_COUNT=1
fi
done

#echo Done.
echo ""
done

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s