use Phoenix.Swoosh, view: Pleroma.Web.EmailView, layout: {Pleroma.Web.LayoutView, :email}
alias Pleroma.Config
+ alias Pleroma.HTML
alias Pleroma.User
alias Pleroma.Web.Endpoint
alias Pleroma.Web.Router
|> from(sender())
|> subject("Password reset")
|> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
end
def user_invitation_email(
|> from(sender())
|> subject("Invitation to #{instance_name()}")
|> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
end
def account_confirmation_email(user) do
)
html_body = """
- <h3>Welcome to #{instance_name()}!</h3>
+ <h3>Thank you for registering on #{instance_name()}</h3>
<p>Email confirmation is required to activate the account.</p>
- <p>Click the following link to proceed: <a href="#{confirmation_url}">activate your account</a>.</p>
+ <p>Please click the following link to <a href="#{confirmation_url}">activate your account</a>.</p>
"""
new()
|> from(sender())
|> subject("#{instance_name()} account confirmation")
|> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
end
def approval_pending_email(user) do
|> from(sender())
|> subject("Your account is awaiting approval")
|> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
+ end
+
+ def successful_registration_email(user) do
+ html_body = """
+ <h3>Hello @#{user.nickname},</h3>
+ <p>Your account at #{instance_name()} has been registered successfully.</p>
+ <p>No further action is required to activate your account.</p>
+ """
+
+ new()
+ |> to(recipient(user))
+ |> from(sender())
+ |> subject("Account registered on #{instance_name()}")
+ |> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
end
@doc """
|> from(sender())
|> subject("Your account archive is ready")
|> html_body(html_body)
+ |> text_body(HTML.strip_tags(html_body))
end
end
end
def post_register_action(%User{is_confirmed: false} = user) do
- with {:ok, _} <- try_send_confirmation_email(user) do
+ with {:ok, _} <- maybe_send_confirmation_email(user) do
{:ok, user}
end
end
with {:ok, user} <- autofollow_users(user),
{:ok, _} <- autofollowing_users(user),
{:ok, user} <- set_cache(user),
- {:ok, _} <- send_welcome_email(user),
- {:ok, _} <- send_welcome_message(user),
- {:ok, _} <- send_welcome_chat_message(user) do
+ {:ok, _} <- maybe_send_registration_email(user),
+ {:ok, _} <- maybe_send_welcome_email(user),
+ {:ok, _} <- maybe_send_welcome_message(user),
+ {:ok, _} <- maybe_send_welcome_chat_message(user) do
{:ok, user}
end
end
{:ok, :enqueued}
end
- def send_welcome_message(user) do
+ defp maybe_send_welcome_message(user) do
if User.WelcomeMessage.enabled?() do
User.WelcomeMessage.post_message(user)
{:ok, :enqueued}
end
end
- def send_welcome_chat_message(user) do
+ defp maybe_send_welcome_chat_message(user) do
if User.WelcomeChatMessage.enabled?() do
User.WelcomeChatMessage.post_message(user)
{:ok, :enqueued}
end
end
- def send_welcome_email(%User{email: email} = user) when is_binary(email) do
+ defp maybe_send_welcome_email(%User{email: email} = user) when is_binary(email) do
if User.WelcomeEmail.enabled?() do
User.WelcomeEmail.send_email(user)
{:ok, :enqueued}
end
end
- def send_welcome_email(_), do: {:ok, :noop}
+ defp maybe_send_welcome_email(_), do: {:ok, :noop}
- @spec try_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
- def try_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
+ @spec maybe_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
+ def maybe_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
when is_binary(email) do
if Config.get([:instance, :account_activation_required]) do
send_confirmation_email(user)
end
end
- def try_send_confirmation_email(_), do: {:ok, :noop}
+ def maybe_send_confirmation_email(_), do: {:ok, :noop}
@spec send_confirmation_email(Uset.t()) :: User.t()
def send_confirmation_email(%User{} = user) do
user
end
+ @spec maybe_send_registration_email(User.t()) :: {:ok, :enqueued | :noop}
+ defp maybe_send_registration_email(%User{email: email} = user) when is_binary(email) do
+ with false <- User.WelcomeEmail.enabled?(),
+ false <- Config.get([:instance, :account_activation_required], false),
+ false <- Config.get([:instance, :account_approval_required], false) do
+ user
+ |> Pleroma.Emails.UserEmail.successful_registration_email()
+ |> Pleroma.Emails.Mailer.deliver_async()
+
+ {:ok, :enqueued}
+ else
+ _ ->
+ {:ok, :noop}
+ end
+ end
+
+ defp maybe_send_registration_email(_), do: {:ok, :noop}
+
def needs_update?(%User{local: true}), do: false
def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
)
end
+ test "it sends a registration confirmed email if no others will be sent" do
+ clear_config([:welcome, :email, :enabled], false)
+ clear_config([:instance, :account_activation_required], false)
+ clear_config([:instance, :account_approval_required], false)
+
+ {:ok, user} =
+ User.register_changeset(%User{}, @full_user_data)
+ |> User.register()
+
+ ObanHelpers.perform_all()
+
+ instance_name = Pleroma.Config.get([:instance, :name])
+ sender = Pleroma.Config.get([:instance, :notify_email])
+
+ assert_email_sent(
+ from: {instance_name, sender},
+ to: {user.name, user.email},
+ subject: "Account registered on #{instance_name}"
+ )
+ end
+
test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
clear_config([:instance, :account_activation_required], true)