One great thing about using Bamboo.LocalAdapter is that we can view our sent emails in development with Bamboo.EmailPreviewPlug.

To use this we’ll open our router.

And we’ll forward the “/sent_emails” endpoint to the Bamboo.EmailPreviewPlug.

We also only want this in our development environment, so let wrap it in a conditional to only be included if our environment is dev.

web/router.ex
if Mix.env == :dev do
forward "/sent_emails", Bamboo.EmailPreviewPlug
end

Great now let’s go to the command line and stop our sever and download our updated dependencies.

$ mix deps.get

Bamboo separates the composition of an email from its delivery. Let’s start by defining our “Mailer”, which is in charge of sending our email.

We’ll create a module in “/lib/teacher” named “mailer.ex”.

Now the only line in our module adds the ‘deliver’ functions we’ll use to send our emails and configures Bamboo to use the config we set for our app.

lib/teacher/mailer.ex
defmodule Teacher.Mailer do
use Bamboo.Mailer, otp_app: :teacher
end

Now let’s create a module we can use to compose our emails. Let’s create another file in the same directory called “email.ex”. And in it we’ll import ‘Bamboo.Email’

Then let’s define a function named “movie_removal_email”, which we’ll use to compose our email.

In it we’re using the “new_email” function we get from importing “Bamboo.Email” above and then passing our email data into it. We’re including the “from”, “to”, “subject”, “text_body”, and “html_body”.

Since we want ours to be sent when a movie is deleted, let’s open our “MovieController”module.

Then we’ll alias our new modules: “Mailer” and “Email”.

Now let’s create a private function named send_removal_notifcation. And in it we’ll call our Email.movie_removal_email() and pipe its result into the deliver function in the “Mailer” module we want to use.

There are two functions to choose from, deliver_now which will send our email immediately, but will also slow down our request.

And deliver_later which will send our email in the background, and does so without linking to the calling process. So if there’s a problem sending our email, our app will still work. We’ll use deliver_later.

Then we’ll call send_removal_notifcation from our delete function after the movie is deleted.

Now if we check out development logs we can see that our email was sent with Bamboo. And since we set up the Bamboo.EmailPreviewPlug we can see what our sent email looks like.

Let’s go to the ‘/sent_emails’ route on our app and great, we see the email we sent is displayed. And here we can see the html version and the text version. One thing to keep in mind while testing is that restarting your server will remove the test emails displayed here.