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
+41 -34
View File
@@ -4,11 +4,10 @@ declare(strict_types=1);
namespace KTXM\AuthenticationProviderMail;
use KTXF\Mail\Entity\Address;
use KTXF\Mail\Entity\IMessageBase;
use KTXF\Mail\Queue\SendOptions;
use KTXF\Mail\Service\IServiceBase;
use KTXF\Mail\Service\IServiceSend;
use KTXF\Mail\Object\Address;
use KTXF\Mail\Object\MessagePropertiesBaseInterface;
use KTXF\Mail\Provider\ProviderBaseInterface;
use KTXF\Mail\Submission\EntitySubmitResult;
use KTXF\Security\Authentication\AuthenticationProviderAbstract;
use KTXF\Security\Authentication\ProviderContext;
use KTXF\Security\Authentication\ProviderResult;
@@ -229,47 +228,55 @@ class Provider extends AuthenticationProviderAbstract
ProviderContext $context
): array {
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)
$formattedCode = wordwrap($code, 3, ' ', true);
// Calculate remaining time
$remainingMinutes = ceil(($expires - time()) / 60);
// Build email body
$textBody = $this->buildTextBody($formattedCode, (int)$remainingMinutes);
$htmlBody = $this->buildHtmlBody($formattedCode, (int)$remainingMinutes);
$message->setBodyText($textBody);
$message->setBodyHtml($htmlBody);
// Send immediately (2FA is time-sensitive)
$this->mailManager->send(
// Submit through the system mail route for the authentication channel
$result = $this->mailManager->entitySubmit(
$tenantId,
ProviderBaseInterface::USER_SYSTEM,
'authentication@system',
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];
} catch (\Throwable $e) {