Skip to content

Commit

Permalink
fix: correct receiver and sender email addresses and generate confirm…
Browse files Browse the repository at this point in the history
…ation email (#193)
  • Loading branch information
Rafael-Mori-2022 authored Aug 28, 2024
1 parent 06cbfcc commit 64c59cd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ config :flop, repo: Pescarte.Database.Repo.Replica

config :pescarte, fetch_pesagro_cotacoes: !!System.get_env("FETCH_PESAGRO_COTACOES")

config :pescarte, PescarteWeb, receiver_email: "[email protected]"
config :pescarte, PescarteWeb, sender_email: "[email protected]"

config :pescarte, PescarteWeb, receiver_email: "[email protected]"

config :pescarte,
ecto_repos: [Pescarte.Database.Repo],
Expand Down
68 changes: 42 additions & 26 deletions lib/pescarte_web/controllers/contact_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PescarteWeb.ContactController do
require Logger
alias PescarteWeb.ContactForm

@sender_email Application.compile_env!(:pescarte, [PescarteWeb, :sender_email])
@receiver_email Application.compile_env!(:pescarte, [PescarteWeb, :receiver_email])

def show(conn, _params) do
Expand All @@ -16,45 +17,60 @@ defmodule PescarteWeb.ContactController do
{:ok, contact_form} ->
client = Resend.client()

email_data = %{
from: contact_form.sender_email,
to: @receiver_email,
subject: contact_form.sender_option,
html: """
<p><strong>Nome:</strong> #{contact_form.sender_name}</p>
<p><strong>Assunto:</strong> #{contact_form.sender_option}</p>
<p><strong>Mensagem:</strong> #{contact_form.sender_message}</p>
"""
}

case Resend.Emails.send(client, email_data) do
{:ok, email_response} ->
Logger.info("""
[#{__MODULE__}] ==> Sent email from contact form:
RESPONSE: #{inspect(email_response, pretty: true)}
LOG_UUID: #{Ecto.UUID.generate()}
""")

conn
|> put_flash(:info, "Email enviado com sucesso!")
|> redirect(~p"/")

with {:ok, _} <- send_email_to_pescarte(client, contact_form),
{:ok, _} <- send_confirmation_email(client, contact_form) do
conn
|> put_flash(:info, "Email enviado com sucesso!")
|> redirect(to: ~p"/")
else
{:error, reason} ->
Logger.error("""
[#{__MODULE__}] ==> Error sending email from contact form:
[#{__MODULE__}] ==> Error sending email:
REASON: #{inspect(reason, pretty: true)}
LOG_UUID: #{Ecto.UUID.generate()}
""")

conn
|> put_flash(:error, "Erro ao enviar email.")
|> redirect(~p"/")
|> redirect(to: ~p"/")
end

{:error, _changeset} ->
conn
|> put_flash(:error, "Erro na validação do formulário.")
|> redirect(~p"/")
|> redirect(to: ~p"/")
end
end

defp send_email_to_pescarte(client, contact_form) do
email_data_to_pescarte = %{
from: @sender_email,
to: @receiver_email,
subject: contact_form.form_sender_option,
html: """
<p><strong>Nome:</strong> #{contact_form.form_sender_name}</p>
<p><strong>Assunto:</strong> #{contact_form.form_sender_option}</p>
<p><strong>Mensagem:</strong> #{contact_form.form_sender_message}</p>
"""
}

Resend.Emails.send(client, email_data_to_pescarte)
end

defp send_confirmation_email(client, contact_form) do
email_data_to_form_sender = %{
from: @sender_email,
to: contact_form.form_sender_email,
subject: "Confirmação de recebimento do formulário",
html: """
<p>Olá, #{contact_form.form_sender_name},</p>
<p>Recebemos seu formulário com o assunto: <strong>#{contact_form.form_sender_option}</strong>.</p>
<p>Em breve retornaremos sua mensagem.</p>
<p>Atenciosamente,</p>
<p>Equipe Pescarte</p>
"""
}

Resend.Emails.send(client, email_data_to_form_sender)
end
end
26 changes: 18 additions & 8 deletions lib/pescarte_web/schemas/contact_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ defmodule PescarteWeb.ContactForm do

@primary_key false
embedded_schema do
field :sender_name, :string
field :sender_email, :string
field :sender_option, :string
field :sender_message, :string
field :form_sender_name, :string
field :form_sender_email, :string
field :form_sender_option, :string
field :form_sender_message, :string
end

def changeset(params) do
%__MODULE__{}
|> cast(params, [:sender_name, :sender_email, :sender_option, :sender_message])
|> validate_required([:sender_name, :sender_email, :sender_option, :sender_message])
|> validate_format(:sender_email, ~r/@/)
|> validate_length(:sender_message, min: 1)
|> cast(params, [
:form_sender_name,
:form_sender_email,
:form_sender_option,
:form_sender_message
])
|> validate_required([
:form_sender_name,
:form_sender_email,
:form_sender_option,
:form_sender_message
])
|> validate_format(:form_sender_email, ~r/@/)
|> validate_length(:form_sender_message, min: 1)
end

def apply_action_changeset(params, action) do
Expand Down
16 changes: 8 additions & 8 deletions lib/pescarte_web/templates/contact_html/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<.text size="lg" color="text-black-80">Nome</.text>
<input
type="text"
name="contact_form[sender_name]"
id="sender_name"
name="contact_form[form_sender_name]"
id="form_sender_name"
placeholder="João da Silva"
required
/>
Expand All @@ -17,15 +17,15 @@
<.text size="lg" color="text-black-80">Email</.text>
<input
type="email"
name="contact_form[sender_email]"
id="sender_email"
name="contact_form[form_sender_email]"
id="form_sender_email"
placeholder="[email protected]"
required
/>
</div>
<div class="input-area">
<.text size="lg" color="text-black-80">Assunto</.text>
<select id="sender_option" name="contact_form[sender_option]" required>
<select id="form_sender_option" name="contact_form[form_sender_option]" required>
<option value="Dúvida">Dúvida</option>
<option value="Comentário">Comentário</option>
<option value="Solicitação">Solicitação</option>
Expand All @@ -35,10 +35,10 @@
<div class="input-area">
<.text size="lg" color="text-black-80">Mensagem</.text>
<textarea
name="contact_form[sender_message]"
name="contact_form[form_sender_message]"
rows="10"
id="sender_message"
placeholder="Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quibusdam voluptates, nam provident inventore perferendis ea ipsum? Distinctio, pariatur accusamus. Facilis, deleniti vel repudiandae aliquam voluptatibus sed. Delectus, quae. Iusto, nam?"
id="form_sender_message"
placeholder="Digite aqui sua mensagem..."
required
></textarea>
</div>
Expand Down

0 comments on commit 64c59cd

Please sign in to comment.