<?php

namespace Signteer\Core\Action\Email;

use Plainwork\Email\EmailSender;
use Signteer\Core\Action\ActionResult;

final class SendEmail
{
    public $emailSender = EmailSender::class;

    public function __invoke(string $to, string $subject, string $message, array $headers = []): ActionResult
    {
        $ret = new ActionResult();
        $res = $this->emailSender->send($to, $subject, $message, $headers);
        if (!$res) {
            $err = 'Error sending email';
            // global $ts_mail_errors, $phpmailer;
            // if (!isset($ts_mail_errors)) $ts_mail_errors = array();
            // if (isset($phpmailer)) $ts_mail_errors[] = $phpmailer->ErrorInfo;
            // $err = 'Error sending email: ' . join('---', $ts_mail_errors);

            return $ret->addError($err);
        }

        return $ret;
    }
}
