2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2023-07-18 21:48:44 +00:00
|
|
|
use Misuzu\Config\IConfig;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Symfony\Component\Mime\Email as SymfonyMessage;
|
|
|
|
use Symfony\Component\Mime\Address as SymfonyAddress;
|
|
|
|
use Symfony\Component\Mailer\Transport as SymfonyTransport;
|
|
|
|
|
|
|
|
final class Mailer {
|
|
|
|
private const TEMPLATE_PATH = MSZ_ROOT . '/config/emails/%s.txt';
|
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
private static IConfig $config;
|
2022-09-13 13:14:49 +00:00
|
|
|
private static $transport = null;
|
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
public static function init(IConfig $config): void {
|
|
|
|
self::$config = $config;
|
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
private static function createDsn(): string {
|
|
|
|
$config = self::$config->getValues([
|
|
|
|
'method:s',
|
|
|
|
'host:s',
|
|
|
|
['port:i', 25],
|
|
|
|
'username:s',
|
|
|
|
'password:s',
|
|
|
|
]);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
if($config['method'] !== 'smtp')
|
|
|
|
return 'null://null';
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
$dsn = $config['method'] . '://';
|
2022-09-13 13:14:49 +00:00
|
|
|
|
|
|
|
if(!empty($config['username'])) {
|
|
|
|
$dsn .= $config['username'];
|
|
|
|
|
|
|
|
if(!empty($config['password']))
|
|
|
|
$dsn .= ':' . $config['password'];
|
|
|
|
|
|
|
|
$dsn .= '@';
|
|
|
|
}
|
|
|
|
|
|
|
|
$dsn .= $config['host'] ?? '';
|
|
|
|
$dsn .= ':';
|
|
|
|
$dsn .= $config['port'] ?? 25;
|
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
return $dsn;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTransport() {
|
|
|
|
if(self::$transport === null)
|
2023-07-18 21:48:44 +00:00
|
|
|
self::$transport = SymfonyTransport::fromDsn(self::createDsn());
|
2022-09-13 13:14:49 +00:00
|
|
|
return self::$transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function sendMessage(array $to, string $subject, string $contents, bool $bcc = false): bool {
|
|
|
|
foreach($to as $email => $name) {
|
|
|
|
$to = new SymfonyAddress($email, $name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:48:44 +00:00
|
|
|
$config = self::$config->getValues([
|
2023-07-18 22:24:23 +00:00
|
|
|
['sender.name:s', 'Flashii'],
|
|
|
|
['sender.address:s', 'sys@flashii.net'],
|
2023-07-18 21:48:44 +00:00
|
|
|
]);
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
$message = new SymfonyMessage;
|
|
|
|
|
|
|
|
$message->from(new SymfonyAddress(
|
2023-07-18 21:48:44 +00:00
|
|
|
$config['sender.address'],
|
|
|
|
$config['sender.name']
|
2022-09-13 13:14:49 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if($bcc)
|
|
|
|
$message->bcc($to);
|
|
|
|
else
|
|
|
|
$message->to($to);
|
|
|
|
|
|
|
|
$message->subject(trim($subject));
|
|
|
|
$message->text(trim($contents));
|
|
|
|
|
2023-07-18 22:24:23 +00:00
|
|
|
self::getTransport()->send($message);
|
|
|
|
return true;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function template(string $name, array $vars = []): array {
|
|
|
|
$path = sprintf(self::TEMPLATE_PATH, $name);
|
|
|
|
|
|
|
|
if(!is_file($path))
|
|
|
|
throw new InvalidArgumentException('Invalid e-mail template name.');
|
|
|
|
|
|
|
|
$tpl = file_get_contents($path);
|
|
|
|
|
|
|
|
// Normalise newlines
|
|
|
|
$tpl = str_replace("\n", "\r\n", str_replace("\r\n", "\n", $tpl));
|
|
|
|
|
|
|
|
foreach($vars as $key => $val)
|
|
|
|
$tpl = str_replace("%{$key}%", $val, $tpl);
|
|
|
|
|
|
|
|
[$subject, $message] = explode("\r\n\r\n", $tpl, 2);
|
|
|
|
|
|
|
|
return compact('subject', 'message');
|
|
|
|
}
|
|
|
|
}
|