From 3b513dc670c5f329fc9e675620348dd3316e67db Mon Sep 17 00:00:00 2001 From: Florian Liebe Date: Thu, 16 Jun 2022 16:15:00 +0200 Subject: [PATCH] Fixes #3890 - 'Sender Format' set to 'Agent Name' causes empty display names in triggers --- .../ticket/article/adds_metadata_email.rb | 4 ++++ spec/models/ticket/article_spec.rb | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/ticket/article/adds_metadata_email.rb b/app/models/ticket/article/adds_metadata_email.rb index 76e3a6883..1a3e6fcab 100644 --- a/app/models/ticket/article/adds_metadata_email.rb +++ b/app/models/ticket/article/adds_metadata_email.rb @@ -69,6 +69,10 @@ module Ticket::Article::AddsMetadataEmail elsif Setting.get('ticket_define_email_from') == 'AgentName' sender = User.find(created_by_id) realname = "#{sender.firstname} #{sender.lastname}" + + # avoid "-" as realname, see https://github.com/zammad/zammad/issues/3890 + realname = email_address.realname if sender.id == 1 + self.from = Channel::EmailBuild.recipient_line(realname, email_address.email) else self.from = Channel::EmailBuild.recipient_line(email_address.realname, email_address.email) diff --git a/spec/models/ticket/article_spec.rb b/spec/models/ticket/article_spec.rb index a1e8f3914..384bc121a 100644 --- a/spec/models/ticket/article_spec.rb +++ b/spec/models/ticket/article_spec.rb @@ -34,7 +34,7 @@ RSpec.describe Ticket::Article, type: :model do describe 'Setting of ticket_define_email_from' do subject(:article) do - create(:ticket_article, sender_name: 'Agent', type_name: 'email') + create(:ticket_article, created_by: created_by, sender_name: 'Agent', type_name: 'email') end context 'when AgentName' do @@ -42,9 +42,22 @@ RSpec.describe Ticket::Article, type: :model do Setting.set('ticket_define_email_from', 'AgentName') end - it 'sets the from based on the setting' do - expect(article.reload.from).to eq("\"#{article.created_by.firstname} #{article.created_by.lastname}\" <#{article.ticket.group.email_address.email}>") + context 'with real sender' do + let(:created_by) { create(:user) } + + it 'sets the from to the realname of the user' do + expect(article.reload.from).to eq("\"#{article.created_by.firstname} #{article.created_by.lastname}\" <#{article.ticket.group.email_address.email}>") + end end + + context 'with no real sender (e.g. trigger or scheduler)' do + let(:created_by) { User.find(1) } + + it 'sets the from to realname of the mail address)' do + expect(article.reload.from).to eq("\"#{article.ticket.group.email_address.realname}\" <#{article.ticket.group.email_address.email}>") + end + end + end end