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

@@ -92,6 +92,11 @@ module Email
user_id: user_id
)
if cc_addresses.any?
email_log.cc_addresses = cc_addresses.join(";")
email_log.cc_user_ids = User.with_email(cc_addresses).pluck(:id)
end
host = Email::Sender.host_for(Discourse.base_url)
post_id = header_value('X-Discourse-Post-Id')
@@ -201,6 +206,7 @@ module Email
end
email_log.post_id = post_id if post_id.present?
email_log.topic_id = topic_id if topic_id.present?
# Remove headers we don't need anymore
@message.header['X-Discourse-Topic-Id'] = nil if topic_id.present?
@@ -243,7 +249,16 @@ module Email
# Log when a message is being sent from a group SMTP address, so we
# can debug deliverability issues.
email_log.smtp_group_id = smtp_group_id
if smtp_group_id
email_log.smtp_group_id = smtp_group_id
# Store contents of all outgoing emails using group SMTP
# for greater visibility and debugging. If the size of this
# gets out of hand, we should look into a group-level setting
# to enable this; size should be kept in check by regular purging
# of EmailLog though.
email_log.raw = Email::Cleaner.new(@message).execute
end
DiscourseEvent.trigger(:before_email_send, @message, @email_type)
@@ -270,6 +285,12 @@ module Email
end
end
def cc_addresses
@cc_addresses ||= begin
@message.try(:cc) || []
end
end
def self.host_for(base_url)
host = "localhost"
if base_url.present?