问题
I'm at the point where the entire email and it's parts generate in the correct order and nesting...except for one little thing. The :part_container parameter inserts the inline attachments ABOVE the text/html part instead of BELOW it. If it's above, Thunderbird won't display anything and it'll show the email as having 2 external attachments. If I manually edit the email and move the inline parts below the text/html part, it shows perfectly.
part :content_type => 'multipart/alternative' do |copy|
copy.part :content_type => 'text/plain' do |plain|
plain.body = render(
:file => "main.text.plain",
:body => {
:user => @user,
:text => docParse.to_plain_text
}
)
end
copy.part :content_type => 'multipart/related' do |rel|
rel.part :content_type => 'text/html' do |html|
html.body = render(
:file => "main.text.html",
:body => {
:part_container => rel,
:user => @user,
:content => content,
:email_links => m.email_links,
:nav_image => m.nav_image
}
)
end
end
end
m.attachments.each do |file|
attachment :content_type => "application/octet-stream",
:filename => File.basename(file),
:body => File.read(file)
end
To be clear, the inline_attachments gem works perfectly without any of this code - but that's only when I'm sending an email without any external attachments (only inline images). When I want to send an HTML email with inline images AND external attachments, this is what I have to resort to.
EDIT: To be clear, my question is "how do I make sure that the inline attachment parts are generated AFTER the text/html part using :part_container?
回答1:
Parts contain a parts array...
rel.parts.unshift rel.parts.pop
Essentially an email is comprised of 'parts'...these parts belong to an array of parts and each part can have their own array of parts (nested parts). Seeing as how the :part_container argument from the 'inline_attachments' gem created parts in the wrong order (text/html part LAST instead of FIRST), I reordered the relative parts array by popping the last part item - which happened to be the text/html part (rel.parts.pop) and inserting it as the 1st item in the relative parts array (rel.parts.unshift)
i.e.,
inline-image-part
inline-image-part
inline-image-part
text/html-part
became
text/html-part
inline-image-part
inline-image-part
inline-image-part
And everything displayed properly.
This is the latest script with reference to the script in my problem:
part :content_type => 'multipart/alternative' do |copy|
copy.part :content_type => 'text/plain' do |plain|
plain.body = render(
:file => "main.text.plain",
:body => {
:user => @user,
:text => docParse.to_plain_text
}
)
end
copy.part :content_type => 'multipart/related' do |rel|
rel.part :content_type => 'text/html' do |html|
html.body = render(
:file => "main.text.html",
:body => {
:part_container => rel,
:user => @user,
:content => content,
:email_links => m.email_links,
:nav_image => m.nav_image
}
)
end
# Place the text/html part before all other inline images
rel.parts.unshift rel.parts.pop
end
end
m.attachments.each do |file|
attachment :content_type => "application/octet-stream",
:filename => File.basename(file),
:body => File.read(file)
end
来源:https://stackoverflow.com/questions/6719016/rails-2-3-inline-attachment-gem-incorrect-placement-of-inline-attachments