defmodule ChatApi.Emails.Email do
import Swoosh.Email
import Ecto.Changeset
@from_address System.get_env("FROM_ADDRESS") || ""
@backend_url System.get_env("BACKEND_URL") || ""
defstruct to_address: nil, message: nil
# TODO: Move conversation id out the mailer should only care about the message
def new_message_alert(to_address, message, conversation_id) do
link =
"View in dashboard"
msg = "#{message}"
html = "A new message has arrived:
" <> msg <> "
" <> link
text = "A new message has arrived: #{message}"
new()
|> to(to_address)
|> from({"Papercups", @from_address})
|> subject("A customer has sent you a message!")
|> html_body(html)
|> text_body(text)
end
def conversation_reply(
to: to,
from: from,
reply_to: reply_to,
company: company,
messages: messages,
customer: customer
) do
new()
|> to(to)
|> from({from, @from_address})
|> reply_to(reply_to)
|> subject("New message from #{company}!")
|> html_body(conversation_reply_html(messages, from: from, to: customer, company: company))
|> text_body(conversation_reply_text(messages, from: from, to: customer, company: company))
end
# TODO: figure out a better way to create templates for these
defp conversation_reply_text(messages, from: from, to: customer, company: company) do
"""
Hi #{customer.name || "there"}!
You've received a new message from your chat with #{company} (#{customer.current_url || ""}):
#{
Enum.map(messages, fn msg ->
format_sender(msg, company) <> ": " <> msg.body <> "\n"
end)
}
Best,
#{from}
"""
end
defp format_agent(user, company) do
case user do
%{email: email, profile: nil} ->
company || email
%{email: email, profile: profile} ->
profile.display_name || profile.full_name || company || email
_ ->
company || "Agent"
end
end
defp format_sender(message, company) do
case message do
%{user: user, customer_id: nil} -> format_agent(user, company)
%{customer_id: _customer_id} -> "You"
end
end
defp conversation_reply_html(messages, from: from, to: customer, company: company) do
"""
Hi #{customer.name || "there"}!
You've received a new message from your chat with #{company}:
#{format_sender(msg, company)}
#{msg.body}
Best,
#{from}
Hi there!
Thanks for signing up for Papercups :)
I'm Alex, one of the founders of Papercups along with Kam. If you have any questions, feedback, or need any help getting started, don't hesitate to reach out!
Feel free to reply directly to this email, or contact me at alex@papercups.io
Best,
Alex
PS: We also have a Slack channel if you'd like to see what we're up to :)
https://github.com/papercups-io/papercups#get-in-touch
Hi there!
Click the link below to reset your Papercups password:
#{link}
Best,
Alex & Kam @ Papercups