81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace php;
|
|
|
|
use Monolog\Logger;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
|
|
|
|
/**
|
|
* Sends mails.
|
|
*/
|
|
class Mailer
|
|
{
|
|
/**
|
|
* @var Logger The logger to use for logging.
|
|
*/
|
|
private Logger $logger;
|
|
/**
|
|
* @var array The configuration to use for mailing.
|
|
*/
|
|
private array $config;
|
|
|
|
|
|
/**
|
|
* Constructs a new mailer.
|
|
*
|
|
* @param Logger $logger the logger to use for logging
|
|
* @param array $config the configuration to use for mailing
|
|
*/
|
|
public function __construct(Logger $logger, array $config)
|
|
{
|
|
$this->logger = $logger;
|
|
$this->config = $config;
|
|
}
|
|
|
|
|
|
/**
|
|
* Sends an email for testing purposes.
|
|
*
|
|
* @return Response a response with message `null` if the mail was sent, or a response with a message explaining
|
|
* what went wrong otherwise
|
|
*/
|
|
public function send_test_mail(): Response
|
|
{
|
|
$this->logger->info("Sending test email.");
|
|
|
|
$mail = new PHPMailer();
|
|
$mail->IsSMTP();
|
|
$mail->CharSet = "UTF-8";
|
|
|
|
$mail->SMTPDebug = SMTP::DEBUG_OFF;
|
|
$mail->Host = $this->config["host"];
|
|
$mail->SMTPAuth = true;
|
|
$mail->Port = $this->config["port"];
|
|
$mail->Username = $this->config["username"];
|
|
$mail->Password = $this->config["password"];
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
|
|
try {
|
|
$mail->setFrom($this->config["username"], $this->config["from_name"]);
|
|
$mail->addAddress($this->config["to_address_test"]);
|
|
} catch (Exception $exception) {
|
|
$this->logger->warning("Failed to set email address for test mail.", [$exception]);
|
|
return new Response("Failed to set email address for test email.", false);
|
|
}
|
|
|
|
$mail->Subject = "Test mail";
|
|
$mail->Body = "This is a test mail from death-notifier!";
|
|
|
|
try {
|
|
$mail->send();
|
|
} catch (Exception $exception) {
|
|
$this->logger->warning("Failed to send test email.", [$exception]);
|
|
return new Response("Failed to send test email.", false);
|
|
}
|
|
|
|
return new Response(null, true);
|
|
}
|
|
}
|