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_name "Port Townsend Roasting Co."
|
||||||
@from_email "newsletter@ptcoffee.com"
|
@from_email "newsletter@ptcoffee.com"
|
||||||
|
@chunk_by 900
|
||||||
|
|
||||||
def deliver_message(message) do
|
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()
|
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
|
end
|
||||||
|
|
||||||
defp deliver(recipients, subject, body) do
|
defp deliver(recipients, subject, body) do
|
||||||
@ -42,7 +47,7 @@ defmodule SendIt.Marketing.MessageNotifier do
|
|||||||
|
|
||||||
defp format_recipient_vars(recipients) do
|
defp format_recipient_vars(recipients) do
|
||||||
Enum.reduce(recipients, %{}, fn {name, email}, acc ->
|
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)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -62,7 +67,7 @@ defmodule SendIt.Marketing.MessageNotifier do
|
|||||||
<hr />
|
<hr />
|
||||||
<p>
|
<p>
|
||||||
<strong>Unsubscribe</strong><br />
|
<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>
|
</p>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|||||||
@ -54,7 +54,7 @@ defmodule SendItWeb.MessageLive.FormComponent do
|
|||||||
defp save_message(socket, :new, message_params) do
|
defp save_message(socket, :new, message_params) do
|
||||||
case Marketing.create_message(message_params) do
|
case Marketing.create_message(message_params) do
|
||||||
{:ok, message} ->
|
{:ok, message} ->
|
||||||
{:ok, _email} = Marketing.deliver_message(message)
|
[{:ok, _email} | _rest] = Marketing.deliver_message(message)
|
||||||
|
|
||||||
notify_parent({:saved, message})
|
notify_parent({:saved, message})
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,8 @@ defmodule SendItWeb.SubscriptionLive do
|
|||||||
alias SendIt.Marketing
|
alias SendIt.Marketing
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(params, _session, socket) do
|
||||||
{:ok, assign_form(socket)}
|
{:ok, assign_form(socket, params)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@ -35,14 +35,22 @@ defmodule SendItWeb.SubscriptionLive do
|
|||||||
defp unsubscribe(socket, email) do
|
defp unsubscribe(socket, email) do
|
||||||
case Marketing.unsubscribe(email) do
|
case Marketing.unsubscribe(email) do
|
||||||
{:ok, _contact} ->
|
{: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} ->
|
{:error, reason} ->
|
||||||
{:noreply, put_flash(socket, :error, reason)}
|
{:noreply, put_flash(socket, :error, reason)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp assign_form(socket) do
|
defp assign_form(socket, params) do
|
||||||
assign(socket, :form, to_form(%{"email" => ""}))
|
email = handle_params(params)
|
||||||
|
assign(socket, :form, to_form(%{"email" => email}))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp handle_params(%{"email" => email}), do: email
|
||||||
|
|
||||||
|
defp handle_params(_params), do: ""
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user