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
import (
"bytes"
"errors"
"fmt"
"html/template"
"log"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
// "github.com/emersion/go-sasl"
// "github.com/emersion/go-smtp"
"net/smtp"
"github.com/go-ldap/ldap/v3"
// "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")
}
// Préparation du courriel à envoyer
code := "GPas"
templateMail := template.Must(template.ParseFiles(templatePath + "/invite_mail.txt"))
buf := bytes.NewBuffer([]byte{})
templateMail.Execute(buf, &CodeMailFields{
To: user.OtherMailbox,
From: config.MailFrom,
InviteFrom: "GPas",
Code: code,
WebBaseAddress: config.WebAddress,
})
// code := "GPas"
// templateMail := template.Must(template.ParseFiles(templatePath + "/invite_mail.txt"))
// buf := bytes.NewBuffer([]byte{})
// templateMail.Execute(buf, &CodeMailFields{
// To: user.OtherMailbox,
// From: config.MailFrom,
// InviteFrom: "GPas",
// Code: code,
// WebBaseAddress: config.WebAddress,
// })
message := []byte("Hi " + user.OtherMailbox)
log.Printf("Sending mail to: %s", user.OtherMailbox)
var auth sasl.Client = nil
if config.SMTPUsername != "" {
auth = sasl.NewPlainClient("", config.SMTPUsername, config.SMTPPassword)
}
err = smtp.SendMail(config.SMTPServer, auth, config.MailFrom, []string{user.OtherMailbox}, buf)
// var auth sasl.Client = nil
// if config.SMTPUsername != "" {
// auth = sasl.NewPlainClient("", config.SMTPUsername, config.SMTPPassword)
// }
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 {
return err
}