Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit a28d68c

Browse files
mydearxymmydearxym
authored andcommitted
feat: 🎸 mailer
add bool switch to control send or not
1 parent 5cc39bb commit a28d68c

File tree

4 files changed

+122
-56
lines changed

4 files changed

+122
-56
lines changed

config/config.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ config :groupher_server, GroupherServerWeb.Gettext, default_locale: "zh_CN", loc
6868

6969
# config email services
7070
config :groupher_server, :system_emails,
71-
support: "coderplanets <support@group.coderplanets.com>",
72-
admin: "mydearxym@qq.com"
71+
support_email: "coderplanets <support@group.coderplanets.com>",
72+
admin_email: "mydearxym@qq.com",
73+
welcome_new_register: true,
74+
notify_admin_on_new_user: true,
75+
notify_admin_on_content_created: true
7376

7477
config :groupher_server, GroupherServer.Mailer,
7578
adapter: Bamboo.MailgunAdapter,

lib/groupher_server/mailer/email.ex

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,29 @@ defmodule GroupherServer.Email do
1414
alias Email.Templates
1515
alias Mailer
1616

17-
@support_email get_config(:system_emails, :support)
18-
@admin_email get_config(:system_emails, :admin)
17+
@support_email get_config(:system_emails, :support_email)
18+
@admin_email get_config(:system_emails, :admin_email)
19+
20+
@conf_welcome_new_register get_config(:system_emails, :welcome_new_register)
21+
@conf_notify_admin_on_new_user get_config(:system_emails, :notify_admin_on_new_user)
22+
@conf_notify_admin_on_content_created get_config(
23+
:system_emails,
24+
:notify_admin_on_content_created
25+
)
1926

2027
def welcome(%User{email: email} = user) when not is_nil(email) do
21-
base_mail()
22-
|> to(email)
23-
|> subject("欢迎来到 coderplanets")
24-
|> html_body(Templates.Welcome.html(user))
25-
|> text_body(Templates.Welcome.text())
26-
|> Mailer.deliver_later()
28+
case @conf_welcome_new_register do
29+
true ->
30+
base_mail()
31+
|> to(email)
32+
|> subject("欢迎来到 coderplanets")
33+
|> html_body(Templates.Welcome.html(user))
34+
|> text_body(Templates.Welcome.text())
35+
|> Mailer.deliver_later()
36+
37+
false ->
38+
{:ok, :pass}
39+
end
2740
end
2841

2942
# user has no email log to somewhere
@@ -42,19 +55,27 @@ defmodule GroupherServer.Email do
4255
|> Mailer.deliver_later()
4356
end
4457

58+
# notify admin when new user register
4559
def notify_admin(%User{from_github: true} = user, :new_register) do
46-
base_mail()
47-
|> to(@admin_email)
48-
|> subject("新用户(#{user.nickname})注册")
49-
|> html_body(Templates.NotifyAdminRegister.html(user))
50-
|> text_body(Templates.NotifyAdminRegister.text())
51-
|> Mailer.deliver_later()
60+
case @conf_notify_admin_on_new_user do
61+
true ->
62+
base_mail()
63+
|> to(@admin_email)
64+
|> subject("新用户(#{user.nickname})注册")
65+
|> html_body(Templates.NotifyAdminRegister.html(user))
66+
|> text_body(Templates.NotifyAdminRegister.text())
67+
|> Mailer.deliver_later()
68+
69+
false ->
70+
{:ok, :pass}
71+
end
5272
end
5373

5474
def notify_admin(_user, :new_register) do
5575
{:ok, :pass}
5676
end
5777

78+
# notify admin when someone donote
5879
def notify_admin(%BillRecord{} = record, :payment) do
5980
base_mail()
6081
|> to(@admin_email)
@@ -64,6 +85,22 @@ defmodule GroupherServer.Email do
6485
|> Mailer.deliver_later()
6586
end
6687

88+
# notify admin when new post has created
89+
def notify_admin(:create, _post) do
90+
case @conf_notify_admin_on_content_created do
91+
true ->
92+
base_mail()
93+
|> to(@admin_email)
94+
|> subject("新帖子: ... ")
95+
# |> html_body(Templates.NotifyAdminRegister.html(user))
96+
# |> text_body(Templates.NotifyAdminRegister.text())
97+
|> Mailer.deliver_later()
98+
99+
false ->
100+
{:ok, :pass}
101+
end
102+
end
103+
67104
# some one comment to your post ..
68105
# the author's publish content being deleted ..
69106
# def notify_author, do: IO.inspect("notify_author")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule GroupherServer.Email.Templates.NotifyAdminOnContentCreated do
2+
@moduledoc """
3+
template for notify admin when there is new content created, like but not limit
4+
to: post, job, video, repo ...
5+
6+
if you want change style or debug the template
7+
just copy and paste raw string to: https://mjml.io/try-it-live
8+
"""
9+
def html(record) do
10+
"""
11+
hello
12+
"""
13+
end
14+
15+
def text() do
16+
"""
17+
有人打赏了
18+
"""
19+
end
20+
21+
defp raw() do
22+
"""
23+
TODO
24+
"""
25+
end
26+
end

lib/groupher_server/mailer/templates/welcome.ex

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
128128
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-bottom:0;text-align:center;vertical-align:top;">
129129
<!--[if mso | IE]>
130130
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
131-
131+
132132
<tr>
133-
133+
134134
<td
135135
class="" style="vertical-align:top;width:600px;"
136136
>
@@ -153,9 +153,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
153153
<tbody>
154154
<tr>
155155
<td style="width:550px;"> <a href="https://coderplanets.com" target="_blank">
156-
156+
157157
<img alt="" height="auto" src="https://user-images.githubusercontent.com/6184465/59546312-a1c0ac00-8f5d-11e9-85cc-db681a16ff13.jpg" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;" width="550">
158-
158+
159159
</a> </td>
160160
</tr>
161161
</tbody>
@@ -166,9 +166,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
166166
</div>
167167
<!--[if mso | IE]>
168168
</td>
169-
169+
170170
</tr>
171-
171+
172172
</table>
173173
<![endif]-->
174174
</td>
@@ -199,12 +199,12 @@ defmodule GroupherServer.Email.Templates.Welcome do
199199
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-bottom:0;padding-top:0;text-align:center;vertical-align:top;">
200200
<!--[if mso | IE]>
201201
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
202-
202+
203203
<tr>
204204
<td
205205
class="" width="600px"
206206
>
207-
207+
208208
<table
209209
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
210210
>
@@ -218,9 +218,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
218218
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-left:6px;padding-right:6px;text-align:center;vertical-align:top;">
219219
<!--[if mso | IE]>
220220
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
221-
221+
222222
<tr>
223-
223+
224224
<td
225225
class="" style="vertical-align:top;width:588px;"
226226
>
@@ -281,9 +281,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
281281
</div>
282282
<!--[if mso | IE]>
283283
</td>
284-
284+
285285
</tr>
286-
286+
287287
</table>
288288
<![endif]-->
289289
</td>
@@ -295,10 +295,10 @@ defmodule GroupherServer.Email.Templates.Welcome do
295295
</td>
296296
</tr>
297297
</table>
298-
298+
299299
</td>
300300
</tr>
301-
301+
302302
</table>
303303
<![endif]-->
304304
</td>
@@ -329,12 +329,12 @@ defmodule GroupherServer.Email.Templates.Welcome do
329329
<td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;vertical-align:top;">
330330
<!--[if mso | IE]>
331331
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
332-
332+
333333
<tr>
334334
<td
335335
class="" width="600px"
336336
>
337-
337+
338338
<table
339339
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
340340
>
@@ -348,9 +348,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
348348
<td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;vertical-align:top;">
349349
<!--[if mso | IE]>
350350
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
351-
351+
352352
<tr>
353-
353+
354354
<td
355355
class="" style="vertical-align:top;width:600px;"
356356
>
@@ -368,7 +368,7 @@ defmodule GroupherServer.Email.Templates.Welcome do
368368
align="center" border="0" cellpadding="0" cellspacing="0" role="presentation"
369369
>
370370
<tr>
371-
371+
372372
<td>
373373
<![endif]-->
374374
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="float:none;display:inline-table;">
@@ -386,7 +386,7 @@ defmodule GroupherServer.Email.Templates.Welcome do
386386
</table>
387387
<!--[if mso | IE]>
388388
</td>
389-
389+
390390
</tr>
391391
</table>
392392
<![endif]-->
@@ -405,9 +405,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
405405
</div>
406406
<!--[if mso | IE]>
407407
</td>
408-
408+
409409
</tr>
410-
410+
411411
</table>
412412
<![endif]-->
413413
</td>
@@ -419,15 +419,15 @@ defmodule GroupherServer.Email.Templates.Welcome do
419419
</td>
420420
</tr>
421421
</table>
422-
422+
423423
</td>
424424
</tr>
425-
425+
426426
<tr>
427427
<td
428428
class="" width="600px"
429429
>
430-
430+
431431
<table
432432
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600"
433433
>
@@ -441,9 +441,9 @@ defmodule GroupherServer.Email.Templates.Welcome do
441441
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-top:0;text-align:center;vertical-align:top;">
442442
<!--[if mso | IE]>
443443
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
444-
444+
445445
<tr>
446-
446+
447447
<td
448448
class="" style="width:600px;"
449449
>
@@ -452,7 +452,7 @@ defmodule GroupherServer.Email.Templates.Welcome do
452452
<!--[if mso | IE]>
453453
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
454454
<tr>
455-
455+
456456
<td
457457
style="vertical-align:top;width:600px;"
458458
>
@@ -477,16 +477,16 @@ defmodule GroupherServer.Email.Templates.Welcome do
477477
</div>
478478
<!--[if mso | IE]>
479479
</td>
480-
480+
481481
</tr>
482482
</table>
483483
<![endif]-->
484484
</div>
485485
<!--[if mso | IE]>
486486
</td>
487-
487+
488488
</tr>
489-
489+
490490
</table>
491491
<![endif]-->
492492
</td>
@@ -498,10 +498,10 @@ defmodule GroupherServer.Email.Templates.Welcome do
498498
</td>
499499
</tr>
500500
</table>
501-
501+
502502
</td>
503503
</tr>
504-
504+
505505
</table>
506506
<![endif]-->
507507
</td>
@@ -575,17 +575,17 @@ defmodule GroupherServer.Email.Templates.Welcome do
575575
<mj-text color="#637381" font-size="16px">
576576
欢迎你成为 coderplanets 社区的一员。coderplanets 是一个由各个独立平等的子社区组成的、专注于小众编程语言和框架的 IT 社区平台,在这里,你可以不受打扰的关注你感兴趣的子社区以及找到和你志趣相投的人。
577577
</mj-text>
578-
578+
579579
<mj-text color="#637381" font-size="16px">
580580
祝你生活愉快!
581581
</mj-text>
582-
582+
583583
<mj-divider border-width="1px" border-style="dashed" border-color="#113A41" />
584-
584+
585585
<mj-button background-color="#2F8EA4" align="center" color="#ffffff" font-size="17px" font-weight="bold" href="https://coderplanets.com" width="300px">
586586
去看看
587587
</mj-button>
588-
588+
589589
<mj-text color="#637381" font-size="16px" padding-bottom="0">
590590
coderplanets 的构建受益于世界范围内各种优秀的开源软件,本身的源代码也开放在 github 上,欢迎点击下方链接参与其中,任何 issue/pr/star 等等都是对我们莫大的鼓励。
591591
</mj-text>
@@ -598,11 +598,11 @@ defmodule GroupherServer.Email.Templates.Welcome do
598598
<mj-section>
599599
<mj-column width="100%" padding="0">
600600
<mj-social font-size="15px" icon-size="30px" mode="horizontal" padding="0" align="center">
601-
<mj-social-element name="github" href="https://mjml.io/" background-color="#296C7D">
601+
<mj-social-element name="github" href="https://github.com/coderplanets" background-color="#296C7D">
602602
</mj-social-element>
603603
</mj-social>
604-
605-
604+
605+
606606
<mj-text color="#445566" font-size="11px" align="center" line-height="16px">
607607
&copy; Coderplanets Inc., All Rights Reserved.
608608
</mj-text>

0 commit comments

Comments
 (0)