[#1062] added option to disable send email
[akkoma] / lib / pleroma / emails / mailer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 Swoosh.DeliveryError
13
14 @otp_app :pleroma
15 @mailer_config [otp: :pleroma]
16
17 @spec enabled?() :: boolean()
18 def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled])
19
20 @doc "add email to queue"
21 def deliver_async(email, config \\ []) do
22 PleromaJobQueue.enqueue(:mailer, __MODULE__, [:deliver_async, email, config])
23 end
24
25 @doc "callback to perform send email from queue"
26 def perform(:deliver_async, email, config), do: deliver(email, config)
27
28 @spec deliver(Swoosh.Email.t(), Keyword.t()) :: {:ok, term} | {:error, term}
29 def deliver(email, config \\ [])
30
31 def deliver(email, config) do
32 case enabled?() do
33 true -> Swoosh.Mailer.deliver(email, parse_config(config))
34 false -> {:error, :deliveries_disabled}
35 end
36 end
37
38 @spec deliver!(Swoosh.Email.t(), Keyword.t()) :: term | no_return
39 def deliver!(email, config \\ [])
40
41 def deliver!(email, config) do
42 case deliver(email, config) do
43 {:ok, result} -> result
44 {:error, reason} -> raise DeliveryError, reason: reason
45 end
46 end
47
48 @on_load :validate_dependency
49
50 @doc false
51 def validate_dependency do
52 parse_config([])
53 |> Keyword.get(:adapter)
54 |> Swoosh.Mailer.validate_dependency()
55 end
56
57 defp parse_config(config) do
58 Swoosh.Mailer.parse_config(@otp_app, __MODULE__, @mailer_config, config)
59 end
60 end