summaryrefslogtreecommitdiff
path: root/lib/silmataivas/mailer.ex
blob: 3c11436df144dd8748c9ca5fec19f5529654fcf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule Silmataivas.Mailer do
  use Swoosh.Mailer, otp_app: :silmataivas
  require Logger

  def send_alert(
        email,
        %{
          "main" => %{"temp" => temp},
          "wind" => %{"speed" => speed},
          "dt_txt" => time_str
        } = entry
      ) do
    rain_mm = get_in(entry, ["rain", "3h"]) || 0.0
    wind_kmh = speed * 3.6

    import Swoosh.Email

    body = """
    🚨 Weather alert for your location (#{time_str}):

    🌬️ Wind: #{Float.round(wind_kmh, 1)} km/h
    🌧️ Rain: #{rain_mm} mm
    🌡️ Temperature: #{temp} °C

    Stay safe,
    — Silmätaivas
    """

    email_struct =
      new()
      |> to(email)
      |> from({"Silmätaivas Alerts", "silmataivas@rycerz.cloud"})
      |> subject("⚠️ Weather Alert for Your Location")
      |> text_body(body)

    case deliver(email_struct) do
      {:ok, response} ->
        Logger.info("📨 Email sent via SES: #{inspect(response)}")

      {:error, reason} ->
        Logger.error("❌ Failed to send email: #{inspect(reason)}")
    end
  end
end