1

We have a zimbra mail server for the members and all members have e-mail addresses which we give them to use our services like signing in the wireless network and etc.. Memberships are temporary. We will have new members and leaving members for every six month period. For now we have created current members uploading their information from .csv file. After six months we will have to disable current mail accounts and add new ones.

What I want to do is to accomplish those tasks using PHP. I have found some examples on Zimbra's wiki page. They show how to create accounts reading from .csv files using Perl.

#!/usr/bin/perl

# Lookup the valid COS (Class of Service) ID in the interface or like this
my $cosid = `su - zimbra -c 'zmprov gc Default |grep zimbraId:'`;
$cosid =~ s/zimbraId:\s*|\s*$//g;

while (<>) {
       chomp;

       # CHANGE ME: To the actual fields you use in your CSV file
       my ($email, $password, $first, $last) = split(/\,/, $_, 4);

       my ($uid, $domain) = split(/@/, $email, 2);

       print qq{ca $uid\@$domain $password\n};
       print qq{ma $uid\@$domain zimbraCOSid "$cosid"\n};
       print qq{ma $uid\@$domain givenName "$first"\n};
       print qq{ma $uid\@$domain sn "$last"\n};
       print qq{ma $uid\@$domain cn "$uid"\n};
       print qq{ma $uid\@$domain displayName "$first $last"\n};
       print qq{ma $uid\@$domain zimbraPasswordMustChange TRUE\n};
       print qq{\n};
}

How can I run those zimbra commands using PHP file? I want to read list from a .csv file and create and/or disable accounts on the server.

zkanoca
  • 113
  • 1
  • 8

2 Answers2

1

Zimbra: Create 1000 users and add them to Zimbra automatically in batch mode

Greetings, many times in my laboratories on Zimbra, I need to create random users and that they have a clear account, either to perform migrations, or to perform backups, etc. Obviously creating 10, 50 or 1000 users manually could take us forever, and it would not be profitable or productive, so today I leave you this advice here that I hope will help you:

The first thing we will do is go to /var/tmp with the user zimbra

su - zimbra
cd /var/tmp

Then we will create a file called usercreation.sh

touch usercreation.sh

Inside this script we will introduce the following, please adjust it with your domain, and customize it a bit if you want:

vi usercreation.sh

#!/bin/bash
# Zimbra Collaboration user creation
cd /var/tmp
x=0
while [ $x -lt 1000 ]
do
echo "ca demo$x@lab1.com 'Password$' sn 'Demo User${x}' givenName 'Demo' zimbraPrefFromDisplay 'Demo User${x}'" >> userlist.zmp
x=`expr $x + 1`
done

We will add the privileges to be able to execute it

chown zimbra:zimbra /var/tmp/user*
chmod 777 /var/tmp/user*

And finally we will launch it, it will take a few seconds to run completely

./usercreation.sh

And we can now look at the content of the userlist.zmp file, for example the last lines:

ca demo995@lab1.com 'Password$' sn 'Demo User995' givenName 'Demo' zimbraPrefFromDisplay 'Demo User995'
ca demo996@lab1.com 'Password$' sn 'Demo User996' givenName 'Demo' zimbraPrefFromDisplay 'Demo User996'
ca demo997@lab1.com 'Password$' sn 'Demo User997' givenName 'Demo' zimbraPrefFromDisplay 'Demo User997'
ca demo998@lab1.com 'Password$' sn 'Demo User998' givenName 'Demo' zimbraPrefFromDisplay 'Demo User998'
ca demo999@lab1.com 'Password$' sn 'Demo User999' givenName 'Demo' zimbraPrefFromDisplay 'Demo User999'

Finally, we will launch the command, as the zimbra user, to import the file with all the accounts, into our Zimbra

zmprov -f userlist.zmp

This process will take several seconds, up to minutes depending on the number of accounts to import:

prov> b99e183e-02e1-455b-9140-75957382a720
prov> c25f464d-7a5d-4316-ab7d-123b1949ebff

ref: https://www.jorgedelacruz.es/2016/07/19/zimbra-crear-1000-usuarios-y-anadirlos-zimbra-de-manera-automatica-en-batch-mode/

1

We developed our own interface for Zimbra account management some years back for 6.0.8. At the time, soap support was not that well documented and took some trial and error.

There are some PHP classes available on Google code anf GIThub.

See http://www.plymouth.edu/webapp/code/zimbra.class.phps or https://github.com/libersoft/zcs-php

gmck
  • 11
  • 1