Cleanup unicode emojis from email headers

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
This commit is contained in:
Benjamin Neff 2023-06-09 03:39:34 +02:00
parent 87f17fe907
commit dc9a18e24d
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 36 additions and 2 deletions

View file

@ -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

View file

@ -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⃣23⃣ 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⃣23⃣ 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⃣23⃣ 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⃣23⃣ 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⃣23⃣ 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