rails icon indicating copy to clipboard operation
rails copied to clipboard

ActionMailer fails to create correct multipart message with rfc822 attachment

Open der-flo opened this issue 1 year ago • 1 comments

I cannot re-open #42849 so I create a new issue…

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: <[email protected]>
      Subject: Test
      From: [email protected]
      To: [email protected]
      
      hello world
    MAIL
    attachments['test.eml'] = {
      mime_type: params[:mime_type],
      content: message_to_attach
    }

    mail(from: '[email protected]', to: '[email protected]', 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: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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: <[email protected]>

Message-ID: <[email protected]>
Subject: Test
From: [email protected]
To: [email protected]

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: main

Ruby version: 3.0.2

der-flo avatar Jul 29 '22 11:07 der-flo

For reference: it seems like https://github.com/mikel/mail/pull/1389 will resolve this. I guess the other issue went stale as it's not clear if there's any way to fix the issue in the Rails codebase.

@jeremy sorry to tag you out of the blue but you're a mail committer too... is there any chance you'd be able to look at that PR or tag in someone who could?

ghiculescu avatar Aug 02 '22 19:08 ghiculescu

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.

rails-bot[bot] avatar Oct 31 '22 20:10 rails-bot[bot]

Thanks again @rails-bot! I can confirm this problem still exists in 7-0-stable and main. I also checked mail version 2.8.0.rc1 - the situation is the same.

der-flo avatar Nov 04 '22 09:11 der-flo

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.

rails-bot[bot] avatar Feb 02 '23 10:02 rails-bot[bot]

Is there anything to do on Rails end here? Or just wait for that PR to be merged and released?

zzak avatar Feb 10 '23 03:02 zzak

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.

rails-bot[bot] avatar May 11 '23 04:05 rails-bot[bot]