Merge branch 'update-service-files-of-openrc-and-systemd-to-new-recommended-paths...
[akkoma] / lib / pleroma / emails / user_email.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserEmail do
6 @moduledoc "User emails"
7
8 import Swoosh.Email
9
10 alias Pleroma.Web.{Endpoint, Router}
11
12 defp instance_config, do: Pleroma.Config.get(:instance)
13
14 defp instance_name, do: instance_config()[:name]
15
16 defp sender do
17 {instance_name(), instance_config()[:email]}
18 end
19
20 defp recipient(email, nil), do: email
21 defp recipient(email, name), do: {name, email}
22 defp recipient(%Pleroma.User{} = user), do: recipient(user.email, user.name)
23
24 def password_reset_email(user, password_reset_token) when is_binary(password_reset_token) do
25 password_reset_url =
26 Router.Helpers.util_url(
27 Endpoint,
28 :show_password_reset,
29 password_reset_token
30 )
31
32 html_body = """
33 <h3>Reset your password at #{instance_name()}</h3>
34 <p>Someone has requested password change for your account at #{instance_name()}.</p>
35 <p>If it was you, visit the following link to proceed: <a href="#{password_reset_url}">reset password</a>.</p>
36 <p>If it was someone else, nothing to worry about: your data is secure and your password has not been changed.</p>
37 """
38
39 new()
40 |> to(recipient(user))
41 |> from(sender())
42 |> subject("Password reset")
43 |> html_body(html_body)
44 end
45
46 def user_invitation_email(
47 user,
48 %Pleroma.UserInviteToken{} = user_invite_token,
49 to_email,
50 to_name \\ nil
51 ) do
52 registration_url =
53 Router.Helpers.redirect_url(
54 Endpoint,
55 :registration_page,
56 user_invite_token.token
57 )
58
59 html_body = """
60 <h3>You are invited to #{instance_name()}</h3>
61 <p>#{user.name} invites you to join #{instance_name()}, an instance of Pleroma federated social networking platform.</p>
62 <p>Click the following link to register: <a href="#{registration_url}">accept invitation</a>.</p>
63 """
64
65 new()
66 |> to(recipient(to_email, to_name))
67 |> from(sender())
68 |> subject("Invitation to #{instance_name()}")
69 |> html_body(html_body)
70 end
71
72 def account_confirmation_email(user) do
73 confirmation_url =
74 Router.Helpers.confirm_email_url(
75 Endpoint,
76 :confirm_email,
77 user.id,
78 to_string(user.info.confirmation_token)
79 )
80
81 html_body = """
82 <h3>Welcome to #{instance_name()}!</h3>
83 <p>Email confirmation is required to activate the account.</p>
84 <p>Click the following link to proceed: <a href="#{confirmation_url}">activate your account</a>.</p>
85 """
86
87 new()
88 |> to(recipient(user))
89 |> from(sender())
90 |> subject("#{instance_name()} account confirmation")
91 |> html_body(html_body)
92 end
93 end