Change to more powerful mail library that especially adds a correct Date
heeader for us.
This commit is contained in:
parent
22a079e10f
commit
a05407eba0
4 changed files with 111 additions and 24 deletions
|
@ -7,10 +7,9 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"github.com/wneessen/go-mail"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/smtp"
|
||||
"officeHours/config"
|
||||
"officeHours/models"
|
||||
"officeHours/templating"
|
||||
|
@ -132,13 +131,12 @@ func (r *RequestRepo) newSecret() (string, error) {
|
|||
}
|
||||
|
||||
func (r *RequestRepo) sendConfirmationMail(request models.Request) error {
|
||||
var message bytes.Buffer
|
||||
var messageText bytes.Buffer
|
||||
var data = struct {
|
||||
Config config.Config
|
||||
Request models.Request
|
||||
MessageId template.HTML
|
||||
}{r.config, request, template.HTML("<" + randomString(15) + "@" + r.config.Server.Domain + ">")}
|
||||
err := templating.WriteTemplate(&message, "confirmationMail", data)
|
||||
Config config.Config
|
||||
Request models.Request
|
||||
}{r.config, request}
|
||||
err := templating.WriteTemplate(&messageText, "confirmationMail", data)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error parsing confirmation Mail: %w", err)
|
||||
log.Println(err.Error())
|
||||
|
@ -147,19 +145,40 @@ func (r *RequestRepo) sendConfirmationMail(request models.Request) error {
|
|||
|
||||
switch r.config.Mailer.Type {
|
||||
case "Stdout":
|
||||
fmt.Println(message.String())
|
||||
fmt.Println(messageText.String())
|
||||
case "Smtp":
|
||||
to := []string{request.OfficeHour.Tutor.Email}
|
||||
var auth smtp.Auth
|
||||
if r.config.Mailer.SmtpUseAuth {
|
||||
auth = smtp.PlainAuth(r.config.Mailer.SmtpIdentity, r.config.Mailer.FromAddress, r.config.Mailer.SmtpPassword, r.config.Mailer.SmtpHost)
|
||||
}
|
||||
err = smtp.SendMail(fmt.Sprintf("%s:%d", r.config.Mailer.SmtpHost, r.config.Mailer.SmtpPort), auth, string(r.config.Mailer.FromName), to, message.Bytes())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error sending mail by smtp: %w", err)
|
||||
message := mail.NewMsg()
|
||||
if err := message.From(r.config.Mailer.FromAddress); err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
if err := message.To(request.OfficeHour.Tutor.Email); err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
switch request.Action {
|
||||
case models.RequestActivate:
|
||||
message.Subject("Sprechstunde anlegen")
|
||||
case models.RequestDelete:
|
||||
message.Subject("Sprechstunde löschen")
|
||||
}
|
||||
message.SetBodyString(mail.TypeTextPlain, messageText.String())
|
||||
|
||||
var options []mail.Option
|
||||
if r.config.Mailer.SmtpUseAuth {
|
||||
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthPlain))
|
||||
options = append(options, mail.WithUsername(r.config.Mailer.SmtpIdentity))
|
||||
options = append(options, mail.WithPassword(r.config.Mailer.SmtpPassword))
|
||||
}
|
||||
client, err := mail.NewClient(r.config.Mailer.SmtpHost, mail.WithPort(r.config.Mailer.SmtpPort))
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
if err := client.DialAndSend(message); err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue