Skip to content

Commit

Permalink
add templates for welcome, forgot password and onboarding + add email…
Browse files Browse the repository at this point in the history
… handler
  • Loading branch information
cedricve committed Jun 12, 2024
1 parent 479e2d8 commit 57c1194
Show file tree
Hide file tree
Showing 11 changed files with 1,740 additions and 89 deletions.
85 changes: 82 additions & 3 deletions api/controllers/email.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
package controllers

func SendEmail(title string, body string) error {
// Send email
return nil
import (
"log"
"os"

"github.com/uug-ai/facial-access-control/api/models"
"github.com/uug-ai/facial-access-control/api/notifications"
)

type Email struct {
Type string
Mailgun notifications.Mail
SMTP notifications.SMTP
}

func (email Email) Send(m notifications.Message) {
if email.Type == "mailgun" {
err := email.Mailgun.Send(m) // TODO add error handler
if err != nil {
log.Println(err)
}
} else if email.Type == "smtp" {
err := email.SMTP.Send(m) // TODO add error handler
if err != nil {
log.Println(err)
}
}
}

func Mail(mailto string, template string) *Email {
emailProvider := os.Getenv("MAIL_PROVIDER")
if emailProvider == "" {
emailProvider = "mailgun"
}
var email Email
if emailProvider == "mailgun" {
mailgun := notifications.Mail{
Domain: os.Getenv("MAILGUN_DOMAIN"),
ApiKey: os.Getenv("MAILGUN_API_KEY"),
EmailFrom: os.Getenv("EMAIL_FROM"),
EmailTo: mailto,
TemplateId: template,
}
email.Type = "mailgun"
email.Mailgun = mailgun
} else if emailProvider == "smtp" {
smtp := notifications.SMTP{
Server: os.Getenv("SMTP_SERVER"),
Port: os.Getenv("SMTP_PORT"),
Username: os.Getenv("SMTP_USERNAME"),
Password: os.Getenv("SMTP_PASSWORD"),
EmailFrom: os.Getenv("EMAIL_FROM"),
EmailTo: mailto,
TemplateId: template,
}
email.Type = "smtp"
email.SMTP = smtp
}
return &email
}

func SendWelcomeEmail(user models.User, m notifications.Message) {
m.Title = os.Getenv("WELCOME_TITLE")
email := Mail(user.Email, os.Getenv("WELCOME_TEMPLATE"))
email.Send(m)
}

func SendShareEmail(user models.User, m notifications.Message) {
m.Title = os.Getenv("SHARE_TITLE")
email := Mail(user.Email, os.Getenv("SHARE_TEMPLATE"))
email.Send(m) // TODO add error handler
}

func SendActivationEmail(user models.User, m notifications.Message) {
m.Title = os.Getenv("ACTIVATE_TITLE")
email := Mail(user.Email, os.Getenv("ACTIVATE_TEMPLATE"))
email.Send(m) // TODO add error handler
}

func SendForgotEmail(user models.User, m notifications.Message) {
m.Title = os.Getenv("FORGOT_TITLE")
email := Mail(user.Email, os.Getenv("FORGOT_TEMPLATE"))
email.Send(m) // TODO add error handler
}
43 changes: 26 additions & 17 deletions api/go.mod
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
module github.com/uug-ai/facial-access-control/api

go 1.22.1
go 1.22.2

require (
github.com/appleboy/gin-jwt/v2 v2.9.2
github.com/gin-contrib/cors v1.7.1
github.com/gin-contrib/pprof v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/gin-contrib/cors v1.7.2
github.com/gin-contrib/pprof v1.5.0
github.com/gin-gonic/gin v1.10.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/mailgun/mailgun-go/v4 v4.12.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/sirupsen/logrus v1.9.3
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 57c1194

Please sign in to comment.