diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 4232a647972..253e94d9a1c 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -83,26 +83,33 @@ class UserNotifications < ActionMailer::Base def user_replied(user, opts) opts[:allow_reply_by_email] = true + opts[:use_site_subject] = true notification_email(user, opts) end def user_quoted(user, opts) opts[:allow_reply_by_email] = true + opts[:use_site_subject] = true notification_email(user, opts) end def user_mentioned(user, opts) opts[:allow_reply_by_email] = true + opts[:use_site_subject] = true notification_email(user, opts) end def user_posted(user, opts) opts[:allow_reply_by_email] = true + opts[:use_site_subject] = true + opts[:add_re_to_subject] = true notification_email(user, opts) end def user_private_message(user, opts) opts[:allow_reply_by_email] = true + opts[:use_site_subject] = true + opts[:add_re_to_subject] = true # We use the 'user_posted' event when you are emailed a post in a PM. opts[:notification_type] = 'posted' @@ -116,6 +123,8 @@ class UserNotifications < ActionMailer::Base post: post, from_alias: post.user.username, allow_reply_by_email: true, + use_site_subject: true, + add_re_to_subject: true, notification_type: "posted", user: user ) @@ -163,12 +172,16 @@ class UserNotifications < ActionMailer::Base title = @notification.data_hash[:topic_title] allow_reply_by_email = opts[:allow_reply_by_email] unless user.suspended? + use_site_subject = opts[:use_site_subject] + add_re_to_subject = opts[:add_re_to_subject] send_notification_email( title: title, post: @post, from_alias: username, allow_reply_by_email: allow_reply_by_email, + use_site_subject: use_site_subject, + add_re_to_subject: add_re_to_subject, notification_type: notification_type, user: user ) @@ -179,6 +192,8 @@ class UserNotifications < ActionMailer::Base post = opts[:post] title = opts[:title] allow_reply_by_email = opts[:allow_reply_by_email] + use_site_subject = opts[:use_site_subject] + add_re_to_subject = opts[:add_re_to_subject] && post.post_number > 1 from_alias = opts[:from_alias] notification_type = opts[:notification_type] user = opts[:user] @@ -224,6 +239,8 @@ class UserNotifications < ActionMailer::Base username: from_alias, add_unsubscribe_link: true, allow_reply_by_email: allow_reply_by_email, + use_site_subject: use_site_subject, + add_re_to_subject: add_re_to_subject, private_reply: post.topic.private_message?, include_respond_instructions: !user.suspended?, template: template, diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e2aef5a6cd1..1ad099e1e2b 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -690,6 +690,7 @@ en: notification_email: "The from: email address used when sending all essential system emails. The domain specified here must have SPF, DKIM and reverse PTR records set correctly for email to arrive." email_custom_headers: "A pipe-delimited list of custom email headers" + email_subject: "Subject format for standard emails." use_https: "Should the full url for the site (Discourse.base_url) be http or https? DO NOT ENABLE THIS UNLESS HTTPS IS ALREADY SET UP AND WORKING!" summary_score_threshold: "The minimum score required for a post to be included in 'Summarize This Topic'" summary_posts_required: "Minimum posts in a topic before 'Summarize This Topic' is enabled" @@ -1576,6 +1577,9 @@ en: unsubscribe_link: "To unsubscribe from these emails, visit your [user preferences](%{user_preferences_url})." + subject_re: "Re: " + subject_pm: "[PM] " + user_notifications: previous_discussion: "Previous Replies" unsubscribe: diff --git a/config/site_settings.yml b/config/site_settings.yml index 8bcf1441904..1116324644b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -390,6 +390,7 @@ email: default: 7 enum: 'DigestEmailSiteSetting' email_custom_headers: 'Auto-Submitted: auto-generated' + email_subject: '%{optional_re}[%{site_name}] %{optional_pm}%{topic_title}' reply_by_email_enabled: false reply_by_email_address: '' pop3_polling_enabled: false diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index b488bff4578..ed8ea0b7567 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -40,8 +40,16 @@ module Email end def subject - subject = @opts[:subject] - subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template] + if @opts[:use_site_subject] + subject = String.new(SiteSetting.email_subject) + subject.gsub!("%{site_name}", @template_args[:site_name]) + subject.gsub!("%{optional_re}", @opts[:add_re_to_subject] ? I18n.t('subject_re', template_args) : '') + subject.gsub!("%{optional_pm}", @opts[:private_reply] ? I18n.t('subject_pm', template_args) : '') + subject.gsub!("%{topic_title}", @template_args[:topic_title]) if @template_args[:topic_title] # must be last for safety + else + subject = @opts[:subject] + subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template] + end subject end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index d7bb4b45888..0f612a87403 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -104,6 +104,53 @@ describe UserNotifications do end end + describe '.user_posted' do + let(:post) { Fabricate(:post) } + let(:response) { Fabricate(:post, topic: post.topic)} + let(:user) { Fabricate(:user) } + let(:notification) { Fabricate(:notification, user: user) } + + it 'generates a correct email' do + mail = UserNotifications.user_posted(response.user, post: response, notification: notification) + + # subject should include "Re:" + expect(mail.subject).to match("Re:") + + # 2 respond to links cause we have 1 context post + mail.html_part.to_s.scan(/To respond/).count.should == 2 + + # 1 unsubscribe link + mail.html_part.to_s.scan(/To unsubscribe/).count.should == 1 + + # side effect, topic user is updated with post number + tu = TopicUser.get(post.topic_id, response.user) + tu.last_emailed_post_number.should == response.post_number + end + end + + describe '.user_private_message' do + let(:topic) { Fabricate(:private_message_topic) } + let(:response) { Fabricate(:post, topic: topic)} + let(:user) { Fabricate(:user) } + let(:notification) { Fabricate(:notification, user: user) } + + it 'generates a correct email' do + mail = UserNotifications.user_private_message(response.user, post: response, notification: notification) + + # subject should include "[PM]" + expect(mail.subject).to match("[PM]") + + # 1 respond to link + mail.html_part.to_s.scan(/To respond/).count.should == 1 + + # 1 unsubscribe link + mail.html_part.to_s.scan(/To unsubscribe/).count.should == 1 + + # side effect, topic user is updated with post number + tu = TopicUser.get(topic.id, response.user) + tu.last_emailed_post_number.should == response.post_number + end + end def expects_build_with(condition) UserNotifications.any_instance.expects(:build_email).with(user.email, condition) @@ -126,7 +173,6 @@ describe UserNotifications do end end - shared_examples "notification email building" do let(:post) { Fabricate(:post, user: user) } let(:mail_type) { "user_#{notification_type}"}