feat: smtp update config field, send test email after config

This commit is contained in:
LinkinStar 2022-10-21 17:04:41 +08:00
parent 692b39a1e5
commit ea9eb31073
6 changed files with 126 additions and 42 deletions

View File

@ -4622,15 +4622,18 @@ const docTemplate = `{
"type": "object", "type": "object",
"properties": { "properties": {
"encryption": { "encryption": {
"description": "\"\" SSL TLS", "description": "\"\" SSL",
"type": "string" "type": "string"
}, },
"from_email_address": { "from_email": {
"type": "string" "type": "string"
}, },
"from_name": { "from_name": {
"type": "string" "type": "string"
}, },
"smtp_authentication": {
"type": "boolean"
},
"smtp_host": { "smtp_host": {
"type": "string" "type": "string"
}, },
@ -5530,13 +5533,13 @@ const docTemplate = `{
"type": "object", "type": "object",
"properties": { "properties": {
"encryption": { "encryption": {
"description": "\"\" SSL TLS", "description": "\"\" SSL",
"type": "string", "type": "string",
"enum": [ "enum": [
"SSL" "SSL"
] ]
}, },
"from_email_address": { "from_email": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
}, },
@ -5544,6 +5547,9 @@ const docTemplate = `{
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
}, },
"smtp_authentication": {
"type": "boolean"
},
"smtp_host": { "smtp_host": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
@ -5560,6 +5566,9 @@ const docTemplate = `{
"smtp_username": { "smtp_username": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
},
"test_email_recipient": {
"type": "string"
} }
} }
}, },

View File

@ -4610,15 +4610,18 @@
"type": "object", "type": "object",
"properties": { "properties": {
"encryption": { "encryption": {
"description": "\"\" SSL TLS", "description": "\"\" SSL",
"type": "string" "type": "string"
}, },
"from_email_address": { "from_email": {
"type": "string" "type": "string"
}, },
"from_name": { "from_name": {
"type": "string" "type": "string"
}, },
"smtp_authentication": {
"type": "boolean"
},
"smtp_host": { "smtp_host": {
"type": "string" "type": "string"
}, },
@ -5518,13 +5521,13 @@
"type": "object", "type": "object",
"properties": { "properties": {
"encryption": { "encryption": {
"description": "\"\" SSL TLS", "description": "\"\" SSL",
"type": "string", "type": "string",
"enum": [ "enum": [
"SSL" "SSL"
] ]
}, },
"from_email_address": { "from_email": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
}, },
@ -5532,6 +5535,9 @@
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
}, },
"smtp_authentication": {
"type": "boolean"
},
"smtp_host": { "smtp_host": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
@ -5548,6 +5554,9 @@
"smtp_username": { "smtp_username": {
"type": "string", "type": "string",
"maxLength": 256 "maxLength": 256
},
"test_email_recipient": {
"type": "string"
} }
} }
}, },

View File

@ -513,12 +513,14 @@ definitions:
schema.GetSMTPConfigResp: schema.GetSMTPConfigResp:
properties: properties:
encryption: encryption:
description: '"" SSL TLS' description: '"" SSL'
type: string type: string
from_email_address: from_email:
type: string type: string
from_name: from_name:
type: string type: string
smtp_authentication:
type: boolean
smtp_host: smtp_host:
type: string type: string
smtp_password: smtp_password:
@ -1167,16 +1169,18 @@ definitions:
schema.UpdateSMTPConfigReq: schema.UpdateSMTPConfigReq:
properties: properties:
encryption: encryption:
description: '"" SSL TLS' description: '"" SSL'
enum: enum:
- SSL - SSL
type: string type: string
from_email_address: from_email:
maxLength: 256 maxLength: 256
type: string type: string
from_name: from_name:
maxLength: 256 maxLength: 256
type: string type: string
smtp_authentication:
type: boolean
smtp_host: smtp_host:
maxLength: 256 maxLength: 256
type: string type: string
@ -1190,6 +1194,8 @@ definitions:
smtp_username: smtp_username:
maxLength: 256 maxLength: 256
type: string type: string
test_email_recipient:
type: string
type: object type: object
schema.UpdateTagReq: schema.UpdateTagReq:
properties: properties:

View File

@ -27,22 +27,25 @@ type SiteInfoResp struct {
// UpdateSMTPConfigReq get smtp config request // UpdateSMTPConfigReq get smtp config request
type UpdateSMTPConfigReq struct { type UpdateSMTPConfigReq struct {
FromEmailAddress string `validate:"omitempty,gt=0,lte=256" json:"from_email_address"` FromEmail string `validate:"omitempty,gt=0,lte=256" json:"from_email"`
FromName string `validate:"omitempty,gt=0,lte=256" json:"from_name"` FromName string `validate:"omitempty,gt=0,lte=256" json:"from_name"`
SMTPHost string `validate:"omitempty,gt=0,lte=256" json:"smtp_host"` SMTPHost string `validate:"omitempty,gt=0,lte=256" json:"smtp_host"`
SMTPPort int `validate:"omitempty,min=1,max=65535" json:"smtp_port"` SMTPPort int `validate:"omitempty,min=1,max=65535" json:"smtp_port"`
Encryption string `validate:"omitempty,oneof=SSL" json:"encryption"` // "" SSL TLS Encryption string `validate:"omitempty,oneof=SSL" json:"encryption"` // "" SSL
SMTPUsername string `validate:"omitempty,gt=0,lte=256" json:"smtp_username"` SMTPUsername string `validate:"omitempty,gt=0,lte=256" json:"smtp_username"`
SMTPPassword string `validate:"omitempty,gt=0,lte=256" json:"smtp_password"` 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 // GetSMTPConfigResp get smtp config response
type GetSMTPConfigResp struct { type GetSMTPConfigResp struct {
FromEmailAddress string `json:"from_email_address"` FromEmail string `json:"from_email"`
FromName string `json:"from_name"` FromName string `json:"from_name"`
SMTPHost string `json:"smtp_host"` SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"` SMTPPort int `json:"smtp_port"`
Encryption string `json:"encryption"` // "" SSL TLS Encryption string `json:"encryption"` // "" SSL
SMTPUsername string `json:"smtp_username"` SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"` SMTPPassword string `json:"smtp_password"`
SMTPAuthentication bool `json:"smtp_authentication"`
} }

View File

@ -3,6 +3,7 @@ package export
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"html/template" "html/template"
"github.com/segmentfault/answer/internal/base/reason" "github.com/segmentfault/answer/internal/base/reason"
@ -35,13 +36,14 @@ func NewEmailService(configRepo config.ConfigRepo, emailRepo EmailRepo) *EmailSe
// EmailConfig email config // EmailConfig email config
type EmailConfig struct { type EmailConfig struct {
FromEmailAddress string `json:"from_email_address"` FromEmail string `json:"from_email"`
FromName string `json:"from_name"` FromName string `json:"from_name"`
SMTPHost string `json:"smtp_host"` SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"` SMTPPort int `json:"smtp_port"`
Encryption string `json:"encryption"` // "" SSL Encryption string `json:"encryption"` // "" SSL
SMTPUsername string `json:"smtp_username"` SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"` SMTPPassword string `json:"smtp_password"`
SMTPAuthentication bool `json:"smtp_authentication"`
RegisterTitle string `json:"register_title"` RegisterTitle string `json:"register_title"`
RegisterBody string `json:"register_body"` RegisterBody string `json:"register_body"`
@ -49,6 +51,8 @@ type EmailConfig struct {
PassResetBody string `json:"pass_reset_body"` PassResetBody string `json:"pass_reset_body"`
ChangeTitle string `json:"change_title"` ChangeTitle string `json:"change_title"`
ChangeBody string `json:"change_body"` ChangeBody string `json:"change_body"`
TestTitle string `json:"test_title"`
TestBody string `json:"test_body"`
} }
func (e *EmailConfig) IsSSL() bool { func (e *EmailConfig) IsSSL() bool {
@ -70,16 +74,21 @@ type ChangeEmailTemplateData struct {
ChangeEmailUrl string ChangeEmailUrl string
} }
type TestTemplateData struct {
SiteName string
}
// Send email send // Send email send
func (es *EmailService) Send(ctx context.Context, toEmailAddr, subject, body, code, codeContent string) { 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() ec, err := es.GetEmailConfig()
if err != nil { if err != nil {
log.Error(err) log.Errorf("get email config failed: %s", err)
return return
} }
m := gomail.NewMessage() m := gomail.NewMessage()
m.SetHeader("From", ec.FromEmailAddress) m.SetHeader("From", ec.FromEmail)
m.SetHeader("To", toEmailAddr) m.SetHeader("To", toEmailAddr)
m.SetHeader("Subject", subject) m.SetHeader("Subject", subject)
m.SetBody("text/html", body) m.SetBody("text/html", body)
@ -89,12 +98,16 @@ func (es *EmailService) Send(ctx context.Context, toEmailAddr, subject, body, co
d.SSL = true d.SSL = true
} }
if err := d.DialAndSend(m); err != nil { 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 len(code) > 0 {
if err != nil { err = es.emailRepo.SetCode(ctx, code, codeContent)
log.Error(err) if err != nil {
log.Error(err)
}
} }
return return
} }
@ -192,6 +205,35 @@ func (es *EmailService) ChangeEmailTemplate(changeEmailUrl string) (title, body
return titleBuf.String(), bodyBuf.String(), nil 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) { func (es *EmailService) GetEmailConfig() (ec *EmailConfig, err error) {
emailConf, err := es.configRepo.GetString("email.config") emailConf, err := es.configRepo.GetString("email.config")
if err != nil { if err != nil {

View File

@ -132,7 +132,22 @@ func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
// UpdateSMTPConfig get smtp config // UpdateSMTPConfig get smtp config
func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) { func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) {
ec := &export.EmailConfig{} oldEmailConfig, err := s.emailService.GetEmailConfig()
_ = copier.Copy(ec, req) if err != nil {
return s.emailService.SetEmailConfig(ec) 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
} }