Skip to content

Commit bee4cea

Browse files
committed
Add same smtp mail backend for marketing and for info
1 parent 742e44b commit bee4cea

File tree

8 files changed

+75
-17
lines changed

8 files changed

+75
-17
lines changed

.env

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
SIHL_SECRET=secret
1+
SIHL_SECRET=secretsecretsecret
22
DATABASE_URL=postgres://admin:[email protected]:5432/dev
33
DATABASE_POOL_SIZE=10
4-
SMTP_SENDER=[email protected]
5-
SMTP_PORT=587
6-
SMTP_USERNAME=user
7-
SMTP_HOST=smtp.example.com
8-
SMTP_START_TLS=true
9-
SMTP_PASSWORD=yourpassword
10-
CA_DIR=/etc/ssl/certs

app/context/pizza/pizza.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ include Model
22

33
exception Exception of string
44

5-
let clean =
5+
let clean () =
66
if Sihl.Configuration.is_production ()
77
then
88
raise
99
@@ Exception
1010
"Can not clean repository in production, this is most likely not what \
1111
you want"
12-
else Repo.clean
12+
else Repo.clean ()
1313
;;
1414

1515
let find_ingredient name = Repo.find_ingredient name

config/mail.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
SMTP_USERNAME=user
3+
SMTP_PASSWORD=yourpassword
4+
SMTP_HOST=smtp.example.com
5+
SMTP_PORT=587
6+
SMTP_START_TLS=true
7+
CA_DIR=/etc/ssl/certs

dune-project

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ A restaurant serving Pizza and sometimes Lasagna
2626
(depends
2727
(ocaml (>= 4.08.0))
2828
dune
29-
(sihl (= 0.4.0))
30-
(sihl-user (= 0.4.0))
31-
(sihl-queue (= 0.4.0))
29+
(sihl (= 0.4.1))
30+
(sihl-user (= 0.4.1))
31+
(sihl-queue (= 0.4.1))
32+
(sihl-email (= 0.4.1))
3233
(tyxml-ppx (>= 4.4.0))
3334
(caqti-driver-postgresql (>= 1.2.1))
3435
(alcotest-lwt :with-test)

pizza.opam

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ bug-reports: "https://github.com/oxidizing/pizza/issues"
1313
depends: [
1414
"ocaml" {>= "4.08.0"}
1515
"dune"
16-
"sihl" {= "0.4.0"}
17-
"sihl-user" {= "0.4.0"}
18-
"sihl-queue" {= "0.4.0"}
16+
"sihl" {= "0.4.1"}
17+
"sihl-user" {= "0.4.1"}
18+
"sihl-queue" {= "0.4.1"}
19+
"sihl-email" {= "0.4.1"}
1920
"tyxml-ppx" {>= "4.4.0"}
2021
"caqti-driver-postgresql" {>= "1.2.1"}
2122
"alcotest-lwt" {with-test}

run/run.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ let services =
1919
~jobs:
2020
[ Sihl_queue.hide Job.cook_pizza; Sihl_queue.hide Job.order_ingredient ]
2121
()
22+
; Service.MarketingMail.register ()
23+
; Service.InfoMail.register ()
2224
]
2325
;;
2426

service/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(library
22
(name service)
3-
(libraries sihl sihl-user sihl-queue))
3+
(libraries sihl sihl-user sihl-queue sihl-email))

service/service.ml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
11
module Migration = Sihl.Database.Migration.PostgreSql
22
module User = Sihl_user.PostgreSql
33
module Queue = Sihl_queue.PostgreSql
4+
5+
module MarketingSmtpConfig = struct
6+
let config () =
7+
Lwt.return
8+
Sihl_email.
9+
{ sender = "[email protected]"
10+
; username = "vinnie"
11+
; password = "pinapple0nPizz4"
12+
; hostname = "smtp.example.com"
13+
; port = Some 587
14+
; start_tls = true
15+
; ca_path = Some "/etc/ssl/certs"
16+
; ca_cert = None
17+
; console = Some true
18+
}
19+
;;
20+
end
21+
22+
module InfoSmtpConfig = struct
23+
let config () =
24+
let open Lwt.Syntax in
25+
Lwt_io.with_file ~mode:Lwt_io.Input "config/mail.cfg" (fun file ->
26+
let* content = Lwt_stream.to_list @@ Lwt_io.read_lines file in
27+
let config =
28+
content
29+
|> Stdlib.List.map (Stdlib.String.split_on_char '=')
30+
|> Stdlib.List.map (function
31+
| [] -> "", ""
32+
| [ key ] -> key, ""
33+
| [ key; value ] -> key, value
34+
| key :: values -> key, Stdlib.String.concat "" values)
35+
in
36+
Lwt.return
37+
Sihl_email.
38+
{ sender = Stdlib.List.assoc "SMTP_SENDER" config
39+
; username = Stdlib.List.assoc "SMTP_USERNAME" config
40+
; password = Stdlib.List.assoc "SMTP_PASSWORD" config
41+
; hostname = Stdlib.List.assoc "SMTP_HOST" config
42+
; port =
43+
Option.map int_of_string
44+
@@ Stdlib.List.assoc_opt "SMTP_PORT" config
45+
; start_tls =
46+
bool_of_string @@ Stdlib.List.assoc "SMTP_START_TLS" config
47+
; ca_path = Stdlib.List.assoc_opt "CA_DIR" config
48+
; ca_cert = Stdlib.List.assoc_opt "CA_PATH" config
49+
; console =
50+
Option.map bool_of_string
51+
@@ Stdlib.List.assoc_opt "SMTP_CONSOLE" config
52+
})
53+
;;
54+
end
55+
56+
module MarketingMail = Sihl_email.MakeSmtp (MarketingSmtpConfig)
57+
module InfoMail = Sihl_email.MakeSmtp (InfoSmtpConfig)

0 commit comments

Comments
 (0)