Add Account to Distribution List in Zimbra: 3 Easy Fixes

To add account to distribution list members in Zimbra you need one command, zmprov adlm, or a few clicks in the Admin Console. The first version of this post also shared a small bash script that asks for the list and the account, checks whether the account is already a member and adds it if not. The screenshot of that script comes from a real Zimbra server. This rewrite keeps the script, runs it again, and shows three cases where its membership check gives the wrong answer, plus a fixed version that adds many accounts in one call.

Lab note: Zimbra is not installed in this lab. The scripts run in a debian:13 container with a stub zmprov that stores the members in a file and prints gdl output in the same layout as Zimbra's own code (dumpGroup() in ProvUtil.java): a memberCount header, the attributes, then a line members and one address per line. The error text for a missing list is the one Zimbra defines in AccountServiceException. The commands and the Admin Console path come from the Zimbra Administrator's Guide.

Add account to distribution list with zmprov

The admin guide lists these commands to add account to distribution list members and manage static lists. Run them as the zimbra user (su - zimbra):

  • zmprov adlm {list@domain | id} {member@domain}: addDistributionListMember. In ProvUtil.java the syntax is {member@domain}+, so one call can take several members.
  • zmprov rdlm {list@domain | id} {member@domain}: removeDistributionListMember.
  • zmprov gdl {list@domain | id}: getDistributionList, shows the attributes and the members.
  • zmprov gdlm {name@domain | id}: getDistributionListMembership, the lists an address belongs to.

The old FAQ said to remove a member with zmprov rmlm. That command does not exist in the guide or in ProvUtil.java; the correct short name is rdlm.

Here is a test list dl01 with three members, added in a single adlm call, then read back with gdl:

zimbra$ zmprov adlm dl01@mail.huuphan.local huupv1@mail.huuphan.local huupv3@mail.huuphan.local it.huupv@mail.huuphan.local

zimbra$ zmprov gdl dl01@mail.huuphan.local
# distributionList dl01@mail.huuphan.local memberCount=3
mail: dl01@mail.huuphan.local
zimbraMailStatus: enabled

members
huupv1@mail.huuphan.local
huupv3@mail.huuphan.local
it.huupv@mail.huuphan.local

On a real Zimbra distribution list the attribute part is much longer, but the members block at the end is what matters for a membership check.

Add a member to a Zimbra distribution list in the Admin Console

The admin guide path is Home → Manage → Distribution Lists. When you create a list (Gear icon → New), the Members page lets you search the company directory in the right column and click Add Selected or Add This Page. For addresses that are not in the directory, type the full address in the Or enter addresses below box. So yes, external addresses can be members of a Zimbra distribution list. To change an existing Zimbra distribution list, open it from the same Distribution Lists page.

The guide also notes that list owners can manage members themselves from the Distribution List folder of their Address Book, and that a user's "Member Of" page in the Admin Console shows every list the account is in. That page is a quick way to confirm a change without the command line.

The old script to add account to distribution list

This is the script from the original post. It runs as root, asks two questions, and calls zmprov through su - zimbra:

#!/bin/bash
#Author: HuuPV
#Add member to DL
echo -n "Input distribute name, pls (Ex: dl01@mail.huuphan.local): "
read DL
echo -n "Input account add to distribute name, pls (Ex: huupv@mail.huuphan.local): "
read AC
Check_Member_DL=$(su - zimbra -c "zmprov gdl $DL | grep '\b$AC'")
if [ $? == 0 ]
then
       echo "$AC member exists in $DL"
else
       echo "Add member $AC to $DL Distribute List"
       su - zimbra -c "zmprov adlm $DL $AC"
fi

This is the result on the operator's Zimbra server, from the original post:

zimbra_Distribute.sh run on a Zimbra server: one existing member, one new member added
The script on a real Zimbra server: huupv3 already in dl01, huupv10 added.

With the same answers, the lab gives the same two results:

# ./add_to_dl.sh   (answers: dl01@mail.huuphan.local / huupv3@mail.huuphan.local)
huupv3@mail.huuphan.local member exists in dl01@mail.huuphan.local

# ./add_to_dl.sh   (answers: dl01@mail.huuphan.local / huupv10@mail.huuphan.local)
Add member huupv10@mail.huuphan.local to dl01@mail.huuphan.local Distribute List

So the normal path to add account to distribution list members works. The problems show up with other input.

3 cases where the membership check is wrong

1. A similar address counts as a member

huupv@mail.huuphan.local is not in the list, but it.huupv@mail.huuphan.local is. The script says the account already exists and does nothing:

# ./add_to_dl.sh   (answers: dl01@mail.huuphan.local / huupv@mail.huuphan.local)
huupv@mail.huuphan.local member exists in dl01@mail.huuphan.local

The reason is the pattern. \b is a word boundary, and the dot in it.huupv is a word boundary too, so the search matches the end of a longer address:

zimbra$ zmprov gdl dl01@mail.huuphan.local | grep "\bhuupv@mail.huuphan.local"
it.huupv@mail.huuphan.local

The dots in the pattern are also regex wildcards, and grep searches the whole gdl output, attributes included, not only the members block.

2. Pressing Enter on the second question

With an empty answer the pattern is just \b, which matches almost every line, so the script reports a member that has no name:

# ./add_to_dl.sh   (answers: dl01@mail.huuphan.local / )
 member exists in dl01@mail.huuphan.local

3. A typo in the list name

When the Zimbra distribution list does not exist, zmprov gdl fails, grep finds nothing, and the script goes on to the add branch. The user sees "Add member" between two errors, which looks like partial success:

# ./add_to_dl.sh   (answers: dl1@mail.huuphan.local / huupv20@mail.huuphan.local)
ERROR: account.NO_SUCH_DISTRIBUTION_LIST (no such distribution list: dl1@mail.huuphan.local)
Add member huupv20@mail.huuphan.local to dl1@mail.huuphan.local Distribute List
ERROR: account.NO_SUCH_DISTRIBUTION_LIST (no such distribution list: dl1@mail.huuphan.local)

Note also that $? in the script is the status of the last command in the pipeline, which is grep, not zmprov. That is why a failed gdl is never noticed.

A fixed script for the Zimbra distribution list

This version takes the list and one or more addresses as arguments, runs as the zimbra user, stops if the list does not exist, compares only the lines after members as fixed strings (grep -qixF: whole line, any case), and sends all new members in one adlm call:

#!/bin/bash
# usage: add_to_dl2.sh list@domain member@domain [member@domain ...]   (run as zimbra)
set -u
DL=${1:?usage: $0 list@domain member@domain...}; shift
[ $# -gt 0 ] || { echo "no member given" >&2; exit 2; }
current=$(zmprov gdl "$DL") || exit 1          # stops on NO_SUCH_DISTRIBUTION_LIST
members=$(printf "%s\n" "$current" | sed -n "/^members$/,\$p" | tail -n +2)
new=()
for AC in "$@"; do
  if printf "%s\n" "$members" | grep -qixF -- "$AC"; then
    echo "skip: $AC is already in $DL"
  else
    new+=("$AC")
  fi
done
[ ${#new[@]} -eq 0 ] && exit 0
zmprov adlm "$DL" "${new[@]}" && echo "added to $DL: ${new[*]}"

The same three cases with the fixed script:

zimbra$ ./add_to_dl2.sh dl01@mail.huuphan.local huupv@mail.huuphan.local huupv3@mail.huuphan.local HUUPV1@mail.huuphan.local
skip: huupv3@mail.huuphan.local is already in dl01@mail.huuphan.local
skip: HUUPV1@mail.huuphan.local is already in dl01@mail.huuphan.local
added to dl01@mail.huuphan.local: huupv@mail.huuphan.local
[exit=0]

zimbra$ ./add_to_dl2.sh dl1@mail.huuphan.local huupv20@mail.huuphan.local
ERROR: account.NO_SUCH_DISTRIBUTION_LIST (no such distribution list: dl1@mail.huuphan.local)
[exit=1]

zimbra$ ./add_to_dl2.sh dl01@mail.huuphan.local
no member given
[exit=2]
Old check vs fixed script when you add account to distribution list in Zimbra
The old grep check reports huupv@ as a member because it.huupv@ is one; the fixed script adds it.

Add many accounts to a Zimbra distribution list from a file

The old post also had this loop over accounts.txt. I tested it with a file saved on Windows (CRLF line ends) whose last line has no newline, which is common when the list comes from a spreadsheet:

zimbra$ while read email; do zmprov adlm team@mail.huuphan.local "$email"; done < accounts.txt; zmprov gdl team@mail.huuphan.local | cat -A
# distributionList team@mail.huuphan.local memberCount=2$
mail: team@mail.huuphan.local$
zimbraMailStatus: enabled$

members$
a1@mail.huuphan.local^M$
a2@mail.huuphan.local$

Two problems: the third address was never sent, because read returns false on a last line without a newline, and the first address reached zmprov with a carriage return (^M) at the end. How a real server treats that extra character was not tested here; it is safer not to send it. Strip the \r and pass the whole file to the fixed script in one go:

zimbra$ tr -d "\r" < accounts.txt | xargs ./add_to_dl2.sh team@mail.huuphan.local; zmprov gdl team@mail.huuphan.local
added to team@mail.huuphan.local: a1@mail.huuphan.local a2@mail.huuphan.local a3@mail.huuphan.local
# distributionList team@mail.huuphan.local memberCount=3
mail: team@mail.huuphan.local
zimbraMailStatus: enabled

members
a1@mail.huuphan.local
a2@mail.huuphan.local
a3@mail.huuphan.local

One adlm call with many members also starts zmprov only once, instead of once per line.

Tips before you add account to distribution list in bulk

Conclusion

The quickest way to add account to distribution list members is zmprov adlm list@domain user1@domain user2@domain, and zmprov gdl shows the result. The original helper script works for the normal case, as the screenshot shows, but its grep '\b$AC' check gives a false "member exists" for similar addresses and empty input, and it does not stop on a wrong list name. The fixed script checks only the members block, fails early, and handles many addresses at once, which keeps every Zimbra distribution list accurate.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance