Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ Email API:
- Batch send with Template (Transactional) – [`batch/transactional_template.php`](examples/batch/transactional_template.php)
- Batch send with Template (Bulk) – [`batch/bulk_template.php`](examples/batch/bulk_template.php)
- Sending domain management CRUD – [`sending-domains/all.php`](examples/sending-domains/all.php)
- Sending domain company info – [`company-info/all.php`](examples/company-info/all.php)
- Suppressions (find & delete) – [`sending/suppressions.php`](examples/sending/suppressions.php)
- Email Logs (list & get by message ID) – [`sending/email-logs.php`](examples/sending/email-logs.php)

Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Central index of runnable example scripts demonstrating Mailtrap PHP SDK feature
|---------|------|
| Templates CRUD | [`templates/all.php`](templates/all.php) |
| Sending domains CRUD | [`sending-domains/all.php`](sending-domains/all.php) |
| Sending domain company info | [`company-info/all.php`](company-info/all.php) |

### General API

Expand Down
76 changes: 76 additions & 0 deletions examples/company-info/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

use Mailtrap\Config;
use Mailtrap\DTO\Request\Domain\CompanyInfo;
use Mailtrap\DTO\Request\Domain\CreateCompanyInfo;
use Mailtrap\DTO\Request\Domain\UpdateCompanyInfo;
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapSendingClient;

require __DIR__ . '/../../vendor/autoload.php';

$domainId = (int) $_ENV['MAILTRAP_DOMAIN_ID'];
$config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens

$companyInfo = (new MailtrapSendingClient($config))->companyInfo($domainId); #required parameter is domainId
Comment thread
mklocek marked this conversation as resolved.

/**
* Create company info for a sending domain
*
* POST https://mailtrap.io/api/domains/{domain_id}/company_info
*/
try {
$response = $companyInfo->createCompanyInfo(
new CreateCompanyInfo(
name: 'Mailtrap',
address: '123 Main St',
city: 'San Francisco',
country: 'US',
zipCode: '94105',
websiteUrl: 'https://mailtrap.io',
phone: '+1-555-0100',
privacyPolicyUrl: 'https://mailtrap.io/privacy',
termsOfServiceUrl: 'https://mailtrap.io/terms',
infoLevel: CompanyInfo::INFO_LEVEL_BUSINESS
)
);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get company info for a sending domain
*
* GET https://mailtrap.io/api/domains/{domain_id}/company_info
*/
try {
$response = $companyInfo->getCompanyInfo();

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Update company info for a sending domain
*
* PATCH https://mailtrap.io/api/domains/{domain_id}/company_info
*
* Only the fields provided are updated.
*/
try {
$response = $companyInfo->updateCompanyInfo(new UpdateCompanyInfo(city: 'New York', zipCode: '10001'));

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
27 changes: 27 additions & 0 deletions examples/sending-domains/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Mailtrap\Config;
use Mailtrap\DTO\Request\Domain\UpdateDomain;
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapSendingClient;

Expand Down Expand Up @@ -80,6 +81,32 @@
}


/**
* Update sending domain configuration settings
*
* PATCH https://mailtrap.io/api/accounts/{account_id}/domains/{domain_id}
*/
try {
$domainId = (int) $_ENV['MAILTRAP_DOMAIN_ID']; // Set this environment variable with a valid domain ID

$response = $sendingDomains->updateSendingDomain(
$domainId,
new UpdateDomain(
openTrackingEnabled: true,
clickTrackingEnabled: true,
trackingOptOutEnabled: true,
autoUnsubscribeLinkEnabled: false,
inboundEnabled: false
)
);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Delete a sending domain
*
Expand Down
76 changes: 76 additions & 0 deletions src/Api/Sending/CompanyInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Mailtrap\Api\Sending;

use Mailtrap\Api\AbstractApi;
use Mailtrap\ConfigInterface;
use Mailtrap\DTO\Request\Domain\CreateCompanyInfo;
use Mailtrap\DTO\Request\Domain\UpdateCompanyInfo;
use Psr\Http\Message\ResponseInterface;

/**
* Class CompanyInfo
*/
class CompanyInfo extends AbstractApi implements SendingInterface
{
public function __construct(ConfigInterface $config, private int $domainId)
{
parent::__construct($config);
}

/**
* Get the company info associated with the sending domain.
*
* @return ResponseInterface
*/
public function getCompanyInfo(): ResponseInterface
{
return $this->handleResponse(
$this->httpGet($this->getBasePath())
);
}

/**
* Create the company info for the sending domain. Company info is required
* for domain compliance verification.
*
* @param CreateCompanyInfo $companyInfo
* @return ResponseInterface
*/
public function createCompanyInfo(CreateCompanyInfo $companyInfo): ResponseInterface
{
return $this->handleResponse(
$this->httpPost(
path: $this->getBasePath(),
body: [
'company_info' => $companyInfo->toArray()
]
)
);
}

/**
* Update the company info for the sending domain.
*
* @param UpdateCompanyInfo $companyInfo
* @return ResponseInterface
*/
public function updateCompanyInfo(UpdateCompanyInfo $companyInfo): ResponseInterface
{
return $this->handleResponse(
$this->httpPatch(
path: $this->getBasePath(),
body: [
'company_info' => $companyInfo->toArray()
]
)
);
}

private function getBasePath(): string
{
return sprintf('%s/api/domains/%s/company_info', $this->getHost(), $this->domainId);
}
}
20 changes: 20 additions & 0 deletions src/Api/Sending/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Mailtrap\Api\AbstractApi;
use Mailtrap\ConfigInterface;
use Mailtrap\DTO\Request\Domain\UpdateDomain;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -65,6 +66,25 @@ public function getDomainById(int $domainId): ResponseInterface
);
}

/**
* Update configuration settings for a sending domain.
*
* @param int $domainId
* @param UpdateDomain $domain
* @return ResponseInterface
*/
public function updateSendingDomain(int $domainId, UpdateDomain $domain): ResponseInterface
{
return $this->handleResponse(
$this->httpPatch(
path: sprintf('%s/%s', $this->getBasePath(), $domainId),
body: [
'sending_domain' => $domain->toArray()
]
Comment thread
mklocek marked this conversation as resolved.
)
);
}

/**
* Delete a sending domain by ID.
*
Expand Down
18 changes: 18 additions & 0 deletions src/DTO/Request/Domain/CompanyInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Mailtrap\DTO\Request\Domain;

/**
* Company info vocabulary: sender info levels.
*/
final class CompanyInfo
{
public const INFO_LEVEL_BUSINESS = 'business';
public const INFO_LEVEL_INDIVIDUAL = 'individual';

private function __construct()
{
}
}
70 changes: 70 additions & 0 deletions src/DTO/Request/Domain/CreateCompanyInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Mailtrap\DTO\Request\Domain;

/**
* Class CreateCompanyInfo
*
* Company information for a sending domain. Required for domain compliance
* verification.
*/
final class CreateCompanyInfo implements DomainInterface
{
/**
* @param string $name Company or individual name
* @param string $address Street address
* @param string $city City
* @param string $country Country
* @param string $zipCode ZIP or postal code
* @param string $websiteUrl Company website URL
* @param string|null $phone Phone number
* @param string|null $privacyPolicyUrl URL to the privacy policy page
* @param string|null $termsOfServiceUrl URL to the terms of service page
* @param string|null $infoLevel One of CompanyInfo::INFO_LEVEL_*
*/
public function __construct(
private string $name,
private string $address,
private string $city,
private string $country,
private string $zipCode,
private string $websiteUrl,
private ?string $phone = null,
private ?string $privacyPolicyUrl = null,
private ?string $termsOfServiceUrl = null,
private ?string $infoLevel = null,
) {
}

public function toArray(): array
{
$payload = [
'name' => $this->name,
'address' => $this->address,
'city' => $this->city,
'country' => $this->country,
'zip_code' => $this->zipCode,
'website_url' => $this->websiteUrl,
];

if ($this->phone !== null) {
$payload['phone'] = $this->phone;
}

if ($this->privacyPolicyUrl !== null) {
$payload['privacy_policy_url'] = $this->privacyPolicyUrl;
}

if ($this->termsOfServiceUrl !== null) {
$payload['terms_of_service_url'] = $this->termsOfServiceUrl;
}

if ($this->infoLevel !== null) {
$payload['info_level'] = $this->infoLevel;
}

return $payload;
}
}
11 changes: 11 additions & 0 deletions src/DTO/Request/Domain/DomainInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Mailtrap\DTO\Request\Domain;

use Mailtrap\DTO\Request\RequestInterface;

interface DomainInterface extends RequestInterface
{
}
Loading
Loading