Allow safe debugging of mail transfer by adding debug_mail_transfer build flag
This commit is contained in:
parent
30bf793598
commit
a4fd43e03d
3 changed files with 77 additions and 44 deletions
|
@ -3,14 +3,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"mime"
|
||||
"mime/quotedprintable"
|
||||
"net/mail"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MailContent struct {
|
||||
|
@ -86,48 +84,6 @@ func sendNotices(recipient string, notices []*WidNotice, template MailTemplate,
|
|||
return nil
|
||||
}
|
||||
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
|
||||
addr := fmt.Sprintf("%v:%v", smtpConf.ServerHost, smtpConf.ServerPort)
|
||||
logger.debug("Connecting to mail server at " + addr + " ...")
|
||||
connection, err := smtp.Dial(addr)
|
||||
if err != nil { return err }
|
||||
defer connection.Close()
|
||||
// can leave out connection.Hello
|
||||
hasTlsExt, _ := connection.Extension("starttls")
|
||||
if hasTlsExt {
|
||||
err = connection.StartTLS(&tls.Config{ServerName: smtpConf.ServerHost})
|
||||
if err != nil { return err }
|
||||
logger.debug("Mail Server supports TLS")
|
||||
} else {
|
||||
logger.debug("Mail Server doesn't support TLS")
|
||||
}
|
||||
logger.debug("Authenticating to mail server ...")
|
||||
err = connection.Auth(auth)
|
||||
if err != nil { return err }
|
||||
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 {
|
||||
err = connection.Mail(smtpConf.From)
|
||||
if err != nil { return err }
|
||||
err = connection.Rcpt(to)
|
||||
if err != nil { return err }
|
||||
writer, err := connection.Data()
|
||||
if err != nil { return err }
|
||||
_, err = writer.Write(d)
|
||||
if err != nil { return err }
|
||||
err = writer.Close()
|
||||
if err != nil { return err }
|
||||
if logger.LogLevel >= 3 {
|
||||
print(".")
|
||||
}
|
||||
}
|
||||
if logger.LogLevel >= 3 {
|
||||
print("\n")
|
||||
}
|
||||
return connection.Quit()
|
||||
}
|
||||
|
||||
func mailAddressIsValid(address string) bool {
|
||||
_, err := mail.ParseAddress(address);
|
||||
return err == nil
|
54
mail_transfer.go
Normal file
54
mail_transfer.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// +build !debug_mail_transfer
|
||||
// Copyright (c) 2023 Julian Müller (ChaoticByte)
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
|
||||
addr := fmt.Sprintf("%v:%v", smtpConf.ServerHost, smtpConf.ServerPort)
|
||||
logger.debug("Connecting to mail server at " + addr + " ...")
|
||||
connection, err := smtp.Dial(addr)
|
||||
if err != nil { return err }
|
||||
defer connection.Close()
|
||||
// can leave out connection.Hello
|
||||
hasTlsExt, _ := connection.Extension("starttls")
|
||||
if hasTlsExt {
|
||||
err = connection.StartTLS(&tls.Config{ServerName: smtpConf.ServerHost})
|
||||
if err != nil { return err }
|
||||
logger.debug("Mail Server supports StartTLS")
|
||||
} else {
|
||||
logger.debug("Mail Server doesn't support StartTLS")
|
||||
}
|
||||
logger.debug("Authenticating to mail server ...")
|
||||
err = connection.Auth(auth)
|
||||
if err != nil { return err }
|
||||
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 {
|
||||
err = connection.Mail(smtpConf.From)
|
||||
if err != nil { return err }
|
||||
err = connection.Rcpt(to)
|
||||
if err != nil { return err }
|
||||
writer, err := connection.Data()
|
||||
if err != nil { return err }
|
||||
_, err = writer.Write(d)
|
||||
if err != nil { return err }
|
||||
err = writer.Close()
|
||||
if err != nil { return err }
|
||||
if logger.LogLevel >= 3 {
|
||||
print(".")
|
||||
}
|
||||
}
|
||||
if logger.LogLevel >= 3 {
|
||||
print("\n")
|
||||
}
|
||||
return connection.Quit()
|
||||
}
|
23
mail_transfer_debug.go
Normal file
23
mail_transfer_debug.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
// +build debug_mail_transfer
|
||||
// Copyright (c) 2023 Julian Müller (ChaoticByte)
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
|
||||
logger.warn("Mail Transfer Debugging is active. Not connecting.")
|
||||
logger.info("MAIL TRANSFER: \n\n")
|
||||
for _, d := range data {
|
||||
fmt.Println("MAIL FROM:" + smtpConf.From)
|
||||
fmt.Println("RCPT TO:" + to)
|
||||
fmt.Println("DATA")
|
||||
fmt.Println(string(d))
|
||||
fmt.Println(".")
|
||||
}
|
||||
fmt.Print("\n\n")
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue