Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save warrenski/261176c3ef129e5b5e2fbe8e61ec7ff7 to your computer and use it in GitHub Desktop.
Save warrenski/261176c3ef129e5b5e2fbe8e61ec7ff7 to your computer and use it in GitHub Desktop.
Provision Migadu Email to a Cloudflare domain via Postman
//Parent Request to https://api.cloudflare.com/client/v4/zones/**zone_id**/dns_records?type=mx,txt,cname,srv
const current_zone = ''; //Zone ID
const verify_code = ''; //value from the hosted-email-verify TXT entry supplied by Migadu
const host = ''; //Hostname
const cloudflare_token = ''; //Cloudflare API Token
const base_url = 'https://api.cloudflare.com/client/v4';
const dns_records_url = base_url + '/zones/' + current_zone + '/dns_records/';
request = {header: {"Authorization" : "Bearer " + cloudflare_token,"Content-Type" : "application/json"}};
const updates = [
{"name": "**host**","type": "MX","content": "aspmx1.migadu.com","priority": 10,"proxied": false},
{"name": "**host**","type": "MX","content": "aspmx2.migadu.com","priority": 20,"proxied": false},
{"name": "**host**","type": "TXT","content": "v=spf1 include:spf.migadu.com -all","proxied": false},
{"name": "**host**","type": "TXT","content": "hosted-email-verify=**verify_code**"},
{"name": "_dmarc.**host**","type": "TXT","content": "v=DMARC1; p=quarantine;"},
{"name": "autoconfig.**host**","type": "CNAME", "content": "autoconfig.migadu.com","proxied": false},
{"name": "key1._domainkey.**host**","type": "CNAME", "content": "key1.**host**._domainkey.migadu.com", "proxied": false},
{"name": "key2._domainkey.**host**","type": "CNAME", "content": "key2.**host**._domainkey.migadu.com","proxied": false},
{"name": "key3._domainkey.**host**","type": "CNAME", "content": "key3.**host**._domainkey.migadu.com","proxied": false},
{"type": "SRV","data": {"service": "_autodiscover", "proto" : "_tcp","name": "**host**","target": "autodiscover.migadu.com","port": 443, "weight" : 1,"priority": 0}},
{"type": "SRV","data": {"service": "_submissions","name": "**host**","port": 993,"priority": 0,"proto": "_tcp","target": "smtp.migadu.com","weight": 1}},
{"type": "SRV","data": {"service": "_imaps","name": "**host**","port": 993,"priority": 0,"proto": "_tcp","target": "imap.migadu.com","weight": 1}}
];
for (let r of pm.response.json().result){
request.url = dns_records_url + r.id;
request.method = 'DELETE';
pm.sendRequest(request, (err, resp) => {console.log(err, resp);})
}
for (let rule of updates){
if (rule.type == 'SRV'){
rule.data.name = rule.data.name.replace('**host**', host);
} else {
request.url = dns_records_url;
request.method = 'POST';
rule.name = rule.name.replace('**host**', host);
rule.content = rule.content.replace('**host**', host);
rule.content = rule.content.replace('**verify_code**', verify_code);
}
request.body = {mode: 'raw', raw: JSON.stringify(rule)}
pm.sendRequest(request, (err, resp) => {console.log(err, resp);})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment