feat(CW-6187): include headers from incoming emails (#13139)
This commit is contained in:
@@ -95,6 +95,7 @@ class MailPresenter < SimpleDelegator
|
|||||||
content_type: content_type,
|
content_type: content_type,
|
||||||
date: date,
|
date: date,
|
||||||
from: from,
|
from: from,
|
||||||
|
headers: headers_data,
|
||||||
html_content: html_content,
|
html_content: html_content,
|
||||||
in_reply_to: in_reply_to,
|
in_reply_to: in_reply_to,
|
||||||
message_id: message_id,
|
message_id: message_id,
|
||||||
@@ -136,6 +137,16 @@ class MailPresenter < SimpleDelegator
|
|||||||
from_email_address(@mail[:reply_to].try(:value)) || @mail['X-Original-Sender'].try(:value) || from_email_address(from.first)
|
from_email_address(@mail[:reply_to].try(:value)) || @mail['X-Original-Sender'].try(:value) || from_email_address(from.first)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def headers_data
|
||||||
|
headers = {
|
||||||
|
'x-original-from' => @mail['X-Original-From']&.value,
|
||||||
|
'x-original-sender' => @mail['X-Original-Sender']&.value,
|
||||||
|
'x-forwarded-for' => @mail['X-Forwarded-For']&.value
|
||||||
|
}.compact
|
||||||
|
|
||||||
|
headers.presence
|
||||||
|
end
|
||||||
|
|
||||||
def from_email_address(email)
|
def from_email_address(email)
|
||||||
Mail::Address.new(email).address
|
Mail::Address.new(email).address
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ RSpec.describe ReplyMailbox do
|
|||||||
let(:conversation) { create(:conversation, assignee: agent, inbox: create(:inbox, account: account, greeting_enabled: false), account: account) }
|
let(:conversation) { create(:conversation, assignee: agent, inbox: create(:inbox, account: account, greeting_enabled: false), account: account) }
|
||||||
let(:described_subject) { described_class.receive reply_mail }
|
let(:described_subject) { described_class.receive reply_mail }
|
||||||
let(:serialized_attributes) do
|
let(:serialized_attributes) do
|
||||||
%w[bcc cc content_type date from html_content in_reply_to message_id multipart number_of_attachments references subject text_content to
|
%w[bcc cc content_type date from headers html_content in_reply_to message_id multipart number_of_attachments references subject text_content
|
||||||
auto_reply]
|
to auto_reply]
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with reply uuid present' do
|
context 'with reply uuid present' do
|
||||||
@@ -397,7 +397,7 @@ RSpec.describe ReplyMailbox do
|
|||||||
let(:support_in_reply_to_mail) { create_inbound_email_from_fixture('support_in_reply_to.eml') }
|
let(:support_in_reply_to_mail) { create_inbound_email_from_fixture('support_in_reply_to.eml') }
|
||||||
let(:described_subject) { described_class.receive support_mail }
|
let(:described_subject) { described_class.receive support_mail }
|
||||||
let(:serialized_attributes) do
|
let(:serialized_attributes) do
|
||||||
%w[bcc cc content_type date from html_content in_reply_to message_id multipart number_of_attachments references subject
|
%w[bcc cc content_type date from headers html_content in_reply_to message_id multipart number_of_attachments references subject
|
||||||
text_content to auto_reply]
|
text_content to auto_reply]
|
||||||
end
|
end
|
||||||
let(:conversation) { Conversation.where(inbox_id: channel_email.inbox).last }
|
let(:conversation) { Conversation.where(inbox_id: channel_email.inbox).last }
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ RSpec.describe MailPresenter do
|
|||||||
:content_type,
|
:content_type,
|
||||||
:date,
|
:date,
|
||||||
:from,
|
:from,
|
||||||
|
:headers,
|
||||||
:html_content,
|
:html_content,
|
||||||
:in_reply_to,
|
:in_reply_to,
|
||||||
:message_id,
|
:message_id,
|
||||||
@@ -60,6 +61,39 @@ RSpec.describe MailPresenter do
|
|||||||
expect(data[:auto_reply]).to eq(decorated_mail.auto_reply?)
|
expect(data[:auto_reply]).to eq(decorated_mail.auto_reply?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'includes forwarded headers in serialized_data' do
|
||||||
|
mail_with_headers = Mail.new do
|
||||||
|
from 'Sender <sender@example.com>'
|
||||||
|
to 'Inbox <inbox@example.com>'
|
||||||
|
subject :header
|
||||||
|
body 'Hi'
|
||||||
|
header['X-Original-From'] = 'Original <original@example.com>'
|
||||||
|
header['X-Original-Sender'] = 'original@example.com'
|
||||||
|
header['X-Forwarded-For'] = 'forwarder@example.com'
|
||||||
|
end
|
||||||
|
|
||||||
|
data = described_class.new(mail_with_headers).serialized_data
|
||||||
|
|
||||||
|
expect(data[:headers]).to eq(
|
||||||
|
'x-original-from' => 'Original <original@example.com>',
|
||||||
|
'x-original-sender' => 'original@example.com',
|
||||||
|
'x-forwarded-for' => 'forwarder@example.com'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns nil headers when forwarding headers are missing' do
|
||||||
|
mail_without_headers = Mail.new do
|
||||||
|
from 'Sender <sender@example.com>'
|
||||||
|
to 'Inbox <inbox@example.com>'
|
||||||
|
subject :header
|
||||||
|
body 'Hi'
|
||||||
|
end
|
||||||
|
|
||||||
|
data = described_class.new(mail_without_headers).serialized_data
|
||||||
|
|
||||||
|
expect(data[:headers]).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
it 'give email from in downcased format' do
|
it 'give email from in downcased format' do
|
||||||
expect(decorated_mail.from.first.eql?(mail.from.first.downcase)).to be true
|
expect(decorated_mail.from.first.eql?(mail.from.first.downcase)).to be true
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user