Skip to content

Commit 8e8b100

Browse files
committed
feat: add preheader field to campaign form and email delivery
1 parent 164fee1 commit 8e8b100

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lib/algora/activities/jobs/send_campaign_email.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ defmodule Algora.Activities.Jobs.SendCampaignEmail do
1717
"recipient" => encoded_recipient,
1818
"template_params" => encoded_template_params,
1919
"from_name" => from_name,
20-
"from_email" => from_email
20+
"from_email" => from_email,
21+
"preheader" => preheader
2122
}
2223
}) do
2324
recipient = Algora.Util.base64_to_term!(encoded_recipient)
@@ -29,7 +30,7 @@ defmodule Algora.Activities.Jobs.SendCampaignEmail do
2930
{:ok, _owner} <- Workspace.ensure_user(token, recipient["repo_owner"]),
3031
{:ok, _contributors} <- Workspace.ensure_contributors(token, repo),
3132
{:ok, _languages} <- Workspace.ensure_repo_tech_stack(token, repo) do
32-
CampaignLive.deliver_email(recipient, subject, template_params, from)
33+
CampaignLive.deliver_email(recipient, subject, template_params, from, preheader: preheader)
3334
end
3435
end
3536
end

lib/algora/mailer.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Algora.Mailer do
2222
end
2323
end
2424

25-
def html_template(template_params) do
25+
def html_template(template_params, opts \\ []) do
2626
"""
2727
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2828
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
@@ -37,6 +37,9 @@ defmodule Algora.Mailer do
3737
</xml><![endif]-->
3838
</head>
3939
<body style="margin: 0; padding: 0; min-width: 100%; background-color: #ffffff;">
40+
<div class="preheader" style="display: none; max-width: 0; max-height: 0; overflow: hidden; font-size: 1px; line-height: 1px; color: #fff; opacity: 0;">
41+
#{opts[:preheader]}
42+
</div>
4043
<div style="background-color: #ffffff; box-sizing: border-box; display: block; padding: 0;">
4144
<table cellpadding="0" cellspacing="0" width="100%">
4245
<tr>

lib/algora_web/live/admin/campaign_live.ex

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ defmodule AlgoraWeb.Admin.CampaignLive do
2323
field :csv, :string
2424
field :from_name, :string
2525
field :from_email, :string
26+
field :preheader, :string
2627
end
2728

2829
def changeset(campaign, attrs \\ %{}) do
2930
campaign
30-
|> cast(attrs, [:subject, :template, :csv, :from_name, :from_email])
31+
|> cast(attrs, [:subject, :template, :csv, :from_name, :from_email, :preheader])
3132
|> validate_required([:subject, :template, :csv, :from_name, :from_email])
3233
|> validate_length(:subject, min: 1)
3334
|> validate_length(:template, min: 1)
@@ -86,8 +87,9 @@ defmodule AlgoraWeb.Admin.CampaignLive do
8687
template = get_change(socket.assigns.form.source, :template)
8788
from_name = get_change(socket.assigns.form.source, :from_name)
8889
from_email = get_change(socket.assigns.form.source, :from_email)
90+
preheader = get_change(socket.assigns.form.source, :preheader)
8991

90-
case enqueue_emails(recipients, subject, template, from_name, from_email) do
92+
case enqueue_emails(recipients, subject, template, from_name, from_email, preheader) do
9193
{:ok, _} ->
9294
{:noreply, put_flash(socket, :info, "Enqueued #{length(recipients)} emails for sending")}
9395

@@ -119,6 +121,12 @@ defmodule AlgoraWeb.Admin.CampaignLive do
119121
<.input type="email" field={@form[:from_email]} label="From Email" />
120122
</div>
121123
<.input type="text" field={@form[:subject]} label="Subject" />
124+
<.input
125+
type="text"
126+
field={@form[:preheader]}
127+
label="Preheader"
128+
helptext="A short summary that appears after the subject line in email clients"
129+
/>
122130
123131
<.input
124132
type="textarea"
@@ -137,6 +145,10 @@ defmodule AlgoraWeb.Admin.CampaignLive do
137145
<dt class="font-bold">Subject:</dt>
138146
<dd>{get_change(@form.source, :subject)}</dd>
139147
</div>
148+
<div class="flex gap-1">
149+
<dt class="font-bold">Preheader:</dt>
150+
<dd class="line-clamp-1">{get_change(@form.source, :preheader)}</dd>
151+
</div>
140152
<div class="flex gap-1">
141153
<dt class="font-bold">From:</dt>
142154
<dd>
@@ -284,10 +296,11 @@ defmodule AlgoraWeb.Admin.CampaignLive do
284296
subject :: String.t(),
285297
template :: String.t(),
286298
from_name :: String.t(),
287-
from_email :: String.t()
299+
from_email :: String.t(),
300+
preheader :: String.t()
288301
) ::
289302
{:ok, term} | {:error, term}
290-
def enqueue_emails(recipients, subject, template, from_name, from_email) do
303+
def enqueue_emails(recipients, subject, template, from_name, from_email, preheader) do
291304
Repo.transact(fn _ ->
292305
recipients
293306
|> Enum.map(fn recipient ->
@@ -306,7 +319,8 @@ defmodule AlgoraWeb.Admin.CampaignLive do
306319
recipient: Algora.Util.term_to_base64(recipient),
307320
template_params: Algora.Util.term_to_base64(template_params),
308321
from_name: from_name,
309-
from_email: from_email
322+
from_email: from_email,
323+
preheader: preheader
310324
}
311325
end)
312326
|> Enum.with_index()
@@ -323,35 +337,43 @@ defmodule AlgoraWeb.Admin.CampaignLive do
323337
recipient :: map(),
324338
subject :: String.t(),
325339
template_params :: Keyword.t(),
326-
from :: {String.t(), String.t()}
340+
from :: {String.t(), String.t()},
341+
opts :: Keyword.t()
327342
) ::
328343
{:ok, term} | {:error, term}
329-
def deliver_email(recipient, subject, template_params, from) do
344+
def deliver_email(recipient, subject, template_params, from, opts \\ []) do
330345
case :get
331346
|> Finch.build("https://algora.io/og/go/#{recipient["repo_owner"]}/#{recipient["repo_name"]}")
332347
|> Finch.request(Algora.Finch) do
333348
{:ok, %Finch.Response{status: status, body: body}} when status in 200..299 ->
334-
deliver(recipient["email"], subject, template_params, from, [
335-
Swoosh.Attachment.new({:data, body},
336-
filename: "#{recipient["repo_owner"]}.png",
337-
content_type: "image/png",
338-
type: :inline
339-
)
340-
])
349+
deliver(
350+
recipient["email"],
351+
subject,
352+
template_params,
353+
from,
354+
[
355+
Swoosh.Attachment.new({:data, body},
356+
filename: "#{recipient["repo_owner"]}.png",
357+
content_type: "image/png",
358+
type: :inline
359+
)
360+
],
361+
opts
362+
)
341363

342364
{:error, reason} ->
343365
raise reason
344366
end
345367
end
346368

347-
defp deliver(to, subject, template_params, from, attachments) do
369+
defp deliver(to, subject, template_params, from, attachments, opts \\ []) do
348370
email =
349371
Email.new()
350372
|> Email.to(to)
351373
|> Email.from(from)
352374
|> Email.subject(subject)
353375
|> Email.text_body(Mailer.text_template(template_params))
354-
|> Email.html_body(Mailer.html_template(template_params))
376+
|> Email.html_body(Mailer.html_template(template_params, preheader: opts[:preheader]))
355377

356378
email = Enum.reduce(attachments, email, fn attachment, acc -> Email.attachment(acc, attachment) end)
357379

0 commit comments

Comments
 (0)