FEATURE: Add more columns to outbound EmailLog (#13449)

This adds the following columns to EmailLog:

* cc_addresses
* cc_user_ids
* topic_id
* raw

This is to bring the EmailLog table closer in parity to
IncomingEmail so it can be better utilized for Group SMTP
and IMAP mailing.

The raw column contains the full content of the outbound email,
but _only_ if the new hidden site setting
enable_raw_outbound_email_logging is enabled. Most sites do not
need it, and it's mostly required for IMAP and SMTP sending.

In the next pull request, there will be a migration to backfill
topic_id on the EmailLog table, at which point we can remove the
topic fallback method on EmailLog.
This commit is contained in:
Martin Brennan
2021-06-22 08:32:01 +10:00
committed by GitHub
parent c3e4389b81
commit 5222247746
5 changed files with 124 additions and 11 deletions

View File

@ -336,6 +336,7 @@ describe Email::Sender do
expect(email_log.email_type).to eq('valid_type')
expect(email_log.to_address).to eq('eviltrout@test.domain')
expect(email_log.user_id).to be_blank
expect(email_log.raw).to eq(nil)
end
context 'when the email is sent using group SMTP credentials' do
@ -355,7 +356,7 @@ describe Email::Sender do
SiteSetting.enable_smtp = true
end
it 'adds the group id to the email log' do
it 'adds the group id and raw content to the email log' do
TopicAllowedGroup.create(topic: post.topic, group: group)
email_sender.send
@ -365,6 +366,7 @@ describe Email::Sender do
expect(email_log.to_address).to eq(post.user.email)
expect(email_log.user_id).to be_blank
expect(email_log.smtp_group_id).to eq(group.id)
expect(email_log.raw).to include("Hello world")
end
it "does not add any of the mailing list headers" do
@ -393,6 +395,7 @@ describe Email::Sender do
it 'should create the right log' do
email_sender.send
expect(email_log.post_id).to eq(post.id)
expect(email_log.topic_id).to eq(topic.id)
expect(email_log.topic.id).to eq(topic.id)
end
end
@ -687,4 +690,30 @@ describe Email::Sender do
end
end
context "with cc addresses" do
let(:message) do
message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body', cc: 'someguy@test.com;otherguy@xyz.com'
message.stubs(:deliver_now)
message
end
fab!(:user) { Fabricate(:user) }
let(:email_sender) { Email::Sender.new(message, :valid_type, user) }
it "logs the cc addresses in the email log (but not users if they do not match the emails)" do
email_sender.send
email_log = EmailLog.last
expect(email_log.cc_addresses).to eq("someguy@test.com;otherguy@xyz.com")
expect(email_log.cc_users).to eq([])
end
it "logs the cc users if they match the emails" do
user1 = Fabricate(:user, email: "someguy@test.com")
user2 = Fabricate(:user, email: "otherguy@xyz.com")
email_sender.send
email_log = EmailLog.last
expect(email_log.cc_addresses).to eq("someguy@test.com;otherguy@xyz.com")
expect(email_log.cc_users).to match_array([user1, user2])
end
end
end