-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from cciuenf/feature/mailer
feature/mailer
- Loading branch information
Showing
14 changed files
with
608 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
# ---------------------------# | ||
# Sentry | ||
# ---------------------------# | ||
SENTRY_DNS= | ||
SENTRY_ENV= | ||
|
||
# ---------------------------# | ||
# Oban | ||
# ---------------------------# | ||
START_BAN_JOBS= | ||
|
||
# ---------------------------# | ||
# Google Cloud | ||
# ---------------------------# | ||
GCP_CLIENT_ID= | ||
GCP_CLIENT_SECRET= | ||
|
||
# ---------------------------# | ||
# Mailer | ||
# ---------------------------# | ||
MAILSERVICE= | ||
MAIL_SERVER= | ||
MAIL_USERNAME= | ||
MAIL_PASSWORD= | ||
MAIL_PORT= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,48 @@ docker-compose run --rm fuschia mix ecto.setup | |
|
||
----- | ||
|
||
** Aplicações | ||
|
||
Esse projeto está dividio em diversas sub-aplicações que possuem diferentes responsabilidades. | ||
|
||
#+begin_example | ||
|
||
#+end_example | ||
|
||
*** Fuschia.Mailer | ||
|
||
Responsável pelo processamento e envio/disparo dos emails. Estamos utilizando o [[Swoosh][https://github.com/swoosh/swoosh]]. | ||
|
||
Para testar o preview de email, siga a seguinte documentação: | ||
|
||
**** Variáveis de ambiente | ||
Necessárias em produção: | ||
- =MAIL_SERVER=: Server do smtp (default: =smtp.gmail.com=) | ||
- =MAIL_USERNAME=: User do smtp (default: [email protected]=) | ||
- =MAIL_PASSWORD=: Senha do smtp | ||
- =MAIL_PORT=: Porta do smtp (default: =587=) | ||
- =MAIL_SERVICE=: O serviço de email a ser usado. Pode ser =gmail= ou =local=. | ||
(default prod: =gmail=, default dev: =local=) | ||
|
||
*Observação*: Em ambiente de desenvolvimento, toda vez que a variável de ambiente =MAIL_SERVICE= é alterada | ||
para trocar o adapter, toda a aplicação deve ser compilada usando | ||
|
||
#+begin_src sh | ||
mix compile --force | ||
#+end_src | ||
|
||
**** Rodando localmente | ||
Essa aplicação também conta com um servidor para visualizar os emails enviados usando o adaptador local, | ||
basta entrar na aplicação/container e utilizar: | ||
|
||
#+begin_src sh | ||
iex -S mix swoosh.mailbox.server | ||
#+end_src | ||
|
||
Que um servidor no local https://127.0.0.1:4001 vai apresentar uma página web listando os emails | ||
enviados localmente através do `iex` que ficou aberto com o comando anterior. | ||
|
||
----- | ||
|
||
** Rodando os testes | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,37 @@ config :sentry, | |
# ---------------------------# | ||
config :fuschia, Oban, | ||
repo: Fuschia.Repo, | ||
queues: [mailers: 5] | ||
queues: [mailer: 5] | ||
|
||
config :fuschia, :jobs, start: System.get_env("START_OBAN_JOBS", "true") | ||
|
||
# ---------------------------# | ||
# Mailer | ||
# ---------------------------# | ||
adapter = | ||
case System.get_env("MAIL_SERVICE", "local") do | ||
"gmail" -> Swoosh.Adapters.SMTP | ||
_ -> Swoosh.Adapters.Local | ||
end | ||
|
||
if adapter == Swoosh.Adapters.Local do | ||
config :swoosh, serve_mailbox: true, preview_port: 4001 | ||
end | ||
|
||
config :fuschia, Fuschia.Mailer, | ||
adapter: adapter, | ||
relay: System.get_env("MAIL_SERVER", "smtp.gmail.com"), | ||
username: System.get_env("MAIL_USERNAME", "[email protected]"), | ||
password: System.get_env("MAIL_PASSWORD", ""), | ||
ssl: false, | ||
tls: :always, | ||
auth: :always, | ||
port: System.get_env("MAIL_PORT", "587") | ||
|
||
config :fuschia, :pea_pescarte_contact, | ||
notifications_mail: "[email protected]", | ||
telephone: " 0800 026 2828" | ||
|
||
# ---------------------------# | ||
# Timex | ||
# ---------------------------# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Fuschia.Jobs.MailerJob do | ||
@moduledoc false | ||
|
||
use Oban.Worker, queue: :mailer, max_attempts: 4 | ||
|
||
require Logger | ||
|
||
alias Fuschia.{Mailer, Parser} | ||
|
||
@doc """ | ||
Deliver an email to partner | ||
Example: | ||
iex> %{ | ||
to: "[email protected]", | ||
subject: "some subject", | ||
layout: "notificacao", | ||
template: "nova_midia", | ||
assigns: %{user_cpf: "999.999.999-99"} | ||
} | ||
|> Fuschia.Jobs.MailerJob.new() | ||
|> Oban.insert!() | ||
""" | ||
@impl Oban.Worker | ||
def perform(%{args: args}) do | ||
Logger.info("==> [MailerJob] Sending email...") | ||
|
||
Mailer.new_email( | ||
args["to"], | ||
args["subject"], | ||
args["layout"], | ||
args["template"], | ||
Parser.atomize_map(args["assigns"]), | ||
args["base"], | ||
args["bcc"] | ||
) | ||
|> Mailer.deliver!() | ||
|
||
Logger.info("==> [MailerJob] Sent email") | ||
|
||
:ok | ||
rescue | ||
error -> | ||
Logger.error(Exception.format(:error, error, __STACKTRACE__)) | ||
{:error, error} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Fuschia.Mailer do | ||
@moduledoc """ | ||
Mailer public API | ||
""" | ||
|
||
use Swoosh.Mailer, otp_app: :fuschia | ||
|
||
alias Fuschia.Mailer.HTML | ||
alias Swoosh.Email | ||
|
||
@doc """ | ||
Returns an email structure populated with a `recipient` and a | ||
`subject` and assembles the email's html body based on the given | ||
templates `layout` and `email` and given `assigns`. | ||
""" | ||
@spec new_email( | ||
String.t() | {String.t(), String.t()} | [String.t()] | [{String.t(), String.t()}], | ||
String.t(), | ||
String.t(), | ||
String.t(), | ||
map, | ||
String.t(), | ||
String.t() | {String.t(), String.t()} | [String.t()] | [{String.t(), String.t()}] | [] | ||
) :: Email.t() | ||
def new_email(recipient, subject, layout, template, assigns \\ %{}, base \\ "base", bcc \\ []) | ||
when is_map(assigns) do | ||
body = HTML.assemble_body(layout, template, assigns, base) | ||
|
||
Email.new() | ||
|> Email.to(recipient) | ||
|> Email.bcc(bcc) | ||
|> Email.from({"Plataforma PEA Pescarte", notifications_mail()}) | ||
|> Email.subject("[Plataforma PEA Pescarte] #{subject}") | ||
|> Email.html_body(body) | ||
end | ||
|
||
@doc """ | ||
Add a new attachment to the email. | ||
""" | ||
@spec add_attachment(Email.t(), String.t()) :: Email.t() | ||
def add_attachment(%Email{} = struct, file) when is_binary(file) do | ||
Email.attachment(struct, file) | ||
end | ||
|
||
defp notifications_mail do | ||
Application.get_env(:fuschia, :pea_pescarte_contact)[:notifications_mail] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Fuschia.Mailer.HTML do | ||
@moduledoc false | ||
|
||
@doc """ | ||
Inject the `assigns` values into the `email` template's .eex tags | ||
from the "homologacao" or "financiamento" `project` and then render | ||
the resulting HTML page into a base template. | ||
Returns an HTML page in string format. | ||
""" | ||
@spec assemble_body(String.t(), String.t(), map) :: any | ||
def assemble_body(project, email, assigns, base \\ "base") when is_map(assigns) do | ||
assigns | ||
|> rewrite_name() | ||
|> rewrite_email_address() | ||
|> render_email(project, email) | ||
|> render_layout(base) | ||
end | ||
|
||
def templates_path, do: "#{:code.priv_dir(:fuschia)}/templates" | ||
|
||
defp pea_pescarte_contact do | ||
:fuschia | ||
|> Application.get_env(:pea_pescarte_contact) | ||
|> Map.new() | ||
end | ||
|
||
defp rewrite_name(%{user_nome_completo: name} = assigns), do: Map.put(assigns, :nome, name) | ||
defp rewrite_name(%{nome: name} = assigns), do: Map.put(assigns, :nome, name) | ||
defp rewrite_name(assigns), do: Map.put(assigns, :nome, "") | ||
|
||
defp rewrite_email_address(%{email: email} = assigns), do: Map.put(assigns, :email, email) | ||
defp rewrite_email_address(assigns), do: Map.put(assigns, :email, "") | ||
|
||
defp render_email(assigns, project, email) do | ||
EEx.eval_file("#{templates_path()}/email/#{project}/#{email}.html.eex", | ||
assigns: assigns, | ||
pea_pescarte: pea_pescarte_contact() | ||
) | ||
end | ||
|
||
defp render_layout(inner_content, nil), do: render_layout(inner_content, "base") | ||
|
||
defp render_layout(inner_content, base) do | ||
EEx.eval_file("#{templates_path()}/layout/#{base}.html.eex", | ||
assigns: [inner_content: inner_content] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<p>Olá, <strong><%= @nome %></strong>!</p> | ||
<p>Você recebeu um convite para se cadastrar na plataforma PEA Pescarte!</p> | ||
<p>Ao clicar no link abaixo, você irá se cadastrar como pesquisador!</p> | ||
<a | ||
href="<%= @link %>" | ||
title="CONFIRMAR CADASTRO" | ||
style=" | ||
display: inline-block; | ||
text-align: center; | ||
height: 40px; | ||
background: #0E4771; | ||
border-radius: 4px; | ||
text-decoration: none; | ||
font-size: 14px; | ||
color: #333333; | ||
font-weight: 500; | ||
margin-top: 8px; | ||
padding: 0 24px; | ||
line-height: 40px; | ||
text-transform: uppercase; | ||
" | ||
> | ||
<b>CONFIRMAR CADASTRO</b> | ||
</a> |
Oops, something went wrong.