fix sender for welcome email

This commit is contained in:
Maksim Pechnikov 2020-07-22 15:34:47 +03:00
parent b620290dd9
commit 5879d36854
6 changed files with 50 additions and 27 deletions

View file

@ -260,7 +260,7 @@ config :pleroma, :welcome,
], ],
email: [ email: [
enabled: false, enabled: false,
sender_nickname: nil, sender: nil,
subject: "Welcome to <%= instance_name %>", subject: "Welcome to <%= instance_name %>",
html: "Welcome to <%= instance_name %>", html: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>" text: "Welcome to <%= instance_name %>"

View file

@ -990,11 +990,12 @@ config :pleroma, :config_description, [
description: "Enables sends direct message for new user after registration" description: "Enables sends direct message for new user after registration"
}, },
%{ %{
key: :sender_nickname, key: :sender,
type: :string, type: [:string, :tuple],
description: "The nickname of the local user that sends the welcome email", description:
"The email address or tuple with `{nickname, email}` that will use as sender to the welcome email.",
suggestions: [ suggestions: [
"lain" {"Pleroma App", "welcome@pleroma.app"}
] ]
}, },
%{ %{

View file

@ -46,8 +46,6 @@ To add configuration to your config file, you can copy it from the base config.
* `max_pinned_statuses`: The maximum number of pinned statuses. `0` will disable the feature. * `max_pinned_statuses`: The maximum number of pinned statuses. `0` will disable the feature.
* `autofollowed_nicknames`: Set to nicknames of (local) users that every new user should automatically follow. * `autofollowed_nicknames`: Set to nicknames of (local) users that every new user should automatically follow.
* `attachment_links`: Set to true to enable automatically adding attachment link text to statuses. * `attachment_links`: Set to true to enable automatically adding attachment link text to statuses.
* `welcome_message`: A message that will be send to a newly registered users as a direct message.
* `welcome_user_nickname`: The nickname of the local user that sends the welcome message.
* `max_report_comment_size`: The maximum size of the report comment (Default: `1000`). * `max_report_comment_size`: The maximum size of the report comment (Default: `1000`).
* `safe_dm_mentions`: If set to true, only mentions at the beginning of a post will be used to address people in direct messages. This is to prevent accidental mentioning of people when talking about them (e.g. "@friend hey i really don't like @enemy"). Default: `false`. * `safe_dm_mentions`: If set to true, only mentions at the beginning of a post will be used to address people in direct messages. This is to prevent accidental mentioning of people when talking about them (e.g. "@friend hey i really don't like @enemy"). Default: `false`.
* `healthcheck`: If set to true, system data will be shown on ``/api/pleroma/healthcheck``. * `healthcheck`: If set to true, system data will be shown on ``/api/pleroma/healthcheck``.
@ -70,11 +68,29 @@ To add configuration to your config file, you can copy it from the base config.
* `message`: A message that will be send to a newly registered users as a direct message. * `message`: A message that will be send to a newly registered users as a direct message.
* `email`: - welcome message sent as a email. * `email`: - welcome message sent as a email.
* `enabled`: Enables the send a welcome email to a newly registered user. Defaults to `false`. * `enabled`: Enables the send a welcome email to a newly registered user. Defaults to `false`.
* `sender_nickname`: The nickname of the local user that sends the welcome email. * `sender`: The email address or tuple with `{nickname, email}` that will use as sender to the welcome email.
* `subject`: A subject of welcome email. * `subject`: A subject of welcome email.
* `html`: A html that will be send to a newly registered users as a email. * `html`: A html that will be send to a newly registered users as a email.
* `text`: A text that will be send to a newly registered users as a email. * `text`: A text that will be send to a newly registered users as a email.
Example:
```elixir
config :pleroma, :welcome,
direct_message: [
enabled: true,
sender_nickname: "lain",
message: "Hi, @username! Welcome on board!"
],
email: [
enabled: true,
sender: {"Pleroma App", "welcome@pleroma.app"},
subject: "Welcome to <%= instance_name %>",
html: "Welcome to <%= instance_name %>",
text: "Welcome to <%= instance_name %>"
]
```
## Message rewrite facility ## Message rewrite facility
### :mrf ### :mrf

View file

@ -27,7 +27,7 @@ defmodule Pleroma.User.WelcomeEmail do
bindings = [user: user, instance_name: instance_name()] bindings = [user: user, instance_name: instance_name()]
%{} %{}
|> add_sender(Config.get([:welcome, :email, :sender_nickname], nil)) |> add_sender(Config.get([:welcome, :email, :sender], nil))
|> add_option(:subject, bindings) |> add_option(:subject, bindings)
|> add_option(:html, bindings) |> add_option(:html, bindings)
|> add_option(:text, bindings) |> add_option(:text, bindings)
@ -40,28 +40,22 @@ defmodule Pleroma.User.WelcomeEmail do
|> merge_options(opts, option) |> merge_options(opts, option)
end end
def add_sender(opts, nickname) do defp add_sender(opts, {_name, _email} = sender) do
nickname merge_options(sender, opts, :sender)
|> fetch_sender()
|> merge_options(opts, :sender)
end end
defp add_sender(opts, sender) when is_binary(sender) do
add_sender(opts, {instance_name(), sender})
end
defp add_sender(opts, _), do: opts
defp merge_options(nil, options, _option), do: options defp merge_options(nil, options, _option), do: options
defp merge_options(value, options, option) do defp merge_options(value, options, option) do
Map.merge(options, %{option => value}) Map.merge(options, %{option => value})
end end
defp fetch_sender(nickname) when is_binary(nickname) do
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
{instance_name(), user.email}
else
_ -> nil
end
end
defp fetch_sender(_), do: nil
defp eval_string(nil, _), do: nil defp eval_string(nil, _), do: nil
defp eval_string("", _), do: nil defp eval_string("", _), do: nil
defp eval_string(str, bindings), do: EEx.eval_string(str, bindings) defp eval_string(str, bindings), do: EEx.eval_string(str, bindings)

View file

@ -16,11 +16,10 @@ defmodule Pleroma.User.WelcomeEmailTest do
describe "send_email/1" do describe "send_email/1" do
test "send a welcome email" do test "send a welcome email" do
welcome_user = insert(:user)
user = insert(:user, name: "Jimm") user = insert(:user, name: "Jimm")
Config.put([:welcome, :email, :enabled], true) Config.put([:welcome, :email, :enabled], true)
Config.put([:welcome, :email, :sender_nickname], welcome_user.nickname) Config.put([:welcome, :email, :sender], "welcome@pleroma.app")
Config.put( Config.put(
[:welcome, :email, :subject], [:welcome, :email, :subject],
@ -39,7 +38,20 @@ defmodule Pleroma.User.WelcomeEmailTest do
ObanHelpers.perform_all() ObanHelpers.perform_all()
assert_email_sent( assert_email_sent(
from: {instance_name, welcome_user.email}, from: {instance_name, "welcome@pleroma.app"},
to: {user.name, user.email},
subject: "Hello, welcome to pleroma: #{instance_name}",
html_body: "<h1>Hello #{user.name}.</h1> <p>Welcome to #{instance_name}</p>"
)
Config.put([:welcome, :email, :sender], {"Pleroma App", "welcome@pleroma.app"})
{:ok, _job} = WelcomeEmail.send_email(user)
ObanHelpers.perform_all()
assert_email_sent(
from: {"Pleroma App", "welcome@pleroma.app"},
to: {user.name, user.email}, to: {user.name, user.email},
subject: "Hello, welcome to pleroma: #{instance_name}", subject: "Hello, welcome to pleroma: #{instance_name}",
html_body: "<h1>Hello #{user.name}.</h1> <p>Welcome to #{instance_name}</p>" html_body: "<h1>Hello #{user.name}.</h1> <p>Welcome to #{instance_name}</p>"

View file

@ -414,7 +414,7 @@ defmodule Pleroma.UserTest do
Pleroma.Config.put([:welcome, :direct_message, :message], "Hello, this is a cool site") Pleroma.Config.put([:welcome, :direct_message, :message], "Hello, this is a cool site")
Pleroma.Config.put([:welcome, :email, :enabled], true) Pleroma.Config.put([:welcome, :email, :enabled], true)
Pleroma.Config.put([:welcome, :email, :sender_nickname], welcome_user.nickname) Pleroma.Config.put([:welcome, :email, :sender], welcome_user.email)
Pleroma.Config.put( Pleroma.Config.put(
[:welcome, :email, :subject], [:welcome, :email, :subject],