Skip to content

Commit

Permalink
Rename EmailPreviewPlug to SentEmailViewerPlug
Browse files Browse the repository at this point in the history
The new name is more in line with what the plug is actually doing and
allows us to add an EmailPreviewPlug in the future if we so choose.
  • Loading branch information
drapergeek committed Mar 24, 2017
1 parent cf5724d commit f366845
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 58 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Flexible and easy to use email for Elixir.
* **Very composable**. Emails are just a Bamboo.Email struct and can be manipulated with plain functions.
* **Easy to unit test**. Because delivery is separated from email creation, no special functions are needed, just assert against fields on the email.
* **Easy to test delivery in integration tests**. Helpers are provided to make testing easy and robust.
* **Preview sent emails during development**. Bamboo comes with a plug that can be used in your router to preview sent emails.
* **View sent emails during development**. Bamboo comes with a plug that can be used in your router to view sent emails.

See the [docs] for the most up to date information.

Expand Down Expand Up @@ -192,17 +192,17 @@ Phoenix is not required to use Bamboo. However, if you do use Phoenix, you can
use Phoenix views and layouts with Bamboo. See
[Bamboo.Phoenix](https://hexdocs.pm/bamboo/Bamboo.Phoenix.html)

## Previewing Sent Emails
## Viewing Sent Emails

Bamboo comes with a handy plug for viewing emails sent in development. Now you
don't have to look at the logs to get password resets, confirmation links, etc.
Just open up the email preview and click the link.
Just open up the sent email viewer and click the link.

See [Bamboo.EmailPreviewPlug](https://hexdocs.pm/bamboo/Bamboo.EmailPreviewPlug.html)
See [Bamboo.SentEmailViewerPlug](https://hexdocs.pm/bamboo/Bamboo.SentEmailViewerPlug.html)

Here is what it looks like:

![Screenshot of BambooEmailPreview](https://cloud.githubusercontent.com/assets/22394/14929083/bda60b76-0e29-11e6-9e11-5ec60069e825.png)
![Screenshot of BambooSentEmailViewer](https://cloud.githubusercontent.com/assets/22394/14929083/bda60b76-0e29-11e6-9e11-5ec60069e825.png)

## Mandrill Specific Functionality (tags, merge vars, templates, etc.)

Expand Down
2 changes: 1 addition & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"skip_files": [
"lib/mix/start_email_preview_task.ex",
"lib/mix/start_sent_email_viewer_task.ex"
]
}
2 changes: 1 addition & 1 deletion lib/bamboo/adapters/local_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Bamboo.LocalAdapter do
Typically this adapter is used in the dev environment so emails are not
delivered to real email addresses.
You can use this adapter along with `Bamboo.EmailPreviewPlug` to view emails
You can use this adapter along with `Bamboo.SentEmailViewerPlug` to view emails
in the browser.
## Example config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>Email Preview</title>
<title>Sent Emails</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.0.0/normalize.css" charset="utf-8">
<style>
html {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Bamboo.EmailPreviewPlug do
defmodule Bamboo.SentEmailViewerPlug do
use Plug.Router
require EEx
alias Bamboo.SentEmail
Expand Down Expand Up @@ -30,10 +30,10 @@ defmodule Bamboo.EmailPreviewPlug do
if Mix.env == :dev do
# If using Phoenix
forward "/sent_emails", Bamboo.EmailPreviewPlug
forward "/sent_emails", Bamboo.SentEmailViewerPlug
# If using Plug.Router, make sure to add the `to`
forward "/sent_emails", to: Bamboo.EmailPreviewPlug
forward "/sent_emails", to: Bamboo.SentEmailViewerPlug
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Bamboo.EmailPreviewPlug.Helper do
defmodule Bamboo.SentEmailViewerPlug.Helper do
import Bamboo.SentEmail

@moduledoc false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>Email Preview</title>
<title>Sent Email</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.0.0/normalize.css" charset="utf-8">
<style>
body {
Expand Down Expand Up @@ -88,51 +88,51 @@
text-overflow: ellipsis;
}

.email-preview-pane {
.email-detail-pane {
width: 100%;
padding: 35px;
padding-left: 0;
display: flex;
flex-direction: column;
}

.email-preview-hero {
.email-detail-hero {
display: flex;
flex-direction: column;
padding-left: 20px;
}

.email-preview-subject {
.email-detail-subject {
font-weight: bold;
font-size: 22px;
padding: 10px 0;
}

.email-preview-bodies-container {
.email-detail-bodies-container {
border: 1px solid #eee;
border-radius: 5px;
padding: 20px;
margin-top: 30px;
}

.email-preview-body-label {
.email-detail-body-label {
padding: 0;
margin: 0 0 12px 0;
font-size: 15px;
font-weight: 700;
}

.email-preview-recipients,
.email-preview-headers {
.email-detail-recipients,
.email-detail-headers {
color: #aaa;
}

.email-preview-recipients strong,
.email-preview-headers strong {
.email-detail-recipients strong,
.email-detail-headers strong {
color: #555;
}

.email-preview-body {
.email-detail-body {
margin: 15px 0 25px 0;
}
</style>
Expand All @@ -143,36 +143,36 @@
<aside class="email-sidebar">
<%= for email <- @emails do %>
<a
class="email-summary <%= Bamboo.EmailPreviewPlug.Helper.selected_email_class(email, @selected_email) %>"
class="email-summary <%= Bamboo.SentEmailViewerPlug.Helper.selected_email_class(email, @selected_email) %>"
href="<%= "#{@base_path}/#{Bamboo.SentEmail.get_id(email)}" %>">
<span class="email-summary-subject truncate"><%= email.subject %></span>
<span class="email-summary-recipients truncate">
<%= Bamboo.Email.get_address(email.from) %>
to <%= Bamboo.EmailPreviewPlug.Helper.email_addresses(email) %>
to <%= Bamboo.SentEmailViewerPlug.Helper.email_addresses(email) %>
</span>
<span class="email-summary-body-excerpt"><%= email.text_body %></span>
</a>
<% end %>
</aside>
<section class="email-preview-pane">
<section class="email-preview-hero">
<span class="email-preview-subject"><%= @selected_email.subject %></span>
<span class="email-preview-recipients">
<section class="email-detail-pane">
<section class="email-detail-hero">
<span class="email-detail-subject"><%= @selected_email.subject %></span>
<span class="email-detail-recipients">
From <strong><%= Bamboo.Email.get_address(@selected_email.from) %></strong>
to <strong><%= Bamboo.EmailPreviewPlug.Helper.email_addresses(@selected_email) %></strong>
to <strong><%= Bamboo.SentEmailViewerPlug.Helper.email_addresses(@selected_email) %></strong>
</span>
<span class="email-preview-headers">
<span class="email-detail-headers">
<%= for {header, value} <- @selected_email.headers do %>
<div class="email-preview-header">
<%= header %> <strong><%= Bamboo.EmailPreviewPlug.Helper.format_headers(value) %></strong>
<div class="email-detail-header">
<%= header %> <strong><%= Bamboo.SentEmailViewerPlug.Helper.format_headers(value) %></strong>
</div>
<% end %>
</span>
</section>

<section class="email-preview-bodies-container">
<h3 class="email-preview-body-label">HTML body</h3>
<p class="email-preview-body">
<section class="email-detail-bodies-container">
<h3 class="email-detail-body-label">HTML body</h3>
<p class="email-detail-body">
<script>
function adjustFrameHeight(iframe) {
var body = iframe.contentWindow.document.body;
Expand All @@ -182,9 +182,9 @@
<iframe onload="adjustFrameHeight(this)" src="<%= "#{@base_path}/#{Bamboo.SentEmail.get_id(@selected_email)}/html" %>"></iframe>
</p>

<h3 class="email-preview-body-label">Text Body</h3>
<p class="email-preview-body">
<%= Bamboo.EmailPreviewPlug.Helper.format_text(@selected_email.text_body) %>
<h3 class="email-detail-body-label">Text Body</h3>
<p class="email-detail-body">
<%= Bamboo.SentEmailViewerPlug.Helper.format_text(@selected_email.text_body) %>
</p>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>Email Preview</title>
<title>Sent Email</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.0.0/normalize.css" charset="utf-8">
<style>
html {
Expand Down Expand Up @@ -57,7 +57,7 @@
</li>

<li>
<strong>You recently restarted your server.</strong> The previewer
<strong>You recently restarted your server.</strong> The viewer
does not persist emails so whenever you restart the server the list
will be cleared.
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Mix.Tasks.Bamboo.StartEmailPreview do
defmodule Mix.Tasks.Bamboo.StartSentEmailViewer do
use Mix.Task

@moduledoc false
Expand All @@ -9,7 +9,7 @@ defmodule Mix.Tasks.Bamboo.StartEmailPreview do
def run(_) do
Mix.Task.run "app.start"
{:ok, _} = Application.ensure_all_started(:cowboy)
Plug.Adapters.Cowboy.http Bamboo.EmailPreviewPlug, [], port: 4003
Plug.Adapters.Cowboy.http Bamboo.SentEmailViewerPlug, [], port: 4003

for index <- 0..5 do
Bamboo.Email.new_email(
Expand Down Expand Up @@ -39,7 +39,7 @@ defmodule Mix.Tasks.Bamboo.StartEmailPreview do
|> Bamboo.SentEmail.push
end

IO.puts "Running email preview on port 4003"
IO.puts "Running sent email viewer on port 4003"
no_halt()
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Bamboo.EmailPreviewTest do
defmodule Bamboo.SentEmailViewerPlugTest do
use ExUnit.Case
use Plug.Test
import Bamboo.Factory
Expand All @@ -10,16 +10,16 @@ defmodule Bamboo.EmailPreviewTest do
plug :match
plug :dispatch

forward "/sent_emails/foo", to: Bamboo.EmailPreviewPlug
forward "/", to: Bamboo.EmailPreviewPlug
forward "/sent_emails/foo", to: Bamboo.SentEmailViewerPlug
forward "/", to: Bamboo.SentEmailViewerPlug
end

setup do
SentEmail.reset
:ok
end

test "shows list of all sent emails, and previews the newest email" do
test "shows list of all sent emails, and the body of the newest email" do
emails = normalize_and_push_pair(:email)
conn = conn(:get, "/sent_emails/foo")

Expand All @@ -28,8 +28,8 @@ defmodule Bamboo.EmailPreviewTest do
assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert selected_sidebar_email_text(conn) =~ newest_email().subject
assert showing_in_preview_pane?(conn, newest_email())
refute showing_in_preview_pane?(conn, oldest_email())
assert showing_in_detail_pane?(conn, newest_email())
refute showing_in_detail_pane?(conn, oldest_email())
for email <- emails do
assert Floki.raw_html(sidebar(conn)) =~ ~s(href="/sent_emails/foo/#{SentEmail.get_id(email)}")
assert Floki.text(sidebar(conn)) =~ email.subject
Expand All @@ -54,7 +54,7 @@ defmodule Bamboo.EmailPreviewTest do
end
end

test "prints single header in preview" do
test "prints single header in detail pane" do
email = normalize_and_push(:email, headers: %{"Reply-To" => "reply-to@example.com"})
conn = conn(:get, "/sent_emails/foo")

Expand All @@ -67,7 +67,7 @@ defmodule Bamboo.EmailPreviewTest do
assert conn.resp_body =~ "reply-to@example.com"
end

test "prints multiple headers in preview" do
test "prints multiple headers in detail pane" do
email = normalize_and_push(:email, headers: %{"Reply-To" => ["reply-to1@example.com", "reply-to2@example.com"], "Foobar" => "foobar-header"})
conn = conn(:get, "/sent_emails/foo")

Expand Down Expand Up @@ -131,8 +131,8 @@ defmodule Bamboo.EmailPreviewTest do

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert showing_in_preview_pane?(conn, SentEmail.get(selected_email_id))
refute showing_in_preview_pane?(conn, SentEmail.get(unselected_email_id))
assert showing_in_detail_pane?(conn, SentEmail.get(selected_email_id))
refute showing_in_detail_pane?(conn, SentEmail.get(unselected_email_id))
end

test "shows an email's html by id" do
Expand Down Expand Up @@ -177,12 +177,12 @@ defmodule Bamboo.EmailPreviewTest do
SentEmail.all |> List.last
end

defp showing_in_preview_pane?(conn, email) do
Floki.text(preview_pane(conn)) =~ email.subject
defp showing_in_detail_pane?(conn, email) do
Floki.text(detail_pane(conn)) =~ email.subject
end

defp preview_pane(conn) do
conn.resp_body |> Floki.find(".email-preview-pane")
defp detail_pane(conn) do
conn.resp_body |> Floki.find(".email-detail-pane")
end

defp sidebar(conn) do
Expand Down

0 comments on commit f366845

Please sign in to comment.