diff --git a/app/mailers/conversation_reply_mailer.rb b/app/mailers/conversation_reply_mailer.rb index 9b6caa0f6..24ccfb7b6 100644 --- a/app/mailers/conversation_reply_mailer.rb +++ b/app/mailers/conversation_reply_mailer.rb @@ -15,7 +15,7 @@ class ConversationReplyMailer < ApplicationMailer mail({ to: @contact&.email, - from: from_email, + from: from_email_with_name, reply_to: reply_email, subject: mail_subject, message_id: custom_message_id, @@ -34,7 +34,7 @@ class ConversationReplyMailer < ApplicationMailer mail({ to: @contact&.email, - from: from_email, + from: from_email_with_name, reply_to: reply_email, subject: mail_subject, message_id: custom_message_id, @@ -51,7 +51,7 @@ class ConversationReplyMailer < ApplicationMailer mail({ to: to_email, - from: from_email, + from: from_email_with_name, subject: "[##{@conversation.display_id}] #{I18n.t('conversations.reply.transcript_subject')}" }) end @@ -63,6 +63,7 @@ class ConversationReplyMailer < ApplicationMailer @account = @conversation.account @contact = @conversation.contact @agent = @conversation.assignee + @inbox = @conversation.inbox end def conversation_already_viewed? @@ -90,18 +91,24 @@ class ConversationReplyMailer < ApplicationMailer if inbound_email_enabled? "#{assignee_name} " else - @agent&.email + @inbox.email_address || @agent&.email end end - def from_email + def from_email_with_name if inbound_email_enabled? "#{assignee_name} <#{account_support_email}>" else - "#{assignee_name} <#{ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')}>" + "#{assignee_name} <#{from_email_address}>" end end + def from_email_address + return @inbox.email_address if @inbox.email_address + + ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com') + end + def custom_message_id "" end diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 1f01e4bdd..51dc47da7 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -6,6 +6,7 @@ # # id :integer not null, primary key # channel_type :string +# email_address :string # enable_auto_assignment :boolean default(TRUE) # greeting_enabled :boolean default(FALSE) # greeting_message :string diff --git a/db/migrate/20201011152227_add_email_address_to_inbox.rb b/db/migrate/20201011152227_add_email_address_to_inbox.rb new file mode 100644 index 000000000..2691c563d --- /dev/null +++ b/db/migrate/20201011152227_add_email_address_to_inbox.rb @@ -0,0 +1,9 @@ +class AddEmailAddressToInbox < ActiveRecord::Migration[6.0] + def up + add_column :inboxes, :email_address, :string + end + + def down + remove_column(:inboxes, :email_address) + end +end diff --git a/db/schema.rb b/db/schema.rb index 231b95f1b..ea8f5f05e 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.define(version: 2020_09_27_135222) do +ActiveRecord::Schema.define(version: 2020_10_11_152227) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -277,6 +277,7 @@ ActiveRecord::Schema.define(version: 2020_09_27_135222) do t.boolean "enable_auto_assignment", default: true t.boolean "greeting_enabled", default: false t.string "greeting_message" + t.string "email_address" t.index ["account_id"], name: "index_inboxes_on_account_id" end @@ -449,11 +450,9 @@ ActiveRecord::Schema.define(version: 2020_09_27_135222) do t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy" t.index ["taggable_id"], name: "index_taggings_on_taggable_id" - t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id" t.index ["taggable_type"], name: "index_taggings_on_taggable_type" t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type" t.index ["tagger_id"], name: "index_taggings_on_tagger_id" - t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id" end create_table "tags", id: :serial, force: :cascade do |t| diff --git a/spec/mailers/conversation_reply_mailer_spec.rb b/spec/mailers/conversation_reply_mailer_spec.rb index 78262c22d..4558c568f 100644 --- a/spec/mailers/conversation_reply_mailer_spec.rb +++ b/spec/mailers/conversation_reply_mailer_spec.rb @@ -114,6 +114,18 @@ RSpec.describe ConversationReplyMailer, type: :mailer do end end + context 'when inbox email address is available' do + let(:inbox) { create(:inbox, account: account, email_address: 'noreply@chatwoot.com') } + let(:conversation) { create(:conversation, assignee: agent, inbox: inbox, account: account) } + let!(:message) { create(:message, conversation: conversation, account: account) } + let(:mail) { described_class.reply_with_summary(message.conversation, Time.zone.now).deliver_now } + + it 'set reply to email address as inbox email address' do + expect(mail.from).to eq([inbox.email_address]) + expect(mail.reply_to).to eq([inbox.email_address]) + end + end + context 'when the custom domain emails are enabled' do let(:account) { create(:account) } let(:conversation) { create(:conversation, assignee: agent, account: account).reload }