mikel / mail

A Really Ruby Mail Library
MIT License
3.63k stars 935 forks source link

Problems creating a multipart message #1295

Open ghost opened 5 years ago

ghost commented 5 years ago

First, the standard stuff:

Ruby version: 2.5.3 Mail gem version: 2.7.1 OS: Ubuntu Linux 18.04

I have this method that is part of a somewhat large and complex program:

def forward(address)
    fail ValidationError, 
        "'#{address}' is an invalid email address." \
        unless EmailAddress.valid?(address)

    subj = @message.header['Subject'].value
    rcvr = @recipient

    bodymsg = "> Begin forwarded message:\n" +
                  "> \n" +
                  "> From: #{@message.header['From'].value}\n" +
                  "> Subject: #{subj}\n" +
                  "> Date: #{@message.header['Date'].value}\n" +
                  "> To: #{@message.header['To'].value}\n" +
                  "> \n\n"          

    msg = Mail.new

    msg.to = address
    msg.from = rcvr
    msg.subject = "Fwd: " + subj

    bodypart = Mail::Part.new

    text_body = Mail::Part.new
    text_body.body = bodymsg
    text_body.content_type = 'text/plain; charset=UTF-8'

    bodypart.text_part = text_body

    @message.body.parts.each do |p| 

            html_body = Mail::Part.new

        html_body.body = p.body.to_s
        html_body.content_type = p.content_type.to_s

        bodypart.html_part = html_body

    end

    msg.add_part(bodypart)

    msg.delivery_method(*@delivery)

    puts msg.to_s
        #msg.deliver!
end

As you can see it attempts to forward an existing message to address (@message -- obtained from Mail.read) to another address...preserving all the parts of the original message. @recipient is obtained from a command-line parameter. @delivery is obtained from a configuration file (it's splatted because the config file can contain a complete parameter list for Mail.delivery_method).

The method creates a message just fine but when I preview the created message using an MUA, the text_part doesn't show up. When I view the preview as just text, the text_part is there and appears to be formatted correctly. I've previewed this messages with Apple Mail (Version 12.1 (3445.101.1)) and Gmail and the text_part doesn't show up in either of them.

I've tried several iterations of this (based on feedback from other reports here) but I always get the same result. This seems really straightforward to me... What am I missing?

I've attached a preview.

Thanks in advance...

-- Steve Witten

foo.txt