Packages
blossom
0.29.2
0.29.3
0.29.2
0.29.1
0.29.0
0.28.0
0.27.3
0.27.2
0.27.1
0.27.0
0.26.3
0.26.2
0.26.1
0.26.0
0.25.3
0.25.2
0.25.1
0.25.0
0.24.1
0.24.0
0.23.5
0.23.4
0.23.3
0.23.2
0.23.1
0.23.0
0.22.1
0.22.0
0.21.8
0.21.7
0.21.6
0.21.5
0.21.4
0.21.3
0.21.2
0.21.1
0.21.0
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.15.2
0.15.1
0.15.0
0.14.0
0.13.0
0.12.2
0.12.1
0.12.0
0.11.0
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.0
0.7.0
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Easy identity for easy authentication
Current section
Files
Jump to
Current section
Files
lib/blossom/mail.ex
defmodule Blossom.Mail do
alias Blossom.Mail
alias Blossom.Controllers.Settings
alias Swoosh.Email
@default_text %{
password_reset:
"Hello <%= user.username %>,\nyour password reset code for <%= realm.name %> is <%= code.code %>\n",
verification:
"Hello <%= user.username %>,\nyour verification code for <%= realm.name %> is <%= code.code %>\n"
}
def default_text(), do: @default_text
def get_adapter(settings) do
provider = Settings.find_value(settings, "mail_provider")
if provider do
Module.concat([Swoosh, Adapters, String.capitalize(provider)])
end
end
def get_api_key(settings) do
Settings.find_value(settings, "mail_api_key")
end
def get_sender(settings) do
Settings.find_value(settings, "mail_sender")
end
def get_text(settings, kind) do
text = Settings.find_value(settings, "mail_#{kind}_text")
if text == nil do
@default_text[kind]
else
text
end
end
def get_html(settings, kind) do
Settings.find_value(settings, "mail_#{kind}_html")
end
def get_config(realm, settings, kind) do
[
adapter: Mail.get_adapter(settings),
api_key: Mail.get_api_key(settings),
domain: realm.domain,
sender: Mail.get_sender(settings),
text: Mail.get_text(settings, kind),
html: Mail.get_html(settings, kind)
]
end
def can_send_mail?(config) do
if config[:adapter] != nil && config[:api_key] != nil do
true
else
false
end
end
def text(email, config, context) do
email
|> Email.text_body(EEx.eval_string(config[:text], context))
end
def html(email, config, context) do
if config[:html] != nil do
email
|> Email.html_body(EEx.eval_string(config[:html], context))
else
email
end
end
def head(config, context) do
Email.new()
|> Email.to({context[:user].username, context[:user].email})
|> Email.from({context[:realm].name, config[:sender]})
end
def build(subject, config, context) do
config
|> Mail.head(context)
|> Email.subject(subject)
|> Mail.text(config, context)
|> Mail.html(config, context)
end
def send(subject, caller, config, context) do
if Mail.can_send_mail?(config) do
subject
|> Mail.build(config, context)
|> caller.deliver(config)
else
{:ok, nil}
end
end
def email_verification(caller, code, user, realm, settings) do
config = Mail.get_config(realm, settings, :verification)
context = [code: code, user: user, realm: realm]
subject = "Verify your email address"
Mail.send(subject, caller, config, context)
end
def password_reset(caller, code, user, realm, settings) do
config = Mail.get_config(realm, settings, :password_reset)
context = [code: code, user: user, realm: realm]
subject = "Reset your password"
Mail.send(subject, caller, config, context)
end
defmacro __using__(opts \\ []) do
quote do
@opts unquote(opts)
@caller unquote(__CALLER__.module)
use Swoosh.Mailer, otp_app: @opts[:otp_app]
alias Swoosh.Email
def email_verification(code, user, realm, settings) do
Mail.email_verification(@caller, code, user, realm, settings)
end
def password_reset(code, user, realm, settings) do
Mail.password_reset(@caller, code, user, realm, settings)
end
end
end
end