diff --git a/app/models/channel/email_build.rb b/app/models/channel/email_build.rb index b01182cbf..67e6eae51 100644 --- a/app/models/channel/email_build.rb +++ b/app/models/channel/email_build.rb @@ -105,13 +105,13 @@ module Channel::EmailBuild attr[:attachments]&.each do |attachment| if attachment.class == Hash attachment['content-id'] = nil - mail.attachments[ attachment[:filename] ] = attachment + mail.attachments[attachment[:filename]] = attachment else next if attachment.preferences['Content-ID'].present? filename = attachment.filename encoded_filename = Mail::Encodings.decode_encode filename, :encode disposition = attachment.preferences['Content-Disposition'] || 'attachment' - content_type = attachment.preferences['Content-Type'] || 'application/octet-stream' + content_type = attachment.preferences['Content-Type'] || attachment.preferences['Mime-Type'] || 'application/octet-stream' mail.attachments[attachment.filename] = { content_disposition: "#{disposition}; filename=\"#{encoded_filename}\"", content_type: "#{content_type}; filename=\"#{encoded_filename}\"", diff --git a/test/unit/email_build_test.rb b/test/unit/email_build_test.rb index cf6585e4d..71d850ab4 100644 --- a/test/unit/email_build_test.rb +++ b/test/unit/email_build_test.rb @@ -49,8 +49,8 @@ class EmailBuildTest < ActiveSupport::TestCase { 'Mime-Type' => 'image/png', :content => 'xxx', - :filename => 'somename.png', - }, + :filename => 'somename.png' + } ], ) @@ -103,8 +103,8 @@ class EmailBuildTest < ActiveSupport::TestCase { 'Mime-Type' => 'image/png', :content => 'xxx', - :filename => 'somename.png', - }, + :filename => 'somename.png' + } ], ) @@ -114,6 +114,7 @@ class EmailBuildTest < ActiveSupport::TestCase >' assert_equal(should, mail.text_part.body.to_s) assert_nil(mail.html_part) + assert_equal('image/png; filename=somename.png', mail.attachments[0].content_type) parser = Channel::EmailParser.new data = parser.parse(mail.to_s) @@ -137,6 +138,88 @@ class EmailBuildTest < ActiveSupport::TestCase end end + test 'plain email + attachment check 2' do + ticket1 = Ticket.create!( + title: 'some article helper test1', + group: Group.lookup(name: 'Users'), + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1, 'ticket created') + + # create inbound article #1 + article1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message article helper test1
asdasd
', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + store1 = Store.add( + object: 'Ticket::Article', + o_id: article1.id, + data: 'content_file1_normally_should_be_an_ics_calendar_file', + filename: 'schedule.ics', + preferences: { + 'Mime-Type' => 'text/calendar' + }, + created_by_id: 1, + ) + + text = '> Welcome! +> +> Thank you for installing Zammad. äöüß +>' + mail = Channel::EmailBuild.build( + from: 'sender@example.com', + to: 'recipient@example.com', + body: text, + attachments: [ + store1 + ], + ) + + should = '> Welcome! +> +> Thank you for installing Zammad. äöüß +>' + assert_equal(should, mail.text_part.body.to_s) + assert_nil(mail.html_part) + assert_equal('text/calendar; filename=schedule.ics', mail.attachments[0].content_type) + + parser = Channel::EmailParser.new + data = parser.parse(mail.to_s) + + # check body + assert_equal(should, data[:body]) + + # check count of attachments, 2 + assert_equal(1, data[:attachments].length) + + # check attachments + data[:attachments]&.each do |attachment| + if attachment[:filename] == 'schedule.ics' + assert(attachment[:preferences]['Content-ID']) + assert_nil(attachment[:preferences]['content-alternative']) + assert_equal('text/calendar', attachment[:preferences]['Mime-Type']) + assert_equal('UTF-8', attachment[:preferences]['Charset']) + else + assert(false, "invalid attachment, should not be there, #{attachment.inspect}") + end + end + end + test 'plain email + without attachment check' do text = '> Welcome! >