Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the SMTP user endpoints #112

Merged
merged 13 commits into from
Mar 25, 2024
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ MailerSend PHP SDK
* [Update a Sender Identity by email](#update-a-sender-identity-by-email-route)
* [Delete a Sender Identity](#delete-a-sender-identity-route)
* [Delete a Sender Identity by email](#delete-a-sender-identity-by-email-route)
* [SMTP Users](#smtp-users-routing)
* [Get a list of SMTP Users](#get-a-list-of-smtp-users)
* [Get a single SMTP User](#get-a-single-smtp-user)
* [Add SMTP User](#add-smtp-user)
* [Update SMTP User](#update-smtp-user)
* [Delete SMTP User](#delete-smtp-user)
* [Other endpoints](#other-endpoints)
* [Get API quota](#get-api-quota)
* [Debugging validation errors](#debugging-validation-errors)
Expand Down Expand Up @@ -2051,6 +2057,85 @@ $mailersend = new MailerSend(['api_key' => 'key']);
$mailersend->senderIdentity->deleteByEmail('email');
```

<a name="smtp-user-routing"></a>

## SMTP Users

<a name="get-a-list-of-smtp-users"></a>

### Get a list of SMTP Users

```php
use MailerSend\MailerSend;

$mailersend = new MailerSend(['api_key' => 'key']);

$mailersend->smtpUser->getAll('domainId', 25);
```

<a name="get-a-single-smtp-user"></a>

### Get a single SMTP User

```php
use MailerSend\MailerSend;

$mailersend = new MailerSend(['api_key' => 'key']);

$mailersend->smtpUser->find('domainId', 'smtpUserId');
```

<a name="add-smtp-user"></a>

### Add SMTP User

```php
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;
use MailerSend\Helpers\Builder\SmtpUserParams;

$mailersend = new MailerSend(['api_key' => 'key']);

$mailersend->smtpUser->create(
'domainId',
(new SmtpUserParams('name'))
->setEnabled(false)
);
```

<a name="update-smtp-user"></a>

### Update a User

The examples on building the `SMTP User` object portrayed in the 'Add SMTP User' also apply in here.

```php
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\UserParams;

$mailersend = new MailerSend(['api_key' => 'key']);

$mailersend->smtpUser->update(
'domainId',
'smtpUserId',
(new SmtpUserParams('New name'))
->setEnabled(false)
);
```

<a name="delete-smtp-user"></a>

### Delete SMTP User

```php
use MailerSend\MailerSend;

$mailersend = new MailerSend(['api_key' => 'key']);

$mailersend->smtpUser->delete('domainId', 'smtpUserId');
```


<a name="other-endpoints"></a>

## Other endpoints
Expand Down
113 changes: 113 additions & 0 deletions src/Endpoints/SmtpUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace MailerSend\Endpoints;

use Assert\Assertion;
use MailerSend\Common\Constants;
use MailerSend\Helpers\Builder\SmtpUserParams;
use MailerSend\Helpers\GeneralHelpers;

class SmtpUser extends AbstractEndpoint
{
protected string $endpoint = 'domains';

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
* @throws \MailerSend\Exceptions\MailerSendAssertException
*/
public function getAll(string $domainId = null, ?int $limit = Constants::DEFAULT_LIMIT): array
{
if ($limit) {
GeneralHelpers::assert(
fn () => Assertion::range(
$limit,
Constants::MIN_LIMIT,
Constants::MAX_LIMIT,
'Limit is supposed to be between ' . Constants::MIN_LIMIT . ' and ' . Constants::MAX_LIMIT . '.'
)
);
}

return $this->httpLayer->get(
$this->buildUri("$this->endpoint/$domainId/smtp-users", ['limit' => $limit])
);
}

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
* @throws \MailerSend\Exceptions\MailerSendAssertException
*/
public function find(string $domainId, string $smtpUserId): array
{
GeneralHelpers::assert(
fn () => Assertion::minLength($domainId, 1, 'Domain id is required.')
);

GeneralHelpers::assert(
fn () => Assertion::minLength($smtpUserId, 1, 'Smtp user id is required.')
);

return $this->httpLayer->get(
$this->buildUri("$this->endpoint/$domainId/smtp-users/$smtpUserId")
);
}

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
*/
public function create(string $domainId, SmtpUserParams $params): array
{
GeneralHelpers::assert(
fn () => Assertion::minLength($domainId, 1, 'Domain id is required.')
);

return $this->httpLayer->post(
$this->buildUri("$this->endpoint/$domainId/smtp-users"),
$params->toArray(),
);
}

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
*/
public function update(string $domainId, string $smtpUserId, SmtpUserParams $params): array
{
GeneralHelpers::assert(
fn () => Assertion::minLength($domainId, 1, 'Domain id is required.')
);

GeneralHelpers::assert(
fn () => Assertion::minLength($smtpUserId, 1, 'Smtp user id is required.')
);

return $this->httpLayer->put(
$this->buildUri("$this->endpoint/$domainId/smtp-users/$smtpUserId"),
$params->toArray(),
);
}

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
* @throws \MailerSend\Exceptions\MailerSendAssertException
*/
public function delete(string $domainId, string $smtpUserId): array
{
GeneralHelpers::assert(
fn () => Assertion::minLength($smtpUserId, 1, 'Smtp user id is required.')
);

GeneralHelpers::assert(
fn () => Assertion::minLength($domainId, 1, 'Domain id is required.')
);

return $this->httpLayer->delete(
$this->buildUri("$this->endpoint/$domainId/smtp-users/$smtpUserId")
);
}

}
59 changes: 59 additions & 0 deletions src/Helpers/Builder/SmtpUserParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace MailerSend\Helpers\Builder;

use Tightenco\Collect\Contracts\Support\Arrayable;

class SmtpUserParams implements Arrayable, \JsonSerializable
{
protected string $name;
protected ?bool $enabled = true;

/**
* @param string $name
* @param bool $enabled
*/
public function __construct(string $name)
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;
return $this;
}

public function getEnabled(): ?bool
{
return $this->enabled;
}

public function setEnabled(?bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}

/**
* @inheritDoc
*/
public function toArray()
{
return [
'name' => $this->getName(),
'enabled' => $this->getEnabled(),
];
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
}
3 changes: 3 additions & 0 deletions src/MailerSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use MailerSend\Endpoints\SmsNumber;
use MailerSend\Endpoints\SmsRecipient;
use MailerSend\Endpoints\SmsWebhook;
use MailerSend\Endpoints\SmtpUser;
use MailerSend\Endpoints\Template;
use MailerSend\Endpoints\SpamComplaint;
use MailerSend\Endpoints\Unsubscribe;
Expand Down Expand Up @@ -79,6 +80,7 @@ class MailerSend
public SenderIdentity $senderIdentity;
public ApiQuota $apiQuota;
public OnHoldList $onHoldList;
public SmtpUser $smtpUser;

/**
* @param array $options Additional options for the SDK
Expand Down Expand Up @@ -121,6 +123,7 @@ protected function setEndpoints(): void
$this->senderIdentity = new SenderIdentity($this->httpLayer, $this->options);
$this->apiQuota = new ApiQuota($this->httpLayer, $this->options);
$this->onHoldList = new OnHoldList($this->httpLayer, $this->options);
$this->smtpUser = new SmtpUser($this->httpLayer, $this->options);
}

protected function setHttpLayer(?HttpLayer $httpLayer = null): void
Expand Down
Loading