From 0b108edd2ae5c444e578cf99390e3d64d1ee2056 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 14:15:45 +0000 Subject: [PATCH] Add opt-in email subaddressing for mail aliases Members can enable email_subaddressing_enabled so mail sent to an alias is forwarded to user+aliasname@domain, letting them filter per alias. Fixes #582 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKEZkZbPrfd4vaSSDNP --- README.md | 12 ++++++ app/jobs/mail_forward_job.rb | 8 ++-- app/models/mail_alias.rb | 10 +++-- app/models/user.rb | 10 +++++ app/resources/v1/user_resource.rb | 6 +-- ...dd_email_subaddressing_enabled_to_users.rb | 5 +++ db/schema.rb | 3 +- spec/models/mail_alias_spec.rb | 38 +++++++++++++++++++ spec/models/user_spec.rb | 26 +++++++++++++ spec/resources/v1/user_resource_spec.rb | 4 +- 10 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20261202140000_add_email_subaddressing_enabled_to_users.rb diff --git a/README.md b/README.md index 07d03ff1..031acf68 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,18 @@ To test the ImprovMX endpoint you can setup ImprovMX to forward mail to your loc 5. Start the rails server +## Email subaddressing for mail aliases +Members can enable `email_subaddressing_enabled` on their user. Mail sent to any alias they receive +(their own alias, or the alias of a group they are an active member of) is then forwarded to a +subaddress of their own email address, tagged with the alias name. For example, with the address +`john@example.com`, mail to `bestuur@csvalpha.nl` is delivered to `john+bestuur@example.com`. + +Members can use this to organize their mailbox: create a filter or rule on the recipient +(e.g. "to: john+bestuur@example.com") that applies a label or moves the mail to a folder per alias. +Most providers support this, including Gmail, Outlook/Hotmail, iCloud and Proton Mail. Check that +your provider delivers `+tag` addresses before enabling it. Addresses that already contain a `+` +are left as they are. + ## Run specs (tests) The tests are written with [RSpec](http://rspec.info/), a behaviour driven test environment. diff --git a/app/jobs/mail_forward_job.rb b/app/jobs/mail_forward_job.rb index 04186fb8..75f84bcc 100644 --- a/app/jobs/mail_forward_job.rb +++ b/app/jobs/mail_forward_job.rb @@ -4,12 +4,12 @@ class MailForwardJob < ApplicationJob queue_as :mail_handlers def perform(stored_mail) - to_addresses = stored_mail.mail_alias.mail_addresses - to_addresses.each do |to_address| + mail_alias = stored_mail.mail_alias + mail_alias.recipients.each do |recipient| # Don't send the mail to the sender - next if stored_mail.inbound_email.mail.from.include? to_address + next if stored_mail.inbound_email.mail.from.include? recipient.email - MailForwardSendJob.perform_later(stored_mail, to_address) + MailForwardSendJob.perform_later(stored_mail, recipient.email_for_mail_alias(mail_alias)) end end end diff --git a/app/models/mail_alias.rb b/app/models/mail_alias.rb index 3f120fed..8d855ac3 100644 --- a/app/models/mail_alias.rb +++ b/app/models/mail_alias.rb @@ -21,10 +21,14 @@ class MailAlias < ApplicationRecord joins(:moderator_group).where(moderator_group: Group.active_groups_for_user(user)) } - def mail_addresses - return [user.email] if user + def recipients + return [user] if user + + User.active_users_for_group(group) + end - User.active_users_for_group(group).pluck(:email) + def mail_addresses + recipients.map { |recipient| recipient.email_for_mail_alias(self) } end def moderators diff --git a/app/models/user.rb b/app/models/user.rb index e0a441ac..51b0f7ae 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -191,6 +191,16 @@ def to_ical # rubocop:disable Metrics/AbcSize event end + # Returns the address mail sent to the given alias should be forwarded to. With subaddressing + # enabled, the alias name is added as a tag, e.g. mail to bestuur@csvalpha.nl is delivered to + # john+bestuur@example.com, so members can filter their mailbox per alias. + def email_for_mail_alias(mail_alias) + local_part, domain = email.to_s.split('@', 2) + return email unless email_subaddressing_enabled? && domain.present? && local_part.exclude?('+') + + "#{local_part}+#{mail_alias.alias_name}@#{domain}" + end + private def downcase_email! diff --git a/app/resources/v1/user_resource.rb b/app/resources/v1/user_resource.rb index cc5dc867..d23c959d 100644 --- a/app/resources/v1/user_resource.rb +++ b/app/resources/v1/user_resource.rb @@ -7,7 +7,7 @@ class V1::UserResource < V1::ApplicationResource # rubocop:disable Metrics/Class :picture_publication_preference, :ical_secret_key, :ical_categories, :password, :avatar, :avatar_url, :avatar_thumb_url, :user_details_sharing_preference, :allow_sofia_sharing, :trailer_drivers_license, - :sidekiq_access, :setup_complete + :sidekiq_access, :setup_complete, :email_subaddressing_enabled def avatar_url @model.avatar.url @@ -58,7 +58,7 @@ def fetchable_fields emergency_number ifes_data_sharing_preference info_in_almanak almanak_subscription_preference digtus_subscription_preference user_details_sharing_preference allow_sofia_sharing - sidekiq_access setup_complete] + sidekiq_access setup_complete email_subaddressing_enabled] end allowed_keys += %i[picture_publication_preference] if read_or_me? if read_user_details? && !application_is_sofia? @@ -80,7 +80,7 @@ def self.creatable_fields(context) # rubocop:disable Metrics/MethodLength user_details_sharing_preference allow_sofia_sharing picture_publication_preference info_in_almanak ifes_data_sharing_preference ical_categories sidekiq_access - setup_complete] + setup_complete email_subaddressing_enabled] end if user_can_create_or_update?(context) diff --git a/db/migrate/20261202140000_add_email_subaddressing_enabled_to_users.rb b/db/migrate/20261202140000_add_email_subaddressing_enabled_to_users.rb new file mode 100644 index 00000000..503a068f --- /dev/null +++ b/db/migrate/20261202140000_add_email_subaddressing_enabled_to_users.rb @@ -0,0 +1,5 @@ +class AddEmailSubaddressingEnabledToUsers < ActiveRecord::Migration[7.2] + def change + add_column :users, :email_subaddressing_enabled, :boolean, default: false, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 9d625fbe..b2ee2571 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_11_27_120000) do +ActiveRecord::Schema[7.2].define(version: 2026_12_02_140000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -605,6 +605,7 @@ t.boolean "trailer_drivers_license", default: false, null: false t.boolean "setup_complete", default: false, null: false t.string "ical_categories", default: [], array: true + t.boolean "email_subaddressing_enabled", default: false, null: false t.index ["deleted_at"], name: "index_users_on_deleted_at" t.index ["email"], name: "index_users_on_email", unique: true t.index ["login_enabled"], name: "index_users_on_login_enabled" diff --git a/spec/models/mail_alias_spec.rb b/spec/models/mail_alias_spec.rb index 975a3bca..e43e9b6c 100644 --- a/spec/models/mail_alias_spec.rb +++ b/spec/models/mail_alias_spec.rb @@ -103,6 +103,44 @@ it { expect(mail_alias.mail_addresses).to contain_exactly(mail_alias.user.email) } end + + context 'when a recipient has subaddressing enabled' do + subject(:mail_alias) do + create(:mail_alias, :with_group, email: 'bestuur@test.csvalpha.nl') + end + + let(:user) { create(:user, email: 'john@example.com', email_subaddressing_enabled: true) } + let(:other_user) { create(:user, email: 'jane@example.com') } + + before do + create(:membership, group: mail_alias.group, user:) + create(:membership, group: mail_alias.group, user: other_user) + end + + it do + expect(mail_alias.mail_addresses).to include('john+bestuur@example.com', 'jane@example.com') + end + + it { expect(mail_alias.mail_addresses).not_to include('john@example.com') } + end + end + + describe '#recipients' do + context 'when with user' do + subject(:mail_alias) { build_stubbed(:mail_alias, :with_user) } + + it { expect(mail_alias.recipients).to contain_exactly(mail_alias.user) } + end + + context 'when with group' do + subject(:mail_alias) { create(:mail_alias, :with_group) } + + let(:user) { create(:user) } + + before { create(:membership, group: mail_alias.group, user:) } + + it { expect(mail_alias.recipients).to include(user) } + end end describe '#downcase_email' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0251547e..8abf71da 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -648,6 +648,32 @@ end end + describe '#email_for_mail_alias' do + let(:mail_alias) { build_stubbed(:mail_alias, :with_group, email: 'bestuur@test.csvalpha.nl') } + + context 'when subaddressing is disabled' do + let(:user) { build_stubbed(:user, email: 'john@example.com') } + + it { expect(user.email_for_mail_alias(mail_alias)).to eq 'john@example.com' } + end + + context 'when subaddressing is enabled' do + let(:user) do + build_stubbed(:user, email: 'john@example.com', email_subaddressing_enabled: true) + end + + it { expect(user.email_for_mail_alias(mail_alias)).to eq 'john+bestuur@example.com' } + end + + context 'when subaddressing is enabled and the email already has a tag' do + let(:user) do + build_stubbed(:user, email: 'john+amber@example.com', email_subaddressing_enabled: true) + end + + it { expect(user.email_for_mail_alias(mail_alias)).to eq 'john+amber@example.com' } + end + end + describe '.password_reset_url' do it { expect(described_class.password_reset_url).to include('forgot-password') } end diff --git a/spec/resources/v1/user_resource_spec.rb b/spec/resources/v1/user_resource_spec.rb index 3b3bb74b..00c2c134 100644 --- a/spec/resources/v1/user_resource_spec.rb +++ b/spec/resources/v1/user_resource_spec.rb @@ -16,7 +16,7 @@ emergency_number ifes_data_sharing_preference info_in_almanak almanak_subscription_preference digtus_subscription_preference user_details_sharing_preference allow_sofia_sharing - sidekiq_access setup_complete] + sidekiq_access setup_complete email_subaddressing_enabled] end let(:read_fields) do %i[picture_publication_preference] @@ -145,7 +145,7 @@ let(:current_user_fields) do %i[otp_required password user_details_sharing_preference allow_sofia_sharing info_in_almanak ifes_data_sharing_preference picture_publication_preference - sidekiq_access setup_complete] + sidekiq_access setup_complete email_subaddressing_enabled] end context 'when without permission' do