add send test email button to campaign form
All checks were successful
deploy / deploy (push) Successful in 1m22s
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:
@@ -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")
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user