diff --git a/ldap.go b/ldap.go index e41deff..2050e7c 100644 --- a/ldap.go +++ b/ldap.go @@ -19,6 +19,10 @@ type LDAP struct { BaseDN string ServiceDN string ServicePassword string + MailHost string + MailPort int + MailUser string + MailPassword string } func (l LDAP) Connect() (*LDAPConn, error) { diff --git a/lost.go b/lost.go index d2aa34d..33abb11 100644 --- a/lost.go +++ b/lost.go @@ -116,33 +116,44 @@ func lostPassword(w http.ResponseWriter, r *http.Request) { 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") - // 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 { - cmd := exec.Command("sendmail", "-t") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - pw, err := cmd.StdinPipe() + 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 { - return err + 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 + s = gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error { + cmd := exec.Command("sendmail", "-t") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr - err = cmd.Start() - if err != nil { - return err - } - - var errs [3]error - _, errs[0] = m.WriteTo(pw) - errs[1] = pw.Close() - errs[2] = cmd.Wait() - for _, err = range errs { + pw, err := cmd.StdinPipe() if err != nil { return err } - } - return nil - }) + + err = cmd.Start() + if err != nil { + return err + } + + var errs [3]error + _, errs[0] = m.WriteTo(pw) + errs[1] = pw.Close() + errs[2] = cmd.Wait() + for _, err = range errs { + if err != nil { + return err + } + } + return nil + }) + } if err := gomail.Send(s, m); err != nil { log.Println("Unable to send email: " + err.Error()) diff --git a/main.go b/main.go index 7d084bd..4049f75 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,10 @@ import ( ) var myLDAP = LDAP{ - Host: "localhost", - Port: 389, - BaseDN: "dc=example,dc=com", + Host: "localhost", + Port: 389, + BaseDN: "dc=example,dc=com", + MailPort: 587, } type ResponseWriterPrefix struct { @@ -125,6 +126,23 @@ func main() { 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 interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)