Skip to content

Commit

Permalink
fix email send error when usage 465 port
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzai006 committed Dec 5, 2023
1 parent 5f4fd26 commit dc9d07b
Showing 1 changed file with 86 additions and 28 deletions.
114 changes: 86 additions & 28 deletions pkg/message/email_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"mime"
"net"
"net/smtp"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)

Expand All @@ -37,20 +39,8 @@ func (e *EmailMessageHandler) HandleMessageEvent(event *conf.MessageEvent) {
klog.Error("failed to vail params", err)
return
}
var secret corev1.Secret
err := e.Client.Get(context.TODO(), types.NamespacedName{
Namespace: constant.DefaultNamespace,
Name: e.SecretKey,
}, &secret)

if err != nil {
klog.Error("failed to get secret", err)
return
}

auth := smtp.PlainAuth("", string(secret.Data["username"]), string(secret.Data["password"]), e.Address)
err = e.SendMsg(auth, event)
if err != nil {
if err := e.SendMsg(event); err != nil {
klog.Error("failed to send email, err: ", err)
return
}
Expand All @@ -70,8 +60,23 @@ func (e *EmailMessageHandler) setMsg(me *conf.MessageEvent, to string) []byte {
return buffer.Bytes()
}

func (e *EmailMessageHandler) SendMsg(a smtp.Auth, eve *conf.MessageEvent) error {
dial, err := smtp.Dial(fmt.Sprintf("%s:%d", e.Address, e.Port))
func (e *EmailMessageHandler) SendMsg(eve *conf.MessageEvent) error {

var conn net.Conn
var err error

if e.Port == 465 {
tlsConfig := &tls.Config{InsecureSkipVerify: true}
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", e.Address, e.Port), tlsConfig)
} else {
d := net.Dialer{}
conn, err = d.Dial("tcp", fmt.Sprintf("%s:%d", e.Address, e.Port))
}

if err != nil {
return err
}
dial, err := smtp.NewClient(conn, e.Address)
if err != nil {
return err
}
Expand All @@ -80,22 +85,26 @@ func (e *EmailMessageHandler) SendMsg(a smtp.Auth, eve *conf.MessageEvent) error
if err != nil {
return err
}
if ok, _ := dial.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: e.Address}
if err = dial.StartTLS(config); err != nil {
return err
}
//if ok, _ := dial.Extension("STARTTLS"); ok {
// config := &tls.Config{ServerName: e.Address}
// if err = dial.StartTLS(config); err != nil {
// return err
// }
//}

ok, param := dial.Extension("AUTH")
if !ok {
return errors.New("smtp: server doesn't support AUTH")
}
auth, err := e.auth(param)
if err != nil {
return err
}

if a != nil {
auth, _ := dial.Extension("AUTH")
if !auth {
return errors.New("smtp: server doesn't support AUTH")
}
if err = dial.Auth(a); err != nil {
return err
}
if err = dial.Auth(auth); err != nil {
return err
}

if err = dial.Mail(e.Fo); err != nil {
return err
}
Expand Down Expand Up @@ -141,3 +150,52 @@ func (e *EmailMessageHandler) Vail() error {

return nil
}

type mailAuth struct {
username, password string
}

func (e *EmailMessageHandler) auth(authType string) (smtp.Auth, error) {
var secret corev1.Secret
err := e.Client.Get(context.TODO(), types.NamespacedName{
Namespace: constant.DefaultNamespace,
Name: e.SecretKey,
}, &secret)
if err != nil {
return nil, err
}

for _, t := range strings.Split(authType, " ") {
switch t {
case "PLAIN":
return smtp.PlainAuth("", string(secret.Data["username"]), string(secret.Data["password"]), e.Address), nil
//case "LOGIN":
// return MailAuth(string(secret.Data["username"]), string(secret.Data["password"])), nil
}
}

return nil, fmt.Errorf("unknown auth mechanism: %s", authType)
}

func MailAuth(username, password string) smtp.Auth {
return &mailAuth{username, password}
}

func (a *mailAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) {
resp := []byte(a.username + "\x00" + a.password)
return "LOGIN", resp, nil
}

func (a *mailAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch strings.ToLower(string(fromServer)) {
case "username:":
return []byte(a.username), nil
case "password:":
return []byte(a.password), nil
default:
return nil, fmt.Errorf("unexpected server challenge")
}
}
return nil, nil
}

0 comments on commit dc9d07b

Please sign in to comment.