diff --git a/php/src/mail/mailer.php b/php/src/mail/mailer.php index f9510dc..3f92202 100644 --- a/php/src/mail/mailer.php +++ b/php/src/mail/mailer.php @@ -1,6 +1,7 @@ "?string", ]; + static function resolve_params(?array $params=null): array { + $envParams = [ + "backend" => cv::vn(getenv("NULIB_MAIL_BACKEND")), + "debug" => cv::vn(getenv("NULIB_MAIL_DEBUG")), + "host" => cv::vn(getenv("NULIB_MAIL_HOST")), + "port" => cv::vn(getenv("NULIB_MAIL_PORT")), + "auth" => cv::vn(getenv("NULIB_MAIL_AUTH")), + "username" => cv::vn(getenv("NULIB_MAIL_USERNAME")), + "password" => cv::vn(getenv("NULIB_MAIL_PASSWORD")), + "secure" => cv::vn(getenv("NULIB_MAIL_SECURE")), + ]; + $configParams = config::k("mailer"); + foreach (array_keys(self::SCHEMA) as $key) { + $params[$key] ??= $envParams[$key] ?? null; + $params[$key] ??= $configParams[$key] ?? null; + } + return $params; + } + static function get(?array $params=null, ?bool $exceptions=null): PHPMailer { + $params = self::resolve_params($params); + $mailer = new PHPMailer($exceptions); $mailer->setLanguage("fr"); $mailer->CharSet = PHPMailer::CHARSET_UTF8; # backend - $backend = $params["backend"] ?? null; - $backend ??= cv::vn(getenv("NULIB_MAIL_BACKEND")); - $backend ??= "smtp"; + $backend = $params["backend"] ?? "smtp"; switch ($backend) { case "smtp": - # host + # host, port $host = $params["host"] ?? null; - $host ??= cv::vn(getenv("NULIB_MAIL_HOST")); - # port - $port = $params["port"] ?? null; - $port ??= cv::vn(getenv("NULIB_MAIL_PORT")); - $port ??= 25; + $port = $params["port"] ?? 25; if ($host === null) { throw new ValueException("mail host is required"); } @@ -93,9 +109,7 @@ class mailer { throw ValueException::invalid_value($backend, "mailer backend"); } # debug - $debug = $params["debug"] ?? null; - $debug ??= cv::vn(getenv("NULIB_MAIL_DEBUG")); - $debug ??= SMTP::DEBUG_OFF; + $debug = $params["debug"] ?? SMTP::DEBUG_OFF; if (is_int($debug)) { if ($debug < SMTP::DEBUG_OFF) $debug = SMTP::DEBUG_OFF; elseif ($debug > SMTP::DEBUG_LOWLEVEL) $debug = SMTP::DEBUG_LOWLEVEL; diff --git a/php/tbin/mailer-devel.yml b/php/tbin/mailer-devel.yml new file mode 100644 index 0000000..1190d9f --- /dev/null +++ b/php/tbin/mailer-devel.yml @@ -0,0 +1,5 @@ +# -*- coding: utf-8 mode: yaml -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8 + +app: + mailer: + host: maildev.devel.self diff --git a/php/tbin/mailer-test.yml b/php/tbin/mailer-test.yml new file mode 100644 index 0000000..6a3f17e --- /dev/null +++ b/php/tbin/mailer-test.yml @@ -0,0 +1,7 @@ +# -*- coding: utf-8 mode: yaml -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8 + +app: + mailer: + host: maildev.univ-reunion.fr + username: mel + password: gibson