diff options
author | Maksim <parallel588@gmail.com> | 2019-07-10 05:34:21 +0000 |
---|---|---|
committer | kaniini <nenolod@gmail.com> | 2019-07-10 05:34:21 +0000 |
commit | 12b1454245fc2efba22d5633f65539dac727ee3d (patch) | |
tree | 32253756380f31002c726caa8782ab3e634d25c4 /lib | |
parent | b00620b378fea818bcbde49b70548a574b706796 (diff) | |
download | pleroma-12b1454245fc2efba22d5633f65539dac727ee3d.tar.gz pleroma-12b1454245fc2efba22d5633f65539dac727ee3d.zip |
[#1062] added option to disable send email
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/emails/mailer.ex | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/pleroma/emails/mailer.ex b/lib/pleroma/emails/mailer.ex index 53f5a661c..2e4657b7c 100644 --- a/lib/pleroma/emails/mailer.ex +++ b/lib/pleroma/emails/mailer.ex @@ -3,11 +3,58 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Emails.Mailer do - use Swoosh.Mailer, otp_app: :pleroma + @moduledoc """ + Defines the Pleroma mailer. + The module contains functions to delivery email using Swoosh.Mailer. + """ + + alias Swoosh.DeliveryError + + @otp_app :pleroma + @mailer_config [otp: :pleroma] + + @spec enabled?() :: boolean() + def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled]) + + @doc "add email to queue" def deliver_async(email, config \\ []) do PleromaJobQueue.enqueue(:mailer, __MODULE__, [:deliver_async, email, config]) end + @doc "callback to perform send email from queue" def perform(:deliver_async, email, config), do: deliver(email, config) + + @spec deliver(Swoosh.Email.t(), Keyword.t()) :: {:ok, term} | {:error, term} + def deliver(email, config \\ []) + + def deliver(email, config) do + case enabled?() do + true -> Swoosh.Mailer.deliver(email, parse_config(config)) + false -> {:error, :deliveries_disabled} + end + end + + @spec deliver!(Swoosh.Email.t(), Keyword.t()) :: term | no_return + def deliver!(email, config \\ []) + + def deliver!(email, config) do + case deliver(email, config) do + {:ok, result} -> result + {:error, reason} -> raise DeliveryError, reason: reason + end + end + + @on_load :validate_dependency + + @doc false + def validate_dependency do + parse_config([]) + |> Keyword.get(:adapter) + |> Swoosh.Mailer.validate_dependency() + end + + defp parse_config(config) do + Swoosh.Mailer.parse_config(@otp_app, __MODULE__, @mailer_config, config) + end end |