FEATURE: Use group SMTP job and mailer instead of UserNotifications change (#13489)

This PR backtracks a fair bit on this one https://github.com/discourse/discourse/pull/13220/files.

Instead of sending the group SMTP email for each user via `UserNotifications`, we are changing to send only one email with the existing `Jobs::GroupSmtpEmail` job and `GroupSmtpMailer`. We are changing this job and mailer along with `PostAlerter` to make the first topic allowed user the `to_address` for the email and any other `topic_allowed_users` to be the CC address on the email. This is to cut down on emails sent via SMTP, which is subject to daily limits from providers such as Gmail. We log these details in the `EmailLog` table now.

In addition to this, we have changed `PostAlerter` to no longer rely on incoming email email addresses for sending the `GroupSmtpEmail` job. This was unreliable as a user's email could have changed in the meantime. Also it was a little overcomplicated to use the incoming email records -- it is far simpler to reason about to just use topic allowed users.

This also adds a fix to include cc_addresses in the EmailLog.addressed_to_user scope.
This commit is contained in:
Martin Brennan
2021-06-28 08:55:13 +10:00
committed by GitHub
parent f9886ecfa2
commit 87684f7c5e
14 changed files with 484 additions and 284 deletions

View File

@@ -140,7 +140,8 @@ module Email
subject: subject,
body: body,
charset: 'UTF-8',
from: from_value
from: from_value,
cc: @opts[:cc]
}
args[:delivery_method_options] = @opts[:delivery_method_options] if @opts[:delivery_method_options]
@@ -161,11 +162,24 @@ module Email
# please, don't send us automatic responses...
result['X-Auto-Response-Suppress'] = 'All'
if allow_reply_by_email? && !@opts[:use_from_address_for_reply_to]
result[ALLOW_REPLY_BY_EMAIL_HEADER] = true
result['Reply-To'] = reply_by_email_address
else
if !allow_reply_by_email?
# This will end up being the notification_email, which is a
# noreply address.
result['Reply-To'] = from_value
else
# The only reason we use from address for reply to is for group
# SMTP emails, where the person will be replying to the group's
# email_username.
if !@opts[:use_from_address_for_reply_to]
result[ALLOW_REPLY_BY_EMAIL_HEADER] = true
result['Reply-To'] = reply_by_email_address
else
# No point in adding a reply-to header if it is going to be identical
# to the from address/alias. If the from option is not present, then
# the default reply-to address is used.
result['Reply-To'] = from_value if from_value != alias_email(@opts[:from])
end
end
result.merge(MessageBuilder.custom_headers(SiteSetting.email_custom_headers))

View File

@@ -429,6 +429,8 @@ module Email
end
def set_reply_key(post_id, user_id)
# ALLOW_REPLY_BY_EMAIL_HEADER is only added if we are _not_ sending
# via group SMTP and if reply by email site settings are configured
return if !user_id || !post_id || !header_value(Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER).present?
# use safe variant here cause we tend to see concurrency issue