refactor: insecure no auth/tls
This commit is contained in:
+33
-14
@@ -18,6 +18,19 @@ const (
|
||||
delimeter = "**=myohmy689407924327"
|
||||
)
|
||||
|
||||
type InsecureConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
From string // Sender email address
|
||||
}
|
||||
|
||||
type SecureConfig struct {
|
||||
Auth smtp.Auth
|
||||
Host string
|
||||
Port string
|
||||
From string // Sender email address
|
||||
}
|
||||
|
||||
type EmailMessage struct {
|
||||
To string
|
||||
Subject string
|
||||
@@ -46,7 +59,16 @@ type EmailService struct {
|
||||
dial SmtpDialFn
|
||||
}
|
||||
|
||||
func NewInsecure(config MailServiceConfig) *EmailService {
|
||||
func NewInsecureNoAuth(config InsecureConfig) *EmailService {
|
||||
return &EmailService{
|
||||
host: config.Host,
|
||||
port: config.Port,
|
||||
from: config.From,
|
||||
dial: dial,
|
||||
}
|
||||
}
|
||||
|
||||
func NewInsecure(config SecureConfig) *EmailService {
|
||||
return &EmailService{
|
||||
auth: config.Auth,
|
||||
host: config.Host,
|
||||
@@ -121,7 +143,7 @@ func customVerify(host string) func(cs tls.ConnectionState) error {
|
||||
}
|
||||
}
|
||||
|
||||
func NewSecure(config MailServiceConfig) *EmailService {
|
||||
func NewSecure(config SecureConfig) *EmailService {
|
||||
return &EmailService{
|
||||
auth: config.Auth,
|
||||
host: config.Host,
|
||||
@@ -136,7 +158,7 @@ func NewSecure(config MailServiceConfig) *EmailService {
|
||||
}
|
||||
}
|
||||
|
||||
func NewSecure465(config MailServiceConfig) *EmailService {
|
||||
func NewSecure465(config SecureConfig) *EmailService {
|
||||
tlsCfg := tls.Config{
|
||||
// Ideally, InsecureSkipVerify: false,
|
||||
// or do a proper certificate validation
|
||||
@@ -156,13 +178,6 @@ func NewSecure465(config MailServiceConfig) *EmailService {
|
||||
}
|
||||
}
|
||||
|
||||
type MailServiceConfig struct {
|
||||
Auth smtp.Auth
|
||||
Host string
|
||||
Port string
|
||||
From string // Sender email address
|
||||
}
|
||||
|
||||
func dial(hostPort string) (SMTPClientIface, error) {
|
||||
client, err := smtp.Dial(hostPort)
|
||||
if err != nil {
|
||||
@@ -212,13 +227,17 @@ func (e *EmailService) send(to string, msg []byte) error {
|
||||
return fmt.Errorf("DIAL: %s", err)
|
||||
}
|
||||
|
||||
if err = c.StartTLS(e.tlsconfig); err != nil {
|
||||
return fmt.Errorf("c.StartTLS: %s", err)
|
||||
if e.tlsconfig != nil {
|
||||
if err = c.StartTLS(e.tlsconfig); err != nil {
|
||||
return fmt.Errorf("c.StartTLS: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Auth
|
||||
if err = c.Auth(e.auth); err != nil {
|
||||
return fmt.Errorf("c.Auth: %s", err)
|
||||
if e.auth != nil {
|
||||
if err = c.Auth(e.auth); err != nil {
|
||||
return fmt.Errorf("c.Auth: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// To && From
|
||||
|
||||
Reference in New Issue
Block a user