Skip to main content

Interact with mailcow API

Project description

python-mailcow

PyPI version pylint

python-mailcow allows you to interact with the MailCow API. It comes with a cli mailcow. All arguments for the cli are generated by parsing your MailCow instance's OpenApi schema.

See demo.mailcow.email as OpenAPI scheme example.

This means that you can add, get, edit and delete everything that the MailCow API allows and changes to the API should be usable immediately.

...Famous last words... I know...

BTW: The look and feel of the cli and configuration is inspired by the python-gitlab project.

Installation

  • from pypi: pip install python-mailcow
  • from github: pip install git+https://github.com./derJD/python-mailcow.git

Local Build

  • git checkout main
  • python setup.py sdist
  • pip install dist/python-mailcow-9999.999.99.dev9.tar.gz

Usage

quick start

  • Generate a API token in the MailCow UI
  • mailcow --create-example-config
  • Edit settings in ~/.config/python-mailcow.cfg to match your MailCow UI
  • mailcow --help should now show all available options depending on your MailCow API version

Config

python-mailcow read its settings from ~/.config/python-mailcow.cfg. An example configuration looks like this:

[defaults]
server = mailcow.example.com
ssl_verify = true
timeout = 5

[mailcow.example.com]
url = https://mailcow.example.com
token = 123456-abcde-123456-abcde-123456
Argument Type Description
server String Name of the section providing further server information
url String Base URL (MailCow UI location) for connection ie: https://demo.mailcow.email
token String Token for API-Access
ssl_verify Boolean Enable/Disable ssl verification
timeout Integer Connection timeout

CLI

If you installed python-mailcow for the first time you may run following command and change the settings according to your needs:

mailcow --create-example-config

After that you should be able to run commands like these:

  • mailcow alias get --all/--id 5
  • mailcow alias add --address moep@example.com --goto goto@example.com --no-active
  • mailcow alias delete --items 5

mailcow --help:

usage: test.py [-h] [--create-example-config] [--conf CONF] [--vertical] [--json] [--yaml] [--table] [--debug]
               {alias,app-passwd,bcc,dkim,dkim_duplicate,domain,domain-admin,da-acl,domain-policy,fwdhost,mailbox,oauth2-client,recipient_map,relayhost,resource,syncjob,tls-policy-map,transport,mailq,qitem,fail2ban,pushover,quarantine_notification,user-acl,logs,policy_bl_domain,policy_wl_domain,quarantine,rl-mbox,rl-domain,status,syncjobs,spam-score}
               ...

Interact with mailcows API.

positional arguments:
  {alias,app-passwd,bcc,dkim,dkim_duplicate,domain,domain-admin,da-acl,domain-policy,fwdhost,mailbox,oauth2-client,recipient_map,relayhost,resource,syncjob,tls-policy-map,transport,mailq,qitem,fail2ban,pushover,quarantine_notification,user-acl,logs,policy_bl_domain,policy_wl_domain,quarantine,rl-mbox,rl-domain,status,syncjobs,spam-score}

optional arguments:
  -h, --help            show this help message and exit
  --create-example-config
                        Create configuration file
  --conf CONF, -c CONF  Defaults to: ~/.config/python-mailcow.conf
  --vertical, -v        Print (table) results vertically
  --json, -j            Print results as JSON
  --yaml, -y            Print results as YAML
  --table, -t           Print results as Table
  --debug, -d           Enable debugging

mailcow alias add --help:

usage: mailcow alias add [-h] [--active] [--address ADDRESS] [--goto GOTO] [--goto_ham] [--goto_null] [--goto_spam] [--sogo_visible]

optional arguments:
  -h, --help            show this help message and exit
  --active, --no-active
                        is alias active or not
  --address ADDRESS     alias address, for catchall use "@domain.tld"
  --goto GOTO           destination address, comma separated
  --goto_ham, --no-goto_ham
                        learn as ham
  --goto_null, --no-goto_null
                        silently ignore
  --goto_spam, --no-goto_spam
                        learn as spam
  --sogo_visible, --no-sogo_visible
                        toggle visibility in SoGo

Editing alias' active status and visibility should look like this:

mailcow alias edit --items 78 --active --sogo_visible
+---------+----------------------------------------------------------------------------------------+-------------------------------------------+
|   type  |                                          log                                           |                    msg                    |
+---------+----------------------------------------------------------------------------------------+-------------------------------------------+
| success | ['mailbox', 'edit', 'alias', {'id': ['78'], 'active': '1', 'sogo_visible': '1'}, None] | ['alias_modified', 'example@example.com'] |
+---------+----------------------------------------------------------------------------------------+-------------------------------------------+

python

Class MailCow()

Connect to MailCow instance defined in config file and interact via API Requests

MailCow() takes the same arguments as listed in Config and one additional optional argument:

Argument Type Description
conf String Path to config file

Every argument is optional and accessible as attribute.

Attributes Type Description
data dict/None Store API responses here and display response via methods as_json, as_yaml, as_table
json dict/None Store payload for API requests here
request_url string {url}/api/v1
session object Session used for requests
schema dict Schema retrieved from MailCows OpenApi
endpoints dict All endpoints from filtered from schema

Method endpoint(endpoint)

Returns specific endpoint as dict or None

Argument Type Description
endpoint string Name of the endpoint. ie "alias" or "mailbox"

Method deleteRequest(section, items)

Send delete request

Argument Type Description
section string section aka last part of the url. ie alias
items list List of items to delete. ie: ['5']

Method getRequest(section)

Send get request

Argument Type Description
section string section aka last part of the url. ie alias/all

Method addRequest(section, json)

Send add request

Argument Type Description
section string section aka last part of the url. ie alias
json dict attributes send as payload. ie {'active': '1', 'address': 'example@example.com'}

Method editRequest(section, items, attr, action)

Send edit request

Argument Type Description
section string section aka last part of the url. ie alias
items list items send as payload .ie ['5'] or ['domain.tld']
attr dict attributes send as payload. ie {'active': '1', 'address': 'example@example.com'}
action string action send as payload. only needed by mailq section .ie flush

Method as_json()

Return data as json

Method as_yaml()

Return data as yaml

Method as_table(vertical)

Return data as table. vertical expects boolean

Argument Type Description
vertical bool Enable/Disable vertical print of data. Defaults to False

Example

moo = MailCow()
logs = moo.getRequest(section='logs/api/5')
moo.data = logs
print(moo.as_table())
# returns:
# +------------+----------------------------------+--------+-----------+------+
# |    time    |               uri                | method |   remote  | data |
# +------------+----------------------------------+--------+-----------+------+
# | 1611102437 |      /api/v1/get/logs/api/5      |  GET   | xx.x.xx.x |      |
# | 1611096182 | /api/v1/get/rl-mbox/xx@examp.com |  GET   | xx.x.xx.x |      |
# | 1611087846 | /api/v1/get/rl-mbox/xx@examp.com |  GET   | xx.x.xx.x |      |
# | 1611087808 |    /api/v1/get/logs/dovecot/5    |  GET   | xx.x.xx.x |      |
# | 1611087797 | /api/v1/get/syncjobs/all/no_log  |  GET   | xx.x.xx.x |      |
# +------------+----------------------------------+--------+-----------+------+

License

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-mailcow-1.0.4.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

python_mailcow-1.0.4-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file python-mailcow-1.0.4.tar.gz.

File metadata

  • Download URL: python-mailcow-1.0.4.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for python-mailcow-1.0.4.tar.gz
Algorithm Hash digest
SHA256 ac1b44ec1d3157b2bf53093108b811a4355a2d9c8927b606a6a0d33abb228a1c
MD5 1805caea6eef8f9bfd924aecd55488e1
BLAKE2b-256 a1ca4b56d76f1746c0966b9d407f47880326727d694a1fdaf972d4b36d35a6e5

See more details on using hashes here.

File details

Details for the file python_mailcow-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: python_mailcow-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for python_mailcow-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2b49135fef678894c5e5bfad50b7a6a16849cff02bd15197e7c3b07f0ae46e9a
MD5 41397533ac5f52e91d22a5a6cdcea4bc
BLAKE2b-256 ce8194d360be5d5466635246d1a4bdbbff117927ba6ebed3912923853787b6ae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page