TestEmailSetting sends a test email using the provided config.
(ctx context.Context, req *connect.Request[v1pb.TestEmailSettingRequest])
| 759 | |
| 760 | // TestEmailSetting sends a test email using the provided config. |
| 761 | func (s *SettingService) TestEmailSetting(ctx context.Context, req *connect.Request[v1pb.TestEmailSettingRequest]) (*connect.Response[v1pb.TestEmailSettingResponse], error) { |
| 762 | emailSetting := convertEmailSetting(req.Msg.EmailSetting) |
| 763 | if emailSetting == nil { |
| 764 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("email_setting is required")) |
| 765 | } |
| 766 | |
| 767 | // Substitute stored password if not provided. |
| 768 | if smtp := emailSetting.GetSmtp(); smtp != nil && smtp.Password == "" { |
| 769 | workspaceID := common.GetWorkspaceIDFromContext(ctx) |
| 770 | if existing, err := s.store.GetSetting(ctx, workspaceID, storepb.SettingName_EMAIL); err == nil && existing != nil { |
| 771 | if oldEmail, ok := existing.Value.(*storepb.EmailSetting); ok { |
| 772 | if oldSMTP := oldEmail.GetSmtp(); oldSMTP != nil { |
| 773 | smtp.Password = oldSMTP.Password |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | sender, err := mailer.NewSender(emailSetting) |
| 780 | if err != nil { |
| 781 | return connect.NewResponse(&v1pb.TestEmailSettingResponse{ //nolint:nilerr |
| 782 | Success: false, |
| 783 | Error: err.Error(), |
| 784 | }), nil |
| 785 | } |
| 786 | |
| 787 | err = sender.Send(ctx, &mailer.SendRequest{ |
| 788 | To: []string{req.Msg.To}, |
| 789 | Subject: "Bytebase email config test", |
| 790 | TextBody: "This is a test email from Bytebase to verify your email configuration.", |
| 791 | }) |
| 792 | if err != nil { |
| 793 | return connect.NewResponse(&v1pb.TestEmailSettingResponse{ //nolint:nilerr |
| 794 | Success: false, |
| 795 | Error: err.Error(), |
| 796 | }), nil |
| 797 | } |
| 798 | |
| 799 | return connect.NewResponse(&v1pb.TestEmailSettingResponse{ |
| 800 | Success: true, |
| 801 | }), nil |
| 802 | } |
| 803 | |
| 804 | func (s *SettingService) checkSettingPermission(ctx context.Context, req connect.AnyRequest, perm permission.Permission) error { |
| 805 | user, ok := GetUserFromContext(ctx) |
nothing calls this directly
no test coverage detected