rails: ActionMailer fails to create correct multipart message with rfc822 attachment
Steps to reproduce
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', github: 'rails/rails', branch: 'main'
end
require 'minitest/autorun'
require 'action_mailer/railtie'
class TestMailer < ActionMailer::Base
def main
message_to_attach = <<~MAIL.gsub(/\n/, "\r\n")
Message-ID: <12345@foo.example>
Subject: Test
From: test1@foo.example
To: test2@foo.example
hello world
MAIL
attachments['test.eml'] = {
mime_type: params[:mime_type],
content: message_to_attach
}
mail(from: 'test3@foo.example', to: 'test4@foo.example', subject: 'Test outer') do |format|
format.text { "outer text - #{params[:mime_type]} attachment" }
end
end
end
class BugTest < Minitest::Test
def test_with_plain_mime_type
message = TestMailer.with(mime_type: 'text/plain').main
assert_equal message.mime_type, 'multipart/mixed'
end
def test_with_rfc822_mime_type
message = TestMailer.with(mime_type: 'message/rfc822').main
assert_equal message.mime_type, 'multipart/mixed'
end
end
The second test fails, the message object is rendered as follows:
Date: Fri, 23 Jul 2021 13:59:19 +0200
From: test3@foo.example
To: test4@foo.example
Message-ID: <60faaf1738a02_14fbd98-3fb@flo.adigi.ai.mail>
Subject: Test outer
Mime-Version: 1.0
Content-Type: text/plain;
boundary="--==_mimepart_60faaf1738008_14fbd98-484";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_60faaf1738008_14fbd98-484
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
outer text - message/rfc822 attachment
----==_mimepart_60faaf1738008_14fbd98-484
Content-Type: message/rfc822
Content-Transfer-Encoding:
Content-Disposition: attachment;
filename=test.eml
Content-ID: <60faaf1739102_14fbd98-2b@flo.adigi.ai.mail>
Message-ID: <12345@foo.example>
Subject: Test
From: test1@foo.example
To: test2@foo.example
hello world
----==_mimepart_60faaf1738008_14fbd98-484--
Expected behavior
The message.mime_type
is multipart/mixed
in both test cases. ActionMailer handles attachments in the way it is described in https://guides.rubyonrails.org/action_mailer_basics.html#adding-attachments describes.
Actual behavior
When attaching a message/rfc822
- an email message - the generated mail parts are not nested correctly, the outer multipart/mixed
is missing.
Perhaps this behaviour is caused by the special handling of such attachments in the mail
gem.
System configuration
Rails version: master
Ruby version: 3.0.2
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 15 (3 by maintainers)
I cannot re-open this issue so I created a new one: #45691
Can (should?) this be fixed in Rails? If not then I think we should close this in favour of https://github.com/mikel/mail/pull/1389
There is an open PR for the
mail
gem that appears to address this problem (see https://github.com/mikel/mail/pull/1389).I’ve inquired into why it hasn’t been merged, and have developed some tests in Rails to validate that it behaves as expected. Waiting for the response before deciding the way forward.