Rails 2.3“inline_attachment” gem不正确地放置内联附件 [英] Rails 2.3 "inline_attachment" gem incorrect placement of inline attachments
问题描述
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 |
附件:content_type => application / octet-stream,
:filename => File.basename(file),
:body => File.read(file)
end
要清楚的是,inline_attachments gem完美无缺,没有任何这个代码 - 但只有当我发送电子邮件没有任何外部附件(只有内联图像)。当我想发送带有内联图像和外部附件的HTML电子邮件时,这是我必须诉诸的。
编辑:要清楚,我的问题是如何请确保在text / html部分之后使用:part_container生成内联附件零件?
零件包含零件数组...
rel.parts.unshift rel.parts.pop
/ pre>
本质上电子邮件由零件组成...这些零件属于一系列零件,每个零件都可以有自己的零件阵列(嵌套零件)看到:'inline_attachments'gem中的part_container参数如何以错误的顺序创建部分(text / html part LAST而不是FIRST),我通过弹出最后一个部分项来重新排序相关零件数组 - 这恰好是text / html part(rel.parts.pop)并将其作为相对零件数组中的第一个项目(rel.parts.unshift)插入。
ie,
inline-image-part
inline-image-part
inline-image-part
text / html-part
成为
text / html-part
inline-image-part
inline-image-part
inline-image-part
所有内容都正常显示。
这是我问题中引用脚本的最新脚本:
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
#将text / html部分放在所有其他内联图像之前
rel.parts.unshift rel.parts.pop
end
end
m.attachments.each do | file |
附件:content_type => application / octet-stream,
:filename => File.basename(file),
:body => File.read(file)
end
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?
解决方案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-partbecame
text/html-part
inline-image-part
inline-image-part
inline-image-partAnd 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
这篇关于Rails 2.3“inline_attachment” gem不正确地放置内联附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!