Can send mail through a smtp relay
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2021-06-12 10:03:32 +02:00
parent fa494910cf
commit b014c3fb3c
3 changed files with 57 additions and 24 deletions

View File

@ -19,6 +19,10 @@ type LDAP struct {
BaseDN string BaseDN string
ServiceDN string ServiceDN string
ServicePassword string ServicePassword string
MailHost string
MailPort int
MailUser string
MailPassword string
} }
func (l LDAP) Connect() (*LDAPConn, error) { func (l LDAP) Connect() (*LDAPConn, error) {

13
lost.go
View File

@ -116,8 +116,18 @@ func lostPassword(w http.ResponseWriter, r *http.Request) {
m.SetHeader("Subject", "SSO nemunai.re: password recovery") m.SetHeader("Subject", "SSO nemunai.re: password recovery")
m.SetBody("text/plain", "Hello "+cn+"!\n\nSomeone, and we hope it's you, requested to reset your account password. \nIn order to continue, go to:\nhttps://ldap.nemunai.re/reset?l="+r.PostFormValue("login")+"&t="+token+"\n\nBest regards,\n-- \nnemunai.re SSO") m.SetBody("text/plain", "Hello "+cn+"!\n\nSomeone, and we hope it's you, requested to reset your account password. \nIn order to continue, go to:\nhttps://ldap.nemunai.re/reset?l="+r.PostFormValue("login")+"&t="+token+"\n\nBest regards,\n-- \nnemunai.re SSO")
var s gomail.Sender
if myLDAP.MailHost != "" {
d := gomail.NewDialer(myLDAP.MailHost, myLDAP.MailPort, myLDAP.MailUser, myLDAP.MailPassword)
s, err = d.Dial()
if err != nil {
log.Println("Unable to connect to email server: " + err.Error())
displayTmplError(w, http.StatusInternalServerError, "lost.html", map[string]interface{}{"error": "Unable to connect to email server: " + err.Error()})
return
}
} else {
// Using local sendmail: delegate to the local admin sys the responsability to transport the mail // Using local sendmail: delegate to the local admin sys the responsability to transport the mail
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error { s = gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
cmd := exec.Command("sendmail", "-t") cmd := exec.Command("sendmail", "-t")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -143,6 +153,7 @@ func lostPassword(w http.ResponseWriter, r *http.Request) {
} }
return nil return nil
}) })
}
if err := gomail.Send(s, m); err != nil { if err := gomail.Send(s, m); err != nil {
log.Println("Unable to send email: " + err.Error()) log.Println("Unable to send email: " + err.Error())

18
main.go
View File

@ -21,6 +21,7 @@ var myLDAP = LDAP{
Host: "localhost", Host: "localhost",
Port: 389, Port: 389,
BaseDN: "dc=example,dc=com", BaseDN: "dc=example,dc=com",
MailPort: 587,
} }
type ResponseWriterPrefix struct { type ResponseWriterPrefix struct {
@ -125,6 +126,23 @@ func main() {
myLDAP.ServicePassword = val myLDAP.ServicePassword = val
} }
if val, ok := os.LookupEnv("SMTP_HOST"); ok {
myLDAP.MailHost = val
}
if val, ok := os.LookupEnv("SMTP_PORT"); ok {
if port, err := strconv.Atoi(val); err == nil {
myLDAP.MailPort = port
} else {
log.Println("Invalid value for SMTP_PORT:", val)
}
}
if val, ok := os.LookupEnv("SMTP_USER"); ok {
myLDAP.MailUser = val
}
if val, ok := os.LookupEnv("SMTP_PASSWORD"); ok {
myLDAP.MailPassword = val
}
// Prepare graceful shutdown // Prepare graceful shutdown
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)