mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
add mailing list signup with email confirmation (#67)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
defmodule Chesstrainer.MailingList.Notifier do
|
||||
@moduledoc """
|
||||
Emails for the mailing list.
|
||||
"""
|
||||
|
||||
import Swoosh.Email
|
||||
|
||||
alias Chesstrainer.Branding
|
||||
alias Chesstrainer.Mailer
|
||||
|
||||
@doc """
|
||||
Delivers the confirmation email for a mailing-list subscription.
|
||||
"""
|
||||
def deliver_confirmation_instructions(email, url) do
|
||||
deliver(email, "Confirm your mailing list subscription", """
|
||||
|
||||
==============================
|
||||
|
||||
Hi #{email},
|
||||
|
||||
You asked to receive occasional endgame tips and site news from
|
||||
#{Branding.name()}. Confirm your subscription by visiting the URL below:
|
||||
|
||||
#{url}
|
||||
|
||||
If you didn't ask for this, ignore this email and we won't add you.
|
||||
|
||||
==============================
|
||||
""")
|
||||
end
|
||||
|
||||
defp deliver(recipient, subject, body) do
|
||||
email =
|
||||
new()
|
||||
|> to(recipient)
|
||||
|> from({Branding.name(), Branding.support_email()})
|
||||
|> subject(subject)
|
||||
|> text_body(body)
|
||||
|
||||
with {:ok, _metadata} <- Mailer.deliver(email) do
|
||||
{:ok, email}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
defmodule Chesstrainer.MailingList.Subscriber do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Chesstrainer.Accounts.User
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "mailing_list_subscribers" do
|
||||
field :email, :string
|
||||
field :source, Ecto.Enum, values: [:home, :registration], default: :home
|
||||
field :confirmed_at, :utc_datetime
|
||||
belongs_to :user, User
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc """
|
||||
A changeset for subscribing an email address.
|
||||
|
||||
`:source` and `:user_id` are programmatic: the context sets them on the
|
||||
struct, never from user-supplied params.
|
||||
"""
|
||||
def changeset(subscriber, attrs) do
|
||||
subscriber
|
||||
|> cast(attrs, [:email])
|
||||
|> update_change(:email, &String.trim/1)
|
||||
|> validate_required([:email])
|
||||
|> validate_format(:email, ~r/^[^@,;\s]+@[^@,;\s]+$/,
|
||||
message: "must have the @ sign and no spaces"
|
||||
)
|
||||
|> validate_length(:email, max: 160)
|
||||
|> unique_constraint(:email)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user