Skip to content

Commit 164fee1

Browse files
committed
feat: add from_name and from_email fields to campaign form
1 parent 8ae2aa0 commit 164fee1

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

lib/algora/activities/jobs/send_campaign_email.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ defmodule Algora.Activities.Jobs.SendCampaignEmail do
1515
"recipient_email" => _recipient_email,
1616
"subject" => subject,
1717
"recipient" => encoded_recipient,
18-
"template_params" => encoded_template_params
18+
"template_params" => encoded_template_params,
19+
"from_name" => from_name,
20+
"from_email" => from_email
1921
}
2022
}) do
2123
recipient = Algora.Util.base64_to_term!(encoded_recipient)
2224
template_params = Algora.Util.base64_to_term!(encoded_template_params)
23-
25+
from = {from_name, from_email}
2426
token = Algora.Admin.token!()
2527

2628
with {:ok, repo} <- Workspace.ensure_repository(token, recipient["repo_owner"], recipient["repo_name"]),
2729
{:ok, _owner} <- Workspace.ensure_user(token, recipient["repo_owner"]),
2830
{:ok, _contributors} <- Workspace.ensure_contributors(token, repo),
2931
{:ok, _languages} <- Workspace.ensure_repo_tech_stack(token, repo) do
30-
CampaignLive.deliver_email(recipient, subject, template_params)
32+
CampaignLive.deliver_email(recipient, subject, template_params, from)
3133
end
3234
end
3335
end

lib/algora_web/live/admin/campaign_live.ex

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ defmodule AlgoraWeb.Admin.CampaignLive do
2121
field :subject, :string
2222
field :template, :string
2323
field :csv, :string
24+
field :from_name, :string
25+
field :from_email, :string
2426
end
2527

2628
def changeset(campaign, attrs \\ %{}) do
2729
campaign
28-
|> cast(attrs, [:subject, :template, :csv])
29-
|> validate_required([:subject, :template, :csv])
30+
|> cast(attrs, [:subject, :template, :csv, :from_name, :from_email])
31+
|> validate_required([:subject, :template, :csv, :from_name, :from_email])
3032
|> validate_length(:subject, min: 1)
3133
|> validate_length(:template, min: 1)
3234
|> validate_length(:csv, min: 1)
35+
|> validate_length(:from_name, min: 1)
36+
|> validate_format(:from_email, ~r/^[^\s]+@[^\s]+$/, message: "must be a valid email address")
3337
end
3438
end
3539

@@ -80,8 +84,10 @@ defmodule AlgoraWeb.Admin.CampaignLive do
8084
defp handle_send(recipients, socket) do
8185
subject = get_change(socket.assigns.form.source, :subject)
8286
template = get_change(socket.assigns.form.source, :template)
87+
from_name = get_change(socket.assigns.form.source, :from_name)
88+
from_email = get_change(socket.assigns.form.source, :from_email)
8389

84-
case enqueue_emails(recipients, subject, template) do
90+
case enqueue_emails(recipients, subject, template, from_name, from_email) do
8591
{:ok, _} ->
8692
{:noreply, put_flash(socket, :info, "Enqueued #{length(recipients)} emails for sending")}
8793

@@ -108,6 +114,10 @@ defmodule AlgoraWeb.Admin.CampaignLive do
108114
<.form for={@form} phx-change="preview" class="space-y-4">
109115
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
110116
<div class="space-y-6">
117+
<div class="grid grid-cols-2 gap-4">
118+
<.input type="text" field={@form[:from_name]} label="From Name" />
119+
<.input type="email" field={@form[:from_email]} label="From Email" />
120+
</div>
111121
<.input type="text" field={@form[:subject]} label="Subject" />
112122
113123
<.input
@@ -127,6 +137,15 @@ defmodule AlgoraWeb.Admin.CampaignLive do
127137
<dt class="font-bold">Subject:</dt>
128138
<dd>{get_change(@form.source, :subject)}</dd>
129139
</div>
140+
<div class="flex gap-1">
141+
<dt class="font-bold">From:</dt>
142+
<dd>
143+
{get_change(@form.source, :from_name)} &lt;{get_change(
144+
@form.source,
145+
:from_email
146+
)}&gt;
147+
</dd>
148+
</div>
130149
</dl>
131150
<div class="-mt-4">
132151
<%= if @preview do %>
@@ -260,9 +279,15 @@ defmodule AlgoraWeb.Admin.CampaignLive do
260279
assign(socket, :preview, render_preview(template, List.first(socket.assigns.csv_data)))
261280
end
262281

263-
@spec enqueue_emails(recipients :: list(), subject :: String.t(), template :: String.t()) ::
282+
@spec enqueue_emails(
283+
recipients :: list(),
284+
subject :: String.t(),
285+
template :: String.t(),
286+
from_name :: String.t(),
287+
from_email :: String.t()
288+
) ::
264289
{:ok, term} | {:error, term}
265-
def enqueue_emails(recipients, subject, template) do
290+
def enqueue_emails(recipients, subject, template, from_name, from_email) do
266291
Repo.transact(fn _ ->
267292
recipients
268293
|> Enum.map(fn recipient ->
@@ -279,7 +304,9 @@ defmodule AlgoraWeb.Admin.CampaignLive do
279304
subject: subject,
280305
recipient_email: recipient["email"],
281306
recipient: Algora.Util.term_to_base64(recipient),
282-
template_params: Algora.Util.term_to_base64(template_params)
307+
template_params: Algora.Util.term_to_base64(template_params),
308+
from_name: from_name,
309+
from_email: from_email
283310
}
284311
end)
285312
|> Enum.with_index()
@@ -292,14 +319,19 @@ defmodule AlgoraWeb.Admin.CampaignLive do
292319
end)
293320
end
294321

295-
@spec deliver_email(recipient :: map(), subject :: String.t(), template_params :: Keyword.t()) ::
322+
@spec deliver_email(
323+
recipient :: map(),
324+
subject :: String.t(),
325+
template_params :: Keyword.t(),
326+
from :: {String.t(), String.t()}
327+
) ::
296328
{:ok, term} | {:error, term}
297-
def deliver_email(recipient, subject, template_params) do
329+
def deliver_email(recipient, subject, template_params, from) do
298330
case :get
299331
|> Finch.build("https://algora.io/og/go/#{recipient["repo_owner"]}/#{recipient["repo_name"]}")
300332
|> Finch.request(Algora.Finch) do
301333
{:ok, %Finch.Response{status: status, body: body}} when status in 200..299 ->
302-
deliver(recipient["email"], subject, template_params, [
334+
deliver(recipient["email"], subject, template_params, from, [
303335
Swoosh.Attachment.new({:data, body},
304336
filename: "#{recipient["repo_owner"]}.png",
305337
content_type: "image/png",
@@ -312,11 +344,11 @@ defmodule AlgoraWeb.Admin.CampaignLive do
312344
end
313345
end
314346

315-
defp deliver(to, subject, template_params, attachments) do
347+
defp deliver(to, subject, template_params, from, attachments) do
316348
email =
317349
Email.new()
318350
|> Email.to(to)
319-
|> Email.from({"Ioannis R. Florokapis", "[email protected]"})
351+
|> Email.from(from)
320352
|> Email.subject(subject)
321353
|> Email.text_body(Mailer.text_template(template_params))
322354
|> Email.html_body(Mailer.html_template(template_params))

0 commit comments

Comments
 (0)