diff --git a/app/assets/javascripts/discourse/app/components/group-smtp-email-settings.js b/app/assets/javascripts/discourse/app/components/group-smtp-email-settings.js index d9758d2c463..52c6d27ab9d 100644 --- a/app/assets/javascripts/discourse/app/components/group-smtp-email-settings.js +++ b/app/assets/javascripts/discourse/app/components/group-smtp-email-settings.js @@ -37,6 +37,7 @@ export default Component.extend({ EmberObject.create({ email_username: this.group.email_username, email_password: this.group.email_password, + email_from_alias: this.group.email_from_alias, smtp_server: this.group.smtp_server, smtp_port: (this.group.smtp_port || "").toString(), smtp_ssl: this.group.smtp_ssl, @@ -73,6 +74,7 @@ export default Component.extend({ smtp_port: this.form.smtp_port, smtp_ssl: this.form.smtp_ssl, email_username: this.form.email_username, + email_from_alias: this.form.email_from_alias, email_password: this.form.email_password, }); }) diff --git a/app/assets/javascripts/discourse/app/models/group.js b/app/assets/javascripts/discourse/app/models/group.js index a360f8c3d89..2d48c03905e 100644 --- a/app/assets/javascripts/discourse/app/models/group.js +++ b/app/assets/javascripts/discourse/app/models/group.js @@ -243,6 +243,7 @@ const Group = RestModel.extend({ imap_mailbox_name: this.imap_mailbox_name, imap_enabled: this.imap_enabled, email_username: this.email_username, + email_from_alias: this.email_from_alias, email_password: this.email_password, flair_icon: null, flair_upload_id: null, diff --git a/app/assets/javascripts/discourse/app/templates/components/group-manage-email-settings.hbs b/app/assets/javascripts/discourse/app/templates/components/group-manage-email-settings.hbs index 2f20944a3a8..7c8080f79d2 100644 --- a/app/assets/javascripts/discourse/app/templates/components/group-manage-email-settings.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/group-manage-email-settings.hbs @@ -45,5 +45,5 @@
- {{group-manage-save-button model=group disabled=(not emailSettingsValid) beforeSave=beforeSave afterSave=afterSave tabindex="14"}} + {{group-manage-save-button model=group disabled=(not emailSettingsValid) beforeSave=beforeSave afterSave=afterSave tabindex="15"}} diff --git a/app/assets/javascripts/discourse/app/templates/components/group-smtp-email-settings.hbs b/app/assets/javascripts/discourse/app/templates/components/group-smtp-email-settings.hbs index 2b807460c7d..525b66091b1 100644 --- a/app/assets/javascripts/discourse/app/templates/components/group-smtp-email-settings.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/group-smtp-email-settings.hbs @@ -8,11 +8,11 @@
- {{input type="text" name="smtp_server" value=form.smtp_server tabindex="3" onChange=(action "resetSettingsValid")}} + {{input type="text" name="smtp_server" value=form.smtp_server tabindex="4" onChange=(action "resetSettingsValid")}}
@@ -25,7 +25,15 @@
- {{input type="text" name="smtp_port" value=form.smtp_port tabindex="4" onChange=(action "resetSettingsValid" form.smtp_port)}} + {{input type="text" name="smtp_port" value=form.smtp_port tabindex="5" onChange=(action "resetSettingsValid" form.smtp_port)}} +
+ + +
+
+ + {{input type="text" name="from_alias" id="from_alias" value=form.email_from_alias onChange=(action "resetSettingsValid") tabindex="3"}} +

{{i18n "groups.manage.email.settings.from_alias_hint"}}

@@ -43,7 +51,7 @@ action=(action "testSmtpSettings") icon="cog" label="groups.manage.email.test_settings" - tabindex="6" + tabindex="7" title="groups.manage.email.settings_required" }} diff --git a/app/assets/javascripts/discourse/tests/acceptance/group-manage-email-settings-test.js b/app/assets/javascripts/discourse/tests/acceptance/group-manage-email-settings-test.js index 49ae5dd9f2a..cff0acf17b2 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/group-manage-email-settings-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/group-manage-email-settings-test.js @@ -106,6 +106,8 @@ acceptance( await fillIn('input[name="username"]', "myusername@gmail.com"); await fillIn('input[name="password"]', "password@gmail.com"); + await fillIn("#from_alias", "akasomegroup@example.com"); + await click(".test-smtp-settings"); assert.ok(exists(".smtp-settings-ok"), "tested settings are ok"); diff --git a/app/assets/stylesheets/common/base/group.scss b/app/assets/stylesheets/common/base/group.scss index 7ca7384c576..5f1087ecd99 100644 --- a/app/assets/stylesheets/common/base/group.scss +++ b/app/assets/stylesheets/common/base/group.scss @@ -252,7 +252,7 @@ table.group-category-permissions { .group-imap-email-settings { .groups-form { display: grid; - grid-template-columns: 1fr 3fr; + grid-template-columns: 1fr 1fr 1fr; margin-bottom: 0; &.groups-form-imap { diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 2ca448e3237..4d1687fab09 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -713,6 +713,7 @@ class GroupsController < ApplicationController :imap_updated_at, :email_username, :email_password, + :email_from_alias, :primary_group, :visibility_level, :members_visibility_level, diff --git a/app/mailers/group_smtp_mailer.rb b/app/mailers/group_smtp_mailer.rb index ce12e4b0cc2..25bb887b0d5 100644 --- a/app/mailers/group_smtp_mailer.rb +++ b/app/mailers/group_smtp_mailer.rb @@ -48,7 +48,7 @@ class GroupSmtpMailer < ActionMailer::Base add_re_to_subject: true, locale: SiteSetting.default_locale, delivery_method_options: delivery_options, - from: from_group.email_username, + from: from_group.smtp_from_address, from_alias: I18n.t('email_from_without_site', user_name: group_name), html_override: html_override(post), cc: cc_addresses diff --git a/app/models/group.rb b/app/models/group.rb index a53d7e3ce37..3c64ab49d14 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -110,7 +110,8 @@ class Group < ActiveRecord::Base "imap_port", "imap_ssl", "email_username", - "email_password" + "email_password", + "email_from_alias" ] ALIAS_LEVELS = { @@ -290,6 +291,10 @@ class Group < ActiveRecord::Base end end + def smtp_from_address + self.email_from_alias.present? ? self.email_from_alias : self.email_username + end + def downcase_incoming_email self.incoming_email = (incoming_email || "").strip.downcase.presence end @@ -708,7 +713,9 @@ class Group < ActiveRecord::Base def self.find_by_email(email) self.where( - "email_username = :email OR string_to_array(incoming_email, '|') @> ARRAY[:email]", + "email_username = :email OR + string_to_array(incoming_email, '|') @> ARRAY[:email] OR + email_from_alias = :email", email: Email.downcase(email) ).first end @@ -1128,6 +1135,7 @@ end # imap_enabled :boolean default(FALSE) # imap_updated_at :datetime # imap_updated_by_id :integer +# email_from_alias :string # # Indexes # diff --git a/app/serializers/group_show_serializer.rb b/app/serializers/group_show_serializer.rb index 0b51aed01bf..f3b520d19ac 100644 --- a/app/serializers/group_show_serializer.rb +++ b/app/serializers/group_show_serializer.rb @@ -32,6 +32,7 @@ class GroupShowSerializer < BasicGroupSerializer :imap_updated_by, :email_username, :email_password, + :email_from_alias, :imap_last_error, :imap_old_emails, :imap_new_emails, diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index cbb3e1eda50..80b7b8fda1d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -754,6 +754,8 @@ en: title: "Settings" allow_unknown_sender_topic_replies: "Allow unknown sender topic replies." allow_unknown_sender_topic_replies_hint: "Allows unknown senders to reply to group topics. If this is not enabled, replies from email addresses not already invited to the topic will create a new topic." + from_alias: "From Alias" + from_alias_hint: "Alias to use as the from address when sending group SMTP emails. Note this may not be supported by all mail providers, please consult your mail provider's documentation." mailboxes: synchronized: "Synchronized Mailbox" none_found: "No mailboxes were found in this email account." diff --git a/db/migrate/20220124003259_add_email_from_alias_to_groups.rb b/db/migrate/20220124003259_add_email_from_alias_to_groups.rb new file mode 100644 index 00000000000..260dd01bde7 --- /dev/null +++ b/db/migrate/20220124003259_add_email_from_alias_to_groups.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddEmailFromAliasToGroups < ActiveRecord::Migration[6.1] + def change + add_column :groups, :email_from_alias, :string, null: true + end +end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 0bd4eff451b..54bf74c230c 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -1225,6 +1225,23 @@ describe Group do expect(group.smtp_updated_by).to eq(user) end + it "records the change for singular setting changes" do + group.update( + smtp_port: 587, + smtp_ssl: true, + smtp_server: "smtp.gmail.com", + email_username: "test@gmail.com", + email_password: "password", + ) + group.record_email_setting_changes!(user) + group.reload + + old_updated_at = group.smtp_updated_at + group.update(email_from_alias: "somealias@gmail.com") + group.record_email_setting_changes!(user) + expect(group.reload.smtp_updated_at).not_to eq_time(old_updated_at) + end + it "enables imap and records the change" do group.update( imap_port: 587, @@ -1314,5 +1331,12 @@ describe Group do expect(Group.find_by_email("support@test.com")).to eq(group) expect(Group.find_by_email("nope@test.com")).to eq(nil) end + + it "finds the group by its email_from_alias" do + group.update!(email_username: "abc@test.com", email_from_alias: "somealias@test.com") + expect(Group.find_by_email("abc@test.com")).to eq(group) + expect(Group.find_by_email("somealias@test.com")).to eq(group) + expect(Group.find_by_email("nope@test.com")).to eq(nil) + end end end diff --git a/spec/requests/api/schemas/json/group_response.json b/spec/requests/api/schemas/json/group_response.json index a3bf6d151c4..a649b8133a0 100644 --- a/spec/requests/api/schemas/json/group_response.json +++ b/spec/requests/api/schemas/json/group_response.json @@ -221,6 +221,12 @@ "null" ] }, + "email_from_alias": { + "type": [ + "string", + "null" + ] + }, "email_password": { "type": [ "string",