Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2911: Added application name to mail subject #11

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ SMS_DRY_RUN=false

### Mails
[email protected]
MAILS_FROM_NAME='OS2IOT Alert Manager'
[email protected]
[email protected]

Expand All @@ -55,6 +56,7 @@ ALERT_SILENCED_TIMEZONE=Europe/Copenhagen
ALERT_SILENCED_TIME_FORMAT=d-m-y\TH:i:s

ALERT_APPLICATION_CACHE_TTL=3600
ALERT_APPLICATION_URL=https://prod.os2iot.kmd.dk/applications/%%d

ALERT_GATEWAY_CACHE_TTL=21600
ALERT_GATEWAY_ORG_ID=2
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ See [keep a changelog] for information about writing changes to this log.

## [Unreleased]

- Added application name to subject.
- Wrapped application name in device info with link to the application.
- Added from name to mail address.
- Added device EUI to device model.
- Added 'References' header with EUI to mails.

## [1.0.1] - 2024-11-13

- Added `skipBasedOnAppEndDate` to checks.

## [1.0.0] - 2024-10-22

- Bumped version
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ services:
App\Service\MailService:
arguments:
$fromAddress: '%env(string:MAILS_FROM)%'
$fromName: '%env(string:MAILS_FROM_NAME)%'
$replyAddress: '%env(string:MAILS_REPLY_TO)%'
$defaultAddress: '%env(string:MAILS_DEFAULT_CC)%'

App\Service\AlertManager:
arguments:
$applicationCheckStartDate: '%env(bool:ALERT_APPLICATION_CHECK_START_DATE)%'
$applicationCheckEndDate: '%env(bool:ALERT_APPLICATION_CHECK_END_DATE)%'
$applicationBaseUrl: '%env(resolve:ALERT_APPLICATION_URL)%'
$gatewayLimit: '%env(int:ALERT_GATEWAY_LIMIT)%'
$gatewayFallbackMail: '%env(string:ALERT_GATEWAY_FALLBACK_MAIL)%'
$gatewayFallbackPhone: '%env(string:ALERT_GATEWAY_FALLBACK_PHONE)%'
Expand Down
1 change: 1 addition & 0 deletions src/Command/MailTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
],
'battery' => 67.234643,
],
refId: 'TestLinkedMail'
);

$io->success('Successfully send mail');
Expand Down
1 change: 1 addition & 0 deletions src/Model/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(
public \DateTimeImmutable $createdAt,
public \DateTimeImmutable $updatedAt,
public string $name,
public string $eui,
public Location $location,
public ?Message $latestReceivedMessage,
public float $statusBattery,
Expand Down
9 changes: 7 additions & 2 deletions src/Service/AlertManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private LoggerInterface $logger,
private bool $applicationCheckStartDate,
private bool $applicationCheckEndDate,
private string $applicationBaseUrl,
private int $gatewayLimit,
private string $gatewayFallbackMail,
private string $gatewayFallbackPhone,
Expand Down Expand Up @@ -101,6 +102,7 @@ public function checkGateways(\DateTimeImmutable $now, bool $filterOnStatus = tr
'ago' => Carbon::createFromImmutable($gateway->lastSeenAt)->diffForHumans(parts: 4),
'url' => $this->gatewayBaseUrl.$gateway->gatewayId,
],
refId: $gateway->gatewayId,
subject: $subject,
htmlTemplate: 'gateway.html.twig',
textTemplate: 'gateway.txt.twig',
Expand Down Expand Up @@ -235,18 +237,21 @@ public function checkDevice(\DateTimeImmutable $now, int $deviceId, ?Application
// Device limit for last seen is reached.
if (!$noMail) {
$subject = sprintf(
'Enheden "%s" offline siden %s',
'Enheden "%s" offline siden %s (%s)',
$device->name,
$device->latestReceivedMessage->sentTime->format('d-m-Y H:i:s')
$device->latestReceivedMessage->sentTime->format('d-m-Y H:i:s'),
$application->name ?? 'Unamed application'
);
$this->mailService->sendEmail(
to: $this->findDeviceToMailAddress($device, $application, $overrideMail),
context: [
'application' => $application,
'applicationUrl' => sprintf($this->applicationBaseUrl, $application->id ?? 0),
'device' => $device,
'ago' => Carbon::createFromImmutable($device->latestReceivedMessage->sentTime)->diffForHumans(parts: 4),
'url' => sprintf($this->deviceBaseUrl, $device->applicationId, $device->id),
],
refId: $device->eui,
subject: $subject,
htmlTemplate: 'device.html.twig',
textTemplate: 'device.txt.twig',
Expand Down
1 change: 1 addition & 0 deletions src/Service/ApiParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function device(string $content, array $gateways): Device
createdAt: $this->parseDate($data['createdAt']),
updatedAt: $this->parseDate($data['createdAt']),
name: $data['name'],
eui: $data['deviceEUI'] ?? 'unknown',
location: $data['location'] ? $this->parseLocation($data['location']) : $this->parseLocation(['latitude' => 0, 'longitude' => 0]),
latestReceivedMessage: $data['latestReceivedMessage'] ? $this->parseMessage($data['latestReceivedMessage'], $gateways) : null,
statusBattery: $data['lorawanSettings']['deviceStatusBattery'] ?? -1,
Expand Down
12 changes: 10 additions & 2 deletions src/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

final readonly class MailService
Expand All @@ -15,6 +16,7 @@ public function __construct(
private MailerInterface $mailer,
private MetricsService $metricsService,
private string $fromAddress,
private string $fromName,
private string $replyAddress,
private string $defaultAddress,
) {
Expand All @@ -27,6 +29,8 @@ public function __construct(
* The recipient's email address
* @param array $context
* The context for the email template
* @param string $refId
* References header id (used to link mails)
* @param string $subject
* The subject of the email. Defaults to 'Test mail from alert manager'.
* @param string $htmlTemplate
Expand All @@ -36,10 +40,10 @@ public function __construct(
*
* @throws MailException
*/
public function sendEmail(string $to, array $context, string $subject = 'Test mail from alert manager', string $htmlTemplate = 'test.html.twig', string $textTemplate = 'test.txt.twig'): void
public function sendEmail(string $to, array $context, string $refId, string $subject = 'Test mail from alert manager', string $htmlTemplate = 'test.html.twig', string $textTemplate = 'test.txt.twig'): void
{
$email = (new TemplatedEmail())
->from($this->fromAddress)
->from(new Address($this->fromAddress, $this->fromName))
->to($to)
->cc($this->defaultAddress)
->replyTo($this->replyAddress)
Expand All @@ -50,6 +54,10 @@ public function sendEmail(string $to, array $context, string $subject = 'Test ma
->context($context)
;

// Set references header to thread/link mails.
list($localPart, $domainPart) = explode('@', $this->fromAddress);
$email->getHeaders()->addTextHeader('References', $refId.'@'.$domainPart);

try {
$this->mailer->send($email);
$this->metricsService->counter(
Expand Down
4 changes: 2 additions & 2 deletions templates/mails/device.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
</tr>
<tr>
<td>Applikation:</td>
<td>{{ application.name }}</td>
<td><a href="{{ applicationUrl }}">{{ application.name }}</a></td>
</tr>
<tr>
<td>EUI:</td>
<td>{{ device.id }}</td>
<td>{{ device.eui }}</td>
</tr>
<tr>
<td>Batteriniveau:</td>
Expand Down
1 change: 1 addition & 0 deletions templates/mails/device.txt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Seneste kontakt var {{ device.latestReceivedMessage.sentTime|date("d-m-Y H:i:s")
Enhedsinformation
--------------------------------------------------------------------------------
Navn: {{ device.name }}
Applikation: {{ application.name }} ({{ applicationUrl }})
EUI: {{ device.id }}
Batteriniveau: {% if device.statusBattery == -1 %}N/A{% else %}{{ device.statusBattery|number_format(0, ',', '') }}{% endif %}

Expand Down
Loading