Fix leak of first recipient to other recipients because mail headers are also cached and re-used
This commit is contained in:
parent
a4fd43e03d
commit
c1b3d106a0
4 changed files with 23 additions and 17 deletions
|
@ -47,30 +47,30 @@ type SmtpSettings struct {
|
|||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func sendNotices(recipient string, notices []*WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, cache *map[string][]byte) error {
|
||||
func sendNotices(recipient string, notices []*WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, mailContentCache *map[string]*MailContent) error {
|
||||
logger.debug("Generating and sending mails for recipient " + recipient + " ...")
|
||||
cacheHits := 0
|
||||
cacheMisses := 0
|
||||
mails := [][]byte{}
|
||||
mails := []*MailContent{}
|
||||
for _, n := range notices {
|
||||
var data []byte
|
||||
cacheResult := (*cache)[n.Uuid]
|
||||
if len(cacheResult) > 0 {
|
||||
var mc *MailContent
|
||||
cacheResult := (*mailContentCache)[n.Uuid]
|
||||
if cacheResult != nil {
|
||||
cacheHits++
|
||||
data = cacheResult
|
||||
mc = cacheResult
|
||||
} else {
|
||||
cacheMisses++
|
||||
mailContent, err := template.generate(TemplateData{n, Version})
|
||||
mc_, err := template.generate(TemplateData{n, Version})
|
||||
if err != nil {
|
||||
logger.error("Could not create mail from template")
|
||||
logger.error(err)
|
||||
}
|
||||
// serialize mail
|
||||
data = mailContent.serializeValidMail(smtpConfig.From, recipient)
|
||||
} else {
|
||||
mc = &mc_
|
||||
// add to cache
|
||||
(*cache)[n.Uuid] = data
|
||||
(*mailContentCache)[n.Uuid] = mc
|
||||
}
|
||||
mails = append(mails, data)
|
||||
}
|
||||
mails = append(mails, mc)
|
||||
}
|
||||
logger.debug(fmt.Sprintf("%v mail cache hits, %v misses", cacheHits, cacheMisses))
|
||||
err := sendMails(
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, mails []*MailContent) error {
|
||||
addr := fmt.Sprintf("%v:%v", smtpConf.ServerHost, smtpConf.ServerPort)
|
||||
logger.debug("Connecting to mail server at " + addr + " ...")
|
||||
connection, err := smtp.Dial(addr)
|
||||
|
@ -32,7 +32,10 @@ func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte)
|
|||
if logger.LogLevel >= 3 {
|
||||
fmt.Printf("DEBUG %v Sending mails to server ", time.Now().Format("2006/01/02 15:04:05.000000"))
|
||||
}
|
||||
for _, d := range data {
|
||||
for _, mc := range mails {
|
||||
// serialize mail
|
||||
d := mc.serializeValidMail(smtpConf.From, to)
|
||||
// send mail
|
||||
err = connection.Mail(smtpConf.From)
|
||||
if err != nil { return err }
|
||||
err = connection.Rcpt(to)
|
||||
|
|
|
@ -8,10 +8,13 @@ import (
|
|||
"net/smtp"
|
||||
)
|
||||
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, mails []*MailContent) error {
|
||||
logger.warn("Mail Transfer Debugging is active. Not connecting.")
|
||||
logger.info("MAIL TRANSFER: \n\n")
|
||||
for _, d := range data {
|
||||
for _, mc := range mails {
|
||||
// serialize mail
|
||||
d := mc.serializeValidMail(smtpConf.From, to)
|
||||
// output mail
|
||||
fmt.Println("MAIL FROM:" + smtpConf.From)
|
||||
fmt.Println("RCPT TO:" + to)
|
||||
fmt.Println("DATA")
|
||||
|
|
2
main.go
2
main.go
|
@ -98,7 +98,7 @@ func main() {
|
|||
t1 := time.Now().UnixMilli()
|
||||
newNotices := []WidNotice{}
|
||||
lastPublished := map[string]time.Time{} // endpoint id : last published timestamp
|
||||
cache := map[string][]byte{} // cache generated emails for reuse
|
||||
cache := map[string]*MailContent{} // cache generated emails for reuse
|
||||
for _, a := range enabledApiEndpoints {
|
||||
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
|
||||
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue