Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When composing, first lines of body could get interpreted as headers if there is an attachment #811

Closed
DavidEGrayson opened this issue Oct 13, 2014 · 2 comments
Labels
Milestone

Comments

@DavidEGrayson
Copy link

Hello. I would like to a report a bug in version 2.6.1 of the mail gem.

To reproduce this bug, I ran the following code under ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]:

require 'mail'
msg = Mail::Message.new
msg.charset = 'UTF-8'
msg.to = '[email protected]'
msg.attachments['b.txt'] = ''
msg.body = "a:\n"
puts msg.to_s

The output I get from this is:

Date: Mon, 13 Oct 2014 14:58:35 -0700
To: [email protected]
Message-ID: <[email protected]>
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="--==_mimepart_543c4b0b50fd7_1ab33f8bb7ca7308887ee";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_543c4b0b50fd7_1ab33f8bb7ca7308887ee
Content-Type: text/plain;
 filename=b.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=b.txt
Content-ID: <[email protected]>


----==_mimepart_543c4b0b50fd7_1ab33f8bb7ca7308887ee
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
a: 


----==_mimepart_543c4b0b50fd7_1ab33f8bb7ca7308887ee--

Note how there is no blank line before the "a:", so it is actually a header. I would have expected there to be a blank line before the "a:" line, so that it would be part of the body of that MIME part.

I believe this is a bug because I would expect that a string passed into the #body= method would be treated as part of the body, not as a combined thing that contains headers and body.

This bad behavior goes away if I comment out the line that adds the attachments, or if I delete the colon or newline from the body.

@Venousek
Copy link

Me and @xjeline5 have looked into this issue as part of our school project and we can confirm that this bug is still unfixed (under both Ruby 1.9.3 and 2.1.2).

msg.attachments['b.txt'] = ''
msg.body = "a:\n"

The problem is combination of creating multipart email and the way of setting the attachment and the body. Once the email is multipart, the simple assignment to body is treated as creating new part of email. This calls Mail::Part.new(value) in message's body_lazy function and then the value is checked for headers, which are in format "foo: bar". When creating new Part, :body parameter can be supplied instead and this leads to correct setting of the body (the same way convert_to_multipart does it). But because of the project structure design (there is a Message class, but also a Part class, that extends Message as well), there is not an easy way of recognizing that body_lazy function is called on an actual body of the whole email and it cannot be fixed there.

msg.attachments['b.txt'] = ''

This way of setting the attachment does not call the function convert_to_multipart, unlike when calling add_file, but the message is considered multipart internally. convert_to_multipart takes the old body and saves it as a new part correctly. If the text body is set before setting the attachment your way, it is overwritten.

The workaround to avoid this problem is to first set the body, then use add_file to insert attachment or call convert_to_multipart and then set the attachment the way mentioned in your example:

msg.body = "a:\n"
msg.add_file "b.txt"

or:

msg.body = "a:\n"
msg.convert_to_multipart
msg.attachments['b.txt'] = ''

Another way of achieving this that does not depend on the order of setting the body and attachment:

msg.add_file "b.txt"    # or msg.attachments['b.txt'] = ''
msg.text_part = Mail::Part.new(:content_type => 'text/plain', :body => 'a:\n')

If anyone has a hint how to solve the root cause, let us know and we can arrange the fix.

jeremy added a commit to jeremy/mail that referenced this issue May 22, 2017
…rt instead of adding a raw MIME part

Fixes super-confusing API usage where you add an attachment, then set
the body text, and end up parsing the body text as if it were a raw MIME
part.

Closes mikel#1114. Fixes mikel#811. Thanks to @Venousek for diagnosis!
@jeremy
Copy link
Collaborator

jeremy commented May 22, 2017

Fixed by #1114 😊

@jeremy jeremy closed this as completed May 22, 2017
@jeremy jeremy added this to the 2.7.0 milestone May 22, 2017
jeremy added a commit that referenced this issue May 22, 2017
…rt instead of adding a raw MIME part

Fixes super-confusing API usage where you add an attachment, then set
the body text, and end up parsing the body text as if it were a raw MIME
part.

Closes #1114. Fixes #811. Thanks to @Venousek for diagnosis!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants