user_notifier.ex (1885B)
1 defmodule DailyTracker.Accounts.UserNotifier do 2 import Swoosh.Email 3 4 alias DailyTracker.Mailer 5 alias DailyTracker.Accounts.User 6 7 # Delivers the email using the application mailer. 8 defp deliver(recipient, subject, body) do 9 email = 10 new() 11 |> to(recipient) 12 |> from({"DailyTracker", "silas@silasbrack.com"}) 13 |> subject(subject) 14 |> text_body(body) 15 16 with {:ok, _metadata} <- Mailer.deliver(email) do 17 {:ok, email} 18 end 19 end 20 21 @doc """ 22 Deliver instructions to update a user email. 23 """ 24 def deliver_update_email_instructions(user, url) do 25 deliver(user.email, "Update email instructions", """ 26 27 ============================== 28 29 Hi #{user.email}, 30 31 You can change your email by visiting the URL below: 32 33 #{url} 34 35 If you didn't request this change, please ignore this. 36 37 ============================== 38 """) 39 end 40 41 @doc """ 42 Deliver instructions to log in with a magic link. 43 """ 44 def deliver_login_instructions(user, url) do 45 case user do 46 %User{confirmed_at: nil} -> deliver_confirmation_instructions(user, url) 47 _ -> deliver_magic_link_instructions(user, url) 48 end 49 end 50 51 defp deliver_magic_link_instructions(user, url) do 52 deliver(user.email, "Log in instructions", """ 53 54 ============================== 55 56 Hi #{user.email}, 57 58 You can log into your account by visiting the URL below: 59 60 #{url} 61 62 If you didn't request this email, please ignore this. 63 64 ============================== 65 """) 66 end 67 68 defp deliver_confirmation_instructions(user, url) do 69 deliver(user.email, "Confirmation instructions", """ 70 71 ============================== 72 73 Hi #{user.email}, 74 75 You can confirm your account by visiting the URL below: 76 77 #{url} 78 79 If you didn't create an account with us, please ignore this. 80 81 ============================== 82 """) 83 end 84 end