Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support smtp without auth #1101

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions common/message/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
"encoding/base64"
"fmt"
"github.com/songquanpeng/one-api/common/config"
"net"
"net/smtp"
"strings"
"time"
)

func shouldAuth() bool {
return config.SMTPAccount != "" || config.SMTPToken != ""

Check warning on line 16 in common/message/email.go

View check run for this annotation

Codecov / codecov/patch

common/message/email.go#L15-L16

Added lines #L15 - L16 were not covered by tests
}

func SendEmail(subject string, receiver string, content string) error {
if receiver == "" {
return fmt.Errorf("receiver is empty")
Expand Down Expand Up @@ -41,16 +46,24 @@
"Date: %s\r\n"+
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
receiver, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))

auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
to := strings.Split(receiver, ";")

if config.SMTPPort == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.SMTPServer,
if config.SMTPPort == 465 || !shouldAuth() {

Check warning on line 54 in common/message/email.go

View check run for this annotation

Codecov / codecov/patch

common/message/email.go#L54

Added line #L54 was not covered by tests
// need advanced client
var conn net.Conn
var err error
if config.SMTPPort == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.SMTPServer,

Check warning on line 61 in common/message/email.go

View check run for this annotation

Codecov / codecov/patch

common/message/email.go#L56-L61

Added lines #L56 - L61 were not covered by tests
}
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
} else {
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort))

Check warning on line 65 in common/message/email.go

View check run for this annotation

Codecov / codecov/patch

common/message/email.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
if err != nil {
return err
}
Expand All @@ -59,8 +72,10 @@
return err
}
defer client.Close()
if err = client.Auth(auth); err != nil {
return err
if shouldAuth() {
if err = client.Auth(auth); err != nil {
return err

Check warning on line 77 in common/message/email.go

View check run for this annotation

Codecov / codecov/patch

common/message/email.go#L75-L77

Added lines #L75 - L77 were not covered by tests
}
}
if err = client.Mail(config.SMTPFrom); err != nil {
return err
Expand Down
Loading