Add bin/test-email release task to verify SMTP delivery

f68263a · gmorell · 2026-06-23 17:20

7 files +56 -0
Message
{commit_body(@commit)}

Files changed

modified lib/feed_pug/release.ex
+44 −0
@@ -29,6 +29,50 @@ defmodule FeedPug.Release do
29 29 {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
30 30 end
31 31
32 + @doc """
33 + Sends a test email through the configured mailer, to verify SMTP delivery in a
34 + deployed release. Recipient comes from the argument or the `TEST_EMAIL` env.
35 +
36 + bin/feed_pug eval 'FeedPug.Release.send_test_email("you@example.com")'
37 + # or: bin/test-email you@example.com
38 + """
39 + def send_test_email(recipient \\ System.get_env("TEST_EMAIL")) do
40 + load_app()
41 + {:ok, _} = Application.ensure_all_started(:swoosh)
42 + _ = Application.ensure_all_started(:gen_smtp)
43 +
44 + to =
45 + recipient ||
46 + raise "no recipient — pass one or set TEST_EMAIL (e.g. bin/test-email you@example.com)"
47 +
48 + config = Application.get_env(@app, FeedPug.Mailer, [])
49 + IO.puts("Mailer adapter: #{inspect(Keyword.get(config, :adapter))}")
50 + IO.puts("Sending test email to #{to} …")
51 +
52 + email =
53 + Swoosh.Email.new()
54 + |> Swoosh.Email.to(to)
55 + |> Swoosh.Email.from(
56 + {Keyword.get(config, :from_name, "FeedPug"),
57 + Keyword.get(config, :from_address, "noreply@localhost")}
58 + )
59 + |> Swoosh.Email.subject("FeedPug SMTP test")
60 + |> Swoosh.Email.text_body(
61 + "Test email from FeedPug at #{DateTime.utc_now()}.\n" <>
62 + "If you received this, outbound mail delivery is working.\n"
63 + )
64 +
65 + case FeedPug.Mailer.deliver(email) do
66 + {:ok, meta} ->
67 + IO.puts("✓ delivered: #{inspect(meta)}")
68 + :ok
69 +
70 + {:error, reason} ->
71 + IO.puts("✗ delivery failed: #{inspect(reason)}")
72 + System.halt(1)
73 + end
74 + end
75 +
32 76 defp repos do
33 77 Application.fetch_env!(@app, :ecto_repos)
34 78 end
added rel/overlays/bin/test-email
+12 −0
@@ -0,0 +1,12 @@
1 +#!/bin/sh
2 +set -eu
3 +
4 +cd -P -- "$(dirname -- "$0")"
5 +
6 +# Usage: bin/test-email [recipient]
7 +# Falls back to the TEST_EMAIL env var when no argument is given.
8 +if [ "$#" -ge 1 ]; then
9 + exec ./feed_pug eval "FeedPug.Release.send_test_email(\"$1\")"
10 +else
11 + exec ./feed_pug eval "FeedPug.Release.send_test_email()"
12 +fi

Parents: a9a7933