Use Mail::Address to create email addresses with display names

This class escapes special characters in addresses correctly.
This commit is contained in:
Patrick Gansterer 2017-10-12 01:19:19 +02:00
parent f509f85327
commit bce9d9a8c2

View file

@ -98,16 +98,16 @@ class Mailer < ActionMailer::Base
if args[:from].is_a? User if args[:from].is_a? User
args[:reply_to] ||= args[:from] args[:reply_to] ||= args[:from]
args[:from] = "#{I18n.t('mailer.from_via_foodsoft', name: show_user(args[:from]))} <#{FoodsoftConfig[:email_sender]}>" args[:from] = format_address(FoodsoftConfig[:email_sender], I18n.t('mailer.from_via_foodsoft', name: show_user(args[:from])))
end end
[:bcc, :cc, :reply_to, :sender, :to].each do |k| [:bcc, :cc, :reply_to, :sender, :to].each do |k|
user = args[k] user = args[k]
args[k] = "#{show_user user} <#{user.email}>" if user.is_a? User args[k] = format_address(user.email, show_user(user)) if user.is_a? User
end end
if contact_email = FoodsoftConfig[:contact][:email] if contact_email = FoodsoftConfig[:contact][:email]
args[:reply_to] ||= "#{FoodsoftConfig[:name]} <#{contact_email}>" args[:reply_to] ||= format_address(contact_email, FoodsoftConfig[:name])
end end
reply_email_domain = FoodsoftConfig[:reply_email_domain] reply_email_domain = FoodsoftConfig[:reply_email_domain]
@ -140,4 +140,12 @@ class Mailer < ActionMailer::Base
attachments['order.csv'] = OrderCsv.new(order, options).to_csv attachments['order.csv'] = OrderCsv.new(order, options).to_csv
end end
private
def format_address(email, name)
address = Mail::Address.new email
address.display_name = name
address.format
end
end end