instances added god may help
This commit is contained in:
@@ -2,15 +2,21 @@
|
||||
|
||||
namespace MintyPHP\Service\Mail;
|
||||
|
||||
use MintyPHP\Repository\Mail\MailLogRepository;
|
||||
use MintyPHP\I18n;
|
||||
use MintyPHP\Service\Settings\SettingService;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use MintyPHP\Repository\Mail\MailLogRepository;
|
||||
use MintyPHP\Service\Settings\SettingGateway;
|
||||
use PHPMailer\PHPMailer\Exception as MailerException;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
|
||||
class MailService
|
||||
{
|
||||
public static function sendTemplate(
|
||||
public function __construct(
|
||||
private readonly MailLogRepository $mailLogRepository,
|
||||
private readonly SettingGateway $settingGateway
|
||||
) {
|
||||
}
|
||||
|
||||
public function sendTemplate(
|
||||
string $template,
|
||||
array $vars,
|
||||
string $to,
|
||||
@@ -18,21 +24,21 @@ class MailService
|
||||
?string $locale = null
|
||||
): array {
|
||||
$locale = $locale ?: (I18n::$locale ?? I18n::$defaultLocale);
|
||||
[$html, $text] = self::renderTemplate($template, $vars, $locale);
|
||||
[$html, $text] = $this->renderTemplate($template, $vars, $locale);
|
||||
|
||||
return self::send($to, $subject, $html, $text, [
|
||||
return $this->send($to, $subject, $html, $text, [
|
||||
'template' => $template,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function send(
|
||||
public function send(
|
||||
string $to,
|
||||
string $subject,
|
||||
string $html,
|
||||
string $text,
|
||||
array $meta = []
|
||||
): array {
|
||||
$logId = MailLogRepository::create([
|
||||
$logId = $this->mailLogRepository->create([
|
||||
'to_email' => $to,
|
||||
'subject' => $subject,
|
||||
'template' => $meta['template'] ?? null,
|
||||
@@ -41,13 +47,13 @@ class MailService
|
||||
|
||||
if (!class_exists(PHPMailer::class)) {
|
||||
if ($logId) {
|
||||
MailLogRepository::markFailed($logId, 'PHPMailer is not installed');
|
||||
$this->mailLogRepository->markFailed($logId, 'PHPMailer is not installed');
|
||||
}
|
||||
return ['ok' => false, 'error' => 'mailer_missing'];
|
||||
}
|
||||
|
||||
try {
|
||||
$mailer = self::createMailer();
|
||||
$mailer = $this->createMailer();
|
||||
$mailer->addAddress($to);
|
||||
$mailer->Subject = $subject;
|
||||
$mailer->Body = $html;
|
||||
@@ -56,18 +62,18 @@ class MailService
|
||||
$mailer->send();
|
||||
|
||||
if ($logId) {
|
||||
MailLogRepository::markSent($logId, $mailer->getLastMessageID() ?: null);
|
||||
$this->mailLogRepository->markSent($logId, $mailer->getLastMessageID() ?: null);
|
||||
}
|
||||
return ['ok' => true];
|
||||
} catch (MailerException $e) {
|
||||
if ($logId) {
|
||||
MailLogRepository::markFailed($logId, $e->getMessage());
|
||||
$this->mailLogRepository->markFailed($logId, $e->getMessage());
|
||||
}
|
||||
return ['ok' => false, 'error' => 'send_failed'];
|
||||
}
|
||||
}
|
||||
|
||||
private static function renderTemplate(string $template, array $vars, string $locale): array
|
||||
private function renderTemplate(string $template, array $vars, string $locale): array
|
||||
{
|
||||
$base = dirname(__DIR__, 3) . '/templates/emails';
|
||||
$htmlPath = $base . '/' . $locale . '/' . $template . '.html';
|
||||
@@ -126,17 +132,18 @@ class MailService
|
||||
return [$html, $text];
|
||||
}
|
||||
|
||||
private static function createMailer(): PHPMailer
|
||||
private function createMailer(): PHPMailer
|
||||
{
|
||||
$mailer = new PHPMailer(true);
|
||||
$mailer->isSMTP();
|
||||
$host = SettingService::getSmtpHost() ?? (getenv('SMTP_HOST') ?: '');
|
||||
$port = SettingService::getSmtpPort() ?? (int) (getenv('SMTP_PORT') ?: 587);
|
||||
$user = SettingService::getSmtpUser() ?? (getenv('SMTP_USER') ?: '');
|
||||
$pass = SettingService::getSmtpPassword() ?? (getenv('SMTP_PASS') ?: '');
|
||||
$secure = SettingService::getSmtpSecure() ?? strtolower((string) (getenv('SMTP_SECURE') ?: 'tls'));
|
||||
$from = SettingService::getSmtpFrom() ?? (getenv('SMTP_FROM') ?: ($user ?: 'no-reply@localhost'));
|
||||
$fromName = SettingService::getSmtpFromName() ?? (getenv('SMTP_FROM_NAME') ?: appTitle());
|
||||
|
||||
$host = $this->settingGateway->getSmtpHost() ?? (getenv('SMTP_HOST') ?: '');
|
||||
$port = $this->settingGateway->getSmtpPort() ?? (int) (getenv('SMTP_PORT') ?: 587);
|
||||
$user = $this->settingGateway->getSmtpUser() ?? (getenv('SMTP_USER') ?: '');
|
||||
$pass = $this->settingGateway->getSmtpPassword() ?? (getenv('SMTP_PASS') ?: '');
|
||||
$secure = $this->settingGateway->getSmtpSecure() ?? strtolower((string) (getenv('SMTP_SECURE') ?: 'tls'));
|
||||
$from = $this->settingGateway->getSmtpFrom() ?? (getenv('SMTP_FROM') ?: ($user ?: 'no-reply@localhost'));
|
||||
$fromName = $this->settingGateway->getSmtpFromName() ?? (getenv('SMTP_FROM_NAME') ?: appTitle());
|
||||
|
||||
$mailer->Host = $host;
|
||||
$mailer->Port = $port;
|
||||
@@ -148,6 +155,7 @@ class MailService
|
||||
}
|
||||
$mailer->setFrom($from, $fromName);
|
||||
$mailer->CharSet = 'UTF-8';
|
||||
|
||||
return $mailer;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user