Skip to content

Commit

Permalink
Add same smtp mail backend for marketing and for info
Browse files Browse the repository at this point in the history
  • Loading branch information
aronerben committed Apr 7, 2021
1 parent 742e44b commit 5417465
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 17 deletions.
9 changes: 1 addition & 8 deletions .env
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
SIHL_SECRET=secret
SIHL_SECRET=secretsecretsecret
DATABASE_URL=postgres://admin:password@127.0.0.1:5432/dev
DATABASE_POOL_SIZE=10
SMTP_SENDER=sender@example.com
SMTP_PORT=587
SMTP_USERNAME=user
SMTP_HOST=smtp.example.com
SMTP_START_TLS=true
SMTP_PASSWORD=yourpassword
CA_DIR=/etc/ssl/certs
4 changes: 2 additions & 2 deletions app/context/pizza/pizza.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ include Model

exception Exception of string

let clean =
let clean () =
if Sihl.Configuration.is_production ()
then
raise
@@ Exception
"Can not clean repository in production, this is most likely not what \
you want"
else Repo.clean
else Repo.clean ()
;;

let find_ingredient name = Repo.find_ingredient name
Expand Down
7 changes: 7 additions & 0 deletions config/mail.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SMTP_SENDER=sender@example.com
SMTP_USERNAME=user
SMTP_PASSWORD=yourpassword
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_START_TLS=true
CA_DIR=/etc/ssl/certs
7 changes: 4 additions & 3 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ A restaurant serving Pizza and sometimes Lasagna
(depends
(ocaml (>= 4.08.0))
dune
(sihl (= 0.4.0))
(sihl-user (= 0.4.0))
(sihl-queue (= 0.4.0))
(sihl (= 0.4.1))
(sihl-user (= 0.4.1))
(sihl-queue (= 0.4.1))
(sihl-email (= 0.4.1))
(tyxml-ppx (>= 4.4.0))
(caqti-driver-postgresql (>= 1.2.1))
(alcotest-lwt :with-test)
Expand Down
7 changes: 4 additions & 3 deletions pizza.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ bug-reports: "https://github.com/oxidizing/pizza/issues"
depends: [
"ocaml" {>= "4.08.0"}
"dune"
"sihl" {= "0.4.0"}
"sihl-user" {= "0.4.0"}
"sihl-queue" {= "0.4.0"}
"sihl" {= "0.4.1"}
"sihl-user" {= "0.4.1"}
"sihl-queue" {= "0.4.1"}
"sihl-email" {= "0.4.1"}
"tyxml-ppx" {>= "4.4.0"}
"caqti-driver-postgresql" {>= "1.2.1"}
"alcotest-lwt" {with-test}
Expand Down
2 changes: 2 additions & 0 deletions run/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let services =
~jobs:
[ Sihl_queue.hide Job.cook_pizza; Sihl_queue.hide Job.order_ingredient ]
()
; Service.MarketingMail.register ()
; Service.InfoMail.register ()
]
;;

Expand Down
2 changes: 1 addition & 1 deletion service/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
(name service)
(libraries sihl sihl-user sihl-queue))
(libraries sihl sihl-user sihl-queue sihl-email))
54 changes: 54 additions & 0 deletions service/service.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
module Migration = Sihl.Database.Migration.PostgreSql
module User = Sihl_user.PostgreSql
module Queue = Sihl_queue.PostgreSql

module MarketingSmtpConfig = struct
let config () =
Lwt.return
Sihl_email.
{ sender = "marketing@vinniespiz.za"
; username = "vinnie"
; password = "pinapple0nPizz4"
; hostname = "smtp.example.com"
; port = Some 587
; start_tls = true
; ca_path = Some "/etc/ssl/certs"
; ca_cert = None
; console = Some true
}
;;
end

module InfoSmtpConfig = struct
let config () =
let open Lwt.Syntax in
Lwt_io.with_file ~mode:Lwt_io.Input "config/mail.cfg" (fun file ->
let* content = Lwt_stream.to_list @@ Lwt_io.read_lines file in
let config =
content
|> Stdlib.List.map (Stdlib.String.split_on_char '=')
|> Stdlib.List.map (function
| [] -> "", ""
| [ key ] -> key, ""
| [ key; value ] -> key, value
| key :: values -> key, Stdlib.String.concat "" values)
in
Lwt.return
Sihl_email.
{ sender = Stdlib.List.assoc "SMTP_SENDER" config
; username = Stdlib.List.assoc "SMTP_USERNAME" config
; password = Stdlib.List.assoc "SMTP_PASSWORD" config
; hostname = Stdlib.List.assoc "SMTP_HOST" config
; port =
Option.map int_of_string
@@ Stdlib.List.assoc_opt "SMTP_SENDER" config
; start_tls =
bool_of_string @@ Stdlib.List.assoc "SMTP_SENDER" config
; ca_path = Stdlib.List.assoc_opt "SMTP_SENDER" config
; ca_cert = Stdlib.List.assoc_opt "SMTP_SENDER" config
; console =
Option.map bool_of_string
@@ Stdlib.List.assoc_opt "SMTP_SENDER" config
})
;;
end

module MarketingMail = Sihl_email.MakeSmtp (MarketingSmtpConfig)
module InfoMail = Sihl_email.MakeSmtp (InfoSmtpConfig)

0 comments on commit 5417465

Please sign in to comment.