1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emails.Mailer do
7 Defines the Pleroma mailer.
9 The module contains functions to delivery email using Swoosh.Mailer.
12 alias Pleroma.Workers.MailerWorker
13 alias Swoosh.DeliveryError
16 @mailer_config [otp: :pleroma]
18 @spec enabled?() :: boolean()
19 def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled])
21 @doc "add email to queue"
22 def deliver_async(email, config \\ []) do
25 |> :erlang.term_to_binary()
28 MailerWorker.enqueue("email", %{"encoded_email" => encoded_email, "config" => config})
31 @doc "callback to perform send email from queue"
32 def perform(:deliver_async, email, config), do: deliver(email, config)
34 @spec deliver(Swoosh.Email.t(), Keyword.t()) :: {:ok, term} | {:error, term}
35 def deliver(email, config \\ [])
37 def deliver(email, config) do
38 # temporary hackney fix until hackney max_connections bug is fixed
39 # https://git.pleroma.social/pleroma/pleroma/-/issues/2101
41 Swoosh.Email.put_private(email, :hackney_options, ssl_options: [versions: [:"tlsv1.2"]])
44 true -> Swoosh.Mailer.deliver(email, parse_config(config))
45 false -> {:error, :deliveries_disabled}
49 @spec deliver!(Swoosh.Email.t(), Keyword.t()) :: term | no_return
50 def deliver!(email, config \\ [])
52 def deliver!(email, config) do
53 case deliver(email, config) do
54 {:ok, result} -> result
55 {:error, reason} -> raise DeliveryError, reason: reason
59 @on_load :validate_dependency
62 def validate_dependency do
64 |> Keyword.get(:adapter)
65 |> Swoosh.Mailer.validate_dependency()
68 defp parse_config(config) do
69 Swoosh.Mailer.parse_config(@otp_app, __MODULE__, @mailer_config, config)