2026-06-19 18:47:36 +08:00
|
|
|
package email
|
2026-06-06 19:31:16 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/mail"
|
2026-07-26 22:19:41 +08:00
|
|
|
|
|
|
|
|
"github.com/ssdomei232/goodBaby/internal/meta"
|
2026-06-06 19:31:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EmailRuleValidator 邮件规则验证器
|
|
|
|
|
type EmailRuleValidator struct{}
|
|
|
|
|
|
|
|
|
|
func (v *EmailRuleValidator) GetType() string {
|
2026-07-26 22:19:41 +08:00
|
|
|
return RuleType
|
2026-06-06 19:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *EmailRuleValidator) Validate(configJSON string) error {
|
2026-09-12 15:32:27 +08:00
|
|
|
config, err := ParseRuleConfig(configJSON)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-06-06 19:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if config.Title == "" {
|
|
|
|
|
return fmt.Errorf("邮件规则配置中 title 不能为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if config.Msg == "" {
|
|
|
|
|
return fmt.Errorf("邮件规则配置中 msg 不能为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(config.Destinations) == 0 {
|
|
|
|
|
return fmt.Errorf("邮件规则配置中 destinations 不能为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证邮箱地址格式
|
|
|
|
|
for _, dest := range config.Destinations {
|
|
|
|
|
if _, err := mail.ParseAddress(dest); err != nil {
|
|
|
|
|
return fmt.Errorf("无效的邮箱地址: %s", dest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-07-26 22:19:41 +08:00
|
|
|
|
|
|
|
|
func (v *EmailRuleValidator) Meta() meta.RuleMeta {
|
|
|
|
|
return meta.RuleMeta{
|
|
|
|
|
Type: RuleType,
|
|
|
|
|
Label: "发送邮件",
|
|
|
|
|
Description: "触发时通过关联的 SMTP 账号向指定地址发送邮件。",
|
|
|
|
|
Docs: "docs/email-config.md",
|
|
|
|
|
AccountType: AccountType,
|
|
|
|
|
Fields: []meta.Field{
|
2026-09-12 15:32:27 +08:00
|
|
|
{Key: "title", Label: "邮件标题", Type: meta.FieldString, Required: true, GatewayMessage: true},
|
|
|
|
|
{Key: "msg", Label: "邮件正文", Type: meta.FieldTextarea, Required: true, GatewayMessage: true},
|
2026-07-26 22:19:41 +08:00
|
|
|
{Key: "destinations", Label: "收件人", Type: meta.FieldStringList, Required: true, Placeholder: "someone@example.com"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|