From da490db6d30b51f78838a5f8fd0f293ffd42d3d3 Mon Sep 17 00:00:00 2001 From: Oswin Date: Fri, 26 Jul 2024 17:47:36 +0800 Subject: [PATCH] [fix] fix send email error using outlook smtp --- common/email-outlook-auth.go | 32 ++++++++++++++++++++++++++++++++ common/email.go | 3 +++ 2 files changed, 35 insertions(+) create mode 100644 common/email-outlook-auth.go diff --git a/common/email-outlook-auth.go b/common/email-outlook-auth.go new file mode 100644 index 0000000..723a10b --- /dev/null +++ b/common/email-outlook-auth.go @@ -0,0 +1,32 @@ +package common + +import ( + "errors" + "net/smtp" +) + +type outlookAuth struct { + username, password string +} + +func LoginAuth(username, password string) smtp.Auth { + return &outlookAuth{username, password} +} + +func (a *outlookAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) { + return "LOGIN", []byte{}, nil +} + +func (a *outlookAuth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + switch string(fromServer) { + case "Username:": + return []byte(a.username), nil + case "Password:": + return []byte(a.password), nil + default: + return nil, errors.New("unknown fromServer") + } + } + return nil, nil +} diff --git a/common/email.go b/common/email.go index 13345d8..62c9048 100644 --- a/common/email.go +++ b/common/email.go @@ -62,6 +62,9 @@ func SendEmail(subject string, receiver string, content string) error { if err != nil { return err } + } else if strings.HasSuffix(SMTPAccount, "outlook.com") { + auth = LoginAuth(SMTPAccount, SMTPToken) + err = smtp.SendMail(addr, auth, SMTPAccount, to, mail) } else { err = smtp.SendMail(addr, auth, SMTPAccount, to, mail) }