FIX: Improve participant display in group SMTP emails (#13539)

FIX: Improve participant display in group SMTP emails (#13539)

This PR makes several changes to the group SMTP email contents to make it look more like a support inbox message.

  • Remove the context posts, they only add clutter to the email and replies
  • Display email addresses of staged users instead of odd generated usernames
  • Add a “please reply above this line” message to sent emails
diff --git a/app/mailers/group_smtp_mailer.rb b/app/mailers/group_smtp_mailer.rb
index 574facc..045f669 100644
--- a/app/mailers/group_smtp_mailer.rb
+++ b/app/mailers/group_smtp_mailer.rb
@@ -10,16 +10,6 @@ class GroupSmtpMailer < ActionMailer::Base
 
     op_incoming_email = post.topic.first_post.incoming_email
 
-    context_posts = Post
-      .where(topic_id: post.topic_id)
-      .where("post_number < ?", post.post_number)
-      .where(user_deleted: false)
-      .where(hidden: false)
-      .where(post_type: Post.types[:regular])
-      .order(created_at: :desc)
-      .limit(SiteSetting.email_posts_context)
-      .to_a
-
     delivery_options = {
       address: from_group.smtp_server,
       port: from_group.smtp_port,
@@ -35,14 +25,14 @@ class GroupSmtpMailer < ActionMailer::Base
       user_name = post.user.name unless post.user.name.blank?
     end
 
-    group_name = from_group.full_name.presence || from_group.name
+    group_name = from_group.name_full_preferred
     build_email(
       to_address,
       message: post.raw,
       url: post.url(without_slug: SiteSetting.private_email?),
       post_id: post.id,
       topic_id: post.topic_id,
-      context: context(context_posts),
+      context: "",
       username: post.user.username,
       group_name: group_name,
       allow_reply_by_email: true,
@@ -59,45 +49,25 @@ class GroupSmtpMailer < ActionMailer::Base
       delivery_method_options: delivery_options,
       from: from_group.email_username,
       from_alias: I18n.t('email_from', user_name: group_name, site_name: Email.site_title),
-      html_override: html_override(post, context_posts: context_posts),
+      html_override: html_override(post),
       cc: cc_addresses
     )
   end
 
   private
 
-  def context(context_posts)
-    return "" if SiteSetting.private_email?
-
-    context = +""
-
-    if context_posts.size > 0
-      context << +"-- \n*#{I18n.t('user_notifications.previous_discussion')}*\n"
-      context_posts.each { |post| context << email_post_markdown(post, true) }
-    end
-
-    context
-  end
-
-  def email_post_markdown(post, add_posted_by = false)
-    result = +"#{post.raw}\n\n"
-    if add_posted_by
-      result << "#{I18n.t('user_notifications.posted_by', username: post.username, post_date: post.created_at.strftime("%m/%d/%Y"))}\n\n"
-    end
-    result
-  end
-
-  def html_override(post, context_posts: nil)
+  def html_override(post)
     UserNotificationRenderer.render(
       template: 'email/notification',
       format: :html,
       locals: {
-        context_posts: context_posts,
+        context_posts: nil,
         reached_limit: nil,
         post: post,
         in_reply_to_post: post.reply_to_post,
         classes: Rtl.new(nil).css_class,
-        first_footer_classes: ''
+        first_footer_classes: '',
+        reply_above_line: true
       }
     )
   end
@@ -106,14 +76,22 @@ class GroupSmtpMailer < ActionMailer::Base
     list = []
 
     post.topic.allowed_groups.each do |g|
-      list.push("[#{g.name} (#{g.users.count})](#{Discourse.base_url}/groups/#{g.name})")
+      list.push("[#{g.name_full_preferred}](#{Discourse.base_url}/groups/#{g.name})")
     end
 
     post.topic.allowed_users.each do |u|
       if SiteSetting.prioritize_username_in_ux?
-        list.push("[#{u.username}](#{Discourse.base_url}/u/#{u.username_lower})")
+        if u.staged?
+          list.push("[#{u.email}](#{Discourse.base_url}/u/#{u.username_lower})")
+        else
+          list.push("[#{u.username}](#{Discourse.base_url}/u/#{u.username_lower})")
+        end
       else
-        list.push("[#{u.name.blank? ? u.username : u.name}](#{Discourse.base_url}/u/#{u.username_lower})")
+        if u.staged?
+          list.push("[#{u.email}](#{Discourse.base_url}/u/#{u.username_lower})")
+        else
+          list.push("[#{u.name.blank? ? u.username : u.name}](#{Discourse.base_url}/u/#{u.username_lower})")
+        end
       end
     end
 
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 993d46f..d5a20e0 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -632,7 +632,8 @@ class UserNotifications < ActionMailer::Base
                     post: post,
                     in_reply_to_post: in_reply_to_post,
                     classes: Rtl.new(user).css_class,
-                    first_footer_classes: first_footer_classes
+                    first_footer_classes: first_footer_classes,
+                    reply_above_line: false
           }
         )
       end
diff --git a/app/models/email_log.rb b/app/models/email_log.rb
index c089746..e8dd29a 100644
--- a/app/models/email_log.rb
+++ b/app/models/email_log.rb
@@ -96,6 +96,21 @@ class EmailLog < ActiveRecord::Base
   def cc_addresses_split
     @cc_addresses_split ||= self.cc_addresses&.split(";") || []
   end
+
+  def as_mail_message
+    return if self.raw.blank?
+    @mail_message ||= Mail.new(self.raw)
+  end
+
+  def raw_headers
+    return if self.raw.blank?
+    as_mail_message.header.raw_source
+  end
+
+  def raw_body
+    return if self.raw.blank?
+    as_mail_message.body
+  end
 end
 
 # == Schema Information
diff --git a/app/models/group.rb b/app/models/group.rb
index c70918e..8a6d718 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -894,11 +894,15 @@ class Group < ActiveRecord::Base
     SystemMessage.create_from_system_user(
       user,
       owner ? :user_added_to_group_as_owner : :user_added_to_group_as_member,
-      group_name: self.full_name.presence || self.name,
+      group_name: name_full_preferred,
       group_path: "/g/#{self.name}"
     )
   end
 
+  def name_full_preferred
+    self.full_name.presence || self.name
+  end
+
   def message_count
     return 0 unless self.has_messages
     TopicAllowedGroup.where(group_id: self.id).joins(:topic).count
diff --git a/app/views/email/notification.html.erb b/app/views/email/notification.html.erb
index cc30418..b3d3a2c 100644
--- a/app/views/email/notification.html.erb
+++ b/app/views/email/notification.html.erb
@@ -1,4 +1,9 @@
 <div id='main' class=<%= classes %>>
+  <%- if reply_above_line.present? %>
+  <div class="reply-above-line">
+    <%= t('user_notifications.reply_above_line') %>
+  </div>
+  <% end %>
 
   <div class='header-instructions'>%{header_instructions}</div>
 
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 404d3f9..319b772 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3471,6 +3471,7 @@ en:
     only_reply_by_email_pm: "Reply to this email to respond to %{participants}."
     visit_link_to_respond: "[Visit Topic](%{base_url}%{url}) to respond."
     visit_link_to_respond_pm: "[Visit Message](%{base_url}%{url}) to respond to %{participants}."
+    reply_above_line: "## Please type your reply above this line. ##"
 
     posted_by: "Posted by %{username} on %{post_date}"
 
diff --git a/lib/email/styles.rb b/lib/email/styles.rb
index 250ecff..5938dff 100644
--- a/lib/email/styles.rb
+++ b/lib/email/styles.rb
@@ -232,6 +232,7 @@ module Email
       style('.lightbox-wrapper .meta', 'display: none')
       style('div.undecorated-link-footer a', "font-weight: normal;")
       style('.mso-accent-link', "mso-border-alt: 6px solid #{SiteSetting.email_accent_bg_color}; background-color: #{SiteSetting.email_accent_bg_color};")
+      style('.reply-above-line', "font-size: 10px;font-family:'lucida grande',tahoma,verdana,arial,sans-serif;color: #b5b5b5;padding: 5px 0px 20px;border-top: 1px dotted #ddd;")
 
       onebox_styles
       plugin_styles
diff --git a/spec/jobs/regular/group_smtp_email_spec.rb b/spec/jobs/regular/group_smtp_email_spec.rb
index ffba338..1bcae95 100644
--- a/spec/jobs/regular/group_smtp_email_spec.rb
+++ b/spec/jobs/regular/group_smtp_email_spec.rb
@@ -3,10 +3,10 @@
 require 'rails_helper'
 
 RSpec.describe Jobs::GroupSmtpEmail do

[... diff too long, it was truncated ...]

GitHub sha: d3e27cabf6beea7f77f1a14be43d1976853dfe8e

This commit appears in #13539 which was approved by lis2. It was merged by martin.