defmodule Berrypod.Reviews.ReviewNotifier do @moduledoc """ Sends transactional emails for the review system. Verification emails for review submission, and review request emails after order delivery. """ import Swoosh.Email alias Berrypod.Mailer require Logger @doc """ Sends a verification email with a link to leave a review. The link contains a signed token encoding the email and product_id. """ def deliver_review_verification(email, product_title, link) do subject = "Leave a review for #{product_title}" body = """ ============================== Thanks for your purchase! Click here to leave a review for #{product_title}: #{link} This link expires in 1 hour. If you didn't request this, you can ignore this email. ============================== """ deliver(email, subject, body) end # --- Private --- defp deliver(recipient, subject, body) do shop_name = Berrypod.Settings.get_setting("shop_name", "Berrypod") from_address = Berrypod.Settings.get_setting("email_from_address", "contact@example.com") email = new() |> to(recipient) |> from({shop_name, from_address}) |> subject(subject) |> text_body(body) case Mailer.deliver(email) do {:ok, _metadata} = result -> result {:error, reason} = error -> Logger.warning("Failed to send review email to #{recipient}: #{inspect(reason)}") error end end end