Bläddra i källkod

更新邮件发送配置和用户处理逻辑

修改了邮件发送相关的配置处理,确保使用正确的发件人地址格式,并且在用户数据库操作中优化了事务处理逻辑。同时,调整了.gitignore文件以忽略测试目录。
SongZihuan 1 månad sedan
förälder
incheckning
f577908232

+ 1 - 0
.gitignore

@@ -17,3 +17,4 @@ output*
 .task
 .envrc
 go-remote.sh
+testhome

+ 1 - 1
internal/conf/conf.go

@@ -213,7 +213,7 @@ func Init(customConf string) error {
 		if err != nil {
 			return errors.Wrapf(err, "parse mail address %q", Email.From)
 		}
-		Email.FromEmail = parsed.Address
+		Email.FromEmail = parsed
 	}
 
 	// ***********************************

+ 2 - 1
internal/conf/static.go

@@ -5,6 +5,7 @@
 package conf
 
 import (
+	"net/mail"
 	"net/url"
 	"os"
 	"time"
@@ -68,7 +69,7 @@ var (
 		AddPlainTextAlt bool
 
 		// Derived from other static values
-		FromEmail string `ini:"-"` // Parsed email address of From without person's name.
+		FromEmail *mail.Address `ini:"-"` // Parsed email address of From without person's name.
 	}
 
 	// User settings

+ 2 - 2
internal/database/users.go

@@ -387,7 +387,7 @@ func (s *UsersStore) Create(ctx context.Context, username, email string, opts Cr
 	user.Password = userutil.EncodePassword(user.Password, user.Salt)
 
 	err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
-		err := s.db.WithContext(ctx).Create(user).Error
+		err := tx.Create(user).Error
 		if err == nil {
 			return err
 		}
@@ -400,7 +400,7 @@ func (s *UsersStore) Create(ctx context.Context, username, email string, opts Cr
 		return nil
 	})
 
-	return user, s.db.WithContext(ctx).Create(user).Error
+	return user, err
 }
 
 // DeleteCustomAvatar deletes the current user custom avatar and falls back to

+ 1 - 1
internal/email/email.go

@@ -205,7 +205,7 @@ func composeIssueMessage(issue Issue, repo Repository, doer User, tplName string
 	if err != nil {
 		log.Error("HTMLString (%s): %v", tplName, err)
 	}
-	from := gomail.NewMessage().FormatAddress(conf.Email.FromEmail, doer.DisplayName())
+	from := gomail.NewMessage().FormatAddress(conf.Email.FromEmail.Address, doer.DisplayName())
 	msg := NewMessageFrom(tos, from, subject, content)
 	msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
 	return msg

+ 1 - 1
internal/email/message.go

@@ -65,7 +65,7 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message {
 
 // NewMessage creates new mail message object with default From header.
 func NewMessage(to []string, subject, body string) *Message {
-	return NewMessageFrom(to, conf.Email.From, subject, body)
+	return NewMessageFrom(to, conf.Email.FromEmail.String(), subject, body)
 }
 
 type loginAuth struct {