|
5 | 5 | use Eugenefvdm\Api\Contracts\WhmInterface;
|
6 | 6 | use Illuminate\Http\Client\PendingRequest;
|
7 | 7 | use Illuminate\Support\Facades\Http;
|
| 8 | +use Illuminate\Support\Str; |
8 | 9 |
|
9 | 10 | class Whm implements WhmInterface
|
10 | 11 | {
|
@@ -105,7 +106,7 @@ public function suspendEmail(string $cpanelUsername, string $email): array
|
105 | 106 |
|
106 | 107 | /**
|
107 | 108 | * Unsuspend an email account's login ability
|
108 |
| - * @link https://api.docs.cpanel.net/openapi/cpanel/operation/suspend_login/ |
| 109 | + * @link https://api.docs.cpanel.net/openapi/cpanel/operation/unsuspend_login/ |
109 | 110 | * @param string $email The email address to unsuspend
|
110 | 111 | * @param string $cpanelUsername The cPanel username that owns the email account
|
111 | 112 | * @return array Response from the API with HTTP status code
|
@@ -176,6 +177,70 @@ public function cphulkWhitelist(): array
|
176 | 177 | return $this->client()->get('/json-api/read_cphulk_records?api.version=1&list_name=white')->json();
|
177 | 178 | }
|
178 | 179 |
|
| 180 | + /** |
| 181 | + * Create a new email account |
| 182 | + * @link https://api.docs.cpanel.net/openapi/cpanel/operation/add_pop/ WHM API Documentation for add_pop |
| 183 | + * @param string $cpanelUsername The cPanel username that owns the email account |
| 184 | + * @param string $email The email account username (without domain) |
| 185 | + * @param string $password The email account password |
| 186 | + * @param string|null $domain The domain for the email account (defaults to account's main domain) |
| 187 | + * @param int|null $quota The maximum disk space in MB (0 for unlimited) |
| 188 | + * @param bool $sendWelcomeEmail Whether to send client configuration instructions |
| 189 | + * @return array Response from the API with HTTP status code |
| 190 | + */ |
| 191 | + public function createEmail( |
| 192 | + string $cpanelUsername, |
| 193 | + string $email, |
| 194 | + string $password, |
| 195 | + ?string $domain = null, |
| 196 | + ?int $quota = null, |
| 197 | + bool $sendWelcomeEmail = false |
| 198 | + ): array { |
| 199 | + $params = [ |
| 200 | + 'cpanel_jsonapi_apiversion' => 3, |
| 201 | + 'cpanel_jsonapi_user' => $cpanelUsername, |
| 202 | + 'cpanel_jsonapi_module' => 'Email', |
| 203 | + 'cpanel_jsonapi_func' => 'add_pop', |
| 204 | + 'email' => $email, |
| 205 | + 'password' => $password, |
| 206 | + ]; |
| 207 | + |
| 208 | + if ($domain !== null) { |
| 209 | + $params['domain'] = $domain; |
| 210 | + } |
| 211 | + |
| 212 | + if ($quota !== null) { |
| 213 | + $params['quota'] = $quota; |
| 214 | + } |
| 215 | + |
| 216 | + if ($sendWelcomeEmail) { |
| 217 | + $params['send_welcome_email'] = 1; |
| 218 | + } |
| 219 | + |
| 220 | + $response = $this->client()->get('/json-api/cpanel', $params)->json(); |
| 221 | + |
| 222 | + // Check for errors |
| 223 | + if (isset($response['result']['errors'])) { |
| 224 | + return [ |
| 225 | + 'status' => 'error', |
| 226 | + 'code' => 400, |
| 227 | + 'output' => $response['result']['errors'][0] ?? 'Unknown error occurred' |
| 228 | + ]; |
| 229 | + } |
| 230 | + |
| 231 | + // Success case |
| 232 | + return [ |
| 233 | + 'status' => 'success', |
| 234 | + 'code' => 200, |
| 235 | + 'output' => $response['result']['data'] ?? [], |
| 236 | + ]; |
| 237 | + } |
| 238 | + |
| 239 | + public static function generatePassword(): string |
| 240 | + { |
| 241 | + return Str::random(12); |
| 242 | + } |
| 243 | + |
179 | 244 | /**
|
180 | 245 | * Set the HTTP client (used for testing)
|
181 | 246 | *
|
|
0 commit comments