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