. */ namespace SP\Domain\Notification\Providers; use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\PHPMailer; use SP\Domain\Core\AppInfoInterface; use SP\Domain\Notification\Dtos\MailParams; use SP\Domain\Notification\Ports\MailerInterface; use function SP\__u; use function SP\logger; use function SP\processException; /** * A wrapper for PHPMailer */ final readonly class PhpMailerWrapper implements MailerInterface { public function __construct(private PHPMailer $mailer, private bool $debug = false) { } public function isHtml(): MailerInterface { $this->mailer->isHTML(); return $this; } /** * @throws MailProviderException */ public function addAddress(string $address): MailerInterface { try { $this->mailer->addAddress($address); } catch (Exception $e) { throw new MailProviderException($e); } return $this; } public function subject(string $subject): MailerInterface { $this->mailer->set('Subject', $subject); return $this; } public function body(string $body): MailerInterface { $this->mailer->set('Body', $body); return $this; } /** * @throws MailProviderException */ public function send(): bool { try { return $this->mailer->send(); } catch (Exception $e) { throw new MailProviderException($e); } } public function getToAddresses(): array { return $this->mailer->getToAddresses(); } /** * Configure the mailer with the configuration settings * * @throws MailProviderException */ public function configure(MailParams $mailParams): MailerInterface { $instance = clone $this; $appName = AppInfoInterface::APP_NAME; try { $instance->mailer->SMTPAutoTLS = false; $instance->mailer->isSMTP(); $instance->mailer->CharSet = 'utf-8'; $instance->mailer->Host = $mailParams->getServer(); $instance->mailer->Port = $mailParams->getPort(); $instance->mailer->SMTPSecure = strtolower($mailParams->getSecurity()); if ($mailParams->isMailAuthenabled()) { $instance->mailer->SMTPAuth = true; $instance->mailer->Username = $mailParams->getUser(); $instance->mailer->Password = $mailParams->getPass(); } if ($instance->debug) { $instance->mailer->SMTPDebug = 2; $instance->mailer->Debugoutput = static fn($str, $level) => logger($str, strtoupper($level)); } $instance->mailer->setFrom($mailParams->getFrom(), $appName); $instance->mailer->addReplyTo($mailParams->getFrom(), $appName); $instance->mailer->WordWrap = 100; return $instance; } catch (Exception $e) { processException($e); throw MailProviderException::error(__u('Unable to initialize'), $e->getMessage(), $e->getCode(), $e); } } }