Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$c = new Client('https://poste.io/admin/api/v1/', 'admin@poste.io', 'admin');
$c->delete('domains/t.com');
$c->post('domains', ['name' => 't.com']);
$c->post('boxes', ['email' => 't@t.com', 'passwordPlaintext' => 't', 'name' => 't']);
$c->patch('boxes/t@t.com', ['name' => 'a', 'disabled' => true, 'passwordPlaintext' => 'a']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'a' ? "OK\n" : "ERR\n";
echo $box->disabled === true ? "OK\n" : "ERR\n";
$c->patch('boxes/t@t.com', ['name' => 'b', 'disabled' => false, 'passwordPlaintext' => 'b']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'b' ? "OK\n" : "ERR\n";
echo $box->disabled === false ? "OK\n" : "ERR\n";
$c->patch('boxes/t@t.com', ['disabled' => true, 'passwordPlaintext' => 'c']);
$box = $c->get('boxes/t@t.com');
echo $box->name === 'b' ? "OK\n" : "ERR\n";
echo $box->disabled === true ? "OK\n" : "ERR\n";
print_r($box);
$c->delete('domains/t.com');
class Client {
private $base;
private $user;
private $password;
public function __construct($base, $user, $password)
{
$this->base = $base;
$this->user = $user;
$this->password = $password;
}
private function curl($method, $url, $data = null)
{
$headers = ['Accept: application/json', 'Content-Type: application/json'];
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $this->base.$url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_USERPWD, $this->user. ":" . $this->password);
switch ($method) {
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PATCH':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if(!preg_match('~^2~', $code)) {
echo 'Got code '.$code." and response:\n".$response."\n";
return null;
} else {
return $code != 204 ? json_decode($response) : null;
}
}
public function get($url)
{
return $this->curl('GET', $url);
}
public function post($url, $data)
{
return $this->curl('POST', $url, $data);
}
public function patch($url, $data)
{
return $this->curl('PATCH', $url, $data);
}
public function delete($url)
{
return $this->curl('DELETE', $url);
}
public function put($url, $data)
{
return $this->curl('PUT', $url, $data);
}
}
@vincentcox

This comment has been minimized.

Copy link

@vincentcox vincentcox commented Nov 6, 2020

How do we configure DKIM from this API/script?

API documentation can be found here: https://admin%40poste.io:admin@demo.poste.io/admin/api/doc

For those interested in doing a bash scripting deployment:

######### POSTE.IO spawner #########
# command to spawn a test docker (not needed for script):
: '

docker run \
    -d -p 25:25 \
    -p 999:80 \
    -p 9999:443 \
    -p 110:110 \
    -p 143:143 \
    -p 465:465 \
    -p 587:587 \
    -p 993:993 \
    -p 995:995 \
    -v /etc/localtime:/etc/localtime:ro \
    -v /tmp/mailserver:/data \
    -t analogic/poste.io

'
# Variables:
domain=poste.io# EDIT ME
password_admin="securepassword" # EDIT ME PLIS
mailbox="it" # {{mailbox}}, will become {{mailbox}}@{{domain}}
mailbox_password="PASSWORD" # EDIT ME PLIS
api="https://localhost:9999" #hardcoded, it's executed on the server and needs to reach docker (localhost) so that's why!
# Script:
# Create Admin Login (and the authenticaiton basic auth for later requests)
curl -sS -k "$api/admin/install/server" \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-raw "install%5Bhostname%5D=$domain&install%5BsuperAdmin%5D=admin%40$domain&install%5BsuperAdminPassword%5D=$password_admin" > /dev/null

authentication="admin@$domain:$password_admin"

# Create mailbox
curl -sS -k "$api/admin/api/v1/boxes" \
 --user "$authentication" \
 -X POST \
 --data "email=$mailbox@$domain&passwordPlaintext=$mailbox_password" > /dev/null


# Create DKIM
curl -sS -k "$api/admin/api/v1/domains/$domain/dkim" \
 --user "$authentication" \
 -X PUT > /dev/null

# Get DKIM public key:
# Basicly the output of this API is JSON, see here as example: https://pastebin.com/wnU0xAWF . We use jq, which needs to be installed, to parse this json.
# After that we use really smelly code to extract the public key and get it in the right format!
public_key=$(curl -sS -k "$api/admin/api/v1/domains/$domain/dkim" \
 --user "$authentication" | jq -r ".public" |sed '1,1d'|sed '$d'|sed '$d'|tr -d '\n')

# Get DKIM selector
# Again with jq, to select the selector (no pun intended)
selector=$(curl -sS -k "$api/admin/api/v1/domains/$domain/dkim" \
 --user "$authentication" | jq -r ".selector") 

echo "selector:"
echo "$selector._domainkey.$domain."
echo "PUT THIS IN TXT:"
echo "k=rsa; p=$public_key"

Tested on Ubuntu and MacOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment