Skip to content

Commit

Permalink
Make Email Async
Browse files Browse the repository at this point in the history
  • Loading branch information
1-Harshit committed Nov 2, 2021
1 parent 150fbe5 commit 36535ca
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 42 deletions.
7 changes: 5 additions & 2 deletions auth/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ func GenerateOtp(rollNo string) error {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}

if err = mail.SendOTP(rollNo, otp); err != nil {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
emailRequest := mail.EmailRequest{
To: rollNo,
OTP: otp,
}

mail.MailChannel <- emailRequest

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/iitk-coin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "github.com/bhuvansingla/iitk-coin/config"
"github.com/bhuvansingla/iitk-coin/database"
_ "github.com/bhuvansingla/iitk-coin/logger"
"github.com/bhuvansingla/iitk-coin/mail"
"github.com/bhuvansingla/iitk-coin/server"
log "github.com/sirupsen/logrus"
)
Expand All @@ -15,6 +16,12 @@ func main() {
log.Error("Error connecting to database: %s", err)
return
}

err = mail.Test()
if err != nil {
log.Error("Error sending mail: %s", err)
return
}

err = server.Start()
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions mail/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mail

import (
"net/smtp"

"github.com/spf13/viper"
)

type smtpServer struct {
host string
port string
}

func (s *smtpServer) Address() string {
return s.host + ":" + s.port
}

type EmailRequest struct {
To string
OTP string
}

var MailChannel chan EmailRequest
var from string
var password string
var server smtpServer
var auth smtp.Auth
var otpValidity string

func init() {
MailChannel = make(chan EmailRequest)
from = viper.GetString("MAIL.FROM")
password = viper.GetString("MAIL.PASSWORD")

otpValidity = viper.GetString("OTP.EXPIRY_PERIOD_IN_MIN")

server = smtpServer{host: viper.GetString("MAIL.HOST"), port: viper.GetString("MAIL.PORT")}
go mailService(MailChannel)
}

func authorize() {
auth = smtp.PlainAuth("", from, password, server.host)
}
40 changes: 0 additions & 40 deletions mail/send.go

This file was deleted.

34 changes: 34 additions & 0 deletions mail/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mail

import (
"net/smtp"

log "github.com/sirupsen/logrus"
)

func mailService(mailChannel chan EmailRequest) {
authorize()
for request := range mailChannel {
to := []string{request.To+"@iitk.ac.in"}
msg := []byte("To: " + request.To + "@iitk.ac.in" + "\n" +
"From: " + "IITK-Coin<" + from + ">\n" +
"Subject: IITK-Coin One Time Password\n" +
"Your OTP is " + request.OTP + "\n" +
"This OTP is valid for " + otpValidity + " minutes and don't share it with anyone." +
"\n")

err := smtp.SendMail(server.Address(), auth, from, to, msg)

// if error, try to login again
if err != nil {
authorize()
err = smtp.SendMail(server.Address(), auth, from, to, msg)
if err != nil {
log.Error("Error sending mail: " + err.Error())
continue
}
}

log.Info("Mail sent to ", request.To)
}
}
24 changes: 24 additions & 0 deletions mail/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mail

import (
"net/smtp"

log "github.com/sirupsen/logrus"
)

func Test() (err error) {
authorize()
to := []string{from}
msg := []byte("To: " + from + "\n" +
"From: " + "IITK-Coin<" + from + ">\n" +
"Subject: IITK-Coin Test Mail\n" +
"This is a Test Mail" +
"\n")

err = smtp.SendMail(server.Address(), auth, from, to, msg)
if err != nil {
return
}
log.Info("Test mail sent")
return
}

0 comments on commit 36535ca

Please sign in to comment.