|
| 1 | +defmodule GroupherServer.Email do |
| 2 | + @moduledoc """ |
| 3 | + the email dispatch system for Groupher |
| 4 | +
|
| 5 | + welcom_email -> send to new register |
| 6 | + """ |
| 7 | + import Bamboo.Email |
| 8 | + import Helper.Utils, only: [get_config: 2] |
| 9 | + |
| 10 | + alias GroupherServer.Accounts.User |
| 11 | + alias GroupherServer.Billing.BillRecord |
| 12 | + |
| 13 | + alias GroupherServer.Email.Templates |
| 14 | + alias GroupherServer.Mailer |
| 15 | + |
| 16 | + @support_email get_config(:system_emails, :support) |
| 17 | + @admin_email get_config(:system_emails, :admin) |
| 18 | + |
| 19 | + def welcome(%User{email: email} = user) when not is_nil(email) do |
| 20 | + base_mail() |
| 21 | + |> to(email) |
| 22 | + |> subject("欢迎来到 coderplanets") |
| 23 | + |> html_body(Templates.Welcome.html(user)) |
| 24 | + |> text_body(Templates.Welcome.text()) |
| 25 | + |> Mailer.deliver_later() |
| 26 | + end |
| 27 | + |
| 28 | + # user has no email log to somewhere |
| 29 | + def welcome(_user) do |
| 30 | + {:ok, :pass} |
| 31 | + end |
| 32 | + |
| 33 | + def notify_admin(%User{from_github: true} = user, :new_register) do |
| 34 | + base_mail() |
| 35 | + |> to(@admin_email) |
| 36 | + |> subject("新用户(#{user.nickname})注册") |
| 37 | + |> html_body(Templates.NotifyAdminRegister.html(user)) |
| 38 | + |> text_body(Templates.NotifyAdminRegister.text()) |
| 39 | + |> Mailer.deliver_later() |
| 40 | + end |
| 41 | + |
| 42 | + def notify_admin(_user, :new_register) do |
| 43 | + {:ok, :pass} |
| 44 | + end |
| 45 | + |
| 46 | + def notify_admin(%BillRecord{} = record, :payment) do |
| 47 | + base_mail() |
| 48 | + |> to(@admin_email) |
| 49 | + |> subject("打赏 #{record.amount} 元") |
| 50 | + |> html_body(Templates.NotifyAdminPayment.html(record)) |
| 51 | + |> text_body(Templates.NotifyAdminPayment.text()) |
| 52 | + |> Mailer.deliver_later() |
| 53 | + end |
| 54 | + |
| 55 | + # some one comment to your post .. |
| 56 | + # the author's publish content being deleted .. |
| 57 | + def notify_author, do: IO.inspect("notify_author") |
| 58 | + def notify_publish, do: IO.inspect("notify_publish") |
| 59 | + # ... |
| 60 | + |
| 61 | + defp base_mail do |
| 62 | + new_email() |
| 63 | + |> from(@support_email) |
| 64 | + end |
| 65 | +end |
0 commit comments