. */ namespace SP\Domain\Notification\Services; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\Messages\MailMessage; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\AppInfoInterface; use SP\Domain\Core\Bootstrap\UriContextInterface; use SP\Domain\Notification\Ports\MailService; use SP\Domain\Providers\MailerInterface; use SP\Html\Html; use SP\Providers\Mail\MailParams; use function SP\__u; use function SP\processException; /** * Class Mail */ final class Mail extends Service implements MailService { public function __construct( Application $application, private readonly MailerInterface $mailer, private readonly UriContextInterface $uriContext ) { parent::__construct($application); } public static function getParamsFromConfig(ConfigDataInterface $configData): MailParams { return new MailParams( $configData->getMailServer(), $configData->getMailPort(), $configData->getMailUser(), $configData->getMailPass(), $configData->getMailSecurity(), $configData->getMailFrom(), $configData->isMailAuthenabled() ); } /** * Checks mail params by sending a test email * * @throws ServiceException */ public function check(MailParams $mailParams, string $to): void { try { $mailMessage = (new MailMessage()) ->setTitle(__u('Mail test')) ->addDescription( __u('This is a test email in order to verify that the configuration is working right.') ) ->setFooter($this->getEmailFooter()); $this->mailer ->configure($mailParams) ->isHTML() ->addAddress($to) ->subject($this->getSubjectForAction($mailMessage->getTitle())) ->body($mailMessage->composeHtml()) ->send(); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); throw ServiceException::error(__u('Error while sending the email'), $e->getMessage(), $e->getCode(), $e); } } /** * Devolver el pie del email con la firma de la aplicación */ private function getEmailFooter(): array { return [ '', '--', sprintf('%s - %s', AppInfoInterface::APP_NAME, AppInfoInterface::APP_DESC), Html::anchorText($this->uriContext->getWebUri()), ]; } /** * @param string $subject * @param array|string $to * @param MailMessage $mailMessage * * @throws ServiceException */ public function send(string $subject, string|array $to, MailMessage $mailMessage): void { if (!is_array($to)) { $to = [$to]; } foreach ($to as $addr) { $this->mailer->addAddress($addr); } $this->mailer ->isHTML() ->subject($this->getSubjectForAction($subject)) ->body($mailMessage->setFooter($this->getEmailFooter())->composeHtml()); $this->sendMail(); } /** * @param $action * * @return string */ private function getSubjectForAction($action): string { return sprintf('%s - %s', AppInfoInterface::APP_NAME, $action); } /** * @throws ServiceException */ private function sendMail(): void { try { $this->mailer->send(); $this->eventDispatcher->notify( 'send.mail', new Event( $this, EventMessage::factory() ->addDescription(__u('Email sent')) ->addDetail( __u('Recipient'), implode( ',', array_map( static fn($value) => $value[0], $this->mailer->getToAddresses() ) ) ) ) ); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); throw ServiceException::error(__u('Error while sending the email')); } } }