// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . //go:build plus package setting import ( "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/userconfigs" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" ) type EmailTestAction struct { actionutils.ParentAction } func (this *EmailTestAction) Init() { this.Nav("", "", "") } func (this *EmailTestAction) RunGet(params struct{}) { this.Show() } func (this *EmailTestAction) RunPost(params struct { ConfigJSON []byte ToEmail string Subject string Body string Must *actions.Must CSRF *actionutils.CSRF }) { if len(params.ConfigJSON) == 0 { this.Fail("读取不到邮件配置") return } var config = userconfigs.NewEmailSenderConfig() err := json.Unmarshal(params.ConfigJSON, config) if err != nil { this.Fail("解析邮件配置出错:" + err.Error()) return } if len(config.SMTPHost) == 0 { this.Fail("缺少SMTP地址") return } if config.SMTPPort <= 0 { this.Fail("缺少SMTP端口") return } if len(config.Username) == 0 { this.Fail("缺少用户名") return } if len(config.Password) == 0 { this.Fail("缺少密码") return } if len(config.FromEmail) == 0 { this.Fail("缺少发件人Email") return } params.Must.Field("fromEmail", config.FromEmail). Email("发件人Email格式不正确") params.Must. Field("toEmail", params.ToEmail). Require("请输入收件人Email"). Email("请输入正确的收件人Email"). Field("subject", params.Subject). Require("请输入测试标题"). Field("body", params.Body). Require("请输入测试内容") // 发送测试 _, err = this.RPC().MessageMediaRPC().SendMediaMessage(this.AdminContext(), &pb.SendMediaMessageRequest{ MediaType: "email", OptionsJSON: maps.Map{ "smtp": config.SMTPHost + ":" + types.String(config.SMTPPort), "username": config.Username, "password": config.Password, "from": config.FromEmail, "fromName": config.FromName, }.AsJSON(), User: params.ToEmail, Subject: params.Subject, Body: params.Body, }) if err != nil { this.Fail("发送失败:" + err.Error()) return } this.Success() }