From dc9a18e24d3ea6c919b9aee0d5ef9cdc837f23d7 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 9 Jun 2023 03:39:34 +0200 Subject: [PATCH] Cleanup unicode emojis from email headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some email providers (for example gmail) block emails if they have emojis in the from header, as they could be confused with UI elements. So the easy solution is to just filter all emojis from the name. The normal `\p{Emoji}` selector also matches normal numbers, because of the emoji-version of numbers (1️⃣), but the `\p{Emoji_Presentation}` then doesn't match colored emojis anymore (❄️), so we need a mix of both to find all emojis --- app/mailers/notification_mailers/base.rb | 12 +++++++++-- spec/mailers/notifier_spec.rb | 26 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/mailers/notification_mailers/base.rb b/app/mailers/notification_mailers/base.rb index 7b2a6135c..c19c6bd71 100644 --- a/app/mailers/notification_mailers/base.rb +++ b/app/mailers/notification_mailers/base.rb @@ -36,15 +36,23 @@ module NotificationMailers def default_headers from_name = AppConfig.settings.pod_name - from_name += " (#{@sender.profile.full_name.empty? ? @sender.username : @sender.name})" if @sender.present? + from_name += " (#{person_name(@sender)})" if @sender.present? { from: name_and_address(from_name, AppConfig.mail.sender_address), - to: name_and_address(@recipient.name, @recipient.email), + to: name_and_address(person_name(@recipient), @recipient.email), template_name: self.class.name.demodulize.underscore } end + def person_name(person) + if person.profile.full_name.empty? + person.username + else + person.name.gsub(/\p{Emoji}\uFE0F\u20E3?|\p{Emoji_Presentation}/, "").strip + end + end + def with_recipient_locale(&block) I18n.with_locale(@recipient.language, &block) end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index a4e79e631..ead4a115a 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -262,6 +262,11 @@ describe Notifier, type: :mailer do expect(@mail["From"].to_s).to eq("\"#{pod_name} (#{@cnv.author.name})\" <#{AppConfig.mail.sender_address}>") end + it "FROM: removes emojis from sender's name" do + bob.person.profile.update!(first_name: "1️⃣2️3️⃣ Numbers 123", last_name: "👍✅👍🏻Emojis😀😇❄️") + expect(@mail["From"].to_s).to eq("\"#{pod_name} (Numbers 123 Emojis)\" <#{AppConfig.mail.sender_address}>") + end + it "should use a generic subject" do expect(@mail.subject).to eq(I18n.translate("notifier.private_message.subject")) end @@ -302,6 +307,12 @@ describe Notifier, type: :mailer do expect(comment_mail["From"].to_s).to eq("\"#{pod_name} (#{eve.name})\" <#{AppConfig.mail.sender_address}>") end + it "FROM: removes emojis from sender's name" do + eve.person.profile.update!(first_name: "1️⃣2️3️⃣ Numbers 123", last_name: "👍✅👍🏻Emojis😀😇❄️") + expect(comment_mail["From"].to_s) + .to eq("\"#{pod_name} (Numbers 123 Emojis)\" <#{AppConfig.mail.sender_address}>") + end + it "SUBJECT: has a snippet of the post contents, without markdown and without newlines" do expect(comment_mail.subject).to eq("Re: Headline") end @@ -390,6 +401,11 @@ describe Notifier, type: :mailer do expect(mail["From"].to_s).to eq("\"#{pod_name} (#{bob.name})\" <#{AppConfig.mail.sender_address}>") end + it "FROM: removes emojis from sender's name" do + bob.person.profile.update!(first_name: "1️⃣2️3️⃣ Numbers 123", last_name: "👍✅👍🏻Emojis😀😇❄️") + expect(mail["From"].to_s).to eq("\"#{pod_name} (Numbers 123 Emojis)\" <#{AppConfig.mail.sender_address}>") + end + it "SUBJECT: does not show the limited post" do expect(mail.subject).not_to include("Limited headline") end @@ -415,6 +431,11 @@ describe Notifier, type: :mailer do expect(mail["From"].to_s).to eq("\"#{pod_name} (#{bob.name})\" <#{AppConfig.mail.sender_address}>") end + it "FROM: removes emojis from sender's name" do + bob.person.profile.update!(first_name: "1️⃣2️3️⃣ Numbers 123", last_name: "👍✅👍🏻Emojis😀😇❄️") + expect(mail["From"].to_s).to eq("\"#{pod_name} (Numbers 123 Emojis)\" <#{AppConfig.mail.sender_address}>") + end + it "SUBJECT: does not show the limited post" do expect(mail.subject).not_to include("Limited headline") end @@ -446,6 +467,11 @@ describe Notifier, type: :mailer do expect(mail["From"].to_s).to eq("\"#{pod_name} (#{bob.name})\" <#{AppConfig.mail.sender_address}>") end + it "FROM: removes emojis from sender's name" do + bob.person.profile.update!(first_name: "1️⃣2️3️⃣ Numbers 123", last_name: "👍✅👍🏻Emojis😀😇❄️") + expect(mail["From"].to_s).to eq("\"#{pod_name} (Numbers 123 Emojis)\" <#{AppConfig.mail.sender_address}>") + end + it "SUBJECT: does not show the limited post" do expect(mail.subject).not_to include("Limited headline") end