Ensure we can update contentMap on update
[akkoma] / lib / pleroma / emails / mailer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emails.Mailer do
6 @moduledoc """
7 Defines the Pleroma mailer.
8
9 The module contains functions to delivery email using Swoosh.Mailer.
10 """
11
12 alias Pleroma.Workers.MailerWorker
13 alias Swoosh.DeliveryError
14
15 @otp_app :pleroma
16 @mailer_config [otp: :pleroma]
17
18 @spec enabled?() :: boolean()
19 def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled])
20
21 @doc "add email to queue"
22 def deliver_async(email, config \\ []) do
23 encoded_email =
24 email
25 |> :erlang.term_to_binary()
26 |> Base.encode64()
27
28 MailerWorker.enqueue("email", %{"encoded_email" => encoded_email, "config" => config})
29 end
30
31 @doc "callback to perform send email from queue"
32 def perform(:deliver_async, email, config), do: deliver(email, config)
33
34 @spec deliver(Swoosh.Email.t(), Keyword.t()) :: {:ok, term} | {:error, term}
35 def deliver(email, config \\ [])
36
37 def deliver(email, config) do
38 case enabled?() do
39 true -> Swoosh.Mailer.deliver(email, parse_config(config))
40 false -> {:error, :deliveries_disabled}
41 end
42 end
43
44 @spec deliver!(Swoosh.Email.t(), Keyword.t()) :: term | no_return
45 def deliver!(email, config \\ [])
46
47 def deliver!(email, config) do
48 case deliver(email, config) do
49 {:ok, result} -> result
50 {:error, reason} -> raise DeliveryError, reason: reason
51 end
52 end
53
54 @on_load :validate_dependency
55
56 @doc false
57 def validate_dependency do
58 parse_config([])
59 |> Keyword.get(:adapter)
60 |> Swoosh.Mailer.validate_dependency()
61 end
62
63 defp parse_config(config) do
64 Swoosh.Mailer.parse_config(@otp_app, __MODULE__, @mailer_config, config)
65 end
66 end