Topic: bulk user add via api python
==== REQUIRED BASIC INFO OF YOUR IREDMAIL SERVER ====
- iRedMail version (check /etc/iredmail-release): 0.9.9 MySQL Backend
- Deployed with iRedMail Easy or the downloadable installer?
- Linux/BSD distribution name and version: Centos7
- Store mail accounts in which backend (LDAP/MySQL/PGSQL): MySQL
- Web server (Apache or Nginx): nginx
- Manage mail accounts with iRedAdmin-Pro? - yes
====
Hello everyone, I am having a tough time trying to run a bulk user import from either a csv or json formatted file via api. I am able to run the below python code for creating a single user successfully using POST then running the next 2 PUT for updating user and mailing list but I would like to run the same code as below by passing variables from a csv file for example $emailaddress $password $employeeid and have the code loop theough the csv file creating and updating all of the users.
I am by no means a coder any help would be appreciated
Just to note, I am aware that I can run a script locally that will inject the users into SQL database but for several reasons this is the way I am tasked to solve this problem.
below is my code
Thanks
import sys
import requests
url = 'https://domain.com/iredadmin/api'
# Admin email address and password.
admin = 'postmaster@domain.com'
pw = 'domainpassword'
# Login
r = requests.post(url + '/login', data={'username': admin,
'password': pw})
# Get returned JSON data
data = r.json()
if not data['_success']:
sys.exit('Login failed')
cookies = r.cookies
# Create user: <user>@domain.com
# (url + api endpoint , data = parameters
# hard coded preferred language & mail quota
requests.post(url + '/user/test505@domain.com',
cookies=cookies,
data={'cn': 'My Name',
'password': '1@Password12345',
'preferredLanguage': 'en_US',
'quota': '5120'})
# Update user: <user>@containergraphics.com
requests.put(url + '/user/test505@domain.com',
cookies=cookies,
data={'cn': 'My New Name',
'employeeid' : 'testID',
'language': 'en_US',
})
requests.put(url + '/ml/testmailing@domain.com',
cookies=cookies,
data= {'add_subscribers': 'test505@domain.com',
})
----
Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.