📜 ⬆️ ⬇️

Zimbra: delete random or unnecessary emails

The topic may be beaten and is not very relevant if the automatic deletion of emails is set up after a certain period of time.

But if it happens that one employee instead of sending a very private piquant message to another, sends it (message) to the entire holding ... To wait for the day when the automatic cleaning starts is not an option, but everyone should delete it now.

Under the cut description of how this is implemented in me.

Having rummaged in a network, I came across a site where the small script for removal of old mail is described, it is not bad, but did not approach for removal of letters in subfolders.
')
Solved problems in approximation to the main task.

First you need to find all the folders that could get a letter with the details of the evening, for this we will help zmmailbox (powerful and comprehensive assistant). Enter:

/opt/zimbra/bin/zmmailbox -z -m user@corp.org gaf # gaf - getAllFolders 

In response, we get:

  Id View Unread Msg Count Path ---------- ---- ---------- ---------- ---------- 1 unkn 0 0 / 16 docu 0 15 /Briefcase 10 appo 0 52 /Calendar 14 mess 0 1564 /Chats 7 cont 0 7 /Contacts 6 mess 0 56 /Drafts 13 cont 0 179 /Emailed Contacts 2 mess 0 5352 /Inbox 65450 mess 0 12433 /Inbox/ 1.04.2015 31907 mess 0 3125 /Inbox/ 5993 mess 0 881 /Inbox/ 4 mess 0 0 /Junk 5 mess 0 7527 /Sent 15 task 0 8 /Tasks 3 unkn 0 0 /Trash 1770 mess 7 7 /Trash/.  (support@corp.org:703) 3741 docu 0 1 / 87195 docu 0 1 / 1015 docu 0 12 / 663 cont 0 1 /   290 task 0 2 / 1100 mess 4404 22301 /.  -  (support@corp.org:2) 

As we see, we are full of everything. The View column pre-opens our eyes and we see that there are folder types. We need folders with messages - this is mess. Knowing what we need, we can filter the query:

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org gaf | grep mess 

Query output:

  Id View Unread Msg Count Path ---------- ---- ---------- ---------- ---------- 14 mess 0 1564 /Chats 6 mess 0 56 /Drafts 2 mess 0 5352 /Inbox 65450 mess 0 12433 /Inbox/ 1.04.2015 31907 mess 0 3125 /Inbox/ 5993 mess 0 881 /Inbox/ 4 mess 0 0 /Junk 5 mess 0 7527 /Sent 1770 mess 7 7 /Trash/.  (support@corp.org:703) 1100 mess 4404 22301 /.  -  (support@corp.org:2) 

As you can see, we have already more than the desired list, but 1100 mess 4404 22301 / Tech. Support - Inbox (support@corp.org: 2) is a shared folder from another mailbox, and, therefore, it will be checked separately. Add one more exception and immediately remove all unnecessary: ​​types, sequence numbers, remove all before the slash (folder names).

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org gaf | grep mess | cut -d"/" -f 2- | grep -v "@corp.org" 

We get the desired list:

Chats
Drafts
Inbox
Inbox / until 1.04.2015
Inbox / Marketing
Inbox / Eureka
Junk
Sent

The output of the command is redirected to a file, for small and rare convenience, and the script will take the names of the folders from the file already. So, the most difficult thing is not so much to look in folders, as to look in folders whose names contain spaces or special characters.

If you enter in the console:

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org s -l 300 in:Inbox/ | grep `date -d '-5 day' +%m/%d/%y` | grep "" | sed -e "s/^\s\s*//" | sed -e "s/\s\s*/ /g" 

Displays the subject of letters 5 days old. Instead of date -d '-5 day' +% m /% d /% y, you can immediately write the dates in the 01/27/16 format or change -5 to a different value, depending on how many days ago you need to roll back.

But if you enter:

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org s -l 300 in:Inbox/  | grep `date -d '-5 day' +%m/%d/%y` | grep "" | sed -e "s/^\s\s*//" | sed -e "s/\s\s*/ /g" 

That the error will fall out:

 ERROR: mail.NO_SUCH_FOLDER (no such folder path: /Inbox/\) 

And even if you add quotes, apostrophes and screens - it will not help.

Then I pointed to the wiki , which found the answer to the question.
To solve the problem, you need to put the folder name with spaces in certain blocks. "\" Is a block tag.

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org s -l 300 in:"\"Inbox/ "\" 

By doing this in the console, I got the desired result, but my task was to script everything. And in my script, the variable containing the folder name took the data from the file.

 /opt/zimbra/bin/zmmailbox -z -m user@corp.org gaf | grep mess | cut -d"/" -f 2- | grep -v "@corp.org" > /tmp/messfolder.list 

And when inserting a variable into my code:

 /opt/zimbra/bin/zmmailbox -z -m $is -l 300 in:"\"$p"\" | grep `date -d '-5 day' +%m/%d/%y` | grep "" | sed -e "s/^\s\s*//" | sed -e "s/\s\s*/ /g" | 

I fell out the exact same error:

 ERROR: mail.NO_SUCH_FOLDER (no such folder path: /Inbox/\) 

Although the variable is placed in the desired block tags. Looking for some more, I remembered stumbled upon the IFS field separator variable. I asked him the value of IFS = $ '\ n' and ran the script - everything went as planned.

Here is the whole script with descriptions
 #!/bin/bash DOMAIN_NAME="corp.org" #   EMAIL=/tmp/email.list #     MESID=/tmp/mesid.list #  ID    DELTEXT='' #         IFS=$'\n' #      before="$(date +%s)" #    t=0 # ,     /opt/zimbra/bin/zmprov -l gaa $DOMAIN_NAME | sort > $EMAIL #    #      for i in $(cat $EMAIL); do echo $i #      ,    ,          ,    -  ,    ,  mess /opt/zimbra/bin/zmmailbox -z -m $i gaf | grep mess | cut -d"/" -f 2- | grep -v "@corp.org" > /tmp/messfolder.list #         ,       id   .      . for p in $(cat /tmp/messfolder.list); do echo $p #   :   grep     , grep   ,  grep  ,  sed'     cut     ,   . /opt/zimbra/bin/zmmailbox -z -m $is -l 300 in:"\"$p"\" | grep `date -d '-5 day' +%m/%d/%y` | grep "$DELTEXT" | sed -e "s/^\s\s*//" | sed -e "s/\s\s*/ /g" | cut -d" " -f2 > $MESID #      - . cat $MESID count=`grep '' $MESID -c` let t=$t+$count for a in $(cat $MESID | grep ^- | sed s/-//g ) do /opt/zimbra/bin/zmmailbox -z -m $i deleteMessage $a done for a in $(cat $MESID | sed /-/d) do /opt/zimbra/bin/zmmailbox -z -m $i deleteConversation $a done echo -n > $MESID RES=$? if [ "$RES" == "0" ]; then echo "[Ok]"; else echo "[Err]"; fi done echo " : "$t done after="$(date +%s)" elapsed="$(expr $after - $before)" hours=$(($elapsed / 3600)) elapsed=$(($elapsed - $hours * 3600)) minutes=$(($elapsed / 60)) seconds=$(($elapsed - $minutes * 60)) echo " : $hours  $minutes  $seconds " echo " : "$t" " 


Of course, the script can still be refined: remove unnecessary folders, enter the date and subject in advance, but this is not so important. This script is hung on a homemade web-admin and all the dates and topics for removal I enter from it, everything is more convenient.

Someone thinks that this is unnecessary, and such a script is superfluous, but as practice has shown, he saved at least 10 people and a couple of bosses. They feel good, and the admin after they feel good, also became good.

This script has one drawback - this is the time. This script runs through all the folders of 700 users in 4 hours. So far, I have not identified where it is possible to gain time, but if you tell me or point out mistakes, I will be grateful.

Thanks for reading and comments.

Source: https://habr.com/ru/post/277117/


All Articles