mailer prend la configuration dans config aussi
This commit is contained in:
parent
d8b70d7ee0
commit
f52da16f44
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace nulib\mail;
|
||||
|
||||
use nulib\app\config;
|
||||
use nulib\cl;
|
||||
use nulib\cv;
|
||||
use nulib\output\msg;
|
||||
@ -56,23 +57,38 @@ class mailer {
|
||||
"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 {
|
||||
$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;
|
||||
|
5
php/tbin/mailer-devel.yml
Normal file
5
php/tbin/mailer-devel.yml
Normal 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
7
php/tbin/mailer-test.yml
Normal 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
|
Loading…
x
Reference in New Issue
Block a user