Last active
February 12, 2022 15:02
-
-
Save laurenfackler/9f564e6cfa3b39f2f360c4fd70106557 to your computer and use it in GitHub Desktop.
Elixir/Phoenix email preview controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyAppWeb.EmailPreviewController do | |
use MyAppWeb, :controller | |
plug(:put_layout, false) | |
def index(conn, _) do | |
emails = | |
:functions | |
|> Emails.__info__() | |
|> Keyword.keys() | |
|> Enum.uniq() | |
|> List.delete(:render) | |
render(conn, :index, emails: Enum.sort(emails)) | |
end | |
def show(conn, %{"email" => email_name}) do | |
email = apply(__MODULE__, String.to_existing_atom(email_name), []) | |
render(conn, :show, %{email: email}) | |
end | |
def appointment_booked_patient do | |
appointment = get_booked_appointment() | |
Emails.appointment_booked_patient(appointment) | |
end | |
defp get_booked_appointment do | |
Appointment | |
|> Appointments.booked() | |
|> limit(1) | |
|> Repo.one!() | |
end | |
end | |
# in lib/my_app_web/templates/email_preview/index.html.erb | |
<html> | |
<head> | |
<title>Email Previews</title> | |
</head> | |
<body> | |
<ul> | |
<%= for email <- @emails do %> | |
<li><a href="<%= "/email_previews/#{to_string(email)}" %>"><%= email %></a></li> | |
<% end %> | |
</ul> | |
</body> | |
</html> | |
# in lib/my_app_web/templates/email_preview/show.html.erb | |
<div style="text-align:center">Subject: <strong><%= @email.subject %></strong></div> | |
<%= raw(@email.html_body) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment