FIX: Use original from address when forwarding to group inbox (#14114)

When emails were forwarded to a group inbox by the email address
of the group, for example when an email ends up in spam and must
be manually forwarded to the group+site@discoursemail.com address,
the OP of the topic ended up being the group's email address instead
of the sender who originally sent the email to the group inbox.

This commit detects that an email has been forwarded using existing
tools, and if the from address matches one of the group incoming
email addresses, then we look at the forwarded email's from address
and use that instead for the incoming email from address as well as
the staged/regular user used for the Topic.user.

This will make it much cleaner to forward emails into a group inbox,
and will prevent issues with PostAlerter where the OP is double-notified
for these emails.
This commit is contained in:
Martin Brennan
2021-08-24 08:57:28 +10:00
committed by GitHub
parent bde6f7e9b0
commit 2ac9fd9dff
3 changed files with 73 additions and 0 deletions

View File

@@ -584,6 +584,20 @@ module Email
return unless mail[:from]
# For forwarded emails, where the from address matches a group incoming
# email, we want to use the from address of the original email sender,
# which we can extract from embedded_email_raw.
if has_been_forwarded?
if mail[:from].to_s =~ group_incoming_emails_regex && embedded_email[:from].errors.blank?
embedded_email[:from].each do |address_field|
from_address = address_field.address
from_display_name = address_field.display_name&.to_s
next if !from_address&.include?("@")
return [from_address&.downcase, from_display_name&.strip]
end
end
end
# For now we are only using the Reply-To header if the email has
# been forwarded via Google Groups, which is why we are checking the
# X-Original-From header too. In future we may want to use the Reply-To
@@ -885,6 +899,10 @@ module Email
@embedded_email_raw
end
def embedded_email
@embedded_email ||= embedded_email_raw.present? ? Mail.new(embedded_email_raw) : nil
end
def process_forwarded_email(destination, user)
user ||= stage_from_user
case SiteSetting.forwarded_emails_behaviour