Can send mail through a smtp relay
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
fa494910cf
commit
b014c3fb3c
4
ldap.go
4
ldap.go
@ -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) {
|
||||||
|
53
lost.go
53
lost.go
@ -116,33 +116,44 @@ 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")
|
||||||
|
|
||||||
// Using local sendmail: delegate to the local admin sys the responsability to transport the mail
|
var s gomail.Sender
|
||||||
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
|
if myLDAP.MailHost != "" {
|
||||||
cmd := exec.Command("sendmail", "-t")
|
d := gomail.NewDialer(myLDAP.MailHost, myLDAP.MailPort, myLDAP.MailUser, myLDAP.MailPassword)
|
||||||
cmd.Stdout = os.Stdout
|
s, err = d.Dial()
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
pw, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
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()
|
pw, err := cmd.StdinPipe()
|
||||||
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err := gomail.Send(s, m); err != nil {
|
||||||
log.Println("Unable to send email: " + err.Error())
|
log.Println("Unable to send email: " + err.Error())
|
||||||
|
24
main.go
24
main.go
@ -18,9 +18,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var myLDAP = LDAP{
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user