Skip to content

Instantly share code, notes, and snippets.

@desmondmonster
Last active March 14, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desmondmonster/e55f34ff8c29a50b9f7822700ed11d30 to your computer and use it in GitHub Desktop.
Save desmondmonster/e55f34ff8c29a50b9f7822700ed11d30 to your computer and use it in GitHub Desktop.
Quick GenServer for asynchronously sending email
defmodule PopularApp.EmailSender do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
{:ok, %{last_sent: nil, last_data: nil}}
end
# Public API for callers - convenience wrapper around `cast`
def send_email(data) do
GenServer.cast(__MODULE__, {:send_email, data})
end
# Server functions
def handle_cast({:send_email, data}, state) do
data
|> send_email()
{:noreply, %{last_sent: calendar.local_time(), last_data: data}}
end
defp send_email(data) do
# extract to, from, etc from `data`
# send email through your provider
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment