diff --git a/webmail/static/webmail/webmail.js b/webmail/static/webmail/webmail.js index 0899eda74..b1d3bb9a5 100644 --- a/webmail/static/webmail/webmail.js +++ b/webmail/static/webmail/webmail.js @@ -149,7 +149,12 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($ // ── Helper ─────────────────────────────────────────────── function apiCall(url, data, callback, errback) { var config = {headers: {'X-CSRFToken': getCookie('csrftoken')}}; - $http.post(url, data || {}, config).then(function(resp) { + var payload = data || {}; + // Always send current account so backend uses the right email + if ($scope.currentEmail && !payload.fromAccount) { + payload.fromAccount = $scope.currentEmail; + } + $http.post(url, payload, config).then(function(resp) { if (callback) callback(resp.data); }, function(err) { console.error('API error:', url, err); @@ -180,23 +185,28 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($ $scope.switchAccount = function() { var newEmail = $scope.currentEmail; if (!newEmail) return; + + // Reset view state immediately + $scope.currentFolder = 'INBOX'; + $scope.currentPage = 1; + $scope.openMsg = null; + $scope.viewMode = 'list'; + $scope.messages = []; + $scope.contacts = []; + $scope.filteredContacts = []; + $scope.sieveRules = []; + apiCall('/webmail/api/switchAccount', {email: newEmail}, function(data) { if (data.status === 1) { - $scope.currentFolder = 'INBOX'; - $scope.currentPage = 1; - $scope.openMsg = null; - $scope.viewMode = 'list'; - $scope.messages = []; - $scope.contacts = []; - $scope.filteredContacts = []; - $scope.sieveRules = []; $scope.loadFolders(); $scope.loadSettings(); } else { notify(data.error_message || 'Failed to switch account', 'error'); + console.error('switchAccount failed:', data); } - }, function() { - notify('Failed to switch account', 'error'); + }, function(err) { + notify('Failed to switch account: ' + (err.status || 'unknown error'), 'error'); + console.error('switchAccount HTTP error:', err); }); }; @@ -488,6 +498,7 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($ stopDraftAutoSave(); var fd = new FormData(); + fd.append('fromAccount', $scope.currentEmail || ''); fd.append('to', $scope.compose.to); fd.append('cc', $scope.compose.cc || ''); fd.append('bcc', $scope.compose.bcc || ''); @@ -508,7 +519,7 @@ app.controller('webmailCtrl', ['$scope', '$http', '$sce', '$timeout', function($ }).then(function(resp) { $scope.sending = false; if (resp.data.status === 1) { - notify('Message sent.'); + notify('Message sent from ' + (resp.data.sentFrom || 'unknown')); $scope.viewMode = 'list'; $scope.loadMessages(); } else { diff --git a/webmail/templates/webmail/index.html b/webmail/templates/webmail/index.html index e684057c7..ab5b137e6 100644 --- a/webmail/templates/webmail/index.html +++ b/webmail/templates/webmail/index.html @@ -175,6 +175,15 @@

{% trans "Compose" %}

+
+ + + +
- + {% endblock %} diff --git a/webmail/webmailManager.py b/webmail/webmailManager.py index 3895c519d..78e380c60 100644 --- a/webmail/webmailManager.py +++ b/webmail/webmailManager.py @@ -46,6 +46,18 @@ class WebmailManager: return self.request.POST.dict() def _get_email(self): + # Check for explicit email in POST body (from account switcher) + # This ensures the correct account is used even if session is stale + try: + data = json.loads(self.request.body) + explicit = data.get('fromAccount', '') + if explicit: + accounts = self._get_managed_accounts() + if explicit in accounts: + self.request.session['webmail_email'] = explicit + return explicit + except Exception: + pass return self.request.session.get('webmail_email') def _get_master_config(self): @@ -324,6 +336,14 @@ class WebmailManager: def apiSendMessage(self): try: + # For multipart forms, check fromAccount in POST data + if self.request.content_type and 'multipart' in self.request.content_type: + from_account = self.request.POST.get('fromAccount', '') + if from_account: + accounts = self._get_managed_accounts() + if from_account in accounts: + self.request.session['webmail_email'] = from_account + email_addr = self._get_email() if not email_addr: return self._error('Not logged in.') @@ -390,7 +410,7 @@ class WebmailManager: except Exception: pass - return self._success({'messageId': result['message_id']}) + return self._success({'messageId': result['message_id'], 'sentFrom': email_addr}) except Exception as e: return self._error(str(e))