From a8c6c22e7f5b629db3e6f275028a23ee9a9a50f3 Mon Sep 17 00:00:00 2001 From: Biswa Kalyan Bhuyan Date: Sat, 24 May 2025 11:03:37 +0530 Subject: Add custom SMTP authentication support - Add SMTPUsername and SMTPPassword fields to Settings struct - Update DoSMTP method to use custom credentials when available - Add SMTP credential fields to all theme templates - Support independent username/password configuration Fixes authentication issues when using different SMTP credentials than login credentials, useful for app-specific passwords. --- session.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'session.go') diff --git a/session.go b/session.go index a85bff6..f241b6a 100644 --- a/session.go +++ b/session.go @@ -112,7 +112,23 @@ func (s *Session) DoSMTP(f func(*smtp.Client) error) error { } defer c.Close() - auth := sasl.NewPlainClient("", s.username, s.password) + // Try to get custom SMTP credentials from user settings + smtpUsername := s.username + smtpPassword := s.password + + // Load settings to check for custom SMTP credentials + if settings, err := s.loadSMTPSettings(); err == nil { + // Use custom username if provided, otherwise keep login username + if settings.SMTPUsername != "" { + smtpUsername = settings.SMTPUsername + } + // Use custom password if provided, otherwise keep login password + if settings.SMTPPassword != "" { + smtpPassword = settings.SMTPPassword + } + } + + auth := sasl.NewPlainClient("", smtpUsername, smtpPassword) if err := c.Auth(auth); err != nil { return AuthError{err} } @@ -128,6 +144,34 @@ func (s *Session) DoSMTP(f func(*smtp.Client) error) error { return nil } +// SMTPSettings represents custom SMTP authentication settings +type SMTPSettings struct { + SMTPUsername string + SMTPPassword string +} + +// loadSMTPSettings is a helper method to load SMTP settings for this session +func (s *Session) loadSMTPSettings() (*SMTPSettings, error) { + const settingsKey = "base.settings" + var fullSettings struct { + MessagesPerPage int + Signature string + From string + Subscriptions []string + SMTPUsername string + SMTPPassword string + } + + if err := s.Store().Get(settingsKey, &fullSettings); err != nil { + return nil, err + } + + return &SMTPSettings{ + SMTPUsername: fullSettings.SMTPUsername, + SMTPPassword: fullSettings.SMTPPassword, + }, nil +} + // SetHTTPBasicAuth adds an Authorization header field to the request with // this session's credentials. func (s *Session) SetHTTPBasicAuth(req *http.Request) { -- cgit v1.2.3-59-g8ed1b