Guest User

Untitled

a guest
Mar 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. defmodule PopularApp.EmailSender do
  2. use GenServer
  3.  
  4. def start_link do
  5. GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  6. end
  7.  
  8. def init(:ok) do
  9. {:ok, %{last_sent: nil, last_data: nil}}
  10. end
  11.  
  12. # Public API for callers - convenience wrapper around `cast`
  13. def send_email(data) do
  14. GenServer.cast(__MODULE__, {:send_email, data})
  15. end
  16.  
  17. # Server functions
  18. def handle_cast({:send_email, data}, state) do
  19. data
  20. |> send_email()
  21.  
  22. {:noreply, %{last_sent: calendar.local_time(), last_data: data}}
  23. end
  24.  
  25. defp send_email(data) do
  26. # extract to, from, etc from `data`
  27. # send email through your provider
  28. end
  29. end
Add Comment
Please, Sign In to add comment