首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails3使用premailer发送多部分电子邮件,并可以选择是否使用模板

Rails3使用premailer发送多部分电子邮件,并可以选择是否使用模板
EN

Stack Overflow用户
提问于 2013-02-23 03:58:33
回答 1查看 625关注 0票数 0

我可以选择创建一封电子邮件,其中的内容可以是HTML文档的一部分,也可以是整个HTML文档。如果它只是一个部分,Mailer应该使用一个模板来渲染。如果它包含<html>元素(表示它是一个完整的超文本标记语言文档),则在发送时不应该使用模板文件。我几乎所有的设置和工作,除了在发送的电子邮件中的‘电子邮件部分’在某些情况下是无序的和重复的事实。它搞砸了电子邮件的渲染。我认为这与我对mail的调用以及Premailer在deliver方法上所做的事情有关,但我不确定接下来该怎么做。

EBlast.rb

代码语言:javascript
复制
def main(m, args={})
    # 'm' is an Email.rb object
    ready_email 'E-Blast', args # Don't worry about this
    assign_private_id_header # or this

    # Read the Template
    @variable_width = (@user.email =~ /@hotmail|@live|@windows/).nil?
    @page_title = m.subject
    m.prep_for_email # squeeze unnecessary whitespace and create 'quick-nav' links based on h1 tags

    # Read in attachments
    m.inline_attachments.each do |name, loc|
    attachments.inline[name] = File.read(loc)
    end
    m.external_attachments.each do |name, loc|
    attachments[name] = File.read(loc)
    end


    mail_object = mail(:to => @user.email, :subject => m.subject) do |format|
    format.text { render :text => 'placeholder' } # This should be replaced by Premailer html > text conversion upon deliver!

    if m.needs_template?
        # Use the main.html.erb template
        @email = m # Needed for helper methods in template view
        format.html 
    else
        # Use the content of the email as the entire HTML source
        format.html { render :text => m.content }
    end
    end
    # Apply Google Analytics tracking parameters to links pointing to this domain
    m.prep_for_sending mail_object
end

Email.rb

代码语言:javascript
复制
def prep_for_sending(mail_object)

    unless mail_object.html_part.nil?
    # Replace the content with the generated html
    self.content = mail_object.html_part.body.raw_source
    # Add Google analytics tracker info to links
    apply_url_tracker :source => "Eblast Generator", :medium => :email
    # Replace the html raw_source
    mail_object.html_part.body = content

    end

    # Return mail object so mailer can call deliver
    mail_object

end

更新:经过一些变通方法(为“非模板内容”创建一个空白模板),我发现了另一个problem...one,我认为它真正切中了正在发生的事情的核心。当有文本、html和附件(或者仅仅是内联的或者是内联和外部的)时,不会生成multipart/mixed和multipart/related部分。

更新2:部分成功!显然,我是一块吸引奇怪错误的磁铁。在读取附件时,我必须使用File.open(loc, 'rb') { |f| f.read }而不是File.read(loc),因为我在开发机器上使用windows。S.I.G.H.有没有办法在File.read中指定'rb‘标志?我到处都找不到文档。我现在要尝试让Premailer工作,因为我不得不禁用它来测试它。

最终更新:看起来Premailer搞砸了多部分布局。有没有一种方法可以使用Premailer而不连接到自己的deliver?我只需要它内联的CSS和生成的纯文本部分。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-23 09:25:34

已确认这是premailer-rails3 gem here的错误。已使用gem "premailer-rails3", :git => "git://github.com/tdgs/premailer-rails3.git"并正确发送。6个小时过去了:)

附录:看起来Premailer可能和我在this thread中遇到的问题一样。它似乎也复制了html部分,这可能最终会扰乱整个电子邮件解析。因此,实际上最终可能是ActionMailer -或者在将内容分配给邮件部件的正文时由哪个类负责。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15032269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档