Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(mail): improve the mail template (#5656)
h3n4l7638281aae15
diff --git a/backend/api/v1/mail_templates/testmail/statics/banner.png.REMOVED.git-id b/backend/api/v1/mail_templates/testmail/statics/banner.png.REMOVED.git-idnew file mode 100644index 0000000000..574e847e0c--- /dev/null+++ b/backend/api/v1/mail_templates/testmail/statics/banner.png.REMOVED.git-id@@ -0,0 +1 @@+3fbe36487ef992c82934dfb8d6c6757b9ce9a834\ No newline at end of filediff --git a/backend/api/v1/mail_templates/testmail/statics/logo-full.png b/backend/api/v1/mail_templates/testmail/statics/logo-full.pngnew file mode 100644index 0000000000..5119274448Binary files /dev/null and b/backend/api/v1/mail_templates/testmail/statics/logo-full.png differdiff --git a/backend/api/v1/mail_templates/testmail/template.html b/backend/api/v1/mail_templates/testmail/template.htmlnew file mode 100644index 0000000000..6f2c67c5d0--- /dev/null+++ b/backend/api/v1/mail_templates/testmail/template.html@@ -0,0 +1,32 @@+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />+<html lang="en">++ <head data-id="__react-email-head"></head>+ <div id="__react-email-preview" style="display:none;overflow:hidden;line-height:1px;opacity:0;max-height:0;max-width:0">Reliable Database CI/CD for Developers.<div> </div>+ </div>++ <body data-id="__react-email-body" style="background-color:#ffffff;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif">+ <table align="center" width="100%" data-id="__react-email-container" role="presentation" cellSpacing="0" cellPadding="0" border="0" style="max-width:37.5em;margin:0 auto;padding:20px 0 48px">+ <tbody>+ <tr style="width:100%">+ <td><a href="https://www.bytebase.com"><img data-id="react-email-img" alt="Bytebase Logo" src="{{BYTEBASE_LOGO_URL}}" width="205" height="44" style="display:block;outline:none;border:none;text-decoration:none;margin:0 auto" /></a>+ <h1 data-id="react-email-heading" style="font-size:32px;line-height:1.3;font-weight:700;text-align:center;letter-spacing:-1px">Your Bytebase mail server is ready to Go!</h1>+ <p data-id="react-email-text" style="font-size:16px;line-height:26px;margin:16px 0">Hello,</p>+ <p data-id="react-email-text" style="font-size:16px;line-height:26px;margin:16px 0">This is a test email from Bytebase to confirm that your mail server configuration is correct. If you have received this email, it means that your configuration was successful. Thank you for using Bytebase!</p>+ <p data-id="react-email-text" style="font-size:16px;line-height:26px;margin:16px 0">Best regards,<br />The Bytebase Team</p>+ <table align="center" width="100%" data-id="react-email-section" style="text-align:center" border="0" cellPadding="0" cellSpacing="0" role="presentation">+ <tbody>+ <tr>+ <td><a href="{{BYTEBASE_CONSOLE_REDIRECT_URL}}" data-id="react-email-button" target="_blank" style="background-color:#4F46E5;border-radius:10px;color:#ffffff;font-size:16px;text-decoration:none;text-align:center;display:inline-block;line-height:100%;max-width:100%;padding:12px 12px"><span><!--[if mso]><i style="letter-spacing: 12px;mso-font-width:-100%;mso-text-raise:18" hidden> </i><![endif]--></span><span style="max-width:100%;display:inline-block;line-height:120%;mso-padding-alt:0px;mso-text-raise:9px">Get started</span><span><!--[if mso]><i style="letter-spacing: 12px;mso-font-width:-100%" hidden> </i><![endif]--></span></a></td>+ </tr>+ </tbody>+ </table>+ <hr data-id="react-email-hr" style="width:100%;border:none;border-top:1px solid #eaeaea;border-color:#cccccc;margin:20px 0" /><img data-id="react-email-img" alt="Bytebase Banner" src="{{BYTEBASE_BANNER_URL}}" width="900" height="300" style="display:block;outline:none;border:none;text-decoration:none;margin:0 auto" />+ </td>+ </tr>+ </tbody>+ </table>+ </body>++</html>\ No newline at end of filediff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex f541e54bea..0c34a59793 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -1,8 +1,10 @@package v1import (+ "bytes""context""fmt"+ "strings""google.golang.org/grpc/codes""google.golang.org/grpc/status"@@ -11,6 +13,8 @@ import ("github.com/google/cel-go/cel""github.com/pkg/errors"+ "embed"+"github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"@@ -50,6 +54,13 @@ var whitelistSettings = []api.SettingName{api.SettingWorkspaceMailDelivery,}+var (+ //go:embed mail_templates/testmail/template.html+ //go:embed mail_templates/testmail/statics/logo-full.png+ //go:embed mail_templates/testmail/statics/banner.png+ testEmailFs embed.FS+)+// GetSetting gets the setting by name.func (s *SettingService) GetSetting(ctx context.Context, request *v1pb.GetSettingRequest) (*v1pb.Setting, error) {settingName, err := getSettingName(request.Name)@@ -168,7 +179,7 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettinapiValue.Password = &oldValue.Password}if request.ValidateOnly {- if err := sendTestEmail(apiValue); err != nil {+ if err := s.sendTestEmail(ctx, apiValue); err != nil {return nil, status.Errorf(codes.InvalidArgument, "failed to validate smtp setting: %v", err)}apiValue.Password = nil@@ -280,7 +291,7 @@ func validateApprovalTemplate(template *storepb.ApprovalTemplate) error {return nil}-func sendTestEmail(value *v1pb.SMTPMailDeliverySettingValue) error {+func (s *SettingService) sendTestEmail(ctx context.Context, value *v1pb.SMTPMailDeliverySettingValue) error {if value.Password == nil {return status.Errorf(codes.InvalidArgument, "password is required when sending test email")}@@ -291,19 +302,49 @@ func sendTestEmail(value *v1pb.SMTPMailDeliverySettingValue) error {return status.Errorf(codes.InvalidArgument, "from is required when sending test email")}+ consoleRedirectURL := "www.bytebase.com"+ workspaceProfileSettingName := api.SettingWorkspaceProfile+ setting, err := s.store.GetSettingV2(ctx, &store.FindSettingMessage{Name: &workspaceProfileSettingName})+ if err != nil {+ return status.Errorf(codes.Internal, "failed to get workspace profile setting: %v", err)+ }+ if setting != nil {+ settingValue := new(storepb.WorkspaceProfileSetting)+ if err := protojson.Unmarshal([]byte(setting.Value), settingValue); err != nil {+ return status.Errorf(codes.Internal, "failed to unmarshal setting value: %v", err)+ }+ if settingValue.ExternalUrl != "" {+ consoleRedirectURL = settingValue.ExternalUrl+ }+ }+email := mail.NewEmailMsg()- // TODO(zp): beautify the test mail.- email.SetFrom(fmt.Sprintf("Bytebase <%s>", value.From)).AddTo(value.To).SetSubject("Test Email Subject").SetBody(`- <!DOCTYPE html>- <html>- <head>- <title>A test mail from Bytebase.</title>- </head>- <body>- <h1>A test mail from Bytebase.</h1>- </body>- </html>- `)++ logoFull, err := testEmailFs.ReadFile("mail_templates/testmail/statics/logo-full.png")+ if err != nil {+ return status.Errorf(codes.Internal, "failed to read logo-full.png: %v", err)+ }+ banner, err := testEmailFs.ReadFile("mail_templates/testmail/statics/banner.png")+ if err != nil {+ return status.Errorf(codes.Internal, "failed to read banner.png: %v", err)+ }+ mailHTMLBody, err := testEmailFs.ReadFile("mail_templates/testmail/template.html")+ if err != nil {+ return status.Errorf(codes.Internal, "failed to read template.html: %v", err)+ }+ logoFullReader := bytes.NewReader(logoFull)+ bannerReader := bytes.NewReader(banner)+ logoFullFileName, err := email.Attach(logoFullReader, "logo-full.png", "image/png")+ if err != nil {+ return status.Errorf(codes.Internal, "failed to attach logo-full.png: %v", err)+ }+ bannerFileName, err := email.Attach(bannerReader, "banner.png", "image/png")+ if err != nil {+ return status.Errorf(codes.Internal, "failed to attach banner.png: %v", err)+ }++ polishHTMLBody := prepareTestMailContent(string(mailHTMLBody), consoleRedirectURL, logoFullFileName, bannerFileName)+ email.SetFrom(fmt.Sprintf("Bytebase <%s>", value.From)).AddTo(value.To).SetSubject("Bytebase mail server test").SetBody(polishHTMLBody)client := mail.NewSMTPClient(value.Server, int(value.Port))client.SetAuthType(convertToMailSMTPAuthType(value.Authentication)).SetAuthCredentials(value.Username, *value.Password).@@ -315,6 +356,13 @@ func sendTestEmail(value *v1pb.SMTPMailDeliverySettingValue) error {return nil}+func prepareTestMailContent(htmlTemplate, consoleRedirectURL, logoContentID, bannerContentID string) string {+ testEmailContent := strings.ReplaceAll(htmlTemplate, "{{BYTEBASE_LOGO_URL}}", fmt.Sprintf("cid:%s", logoContentID))+ testEmailContent = strings.ReplaceAll(testEmailContent, "{{BYTEBASE_BANNER_URL}}", fmt.Sprintf("cid:%s", bannerContentID))+ testEmailContent = strings.ReplaceAll(testEmailContent, "{{BYTEBASE_CONSOLE_REDIRECT_URL}}", consoleRedirectURL)+ return testEmailContent+}+func convertToMailSMTPAuthType(authType v1pb.SMTPMailDeliverySettingValue_Authentication) mail.SMTPAuthType {switch authType {case v1pb.SMTPMailDeliverySettingValue_AUTHENTICATION_NONE:diff --git a/backend/plugin/mail/mail.go b/backend/plugin/mail/mail.goindex 6e93981874..2162729de8 100644--- a/backend/plugin/mail/mail.go+++ b/backend/plugin/mail/mail.go@@ -25,6 +25,7 @@ package mailimport ("crypto/tls""fmt"+ "io""net/mail""net/smtp"@@ -110,6 +111,25 @@ func (e *Email) SetBody(body string) *Email {return e}+// The ContentType is the type of the content.+// https://cloud.google.com/appengine/docs/legacy/standard/php/mail/mail-with-headers-attachments.+type ContentType string++const (+ // ContentTypeImagePNG is the content type of the file with png extension.+ ContentTypeImagePNG ContentType = "image/png"+)++// Attach attaches the file to the email, and returns the filename of the attachment.+// Caller can use filename as content id to reference the attachment in the email body.+func (e *Email) Attach(reader io.Reader, filename string, contentType ContentType) (string, error) {+ attachment, err := e.e.Attach(reader, filename, string(contentType))+ if err != nil {+ return "", err+ }+ return attachment.Filename, nil+}+// SMTPAuthType is the type of SMTP authentication.type SMTPAuthType uint