Improve localhost detection

This commit is contained in:
Melon 2024-09-21 16:08:33 +01:00
parent 80dd3b55d1
commit 0565d6116b
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

26
mail.go
View File

@ -2,6 +2,7 @@ package simplemail
import (
"bytes"
"context"
"github.com/emersion/go-message/mail"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
@ -31,7 +32,7 @@ func (m *Mail) mailCall(to []string, r io.Reader) error {
if m.Tls {
return smtp.SendMailTLS(m.Server, m.loginInfo(), m.From.String(), to, r)
}
if host == "localhost" || host == "127.0.0.1" {
if isLocalhost(host) {
// internals of smtp.SendMail without STARTTLS for localhost testing
dial, err := smtp.Dial(m.Server)
if err != nil {
@ -46,6 +47,29 @@ func (m *Mail) mailCall(to []string, r io.Reader) error {
return smtp.SendMail(m.Server, m.loginInfo(), m.From.String(), to, r)
}
func isLocalhost(host string) bool {
// lookup host with resolver
ip, err := net.DefaultResolver.LookupNetIP(context.Background(), "ip", host)
if err != nil {
return false
}
// missing list of addresses
if len(ip) < 1 {
return false
}
// if one ip is not loop back then this isn't localhost
for _, i := range ip {
if !i.IsLoopback() {
return false
}
}
// all addresses resolved are localhost
return true
}
func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody, textBody io.Reader) error {
// generate the email in this template
buf := new(bytes.Buffer)