2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-04-10 06:05:05 +02:00
|
|
|
defmodule Pleroma.Emails.Mailer do
|
2019-07-10 07:34:21 +02:00
|
|
|
@moduledoc """
|
|
|
|
Defines the Pleroma mailer.
|
2019-02-20 17:51:25 +01:00
|
|
|
|
2019-07-10 07:34:21 +02:00
|
|
|
The module contains functions to delivery email using Swoosh.Mailer.
|
|
|
|
"""
|
|
|
|
|
2019-08-31 18:08:56 +02:00
|
|
|
alias Pleroma.Workers.MailerWorker
|
2019-07-10 07:34:21 +02:00
|
|
|
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"
|
2019-02-20 17:51:25 +01:00
|
|
|
def deliver_async(email, config \\ []) do
|
2019-08-14 20:42:21 +02:00
|
|
|
encoded_email =
|
|
|
|
email
|
|
|
|
|> :erlang.term_to_binary()
|
|
|
|
|> Base.encode64()
|
|
|
|
|
2019-08-31 20:58:42 +02:00
|
|
|
MailerWorker.enqueue("email", %{"encoded_email" => encoded_email, "config" => config})
|
2019-02-20 17:51:25 +01:00
|
|
|
end
|
|
|
|
|
2019-07-10 07:34:21 +02:00
|
|
|
@doc "callback to perform send email from queue"
|
2019-02-20 17:51:25 +01:00
|
|
|
def perform(:deliver_async, email, config), do: deliver(email, config)
|
2019-07-10 07:34:21 +02:00
|
|
|
|
|
|
|
@spec deliver(Swoosh.Email.t(), Keyword.t()) :: {:ok, term} | {:error, term}
|
|
|
|
def deliver(email, config \\ [])
|
|
|
|
|
|
|
|
def deliver(email, config) do
|
2020-09-21 20:28:02 +02:00
|
|
|
# temporary hackney fix until hackney max_connections bug is fixed
|
|
|
|
# https://git.pleroma.social/pleroma/pleroma/-/issues/2101
|
|
|
|
email =
|
|
|
|
Swoosh.Email.put_private(email, :hackney_options, ssl_options: [versions: [:"tlsv1.2"]])
|
|
|
|
|
2019-07-10 07:34:21 +02:00
|
|
|
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
|
2018-12-10 18:20:50 +01:00
|
|
|
end
|