【问题标题】:Rails 2.3 "inline_attachment" gem incorrect placement of inline attachmentsRails 2.3“inline_attachment”gem 内联附件的位置不正确
【发布时间】:2011-10-06 20:03:09
【问题描述】:

我正处于整个电子邮件及其部分以正确的顺序和嵌套生成的地步……除了一件小事。 :part_container 参数在 text/html 部分上方而不是下方插入内联附件。如果它在上面,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|
    attachment :content_type => "application/octet-stream",
               :filename     => File.basename(file),
               :body         => File.read(file)
end

需要明确的是,inline_attachments gem 可以在没有任何代码的情况下完美运行——但这只是在我发送没有任何外部附件的电子邮件(仅限内联图像)时。当我想发送带有内嵌图像和外部附件的 HTML 电子邮件时,这就是我不得不求助的方法。

编辑:明确地说,我的问题是“我如何确保在使用 :part_container 的 text/html 部分之后生成内联附件部分?

【问题讨论】:

    标签: ruby-on-rails ruby email attachment


    【解决方案1】:

    部件包含一个部件数组...

    rel.parts.unshift rel.parts.pop
    

    基本上,一封电子邮件由“部分”组成......这些部分属于一个部分数组,每个部分都可以有自己的部分数组(嵌套部分)。看到来自“inline_attachments”gem 的 :part_container 参数如何以错误的顺序创建部件(文本/html 部件最后而不是第一),我通过弹出最后一个部件项重新排序了相关部件数组 - 这恰好是文本/ html 部分 (rel.parts.pop) 并将其作为第一项插入到相关部分数组 (rel.parts.unshift) 中

    即,
    内联图像部分
    内联图像部分
    内联图像部分
    文本/html部分

    成为

    文本/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
            # 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
    

    【讨论】:

    • 这是答案、评论还是..?
    • 它被列为答案。我打算将其标记为已回答,但我不得不等待 2 天。
    • 我只是在问,因为对我来说尚不清楚这如何回答您的问题。当然,对您来说这很清楚,但即使您自己回答问题,也要以其他人可以从您的发现中受益的方式来做,即更冗长:)
    猜你喜欢
    • 2012-01-11
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多