X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Femails%2Fmailer.ex;h=8b1bdef754d0a0b23c0a7727ef4fc9b15dc05d6e;hb=46dd276d686e49676101e2af743aad61393f4b70;hp=8d12641f20bd91ca713bd1e9fbd4ba296955d2e9;hpb=b73a1a33de76dc848037a5d0e951866bd21f92c4;p=akkoma
diff --git a/lib/pleroma/emails/mailer.ex b/lib/pleroma/emails/mailer.ex
index 8d12641f2..8b1bdef75 100644
--- a/lib/pleroma/emails/mailer.ex
+++ b/lib/pleroma/emails/mailer.ex
@@ -1,7 +1,66 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
-defmodule Pleroma.Mailer do
- use Swoosh.Mailer, otp_app: :pleroma
+defmodule Pleroma.Emails.Mailer do
+ @moduledoc """
+ Defines the Pleroma mailer.
+
+ The module contains functions to delivery email using Swoosh.Mailer.
+ """
+
+ alias Pleroma.Workers.MailerWorker
+ 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
+ encoded_email =
+ email
+ |> :erlang.term_to_binary()
+ |> Base.encode64()
+
+ MailerWorker.enqueue("email", %{"encoded_email" => encoded_email, "config" => 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