Say, i have a raw mail message in a file and i read it like
m = Mail.read '/path/to/file'
it has attachments, one of which is an inline pic.
pic = m.attachments[0]
=> #<Mail::Part:70130030888740, Multipart: false, Headers: <Content-Type: image/png; name="image001.png">, <Content-Transfer-Encoding: base64>, <Content-ID: <[email protected]>>>
others are just some files.
What i need is to have a way of knowing whether the attachment is inline or not. There is an inline? method, and for non-inline attachments it works like a charm
pdf = m.attachments[1]
=> #<Mail::Part:70130031002140, Multipart: false, Headers: <Content-Type: application/pdf; name="blah blah blah blah
pdf.inline?
=> false
But let's return to our pic here:
pic.inline?
=> nil
Which is just not right. I also tried
pdf['Content-Disposition']
=> #<Mail::Field 0x7f90d729b598 @charset="UTF-8" @name="Content-Disposition" @raw_value="Content-Disposition: attachment;\r\n\tfilename
and
pic['Content-Disposition']
=> nil
which is not too good either.
Is there any way to have a true/false value here?
In your case, the pic doesn't have a
Content-Dispositionheader defined. What to make of this differs a bit between standards (some default toattachment, some toinline). To quote RFC 2183:The mail gem seems to default to
attachmentsince it only checks if the Content-Disposition was explicitly set toinline.If you want to default to
inlineinstead, you can check if the result of theinline?method returns anything else butfalse.In the end, it's up to little defined semantics and you will have to be rather careful since these things tend to be interpreted differently by different people. When you accept mail from unknown sources, you could e.g. check if an attachment without an explicit
Content-Dispositionis referenced in the mail body somehow.