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