1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emails.UserEmail do
6 @moduledoc "User emails"
8 use Phoenix.Swoosh, view: Pleroma.Web.EmailView, layout: {Pleroma.Web.LayoutView, :email}
12 alias Pleroma.Web.Endpoint
13 alias Pleroma.Web.Router
15 defp instance_name, do: Config.get([:instance, :name])
18 email = Config.get([:instance, :notify_email]) || Config.get([:instance, :email])
19 {instance_name(), email}
22 defp recipient(email, nil), do: email
23 defp recipient(email, name), do: {name, email}
24 defp recipient(%User{} = user), do: recipient(user.email, user.name)
26 def password_reset_email(user, token) when is_binary(token) do
27 password_reset_url = Router.Helpers.reset_password_url(Endpoint, :reset, token)
30 <h3>Reset your password at #{instance_name()}</h3>
31 <p>Someone has requested password change for your account at #{instance_name()}.</p>
32 <p>If it was you, visit the following link to proceed: <a href="#{password_reset_url}">reset password</a>.</p>
33 <p>If it was someone else, nothing to worry about: your data is secure and your password has not been changed.</p>
37 |> to(recipient(user))
39 |> subject("Password reset")
40 |> html_body(html_body)
43 def user_invitation_email(
45 %Pleroma.UserInviteToken{} = user_invite_token,
50 Router.Helpers.redirect_url(
53 user_invite_token.token
57 <h3>You are invited to #{instance_name()}</h3>
58 <p>#{user.name} invites you to join #{instance_name()}, an instance of Pleroma federated social networking platform.</p>
59 <p>Click the following link to register: <a href="#{registration_url}">accept invitation</a>.</p>
63 |> to(recipient(to_email, to_name))
65 |> subject("Invitation to #{instance_name()}")
66 |> html_body(html_body)
69 def account_confirmation_email(user) do
71 Router.Helpers.confirm_email_url(
75 to_string(user.confirmation_token)
79 <h3>Welcome to #{instance_name()}!</h3>
80 <p>Email confirmation is required to activate the account.</p>
81 <p>Click the following link to proceed: <a href="#{confirmation_url}">activate your account</a>.</p>
85 |> to(recipient(user))
87 |> subject("#{instance_name()} account confirmation")
88 |> html_body(html_body)
92 Email used in digest email notifications
93 Includes Mentions and New Followers data
94 If there are no mentions (even when new followers exist), the function will return nil
96 @spec digest_email(User.t()) :: Swoosh.Email.t() | nil
97 def digest_email(user) do
98 notifications = Pleroma.Notification.for_user_since(user, user.last_digest_emailed_at)
102 |> Enum.filter(&(&1.activity.data["type"] == "Create"))
103 |> Enum.map(fn notification ->
104 object = Pleroma.Object.normalize(notification.activity)
105 object = update_in(object.data["content"], &format_links/1)
110 from: User.get_by_ap_id(notification.activity.actor)
116 |> Enum.filter(&(&1.activity.data["type"] == "Follow"))
117 |> Enum.map(fn notification ->
120 object: Pleroma.Object.normalize(notification.activity),
121 from: User.get_by_ap_id(notification.activity.actor)
125 unless Enum.empty?(mentions) do
126 styling = Config.get([__MODULE__, :styling])
127 logo = Config.get([__MODULE__, :logo])
130 instance: instance_name(),
133 followers: followers,
134 unsubscribe_link: unsubscribe_url(user, "digest"),
140 Path.join(:code.priv_dir(:pleroma), "static/static/logo.png")
142 Path.join(Config.get([:instance, :static_dir]), logo)
146 |> to(recipient(user))
148 |> subject("Your digest from #{instance_name()}")
150 |> render_body("digest.html", html_data)
151 |> attachment(Swoosh.Attachment.new(logo_path, filename: "logo.png", type: :inline))
155 defp format_links(str) do
156 re = ~r/<a.+href=['"].*>/iU
157 %{link_color: color} = Config.get([__MODULE__, :styling])
159 Regex.replace(re, str, fn link ->
160 String.replace(link, "<a", "<a style=\"color: #{color};text-decoration: none;\"")
165 Generate unsubscribe link for given user and notifications type.
166 The link contains JWT token with the data, and subscription can be modified without
169 @spec unsubscribe_url(User.t(), String.t()) :: String.t()
170 def unsubscribe_url(user, notifications_type) do
172 %{"sub" => user.id, "act" => %{"unsubscribe" => notifications_type}, "exp" => false}
173 |> Pleroma.JWT.generate_and_sign!()
176 Router.Helpers.subscription_url(Endpoint, :unsubscribe, token)