2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Email.User do
|
2018-07-04 17:16:02 +02:00
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent to users
|
|
|
|
"""
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Users.User
|
2018-07-04 14:29:17 +02:00
|
|
|
|
|
|
|
import Bamboo.Email
|
|
|
|
import Bamboo.Phoenix
|
2018-10-11 17:37:39 +02:00
|
|
|
use Bamboo.Phoenix, view: Mobilizon.EmailView
|
|
|
|
import MobilizonWeb.Gettext
|
2018-07-04 14:29:17 +02:00
|
|
|
|
|
|
|
def confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
instance_url = get_config(:instance)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
base_email()
|
|
|
|
|> to(user.email)
|
2018-07-27 10:45:35 +02:00
|
|
|
|> subject(
|
2018-11-06 10:30:27 +01:00
|
|
|
gettext("Mobilizon: Confirmation instructions for %{instance}", instance: instance_url)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> put_header("Reply-To", get_config(:reply_to))
|
|
|
|
|> assign(:token, user.confirmation_token)
|
|
|
|
|> assign(:instance, instance_url)
|
|
|
|
|> render(:registration_confirmation)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password_email(%User{} = user, locale \\ "en") do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
instance_url = get_config(:instance)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
base_email()
|
|
|
|
|> to(user.email)
|
2018-07-27 10:45:35 +02:00
|
|
|
|> subject(
|
|
|
|
gettext(
|
2018-11-06 10:30:27 +01:00
|
|
|
"Mobilizon: Reset your password on %{instance} instructions",
|
2018-07-27 10:45:35 +02:00
|
|
|
instance: instance_url
|
|
|
|
)
|
|
|
|
)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> put_header("Reply-To", get_config(:reply_to))
|
|
|
|
|> assign(:token, user.reset_password_token)
|
|
|
|
|> assign(:instance, instance_url)
|
|
|
|
|> render(:password_reset)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp base_email do
|
|
|
|
# Here you can set a default from, default headers, etc.
|
|
|
|
new_email()
|
2018-10-11 17:37:39 +02:00
|
|
|
|> from(Application.get_env(:mobilizon, MobilizonWeb.Endpoint)[:email_from])
|
|
|
|
|> put_html_layout({Mobilizon.EmailView, "email.html"})
|
|
|
|
|> put_text_layout({Mobilizon.EmailView, "email.text"})
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_config(atom()) :: any()
|
|
|
|
defp get_config(key) do
|
2018-10-11 17:37:39 +02:00
|
|
|
_config = Application.get_env(:mobilizon, MobilizonWeb.Endpoint) |> Keyword.get(key)
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
end
|