Skip to content

Commit 2b8cab5

Browse files
committed
Init
1 parent e4523fd commit 2b8cab5

8 files changed

+261
-2
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Dependencies
2+
composer.lock
3+
vendor/*
4+
5+
# Test
6+
.phpunit*
7+
8+
# Dev
9+
.DS_Store
10+
.nova/*

README.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# email-sender
2-
E-mail sender
1+
# E-mail sender
2+
3+
## Requirments
4+
5+
PHP >= 8.0
6+
7+
8+
## Install
9+
10+
`composer require phant/email-sender`
11+
12+
## Usages
13+
14+
### E-mail address validity checker
15+
16+
```php
17+
use Phant\EmailAddress\Service\ValidityChecker;
18+
19+
$emailValidityChecker = new EmailValidityChecker();
20+
21+
if (!$emailValidityChecker->checkTrashMailBoxService('[email protected]') {
22+
// E-mail address provided by a trash mail box service
23+
}
24+
25+
if (!$emailValidityChecker->checkMxServer('[email protected]') {
26+
// E-mail address linked to a domain name without an e-mail server
27+
}
28+
```
29+
30+
31+
### E-mail Sender via Sendinblue
32+
33+
```php
34+
use Phant\Email\Service\SendinblueEmailSender;
35+
36+
// @todo : Create e-mail with [phant/data-structure](https://github.com/PhantPHP/data-structure)
37+
38+
apiKey = '*****.*****';
39+
40+
$sent = (new SendinblueEmailSender($apiKey))->send(email);
41+
```

component/Port/EmailSender.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\EmailSender\Port;
5+
use Phant\DataStructure\Web\Email;
6+
7+
interface EmailSender
8+
{
9+
public function send(Email $email): bool;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\EmailSender\Service;
5+
6+
use Phant\DataStructure\Web\EmailAddress;
7+
use Phant\DomainName\Service\{
8+
DnsRecord,
9+
ServiceProvided,
10+
};
11+
use Phant\EmailAddress\Error\EmailAddress\{
12+
IsTrashMailBoxService,
13+
EmailServerNotFound,
14+
};
15+
16+
class EmailAddressValiditator
17+
{
18+
public function __construct()
19+
{
20+
}
21+
22+
public function checkTrashMailBoxService(string|EmailAddress $emailAddress): bool
23+
{
24+
if (is_string($emailAddress)) $emailAddress = new EmailAddress($emailAddress);
25+
26+
return (new ServiceProvided())->isTrashMailBoxService(
27+
$emailAddress->getDomainName()
28+
);
29+
}
30+
31+
public function checkMxServer(string|EmailAddress $emailAddress): bool
32+
{
33+
if (is_string($emailAddress)) $emailAddress = new EmailAddress($emailAddress);
34+
35+
return (new DnsRecord())->exist(
36+
$emailAddress->getDomainName(),
37+
DnsRecord::MX
38+
);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Phant\EmailSender\Service;
5+
6+
use Phant\DataStructure\Web\Email;
7+
8+
use GuzzleHttp\Client as HttpClient;
9+
use GuzzleHttp\Exception\ClientException;
10+
11+
/*
12+
* Documentation : https://developers.sendinblue.com/docs
13+
*/
14+
15+
final class SendinblueEmailSender implements \Phant\EmailSender\Port\EmailSender
16+
{
17+
protected string $apiKey;
18+
19+
public function __construct(string $apiKey)
20+
{
21+
$this->apiKey = $apiKey;
22+
}
23+
24+
public function send(Email $email): bool
25+
{
26+
$headers = [
27+
'api-key' => $this->apiKey,
28+
'Accept' => 'application/json',
29+
'Content-Type' => 'application/json',
30+
];
31+
32+
$body = [
33+
'sender' => [
34+
'email' => (string)$email->from->emailAddress,
35+
'name' => $email->from->name,
36+
],
37+
'to' => [
38+
[
39+
'email' => (string)$email->to->emailAddress,
40+
'name' => $email->to->name,
41+
],
42+
],
43+
'subject' => $email->subject,
44+
];
45+
46+
if ($email->messageTxt) {
47+
$body['textContent'] = $email->messageTxt;
48+
}
49+
50+
if ($email->messageHtml) {
51+
$body['htmlContent'] = $email->messageHtml;
52+
}
53+
54+
$response = (new HttpClient([
55+
'base_uri' => 'https://api.sendinblue.com'
56+
]))->request(
57+
'POST',
58+
'/v3/smtp/email',
59+
[
60+
'headers' => $headers,
61+
'json' => $body,
62+
'http_errors' => false,
63+
]
64+
);
65+
66+
if ($response->getStatusCode() != 201) {
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
}

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "phant/email-sender",
3+
"description": "Manage email sending easily",
4+
"license": "MIT",
5+
"keywords": ["email sender manager", "email sender component", "email sender service", "email sender", "sendinblue email sender", "email check", "email checker", "email verify", "email verifier", "email validation"],
6+
"authors": [
7+
{
8+
"name": "Lenny ROUANET",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^8.0",
14+
"phant/data-structure": "1.*",
15+
"phant/domain-name": "1.*",
16+
"guzzlehttp/guzzle": "^7.4"
17+
},
18+
"require-dev": {
19+
"phpstan/phpstan": "^1.4",
20+
"phpunit/phpunit": "^9.5"
21+
},
22+
"scripts": {
23+
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G",
24+
"test": "vendor/bin/phpunit test --testdox"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Phant\\EmailSender\\": "component/"
29+
}
30+
}
31+
}

phpunit.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="component">
4+
<directory>test</directory>
5+
</testsuite>
6+
</testsuites>
7+
<coverage processUncoveredFiles="true">
8+
<include>
9+
<directory suffix=".php">component</directory>
10+
</include>
11+
</coverage>
12+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Test\Service;
5+
use PHPUnit\Framework\TestCase;
6+
7+
use Phant\DataStructure\Web\EmailAddress;
8+
use Phant\EmailSender\Service\EmailAddressValiditator;
9+
10+
final class EmailAddressValiditatorTest extends TestCase
11+
{
12+
public function testCheckTrashMailBoxService(): void
13+
{
14+
$this->assertIsBool(
15+
(new EmailAddressValiditator())
16+
->checkTrashMailBoxService(
17+
18+
)
19+
);
20+
21+
$this->assertIsBool(
22+
(new EmailAddressValiditator())
23+
->checkTrashMailBoxService(
24+
new EmailAddress('[email protected]')
25+
)
26+
);
27+
}
28+
29+
public function testCheckMxServer(): void
30+
{
31+
$this->assertIsBool(
32+
(new EmailAddressValiditator())
33+
->checkMxServer(
34+
35+
)
36+
);
37+
38+
$this->assertIsBool(
39+
(new EmailAddressValiditator())
40+
->checkMxServer(
41+
new EmailAddress('[email protected]')
42+
)
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)