Add support to SSL/TLS/SSLTLS encryption for SMTP ()

This commit is contained in:
Jag_k 2022-09-30 11:26:17 +03:00 committed by GitHub
parent 2c2db61158
commit 63d6e1f391
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 7 deletions
emailer

View file

@ -3,9 +3,9 @@ package emailer
import (
"crypto/tls"
"fmt"
"time"
mail "github.com/xhit/go-simple-mail/v2"
"strings"
"time"
)
type SmtpMail struct {
@ -14,6 +14,7 @@ type SmtpMail struct {
username string
password string
authType mail.AuthType
encryption mail.Encryption
noTLSCheck bool
fromName string
from string
@ -30,8 +31,21 @@ func authType(authType string) mail.AuthType {
}
}
func NewSmtpMail(hostname string, port int, username string, password string, noTLSCheck bool, auth string, fromName, from string) *SmtpMail {
ans := SmtpMail{hostname: hostname, port: port, username: username, password: password, noTLSCheck: noTLSCheck, fromName: fromName, from: from, authType: authType(auth)}
func encryptionType(encryptionType string) mail.Encryption {
switch strings.ToUpper(encryptionType) {
case "SSL":
return mail.EncryptionSSL
case "SSLTLS":
return mail.EncryptionSSLTLS
case "TLS":
return mail.EncryptionTLS
default:
return mail.EncryptionSTARTTLS
}
}
func NewSmtpMail(hostname string, port int, username string, password string, noTLSCheck bool, auth string, fromName, from string, encryption string) *SmtpMail {
ans := SmtpMail{hostname: hostname, port: port, username: username, password: password, noTLSCheck: noTLSCheck, fromName: fromName, from: from, authType: authType(auth), encryption: encryptionType(encryption)}
return &ans
}
@ -50,7 +64,7 @@ func (o *SmtpMail) Send(toName string, to string, subject string, content string
server.Authentication = o.authType
server.Username = o.username
server.Password = o.password
server.Encryption = mail.EncryptionSTARTTLS
server.Encryption = o.encryption
server.KeepAlive = false
server.ConnectTimeout = 10 * time.Second
server.SendTimeout = 10 * time.Second