diff --git a/docs/docs.go b/docs/docs.go index 529f1d8c..bbdc7c89 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4622,15 +4622,18 @@ const docTemplate = `{ "type": "object", "properties": { "encryption": { - "description": "\"\" SSL TLS", + "description": "\"\" SSL", "type": "string" }, - "from_email_address": { + "from_email": { "type": "string" }, "from_name": { "type": "string" }, + "smtp_authentication": { + "type": "boolean" + }, "smtp_host": { "type": "string" }, @@ -5530,13 +5533,13 @@ const docTemplate = `{ "type": "object", "properties": { "encryption": { - "description": "\"\" SSL TLS", + "description": "\"\" SSL", "type": "string", "enum": [ "SSL" ] }, - "from_email_address": { + "from_email": { "type": "string", "maxLength": 256 }, @@ -5544,6 +5547,9 @@ const docTemplate = `{ "type": "string", "maxLength": 256 }, + "smtp_authentication": { + "type": "boolean" + }, "smtp_host": { "type": "string", "maxLength": 256 @@ -5560,6 +5566,9 @@ const docTemplate = `{ "smtp_username": { "type": "string", "maxLength": 256 + }, + "test_email_recipient": { + "type": "string" } } }, diff --git a/docs/swagger.json b/docs/swagger.json index ac6454cd..3bffef99 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4610,15 +4610,18 @@ "type": "object", "properties": { "encryption": { - "description": "\"\" SSL TLS", + "description": "\"\" SSL", "type": "string" }, - "from_email_address": { + "from_email": { "type": "string" }, "from_name": { "type": "string" }, + "smtp_authentication": { + "type": "boolean" + }, "smtp_host": { "type": "string" }, @@ -5518,13 +5521,13 @@ "type": "object", "properties": { "encryption": { - "description": "\"\" SSL TLS", + "description": "\"\" SSL", "type": "string", "enum": [ "SSL" ] }, - "from_email_address": { + "from_email": { "type": "string", "maxLength": 256 }, @@ -5532,6 +5535,9 @@ "type": "string", "maxLength": 256 }, + "smtp_authentication": { + "type": "boolean" + }, "smtp_host": { "type": "string", "maxLength": 256 @@ -5548,6 +5554,9 @@ "smtp_username": { "type": "string", "maxLength": 256 + }, + "test_email_recipient": { + "type": "string" } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9d9b87ef..c0f1febb 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -513,12 +513,14 @@ definitions: schema.GetSMTPConfigResp: properties: encryption: - description: '"" SSL TLS' + description: '"" SSL' type: string - from_email_address: + from_email: type: string from_name: type: string + smtp_authentication: + type: boolean smtp_host: type: string smtp_password: @@ -1167,16 +1169,18 @@ definitions: schema.UpdateSMTPConfigReq: properties: encryption: - description: '"" SSL TLS' + description: '"" SSL' enum: - SSL type: string - from_email_address: + from_email: maxLength: 256 type: string from_name: maxLength: 256 type: string + smtp_authentication: + type: boolean smtp_host: maxLength: 256 type: string @@ -1190,6 +1194,8 @@ definitions: smtp_username: maxLength: 256 type: string + test_email_recipient: + type: string type: object schema.UpdateTagReq: properties: diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go index 39e3f2e7..ff93e72d 100644 --- a/internal/schema/siteinfo_schema.go +++ b/internal/schema/siteinfo_schema.go @@ -27,22 +27,25 @@ type SiteInfoResp struct { // UpdateSMTPConfigReq get smtp config request type UpdateSMTPConfigReq struct { - FromEmailAddress string `validate:"omitempty,gt=0,lte=256" json:"from_email_address"` - FromName string `validate:"omitempty,gt=0,lte=256" json:"from_name"` - SMTPHost string `validate:"omitempty,gt=0,lte=256" json:"smtp_host"` - SMTPPort int `validate:"omitempty,min=1,max=65535" json:"smtp_port"` - Encryption string `validate:"omitempty,oneof=SSL" json:"encryption"` // "" SSL TLS - SMTPUsername string `validate:"omitempty,gt=0,lte=256" json:"smtp_username"` - SMTPPassword string `validate:"omitempty,gt=0,lte=256" json:"smtp_password"` + FromEmail string `validate:"omitempty,gt=0,lte=256" json:"from_email"` + FromName string `validate:"omitempty,gt=0,lte=256" json:"from_name"` + SMTPHost string `validate:"omitempty,gt=0,lte=256" json:"smtp_host"` + SMTPPort int `validate:"omitempty,min=1,max=65535" json:"smtp_port"` + Encryption string `validate:"omitempty,oneof=SSL" json:"encryption"` // "" SSL + SMTPUsername string `validate:"omitempty,gt=0,lte=256" json:"smtp_username"` + SMTPPassword string `validate:"omitempty,gt=0,lte=256" json:"smtp_password"` + SMTPAuthentication bool `validate:"omitempty" json:"smtp_authentication"` + TestEmailRecipient string `validate:"omitempty,email" json:"test_email_recipient"` } // GetSMTPConfigResp get smtp config response type GetSMTPConfigResp struct { - FromEmailAddress string `json:"from_email_address"` - FromName string `json:"from_name"` - SMTPHost string `json:"smtp_host"` - SMTPPort int `json:"smtp_port"` - Encryption string `json:"encryption"` // "" SSL TLS - SMTPUsername string `json:"smtp_username"` - SMTPPassword string `json:"smtp_password"` + FromEmail string `json:"from_email"` + FromName string `json:"from_name"` + SMTPHost string `json:"smtp_host"` + SMTPPort int `json:"smtp_port"` + Encryption string `json:"encryption"` // "" SSL + SMTPUsername string `json:"smtp_username"` + SMTPPassword string `json:"smtp_password"` + SMTPAuthentication bool `json:"smtp_authentication"` } diff --git a/internal/service/export/email_service.go b/internal/service/export/email_service.go index 37f5f354..56dd5530 100644 --- a/internal/service/export/email_service.go +++ b/internal/service/export/email_service.go @@ -3,6 +3,7 @@ package export import ( "bytes" "encoding/json" + "fmt" "html/template" "github.com/segmentfault/answer/internal/base/reason" @@ -35,13 +36,14 @@ func NewEmailService(configRepo config.ConfigRepo, emailRepo EmailRepo) *EmailSe // EmailConfig email config type EmailConfig struct { - FromEmailAddress string `json:"from_email_address"` - FromName string `json:"from_name"` - SMTPHost string `json:"smtp_host"` - SMTPPort int `json:"smtp_port"` - Encryption string `json:"encryption"` // "" SSL - SMTPUsername string `json:"smtp_username"` - SMTPPassword string `json:"smtp_password"` + FromEmail string `json:"from_email"` + FromName string `json:"from_name"` + SMTPHost string `json:"smtp_host"` + SMTPPort int `json:"smtp_port"` + Encryption string `json:"encryption"` // "" SSL + SMTPUsername string `json:"smtp_username"` + SMTPPassword string `json:"smtp_password"` + SMTPAuthentication bool `json:"smtp_authentication"` RegisterTitle string `json:"register_title"` RegisterBody string `json:"register_body"` @@ -49,6 +51,8 @@ type EmailConfig struct { PassResetBody string `json:"pass_reset_body"` ChangeTitle string `json:"change_title"` ChangeBody string `json:"change_body"` + TestTitle string `json:"test_title"` + TestBody string `json:"test_body"` } func (e *EmailConfig) IsSSL() bool { @@ -70,16 +74,21 @@ type ChangeEmailTemplateData struct { ChangeEmailUrl string } +type TestTemplateData struct { + SiteName string +} + // Send email send func (es *EmailService) Send(ctx context.Context, toEmailAddr, subject, body, code, codeContent string) { + log.Infof("try to send email to %s", toEmailAddr) ec, err := es.GetEmailConfig() if err != nil { - log.Error(err) + log.Errorf("get email config failed: %s", err) return } m := gomail.NewMessage() - m.SetHeader("From", ec.FromEmailAddress) + m.SetHeader("From", ec.FromEmail) m.SetHeader("To", toEmailAddr) m.SetHeader("Subject", subject) m.SetBody("text/html", body) @@ -89,12 +98,16 @@ func (es *EmailService) Send(ctx context.Context, toEmailAddr, subject, body, co d.SSL = true } if err := d.DialAndSend(m); err != nil { - log.Error(err) + log.Errorf("send email to %s failed: %s", toEmailAddr, err) + } else { + log.Infof("send email to %s success", toEmailAddr) } - err = es.emailRepo.SetCode(ctx, code, codeContent) - if err != nil { - log.Error(err) + if len(code) > 0 { + err = es.emailRepo.SetCode(ctx, code, codeContent) + if err != nil { + log.Error(err) + } } return } @@ -192,6 +205,35 @@ func (es *EmailService) ChangeEmailTemplate(changeEmailUrl string) (title, body return titleBuf.String(), bodyBuf.String(), nil } +func (es *EmailService) TestTemplate() (title, body string, err error) { + ec, err := es.GetEmailConfig() + if err != nil { + return + } + + templateData := TestTemplateData{ + SiteName: ec.FromName, + } + + titleBuf := &bytes.Buffer{} + bodyBuf := &bytes.Buffer{} + + tmpl, err := template.New("test_title").Parse(ec.TestTitle) + if err != nil { + return "", "", fmt.Errorf("email test title template parse error: %s", err) + } + err = tmpl.Execute(titleBuf, templateData) + if err != nil { + return "", "", fmt.Errorf("email test body template parse error: %s", err) + } + tmpl, err = template.New("test_body").Parse(ec.TestBody) + err = tmpl.Execute(bodyBuf, templateData) + if err != nil { + return "", "", err + } + return titleBuf.String(), bodyBuf.String(), nil +} + func (es *EmailService) GetEmailConfig() (ec *EmailConfig, err error) { emailConf, err := es.configRepo.GetString("email.config") if err != nil { diff --git a/internal/service/siteinfo_service.go b/internal/service/siteinfo_service.go index 022db57f..89db720c 100644 --- a/internal/service/siteinfo_service.go +++ b/internal/service/siteinfo_service.go @@ -132,7 +132,22 @@ func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) ( // UpdateSMTPConfig get smtp config func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) { - ec := &export.EmailConfig{} - _ = copier.Copy(ec, req) - return s.emailService.SetEmailConfig(ec) + oldEmailConfig, err := s.emailService.GetEmailConfig() + if err != nil { + return err + } + _ = copier.Copy(oldEmailConfig, req) + + err = s.emailService.SetEmailConfig(oldEmailConfig) + if err != nil { + return err + } + if len(req.TestEmailRecipient) > 0 { + title, body, err := s.emailService.TestTemplate() + if err != nil { + return err + } + go s.emailService.Send(ctx, req.TestEmailRecipient, title, body, "", "") + } + return }