Zimbra - Generate HTML Signature Based on LDAP Data

Introduction



Zimbra Web Admin has no tools for generating email signatures. Having shoveled a bunch of manuals, I realized that none of them suits me.



The task seems trivial, but a straightforward solution was not immediately found. Hope this article helps someone save tons of time and effort.



On my part, this is a contribution to the community, for the previously used developments.



System requirements



system: Linux Ubuntu 18.04

Zimbra version: Zimbra 8.8.15



HTML signature generation script



Let's start right away with the script itself ... below I will give explanations and a description of how the script works.



#!/bin/bash
# created by Parfentiev Aleksey, Shultz Denis
 
SRC_LOGO="https://mail.domen.ru/home/it@domen.ru/Briefcase/Logo/tl_logo.png"
SRC_TXT=" "
SIGN_NAME="_"
DOMEN="domen.ru"
 
# Check for run as zimbra user
ID=`id -u -n`
if [ x$ID != "xzimbra" ]; then
   echo "Please run as ZIMBRA user"
   echo "Exiting..."
   exit 1
fi
 
# Obtain all user accounts in $DOMEN (template: Family.IN@domen.com or family.in_jr@domen.com)
accounts=`zmaccts | grep 'active'| grep $DOMEN | grep -P "(\b[A-z]+)\.(\D{2,5}@).*" | awk '{print $1}'`
 
for ac in $accounts; do
  echo -ne "Checking account: $ac \t"
 
    # Try delete signature
    /opt/zimbra/bin/zmprov dsig $ac $SIGN_NAME
 
    echo -ne "Setting signature... "
 
    # Obtain signature from LDAP user atributes
 
    declare -A arr
    mapfile -t ARRAY < <(/opt/zimbra/bin/zmprov ga $ac | egrep "(^cn|^title|^mobile|^zimbraPrefFromAddress|^st|^l|^street|^telephoneNumber|^pager|^postalCode)" | sed 's/: /:/')
    for sgn in "${ARRAY[@]}" ; do
        KEY=${sgn%%:*}
        VALUE=${sgn#*:}
        arr[$KEY]=$VALUE
    done
 
    HTML='<div style="display: inline-block;" data-mce-style="display: inline-block;">'
    HTML+='<br><table style="width: 550px; border: 0; border-top: 1px #e0e0e0 solid;" align="left" data-mce-style="width: 550px; border: 0; border-top: 1px #e0e0e0 solid;">'
    HTML+='<tbody>'
    HTML+='<tr><td style="width: 87px; padding: 5px;" valign="middle" align="center" data-mce-style="width: 87px; padding: 5px;">'
    HTML+='<img src="'$SRC_LOGO'"  alt="'$SRC_TXT'"></td>'
    HTML+='<td style="font-size: 10pt; color: #808080; font-family: georgia, serif; width: 389px;" data-mce-style="font-size: 10pt; color: #808080; font-family: georgia, serif; width: 389px;">'
    HTML+="${arr['cn']} - ${arr['title']}   <br>: ${arr["telephoneNumber"]} .${arr["pager"]}   : ${arr["mobile"]}"
    HTML+="<br>e-mail: ${ac} <br>${arr['postalCode']} ${arr['st']} ${arr['l']} ${arr['street']}</td></tr></tbody></table></div>"
    unset arr
 
    # Set signature for account $ac and obtain signature id account
    sign_id=$(/opt/zimbra/bin/zmprov csig $ac $SIGN_NAME zimbraPrefMailSignatureHTML "$HTML")
    # Obtain delegates mailbox accounts
    mapfile -t ARRAY < <(/opt/zimbra/bin/zmprov gid $ac name | cut -d " "  -f 3-7 | grep -P '[^.*]')
    for gid in "${ARRAY[@]}" ; do
    # Set defalt signature position (outlook (before) or internet (after attachments)) for each account
    /opt/zimbra/bin/zmprov modifyIdentity $ac "$gid" zimbraPrefDefaultSignatureId $sign_id zimbraPrefForwardReplySignatureId $sign_id zimbraPrefMailSignatureStyle outlook
    done
    echo "done!"


we transfer this script to the host machine in any way known to you.



Place it in the / usr / local / bin folder ... or create a file zm_sign_html.sh along this path and copy / paste ...



sudo -i
#    sudo user
#    /usr/local/bin/zm_sign_html.sh
touch /usr/local/bin/zm_sign_html.sh
#    copy/paste
#  
chmod 755 /usr/local/bin/zm_sign_html.sh


Description of the script



  1. check if the script is running from the zimbra user (CLI commands are executed from the zimbra user).

    it is possible to do it differently ... use the runuser command and run the script as root.

  2. .



    :



    family.in@domen.ru family.in_jr@domen.ru



    : family โ€” .

    in โ€”

    in_jr โ€” ( .).



    : Zimbra



    , ivanov.aa@domen.ru , zavod@domen.ru .



    grep -P "(\b[A-z]+)\.(\D{2,5}@).*"



    accounts=`zmaccts | grep 'active'| grep $DOMEN | grep -P "(\b[A-z]+)\.(\D{2,5}@).*" | awk '{print $1}'`
    


  3. SIGN_NAME ( โ€” CLI , )

  4. .



    LDAP, , HTML ( HTML ).



    
    mapfile -t ARRAY < <(/opt/zimbra/bin/zmprov ga $ac | egrep "(^cn|^title|^mobile|^zimbraPrefFromAddress|^st|^l|^street|^telephoneNumber|^pager|^postalCode)" | sed 's/: /:/')
    


    , : cn, title ..



    :





  5. ( , ) .



    , ( โ€” ).







    , ( ยซ ยป, ยซ ยป ยซ ยป).



    . .



    :



    /opt/zimbra/bin/zmprov modifyIdentity $ac "$gid" zimbraPrefDefaultSignatureId $sign_id zimbraPrefForwardReplySignatureId $sign_id zimbraPrefMailSignatureStyle outlook


    zimbraPrefMailSignatureStyle: outlook โ€” , internet โ€” โ€ฆ



    โ€” :





  6. .

  7. .





SRC_LOGO = " mail.domen.ru/home/it@domen.ru/Briefcase/Logo/tl_logo.png " - link to the organization's logo.



Create a mailbox for the IT department (it@domen.ru). Zimbra has a built-in document filing service (Portfolio). In the box of the IT department in the Portfolio, create a LOGO folder, put the logo of our organization in it.



Allow access to this folder.



image



Now the logo of Our organization is available at the address:

mail.domen.ru/home/it@domen.ru/Briefcase/Logo/tl_logo.png



SRC_TXT = "LOGO of the Organization" - a text explanation if the loading of images in the browser is disabled.



SIGN_NAME = "Organization" - Signature name (you can transliterate, you can in Russian).



DOMEN= "Domen.ru" - mail domain for which we will install signatures.



Automation of execution



Set up a crontab from the zimbra user:



sudo su zimbra
crontab -e


Be careful, zimbra background jobs are running in this User's crontabe (between the ZIMBRA_START and ZIMBRA_END sections).



We add Our task to the end of the file ... do not forget to leave an empty line at the end!



We run the script /usr/local/bin/zimbra/zm_sign_html.sh every day at 6:00.



Example crontab file:



#
35 3 * * * /opt/zimbra/bin/zmcbpadmin --cleanup >/dev/null 2>&1
# ZIMBRAEND -- DO NOT EDIT ANYTHING BETWEEN THIS LINE AND ZIMBRASTART
0 6 * * * /usr/local/bin/zm_sign_html.sh


Conclusion



That's all for now. You can add a link to e-mail. If someone needs it, I will add it.

Write in the comments ...



Good mood, everyone! And don't get sick!



Links to used articles



phas13.blogspot.com/search/label/Zimbra

wiki.zimbra.com/wiki/Setting_automatic_Default_Signature



PS: Special thanks to Denis Schultz (tlk234) for help in debugging the script and working with the Zimbra CLI documentation.



All Articles