summaryrefslogtreecommitdiff
path: root/lib/pleroma/user/welcome_email.ex
blob: 5322000d4f6bd7fe1fd3e9e8c94fcdb39b18b0e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.User.WelcomeEmail do
  @moduledoc """
  The module represents the functions to send welcome email.
  """

  alias Pleroma.Config
  alias Pleroma.Emails
  alias Pleroma.User

  import Pleroma.Config.Helpers, only: [instance_name: 0]

  @spec enabled?() :: boolean()
  def enabled?, do: Config.get([:welcome, :email, :enabled], false)

  @spec send_email(User.t()) :: {:ok, Oban.Job.t()}
  def send_email(%User{} = user) do
    user
    |> Emails.UserEmail.welcome(email_options(user))
    |> Emails.Mailer.deliver_async()
  end

  defp email_options(user) do
    bindings = [user: user, instance_name: instance_name()]

    %{}
    |> add_sender(Config.get([:welcome, :email, :sender], nil))
    |> add_option(:subject, bindings)
    |> add_option(:html, bindings)
    |> add_option(:text, bindings)
  end

  defp add_option(opts, option, bindings) do
    [:welcome, :email, option]
    |> Config.get(nil)
    |> eval_string(bindings)
    |> merge_options(opts, option)
  end

  defp add_sender(opts, {_name, _email} = sender) do
    merge_options(sender, opts, :sender)
  end

  defp add_sender(opts, sender) when is_binary(sender) do
    add_sender(opts, {instance_name(), sender})
  end

  defp add_sender(opts, _), do: opts

  defp merge_options(nil, options, _option), do: options

  defp merge_options(value, options, option) do
    Map.merge(options, %{option => value})
  end

  defp eval_string(nil, _), do: nil
  defp eval_string("", _), do: nil
  defp eval_string(str, bindings), do: EEx.eval_string(str, bindings)
end