Skip to content

Commit c9cb016

Browse files
committed
Enhance Email class by introducing a Domain instance for improved domain parsing and validation. Refactor getProvider, getSubdomain, and hasSubdomain methods to utilize the new Domain functionality, ensuring better handling of email domains.
1 parent 8ab64f5 commit c9cb016

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/Emails/Email.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Emails;
44

55
use Exception;
6+
use Utopia\Domains\Domain;
67
use Utopia\Emails\Canonicals\Provider;
78
use Utopia\Emails\Canonicals\Providers\Fastmail;
89
use Utopia\Emails\Canonicals\Providers\Generic;
@@ -86,6 +87,13 @@ class Email
8687
*/
8788
protected static $providers = null;
8889

90+
/**
91+
* Domain instance for domain parsing
92+
*
93+
* @var Domain
94+
*/
95+
protected $domainInstance = null;
96+
8997
/**
9098
* Email constructor.
9199
*/
@@ -109,6 +117,9 @@ public function __construct(string $email)
109117
if (empty($this->local) || empty($this->domain)) {
110118
throw new Exception("'{$email}' must be a valid email address");
111119
}
120+
121+
// Initialize domain instance for domain parsing
122+
$this->domainInstance = new Domain($this->domain);
112123
}
113124

114125
/**
@@ -186,6 +197,11 @@ public function hasValidDomain(): bool
186197
return false;
187198
}
188199

200+
// Use utopia domains to check if domain is known and valid
201+
if (!$this->domainInstance->isKnown() && !$this->domainInstance->isTest()) {
202+
return false;
203+
}
204+
189205
return true;
190206
}
191207

@@ -236,40 +252,32 @@ public function isCorporate(): bool
236252
*/
237253
public function getProvider(): string
238254
{
239-
$domainParts = explode('.', $this->domain);
240-
241-
if (count($domainParts) < 2) {
255+
// Use utopia domains to get the registerable domain (provider)
256+
$registerable = $this->domainInstance->getRegisterable();
257+
258+
// If registerable domain is not available, fall back to the full domain
259+
if (empty($registerable)) {
242260
return $this->domain;
243261
}
244262

245-
// For domains like mail.company.com, return company.com
246-
if (count($domainParts) > 2) {
247-
return implode('.', array_slice($domainParts, -2));
248-
}
249-
250-
return $this->domain;
263+
return $registerable;
251264
}
252265

253266
/**
254267
* Get email subdomain (if any)
255268
*/
256269
public function getSubdomain(): string
257270
{
258-
$domainParts = explode('.', $this->domain);
259-
260-
if (count($domainParts) <= 2) {
261-
return '';
262-
}
263-
264-
return implode('.', array_slice($domainParts, 0, -2));
271+
// Use utopia domains to get the subdomain
272+
return $this->domainInstance->getSub();
265273
}
266274

267275
/**
268276
* Check if email has subdomain
269277
*/
270278
public function hasSubdomain(): bool
271279
{
272-
return ! empty($this->getSubdomain());
280+
return ! empty($this->domainInstance->getSub());
273281
}
274282

275283
/**

0 commit comments

Comments
 (0)