mirror of
https://github.com/certimate-go/certimate.git
synced 2026-09-24 23:10:13 +08:00
feat: 完成邮件通知的html渲染功能 (#1183)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package email
|
||||
|
||||
const (
|
||||
MESSAGE_FORMAT_PLAIN = "plain"
|
||||
MESSAGE_FORMAT_HTML = "html"
|
||||
)
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
|
||||
"github.com/certimate-go/certimate/internal/tools/smtp"
|
||||
"github.com/certimate-go/certimate/pkg/core/notifier"
|
||||
)
|
||||
@@ -28,6 +30,10 @@ type NotifierConfig struct {
|
||||
SenderName string `json:"senderName,omitempty"`
|
||||
// 收件人邮箱。
|
||||
ReceiverAddress string `json:"receiverAddress"`
|
||||
// 消息格式。
|
||||
// 可取值 [MESSAGE_FORMAT_PLAIN]、[MESSAGE_FORMAT_HTML]。
|
||||
// 零值时默认值 [MESSAGE_FORMAT_PLAIN]。
|
||||
MessageFormat string `json:"messageFormat,omitempty"`
|
||||
// 是否允许不安全的连接。
|
||||
AllowInsecureConnections bool `json:"allowInsecureConnections,omitempty"`
|
||||
}
|
||||
@@ -75,7 +81,16 @@ func (n *Notifier) Notify(ctx context.Context, subject string, message string) (
|
||||
|
||||
msg := smtp.NewMessage()
|
||||
msg.Subject(subject)
|
||||
msg.SetBodyString(smtp.MIMETypeTextPlain, message)
|
||||
switch n.config.MessageFormat {
|
||||
case "", MESSAGE_FORMAT_PLAIN:
|
||||
msg.SetBodyString(smtp.MIMETypeTextPlain, message)
|
||||
case MESSAGE_FORMAT_HTML:
|
||||
msg.SetBodyString(smtp.MIMETypeTextHTML, bluemonday.UGCPolicy().Sanitize(message))
|
||||
msg.AddAlternativeString(smtp.MIMETypeTextPlain, bluemonday.StrictPolicy().Sanitize(message))
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported message format: '%s'", n.config.MessageFormat)
|
||||
}
|
||||
|
||||
if n.config.SenderName == "" {
|
||||
msg.From(n.config.SenderAddress)
|
||||
} else {
|
||||
|
||||
@@ -11,8 +11,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
mockSubject = "test_subject"
|
||||
mockMessage = "test_message"
|
||||
mockSubject = "test_subject"
|
||||
mockMessage = "test_message"
|
||||
mockHtmlMessage = "<h1>Hello Certimate!</h1><a onblur=\"alert(secret)\" href=\"http://www.google.com\">Google</a>"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -86,4 +87,40 @@ func TestNotify(t *testing.T) {
|
||||
|
||||
t.Logf("ok: %v", res)
|
||||
})
|
||||
|
||||
t.Run("Notify_Html", func(t *testing.T) {
|
||||
t.Log(strings.Join([]string{
|
||||
"args:",
|
||||
fmt.Sprintf("SMTPHOST: %v", fSmtpHost),
|
||||
fmt.Sprintf("SMTPPORT: %v", fSmtpPort),
|
||||
fmt.Sprintf("SMTPTLS: %v", fSmtpTLS),
|
||||
fmt.Sprintf("USERNAME: %v", fUsername),
|
||||
fmt.Sprintf("PASSWORD: %v", fPassword),
|
||||
fmt.Sprintf("SENDERADDRESS: %v", fSenderAddress),
|
||||
fmt.Sprintf("RECEIVERADDRESS: %v", fReceiverAddress),
|
||||
}, "\n"))
|
||||
|
||||
provider, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
SmtpHost: fSmtpHost,
|
||||
SmtpPort: int32(fSmtpPort),
|
||||
SmtpTls: fSmtpTLS,
|
||||
Username: fUsername,
|
||||
Password: fPassword,
|
||||
SenderAddress: fSenderAddress,
|
||||
ReceiverAddress: fReceiverAddress,
|
||||
MessageFormat: provider.MESSAGE_FORMAT_HTML,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := provider.Notify(context.Background(), mockSubject, mockHtmlMessage)
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Logf("ok: %v", res)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user