refactor: use mail manager

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-07-19 08:30:02 -04:00
parent 9ad898e3b6
commit c451283863
+39 -32
View File
@@ -4,11 +4,10 @@ declare(strict_types=1);
namespace KTXM\AuthenticationProviderMail; namespace KTXM\AuthenticationProviderMail;
use KTXF\Mail\Entity\Address; use KTXF\Mail\Object\Address;
use KTXF\Mail\Entity\IMessageBase; use KTXF\Mail\Object\MessagePropertiesBaseInterface;
use KTXF\Mail\Queue\SendOptions; use KTXF\Mail\Provider\ProviderBaseInterface;
use KTXF\Mail\Service\IServiceBase; use KTXF\Mail\Submission\EntitySubmitResult;
use KTXF\Mail\Service\IServiceSend;
use KTXF\Security\Authentication\AuthenticationProviderAbstract; use KTXF\Security\Authentication\AuthenticationProviderAbstract;
use KTXF\Security\Authentication\ProviderContext; use KTXF\Security\Authentication\ProviderContext;
use KTXF\Security\Authentication\ProviderResult; use KTXF\Security\Authentication\ProviderResult;
@@ -229,26 +228,6 @@ class Provider extends AuthenticationProviderAbstract
ProviderContext $context ProviderContext $context
): array { ): array {
try { try {
// Get the default system mail service
$service = $this->mailManager->serviceFindByAddress(
$tenantId,
null,
'authentication@system'
);
if ($service instanceof IServiceSend === false) {
return [
'success' => false,
'error' => 'No mail service configured',
];
}
// Build the message
$message = $service->messageFresh();
$message->setTo([new Address($recipientEmail)]);
$message->setSubject('Your verification code');
// Format code with spaces for readability (123 456) // Format code with spaces for readability (123 456)
$formattedCode = wordwrap($code, 3, ' ', true); $formattedCode = wordwrap($code, 3, ' ', true);
@@ -259,17 +238,45 @@ class Provider extends AuthenticationProviderAbstract
$textBody = $this->buildTextBody($formattedCode, (int)$remainingMinutes); $textBody = $this->buildTextBody($formattedCode, (int)$remainingMinutes);
$htmlBody = $this->buildHtmlBody($formattedCode, (int)$remainingMinutes); $htmlBody = $this->buildHtmlBody($formattedCode, (int)$remainingMinutes);
$message->setBodyText($textBody); // Submit through the system mail route for the authentication channel
$message->setBodyHtml($htmlBody); $result = $this->mailManager->entitySubmit(
// Send immediately (2FA is time-sensitive)
$this->mailManager->send(
$tenantId, $tenantId,
ProviderBaseInterface::USER_SYSTEM,
'authentication@system',
null, null,
$message, [
new SendOptions(immediate: true) MessagePropertiesBaseInterface::PROPERTY_TO => [(new Address($recipientEmail))->toArray()],
MessagePropertiesBaseInterface::PROPERTY_SUBJECT => 'Your verification code',
MessagePropertiesBaseInterface::PROPERTY_BODY => [
'partId' => 'body',
'type' => 'multipart/alternative',
'subParts' => [
[
'partId' => 'body.text',
'type' => 'text/plain',
'charset' => 'utf-8',
'content' => $textBody,
'disposition' => 'inline',
],
[
'partId' => 'body.html',
'type' => 'text/html',
'charset' => 'utf-8',
'content' => $htmlBody,
'disposition' => 'inline',
],
],
],
]
); );
if ($result->disposition !== EntitySubmitResult::DISPOSITION_SENT) {
return [
'success' => false,
'error' => $result->errorMessage ?? 'Mail submission failed',
];
}
return ['success' => true]; return ['success' => true];
} catch (\Throwable $e) { } catch (\Throwable $e) {