Skip to content

Instantly share code, notes, and snippets.

@ciaoben
Created April 28, 2022 08:47
Show Gist options
  • Save ciaoben/2446f4ef33d6a9428a697dd0b12577e5 to your computer and use it in GitHub Desktop.
Save ciaoben/2446f4ef33d6a9428a697dd0b12577e5 to your computer and use it in GitHub Desktop.
Create email with Qboxmail API
<?php
//PHP version: PHP 7.3.29
$curl = curl_init();
$url = "https://api.qboxmail.com/api/domains/D764696564/email_accounts";
$data = [
"name" => "info9",
"firstname" => "info",
"password" => "Qwertyuiop0",
"password_confirmation" => "Qwertyuiop0",
"max_email_quota" => "26843545600",
"webmail_date_format" => "MM/DD/YYYY"
];
// configure POST request
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
// configure headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Api-Token: Gh087AF_LdyBQBbNPSv6jf_uI9Nfo8HKqWbGoE9u6Xeh2Mb_UWMuM14HYRPDbXCp',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// execute
$result = curl_exec($curl);
$response_info = curl_getinfo($curl);
$response_status_code = $response_info['http_code'];
$response_body = json_decode($result, true);
// Work with the result
if ($response_status_code == 500) {
print_r('Server 500 error. Contact the support.');
} else if ($response_status_code == 403) {
// handle 403
print_r('Server 403 error: ' . $response_body['message']);
} else if ($response_status_code == 422) {
// handle validation error
print_r('Server 422 error: ' . $response_body['message'] . "\n");
foreach ($response_body['errors'] as $error) {
print_r('Field: ' . $error['field'] . ' - Error: ' . $error['description'] . "\n");
}
} else {
// Do what you want with body and infos
// parse headers in readable format
print_r($response_body);
print_r('New email created with code: ' . $response_body['resource_code']);
}
curl_close($curl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment