mailer prend la configuration dans config aussi

This commit is contained in:
Jephté Clain 2025-10-05 20:07:30 +04:00
parent d8b70d7ee0
commit f52da16f44
3 changed files with 38 additions and 12 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace nulib\mail; namespace nulib\mail;
use nulib\app\config;
use nulib\cl; use nulib\cl;
use nulib\cv; use nulib\cv;
use nulib\output\msg; use nulib\output\msg;
@ -56,23 +57,38 @@ class mailer {
"secure" => "?string", "secure" => "?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 { static function get(?array $params=null, ?bool $exceptions=null): PHPMailer {
$params = self::resolve_params($params);
$mailer = new PHPMailer($exceptions); $mailer = new PHPMailer($exceptions);
$mailer->setLanguage("fr"); $mailer->setLanguage("fr");
$mailer->CharSet = PHPMailer::CHARSET_UTF8; $mailer->CharSet = PHPMailer::CHARSET_UTF8;
# backend # backend
$backend = $params["backend"] ?? null; $backend = $params["backend"] ?? "smtp";
$backend ??= cv::vn(getenv("NULIB_MAIL_BACKEND"));
$backend ??= "smtp";
switch ($backend) { switch ($backend) {
case "smtp": case "smtp":
# host # host, port
$host = $params["host"] ?? null; $host = $params["host"] ?? null;
$host ??= cv::vn(getenv("NULIB_MAIL_HOST")); $port = $params["port"] ?? 25;
# port
$port = $params["port"] ?? null;
$port ??= cv::vn(getenv("NULIB_MAIL_PORT"));
$port ??= 25;
if ($host === null) { if ($host === null) {
throw new ValueException("mail host is required"); throw new ValueException("mail host is required");
} }
@ -93,9 +109,7 @@ class mailer {
throw ValueException::invalid_value($backend, "mailer backend"); throw ValueException::invalid_value($backend, "mailer backend");
} }
# debug # debug
$debug = $params["debug"] ?? null; $debug = $params["debug"] ?? SMTP::DEBUG_OFF;
$debug ??= cv::vn(getenv("NULIB_MAIL_DEBUG"));
$debug ??= SMTP::DEBUG_OFF;
if (is_int($debug)) { if (is_int($debug)) {
if ($debug < SMTP::DEBUG_OFF) $debug = SMTP::DEBUG_OFF; if ($debug < SMTP::DEBUG_OFF) $debug = SMTP::DEBUG_OFF;
elseif ($debug > SMTP::DEBUG_LOWLEVEL) $debug = SMTP::DEBUG_LOWLEVEL; elseif ($debug > SMTP::DEBUG_LOWLEVEL) $debug = SMTP::DEBUG_LOWLEVEL;

View File

@ -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

7
php/tbin/mailer-test.yml Normal file
View File

@ -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