Skip to content

Commit

Permalink
adding addMailboxes tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mschickervxob committed Aug 8, 2020
1 parent 38f2a78 commit 85f1827
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Mailcow - simple tools that use the mailcow API

Currently there a two tools addMailboxes and addSyncJobs.
I made them for my own use and maybe it is useful to someone else.

I am not affiliated with mailcow in any way.

More infos about [mailcow](https://mailcow.email/) and the [mailcow API](https://mx.mailcow.email/api/).

Support mailcow it is great :-) !
56 changes: 56 additions & 0 deletions addMailboxes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Mailcow - Add Mailboxes

This tool is made to bulk create mailboxes on mailcow. I made it for my own use and maybe it is useful to someone else.

I am not affiliated with mailcow in any way.

More infos about [mailcow](https://mailcow.email/) and the [mailcow API](https://mx.mailcow.email/api/).
Support mailcow it is great :-) !

### What you need
Just create a CSV file (comma-separated) with four columns eg.

"[email protected]","Joe Example","A-Very-Secret-Password",500
"[email protected]","Jane Example","A-Even-More-Secret-Password",900

You MUST not use a header line.

You need an API key for your mailcow instance.

### Using addMailboxes
```
Usage: addMailboxes [options]
Options:
-V, --version output the version number
-i, --importfile <importfile> Path to import file CSV
-s, --serverurl <serverurl> URL of mailcow server : https://mailcow.example.org
-a, --apikey <apikey> APIKEY for mailcow API
-e, --exitonerror exit on first error
-h, --help display help for command
```

For example:

```
node addMailboxes.js -i ./myimportlist -a XXXXX-ZZZZZZ-TTTTT-YYYYY-SSSSS -e -s https://mailcow.example.com
```

or if you use precompiled binaries:

```
addMailboxes-{platform} -i ./myimportlist -a XXXXX-ZZZZZZ-TTTTT-YYYYY-SSSSS -e -s https://mailcow.example.com
```

Use -e to stop on the first error that occurs.

## Using the source

As usual:
```
yarn install
```

## Pre-built binaries

I build pre-built with [pkg](https://github.com/vercel/pkg#readme) you can also download here.
98 changes: 98 additions & 0 deletions addMailboxes/addMailboxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Written by Markus Schicker, [email protected]
// MIT License

// Copyright (c) 2020 Markus Schicker

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

const { program } = require('commander');
const axios = require('axios');
const process = require('process');
const csvtojsonV2 = require("csvtojson/v2");

let axiosInstance = null;

const configureAxios = () => {
const instance = axios.create({
baseURL: program.serverurl,
headers: { 'X-API-Key': program.apikey, 'Content-Type': 'application/json' }
});
return instance;
}

const importFile = async (filename) => {
let importJSON = null;

try {
importJSON = await csvtojsonV2({
noheader: true,
headers: ['email', 'name', 'password', 'quota']
}).fromFile(filename);
} catch (error) {
console.error(`Error while import:\n${error}`);
process.exit(-1);
}
return importJSON.map(element => {
const emailParts = element.email.split('@');
delete element.email;
return { ...element, local_part: emailParts[0], domain: emailParts[1], active: "1", password2: element.password }
});
}

const addMailbox = async (mailboxInfo) => {
try {
const result = await axiosInstance.post('/api/v1/add/mailbox', mailboxInfo);
if (result.status !== 200) {
console.error(`Error while creating mailbox ${mailboxInfo.local_part}@${mailboxInfo.domain}.`);
if (program.exitonerror) {
process.exit(3);
}
}
console.log(`Created mailbox ${mailboxInfo.local_part}@${mailboxInfo.domain} with quota ${mailboxInfo.quota} MB`);
} catch (error) {
console.error(`Error while adding Mailbox ${mailboxInfo.local_part}@${mailboxInfo.domain}:\n${error}`);
process.exit(2);
}
}

const addMailboxes = async (mailboxInfos) => {
console.log(`Beginning import of ${mailboxInfos.length} mailboxes`);
mailboxInfos.map(async (mailboxInfo) => {
await addMailbox(mailboxInfo);
})
}

const main = async () => {
program.version('1.0.0');
program
.requiredOption('-i, --importfile <importfile>', 'Path to import file CSV')
.requiredOption('-s, --serverurl <serverurl>', 'URL of mailcow server : https://mailcow.example.org')
.requiredOption('-a, --apikey <apikey>', 'APIKEY for mailcow API')
.option('-e, --exitonerror', 'exit on first error');

program.parse(process.argv);
axiosInstance = configureAxios();

const mailboxInfos = await importFile(program.importfile);
await addMailboxes(mailboxInfos);
}

main();


11 changes: 11 additions & 0 deletions addMailboxes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "mailcowaddmailboxes",
"version": "1.0.0",
"main": "addMailboxes.js",
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"commander": "^6.0.0",
"csvtojson": "^2.0.10"
}
}
65 changes: 65 additions & 0 deletions addMailboxes/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

bluebird@^3.5.1:
version "3.7.2"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==

commander@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.0.0.tgz#2b270da94f8fb9014455312f829a1129dbf8887e"
integrity sha512-s7EA+hDtTYNhuXkTlhqew4txMZVdszBmKWSPEMxGr8ru8JXR7bLUFIAtPhcSuFdJQ0ILMxnJi8GkQL0yvDy/YA==

csvtojson@^2.0.10:
version "2.0.10"
resolved "https://registry.yarnpkg.com/csvtojson/-/csvtojson-2.0.10.tgz#11e7242cc630da54efce7958a45f443210357574"
integrity sha512-lUWFxGKyhraKCW8Qghz6Z0f2l/PqB1W3AO0HKJzGIQ5JRSlR651ekJDiGJbBT4sRNNv5ddnSGVEnsxP9XRCVpQ==
dependencies:
bluebird "^3.5.1"
lodash "^4.17.3"
strip-bom "^2.0.0"

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

[email protected]:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

is-utf8@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72"
integrity sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI=

lodash@^4.17.3:
version "4.17.19"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=

strip-bom@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e"
integrity sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=
dependencies:
is-utf8 "^0.2.0"

0 comments on commit 85f1827

Please sign in to comment.