I migrated my mailserver from a standalone postfix+dovecot to a mailcow installation in several Docker containers. The problem I had was that I had 422 aliases, spread over several domains, and entering them all by hand on the website seemed…tedious. Luckily, mailcow has a good API and while I am not a web developer, I tried to hack a script together to automate this process. As this script only needs to be run several times, I chose to munge aliases data from postfix by hand. you basically have to change the format. I'll explain this here
The list of available Functions:
- getMailboxes(domain)
- getAliases(id)
- getDomainAliases(mailbox)
- addAlias(aliasEmail, mailbox)
- getBlacklist(domain)
- addBlacklist(domain, email)
- addBlacklistList(domain, emails)
- addAliasesFromHash(aliasHash)
- getDKIM(domain)
python is not my forte, and ruby was an old love of mine, so this script is written in ruby. It requires the following libraries:
- 'json'
- 'net/http'
I installed these libraries using
gem install json
gem install net-http
All the code is contained in the addalias.rb
file. Place server specific sensitive data, like hostname and api keys in a secrets.rb
file, which is included in the main script, but excluded from git commits.
The requested actions I put at the end of the addalias.rb
file.
Because you don't want any credentials or tokens on public repositories, I placed these in a secrets.rb
file, which is included in the .gitignore
, so it is never uploaded to the github repository.
I have created a secrets-sample.rb
with an example of the contents.
You should duplicate this as secrets.rb
and change the variable values.
$BaseURL
contains the hostname where the mailcow api is hosted.
You need to create one or two API keys, on the admin
page of your mailcow installation.
If you only want to experiment first with the fetch scripts without making irreversible changes to you mailcow installation, create and use the Read-Only Access
API. Then, when you're ready to add aliases and domains, create and add the Read-Write Access
API key.
I could write ruby to read the postfix aliases files, but this is a hassle, as they're filled with old remarks etc. Some sanitising was necessary anyway! So, instead I decided to rewrite the aliases to ruby compatible format myself, using a trusty text editor. BBEdit in my case.
example.com just your regular example domain
[email protected] you
[email protected] you
This needs to be rewritten and added to the aliases.rb
file:
$aliases = {
"[email protected]" => "[email protected]",
"[email protected]" => "[email protected]"
}
With some regular expressions and a good editor this should be a couple of minutes work. e.g. this is what I used:
find ^(\S*)\s+(\S*)$
replace with: "\1" => "\[email protected]",