Skip to content

Commit 315c95c

Browse files
committed
Code for step 3
1 parent 0801ac3 commit 315c95c

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

config/dev.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ config :mjml_demo, MjmlDemo.Repo,
99
show_sensitive_data_on_connection_error: true,
1010
pool_size: 10
1111

12+
config :mjml_demo, MjmlDemo.Emails, adapter: Swoosh.Adapters.Local
13+
1214
# For development, we disable any cache and enable
1315
# debugging and code reloading.
1416
#

lib/mjml_demo/emails.ex

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
defmodule MjmlDemo.Emails do
2+
use Swoosh.Mailer, otp_app: :mjml_demo
3+
4+
import Swoosh.Email
5+
6+
alias __MODULE__.ConfirmationInstructions
7+
8+
def confirmation_instructions(user, confirmation_url) do
9+
# You'll want to update the User schema and the DB migration to support
10+
# storing first+last names, plan tiers, and whatever else you want to
11+
# associate to the user registration.
12+
rendered_email =
13+
ConfirmationInstructions.render(
14+
first_name: "Agent",
15+
last_name: "Smith",
16+
confirmation_url: confirmation_url,
17+
tier: "Gold"
18+
)
19+
20+
new()
21+
|> to(user.email)
22+
|> from({"Onboarding Team", "welcome@saas-central.com"})
23+
|> subject("Welcome to SaaS-Central!")
24+
|> html_body(rendered_email)
25+
|> text_body("""
26+
Hello Agent Smith and thank you for signing up for the Gold plan!!!
27+
28+
You can confirm your account by visiting the following URL: #{confirmation_url}
29+
""")
30+
|> deliver()
31+
end
32+
33+
def generate_template(file_path) do
34+
{:ok, template} =
35+
file_path
36+
|> File.read!()
37+
|> Mjml.to_html()
38+
39+
~r/{{\s*([^}^\s]+)\s*}}/
40+
|> Regex.replace(template, fn _, variable_name ->
41+
"<%= @#{variable_name} %>"
42+
end)
43+
end
44+
end

lib/mjml_demo_web/router.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ defmodule MjmlDemoWeb.Router do
4343
end
4444
end
4545

46+
if Mix.env() == :dev do
47+
scope "/dev" do
48+
pipe_through [:browser]
49+
50+
forward "/mailbox", Plug.Swoosh.MailboxPreview, base_path: "/dev/mailbox"
51+
end
52+
end
53+
4654
## Authentication routes
4755

4856
scope "/", MjmlDemoWeb do

0 commit comments

Comments
 (0)