Skip to content

Commit

Permalink
add: adding mailer functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
puni9869 committed May 7, 2024
1 parent 3380433 commit c88b397
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ tmp/
# Go workspace file
go.work
# Config files for localhost
config/local.json
local.json
prod.json

8 changes: 8 additions & 0 deletions config/local.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"useCDN": false,
"hostname": "localhost"
},
"mailer": {
"smtpHost": "",
"smtpPort": "",
"emailId": "",
"bccEmailId": "",
"username": "",
"password": ""
},
"database": {
"type": "postgres",
"host": "127.0.0.1",
Expand Down
10 changes: 9 additions & 1 deletion config/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
"environment": "local",
"appConfig": {
"debug": false,
"secretKey": "APP_SECRET_KEY",
"secretKey": "$APP_SECRET_KEY",
"defaultPort": "8080",
"customPort": "",
"useCDN": false,
"hostname": "pinmyblogs.com"
},
"mailer": {
"smtpHost": "",
"smtpPort": "",
"emailId": "",
"bccEmailId": "",
"username": "",
"password": ""
},
"database": {
"type": "postgres",
"username": "username",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ require (
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,15 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
1 change: 1 addition & 0 deletions internal/signup/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package signup

import (
"errors"

"github.com/gin-gonic/gin"
"github.com/puni9869/pinmyblogs/models"
"github.com/puni9869/pinmyblogs/pkg/mailer"
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package config

import (
"errors"

"github.com/spf13/viper"
)

var (
defaultEnv = "local"
defaultPath = "./config"
LocalEnv = "local"
ProdEnv = "prod"
environments = []string{"local", "prod"}
)

Expand Down Expand Up @@ -37,12 +40,22 @@ type Authentication struct {
EnableRegistration bool `json:"enableRegistration"`
}

type Mailer struct {
SmtpHost string `json:"smtpHost"`
SmtpPort int `json:"smtpPort"`
EmailId string `json:"emailId"`
BccEmailId string `json:"bccEmailId"`
Username string `json:"username"`
Password string `json:"password"`
}

// ConfigProvider ie converted value in go types
type ConfigProvider struct {
EnableSSL bool `json:"enableSSL"`
Database Database `json:"database"`
AppConfig AppConfig `json:"appConfig"`
Authentication Authentication `json:"authentication"`
Mailer Mailer `json:"mailer"`
}

var C ConfigProvider
Expand Down
45 changes: 44 additions & 1 deletion pkg/mailer/registration_mailer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package mailer

import (
"fmt"

"github.com/puni9869/pinmyblogs/models"
"github.com/puni9869/pinmyblogs/pkg/config"
"github.com/puni9869/pinmyblogs/pkg/logger"
"github.com/sirupsen/logrus"
"gopkg.in/gomail.v2"
)

type UserRegister struct {
Expand All @@ -13,11 +17,50 @@ type UserRegister struct {
}

func (u *UserRegister) Send() {
if config.GetEnv() != config.LocalEnv {
u.log.WithFields(map[string]any{
"user": u.user.Email,
"id": u.user.ID,
"env": config.GetEnv(),
}).Info("user registration confirmation mail sent")
return
}
tmpl := fmt.Sprintf(`<!DOCTYPE>
<html>
<body>
Hi there, welcome to pinmyblogs.
We have successfully registered the user with email: %s<br/>
<br/>
Regards,
<br/>
pinmyblogs and team
</body>
</html>
`, u.user.Email)

msg := gomail.NewMessage()
msg.SetHeader("From", config.C.Mailer.EmailId)
msg.SetHeader("To", u.user.Email)
msg.SetHeader("Bcc", config.C.Mailer.BccEmailId)
msg.SetHeader("Subject", "Welcome to pinmyblogs.com")
msg.SetBody("text/html", tmpl)

m := gomail.NewDialer(
config.C.Mailer.SmtpHost,
config.C.Mailer.SmtpPort,
config.C.Mailer.Username,
config.C.Mailer.Password,
)

if err := m.DialAndSend(msg); err != nil {
u.log.WithError(err).Error("error sending email")
return
}
u.log.WithFields(map[string]any{
"user": u.user.Email,
"id": u.user.ID,
"env": config.GetEnv(),
}).Info("user registration confirmation mail sent")

}

func NewUserRegisterMailer(user models.User) MailerAPI {
Expand Down
2 changes: 2 additions & 0 deletions templates/mailer/user_registered.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{define "mailer/user_registered"}}
{{end}}

0 comments on commit c88b397

Please sign in to comment.