add send test email button to campaign form
All checks were successful
deploy / deploy (push) Successful in 1m22s

Sends the campaign to the admin's own email address as a preview,
with [Test] prefix in subject line. Uses the same HTML template
and formatting as real sends. Does not affect campaign status or
sent counts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey 2026-02-28 23:52:11 +00:00
parent aa008f83b2
commit 7f6fd012a5
3 changed files with 67 additions and 0 deletions

View File

@ -42,6 +42,27 @@ defmodule Berrypod.Newsletter.Notifier do
deliver(subscriber.email, "Confirm your subscription", text, html)
end
@doc "Sends a test/preview email of a campaign to the given address."
def deliver_test(campaign, to_email) do
shop_name = Berrypod.Settings.get_setting("shop_name", "Berrypod")
sample_unsub = BerrypodWeb.Endpoint.url() <> "/unsubscribe/test-preview"
body_with_url =
String.replace(campaign.body, "{{unsubscribe_url}}", sample_unsub)
text = """
[TEST] #{body_with_url}
---
This is a test email. No subscribers received this.
"""
html_content = text_to_html(body_with_url, sample_unsub)
html = wrap_html(shop_name, html_content, sample_unsub)
deliver(to_email, "[Test] #{campaign.subject}", text, html)
end
@doc "Sends a campaign email to a single subscriber."
def deliver_campaign(campaign, subscriber) do
shop_name = Berrypod.Settings.get_setting("shop_name", "Berrypod")

View File

@ -74,6 +74,29 @@ defmodule BerrypodWeb.Admin.Newsletter.CampaignForm do
end
end
def handle_event("send_test", _params, socket) do
params = current_form_params(socket)
to_email = socket.assigns.current_scope.user.email
if params["subject"] == "" || params["body"] == "" do
{:noreply, put_flash(socket, :error, "Fill in subject and body first")}
else
# Build a temporary campaign struct for the notifier
campaign = %Newsletter.Campaign{
subject: params["subject"],
body: params["body"]
}
case Newsletter.Notifier.deliver_test(campaign, to_email) do
{:ok, _} ->
{:noreply, put_flash(socket, :info, "Test email sent to #{to_email}")}
{:error, _} ->
{:noreply, put_flash(socket, :error, "Failed to send test email")}
end
end
end
def handle_event("schedule", _params, socket) do
params = current_form_params(socket)
scheduled_at = parse_schedule_time(params["scheduled_at"])
@ -184,6 +207,14 @@ defmodule BerrypodWeb.Admin.Newsletter.CampaignForm do
Save draft
</.button>
<button
type="button"
phx-click="send_test"
class="admin-btn admin-btn-ghost"
>
<.icon name="hero-envelope" class="size-4" /> Send test
</button>
<button
type="button"
phx-click="send_now"

View File

@ -82,6 +82,21 @@ defmodule Berrypod.Newsletter.NotifierTest do
end
end
describe "deliver_test/2" do
test "sends test email with [Test] prefix in subject" do
campaign = campaign_fixture(subject: "Big launch", body: "Preview this!\n\n{{unsubscribe_url}}")
assert {:ok, _} = Notifier.deliver_test(campaign, "admin@example.com")
assert_email_sent(fn email ->
assert email.to == [{"", "admin@example.com"}]
assert email.subject == "[Test] Big launch"
assert email.text_body =~ "Preview this!"
assert email.html_body =~ "<!DOCTYPE html>"
end)
end
end
describe "text_to_html/1" do
test "wraps paragraphs split by blank lines" do
html = Notifier.text_to_html("First paragraph.\n\nSecond paragraph.")