feat: Allow custom email address for inbox (#1336)

This commit is contained in:
Pranav Raj S 2020-10-11 23:24:11 +05:30 committed by GitHub
parent 58c0792920
commit 2c324d9421
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 9 deletions

View file

@ -15,7 +15,7 @@ class ConversationReplyMailer < ApplicationMailer
mail({ mail({
to: @contact&.email, to: @contact&.email,
from: from_email, from: from_email_with_name,
reply_to: reply_email, reply_to: reply_email,
subject: mail_subject, subject: mail_subject,
message_id: custom_message_id, message_id: custom_message_id,
@ -34,7 +34,7 @@ class ConversationReplyMailer < ApplicationMailer
mail({ mail({
to: @contact&.email, to: @contact&.email,
from: from_email, from: from_email_with_name,
reply_to: reply_email, reply_to: reply_email,
subject: mail_subject, subject: mail_subject,
message_id: custom_message_id, message_id: custom_message_id,
@ -51,7 +51,7 @@ class ConversationReplyMailer < ApplicationMailer
mail({ mail({
to: to_email, to: to_email,
from: from_email, from: from_email_with_name,
subject: "[##{@conversation.display_id}] #{I18n.t('conversations.reply.transcript_subject')}" subject: "[##{@conversation.display_id}] #{I18n.t('conversations.reply.transcript_subject')}"
}) })
end end
@ -63,6 +63,7 @@ class ConversationReplyMailer < ApplicationMailer
@account = @conversation.account @account = @conversation.account
@contact = @conversation.contact @contact = @conversation.contact
@agent = @conversation.assignee @agent = @conversation.assignee
@inbox = @conversation.inbox
end end
def conversation_already_viewed? def conversation_already_viewed?
@ -90,18 +91,24 @@ class ConversationReplyMailer < ApplicationMailer
if inbound_email_enabled? if inbound_email_enabled?
"#{assignee_name} <reply+#{@conversation.uuid}@#{current_domain}>" "#{assignee_name} <reply+#{@conversation.uuid}@#{current_domain}>"
else else
@agent&.email @inbox.email_address || @agent&.email
end end
end end
def from_email def from_email_with_name
if inbound_email_enabled? if inbound_email_enabled?
"#{assignee_name} <#{account_support_email}>" "#{assignee_name} <#{account_support_email}>"
else else
"#{assignee_name} <#{ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')}>" "#{assignee_name} <#{from_email_address}>"
end end
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 def custom_message_id
"<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{current_domain}>" "<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{current_domain}>"
end end

View file

@ -6,6 +6,7 @@
# #
# id :integer not null, primary key # id :integer not null, primary key
# channel_type :string # channel_type :string
# email_address :string
# enable_auto_assignment :boolean default(TRUE) # enable_auto_assignment :boolean default(TRUE)
# greeting_enabled :boolean default(FALSE) # greeting_enabled :boolean default(FALSE)
# greeting_message :string # greeting_message :string

View file

@ -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

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto" 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 "enable_auto_assignment", default: true
t.boolean "greeting_enabled", default: false t.boolean "greeting_enabled", default: false
t.string "greeting_message" t.string "greeting_message"
t.string "email_address"
t.index ["account_id"], name: "index_inboxes_on_account_id" t.index ["account_id"], name: "index_inboxes_on_account_id"
end 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", "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", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id" 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 ["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", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id" 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 end
create_table "tags", id: :serial, force: :cascade do |t| create_table "tags", id: :serial, force: :cascade do |t|

View file

@ -114,6 +114,18 @@ RSpec.describe ConversationReplyMailer, type: :mailer do
end end
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 context 'when the custom domain emails are enabled' do
let(:account) { create(:account) } let(:account) { create(:account) }
let(:conversation) { create(:conversation, assignee: agent, account: account).reload } let(:conversation) { create(:conversation, assignee: agent, account: account).reload }