mirror of
https://github.com/1f349/simplemail.git
synced 2024-12-22 08:04:11 +00:00
Improve localhost detection
This commit is contained in:
parent
80dd3b55d1
commit
0565d6116b
26
mail.go
26
mail.go
@ -2,6 +2,7 @@ package simplemail
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"github.com/emersion/go-message/mail"
|
"github.com/emersion/go-message/mail"
|
||||||
"github.com/emersion/go-sasl"
|
"github.com/emersion/go-sasl"
|
||||||
"github.com/emersion/go-smtp"
|
"github.com/emersion/go-smtp"
|
||||||
@ -31,7 +32,7 @@ func (m *Mail) mailCall(to []string, r io.Reader) error {
|
|||||||
if m.Tls {
|
if m.Tls {
|
||||||
return smtp.SendMailTLS(m.Server, m.loginInfo(), m.From.String(), to, r)
|
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
|
// internals of smtp.SendMail without STARTTLS for localhost testing
|
||||||
dial, err := smtp.Dial(m.Server)
|
dial, err := smtp.Dial(m.Server)
|
||||||
if err != nil {
|
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)
|
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 {
|
func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody, textBody io.Reader) error {
|
||||||
// generate the email in this template
|
// generate the email in this template
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
Loading…
Reference in New Issue
Block a user