This commit is contained in:
Chris Mann 2023-07-23 09:16:17 +02:00
parent dca6b2190b
commit 7eedf221d5

40
gpas.go
View file

@ -5,14 +5,14 @@ gpas is GVoisin password reset
package main package main
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"html/template"
"log" "log"
"github.com/emersion/go-sasl" // "github.com/emersion/go-sasl"
"github.com/emersion/go-smtp" // "github.com/emersion/go-smtp"
"net/smtp"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
// "strings" // "strings"
) )
@ -46,22 +46,24 @@ func passwordLost(user User, config *ConfigFile, ldapConn *ldap.Conn) error {
return errors.New("Il n'y a pas d'utilisateur qui correspond") return errors.New("Il n'y a pas d'utilisateur qui correspond")
} }
// Préparation du courriel à envoyer // Préparation du courriel à envoyer
code := "GPas" // code := "GPas"
templateMail := template.Must(template.ParseFiles(templatePath + "/invite_mail.txt")) // templateMail := template.Must(template.ParseFiles(templatePath + "/invite_mail.txt"))
buf := bytes.NewBuffer([]byte{}) // buf := bytes.NewBuffer([]byte{})
templateMail.Execute(buf, &CodeMailFields{ // templateMail.Execute(buf, &CodeMailFields{
To: user.OtherMailbox, // To: user.OtherMailbox,
From: config.MailFrom, // From: config.MailFrom,
InviteFrom: "GPas", // InviteFrom: "GPas",
Code: code, // Code: code,
WebBaseAddress: config.WebAddress, // WebBaseAddress: config.WebAddress,
}) // })
message := []byte("Hi " + user.OtherMailbox)
log.Printf("Sending mail to: %s", user.OtherMailbox) log.Printf("Sending mail to: %s", user.OtherMailbox)
var auth sasl.Client = nil // var auth sasl.Client = nil
if config.SMTPUsername != "" { // if config.SMTPUsername != "" {
auth = sasl.NewPlainClient("", config.SMTPUsername, config.SMTPPassword) // auth = sasl.NewPlainClient("", config.SMTPUsername, config.SMTPPassword)
} // }
err = smtp.SendMail(config.SMTPServer, auth, config.MailFrom, []string{user.OtherMailbox}, buf) auth := smtp.PlainAuth("", config.SMTPUsername, config.SMTPPassword, config.SMTPServer)
err = smtp.SendMail(config.SMTPServer+":587", auth, config.SMTPUsername, []string{user.OtherMailbox}, message)
if err != nil { if err != nil {
return err return err
} }