-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |