Rails 2.3“inline_attachment” gem不正确地放置内联附件 [英] Rails 2.3 "inline_attachment" gem incorrect placement of inline attachments

查看:201
本文介绍了Rails 2.3“inline_attachment” gem不正确地放置内联附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个电子邮件和它的零件生成正确的顺序和嵌套...除了一个小事情。 :part_container参数将内联附件插入到文本/ html部分而不是BELOW中。如果是,Thunderbird将不会显示任何内容,它会显示电子邮件具有2个外部附件。如果我手动编辑电子邮件并移动text / html部分下方的内联部分,它将完美显示。

  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-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

这篇关于Rails 2.3“inline_attachment” gem不正确地放置内联附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆