Add batch processing of emails
This commit is contained in:
parent
7090e7d3a9
commit
8336350ed4
@ -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
|
||||
<hr />
|
||||
<p>
|
||||
<strong>Unsubscribe</strong><br />
|
||||
If you no longer wish to receive our emails, you can <a href="#{url}?%recipient%">unsubscribe here</a>.
|
||||
If you no longer wish to receive our emails, you can <a href="#{url}?email=%recipient.email%">unsubscribe here</a>.
|
||||
</p>
|
||||
"""
|
||||
end
|
||||
|
||||
@ -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})
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user