diff --git a/lib/send_it/marketing/message_notifier.ex b/lib/send_it/marketing/message_notifier.ex index 65aeaf7..af06d01 100644 --- a/lib/send_it/marketing/message_notifier.ex +++ b/lib/send_it/marketing/message_notifier.ex @@ -8,12 +8,17 @@ defmodule SendIt.Marketing.MessageNotifier do @from_name "Port Townsend Roasting Co." @from_email "newsletter@ptcoffee.com" + @chunk_by 900 def deliver_message(message) do - contacts = message.contacts |> format_recipients() + contacts = message.contacts |> format_recipients() |> Enum.chunk_every(@chunk_by) body = message.content |> format_body() - deliver(contacts, message.subject, body) + Enum.map(contacts, &process_batch(&1, message.subject, body)) + end + + defp process_batch(batch, subject, body) do + Task.async(fn -> deliver(batch, subject, body) end) |> Task.await() end defp deliver(recipients, subject, body) do @@ -42,7 +47,7 @@ defmodule SendIt.Marketing.MessageNotifier do defp format_recipient_vars(recipients) do Enum.reduce(recipients, %{}, fn {name, email}, acc -> - Map.put_new(acc, email, %{name: name}) + Map.put_new(acc, email, %{name: name, email: email}) end) end @@ -62,7 +67,7 @@ defmodule SendIt.Marketing.MessageNotifier do

Unsubscribe
- If you no longer wish to receive our emails, you can unsubscribe here. + If you no longer wish to receive our emails, you can unsubscribe here.

""" end diff --git a/lib/send_it_web/live/message_live/form_component.ex b/lib/send_it_web/live/message_live/form_component.ex index e2b49fa..e65a216 100644 --- a/lib/send_it_web/live/message_live/form_component.ex +++ b/lib/send_it_web/live/message_live/form_component.ex @@ -54,7 +54,7 @@ defmodule SendItWeb.MessageLive.FormComponent do defp save_message(socket, :new, message_params) do case Marketing.create_message(message_params) do {:ok, message} -> - {:ok, _email} = Marketing.deliver_message(message) + [{:ok, _email} | _rest] = Marketing.deliver_message(message) notify_parent({:saved, message}) diff --git a/lib/send_it_web/live/subscription_live.ex b/lib/send_it_web/live/subscription_live.ex index 60dacb8..424fd67 100644 --- a/lib/send_it_web/live/subscription_live.ex +++ b/lib/send_it_web/live/subscription_live.ex @@ -4,8 +4,8 @@ defmodule SendItWeb.SubscriptionLive do alias SendIt.Marketing @impl true - def mount(_params, _session, socket) do - {:ok, assign_form(socket)} + def mount(params, _session, socket) do + {:ok, assign_form(socket, params)} end @impl true @@ -35,14 +35,22 @@ defmodule SendItWeb.SubscriptionLive do defp unsubscribe(socket, email) do case Marketing.unsubscribe(email) do {:ok, _contact} -> - {:noreply, put_flash(socket, :info, "You have been unsubscribed from all emails.")} + {:noreply, + socket + |> assign_form(%{}) + |> put_flash(:info, "You have been unsubscribed from all emails.")} {:error, reason} -> {:noreply, put_flash(socket, :error, reason)} end end - defp assign_form(socket) do - assign(socket, :form, to_form(%{"email" => ""})) + defp assign_form(socket, params) do + email = handle_params(params) + assign(socket, :form, to_form(%{"email" => email})) end + + defp handle_params(%{"email" => email}), do: email + + defp handle_params(_params), do: "" end