发送前如何修改actionmailer email html_part [英] How to modify actionmailer email html_part before sending

查看:164
本文介绍了发送前如何修改actionmailer email html_part的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我即将发送电子邮件的时候我有一切,但是我需要修改所有的链接以包含Google Analytics(分析)的属性。问题是如果我尝试读/写html_part.body的电子邮件,整个html字符串不知何故变得编码,不能正确地显示电子邮件(即< html> 成为& lt; html& gt; )。我已经在记录器中记录了html_part.body.raw_source,它显示为正确的未编码的HTML,只有当电子邮件实际发送时才会发生编码。



EBlast.rb(ActionMailer)

  def main(m,args = {})

#解析内容附件引用(他们不使用布局的帮助者)
#并以其他方式修改HTML
m.prep_for_email self

@email = m#需要帮助方法查看
mail_args = {
:to => @ user.email,
:subject => m.subject,
:template_path => 'e_blast',
:template_name => 'no_template'
}
mail_args [:template_name] ='main'如果m.needs_template?

m.prep_for_sending mail(mail_args)
end

Email.rb

  def prep_for_sending(mail_object)

如果mail_object.html_part

#如果我只是做一个'return mail_object',电子邮件发送很好...
#但是url跟踪器不被应用。

#将内容替换为整个生成的html
self.content = mail_object.html_part.body.decoded

#将Google分析跟踪器信息添加到内容中的链接
apply_url_tracker:source => Eblast Generator,:medium => :email

#替换html_part内容
mail_object.html_part.body = content

#此时,mail_object.html_part.body包含整个
#HTML字符串,未编码。但是当我发送电子邮件时,它会获得
#个实体的转换,并且电子邮件已被锁定。

end

#发送电子邮件
mail_object

end


解决方案

看起来我正在回答自己的问题 - 本周我在滚动。



显然,设置身体直接创建一些名为body_raw的奇怪属性,而不是替换html_part的raw_contents。所以基本上我最终把一个重复的部分嵌入邮件对象(我不知道为什么这样做)。创建一个单独的Mail :: Part并将其分配给html_part只是添加了另一个部分,而不是替换html_part! WTF?!



新编辑:抓住我关于String.replace的最后一句话。看起来它正在工作,但是当我去另一台电脑进行测试时,发生了同样的重复问题。



另一个编辑:最后?
$ b

在执行apply_url_tracker方法之前,我重置了电子邮件的内容(为了更改渲染视图中的所有链接)。我不知道为什么使用Mail对象考虑该消息的螺丝应该已经被渲染了,但是将我的方法改为以下方法已经修复了电子邮件部分的重复以及后续的重新编码。我不再更改内容属性,我只更改了html_part:

  def prep_for_sending(message)

如果message.html_part
#替换html raw_source
message.html_part.body.raw_source.replace apply_url_tracker(message.html_part.body.decoded,:source =>Eblast Generator,:medium = >:email)
end

message

end

澄清:
即使对mail()的调用生成具有完全渲染的HTML / Text部分(即完全呈现的视图)的Mail对象,更改由这些视图使用的属性(在我的情况下,'内容'属性)拧紧最终发送。发送前不要修改您的型号,只能直接修改邮件部分。


I have everything at the point where I'm about to send out the email but I need to modify all links to include Google Analytics attributes. The problem is that if I try and read/write the html_part.body of the email, the entire html string somehow becomes encoded and doesn't display the email properly (i.e. <html> becomes &lt;html&gt;). I have logged the html_part.body.raw_source in the logger and it shows as proper unencoded HTML, it's only when the email is actually sent does the encoding occur.

EBlast.rb (ActionMailer)

def main(m, args={})

    # Parse content attachment references (they don't use helpers like the layout does)
    # and modify HTML in other ways
    m.prep_for_email self

    @email = m # Needed for helper methods in view
    mail_args = {
    :to => @user.email,
    :subject => m.subject,
    :template_path => 'e_blast',
    :template_name => 'no_template'
    }
    mail_args[:template_name] = 'main' if m.needs_template?

    m.prep_for_sending mail(mail_args)
end

Email.rb

def prep_for_sending(mail_object)

    if mail_object.html_part

    # If I simply do a 'return mail_object', the email sends just fine...
    # but the url trackers aren't applied.

    # Replace the content with the entire generated html
    self.content = mail_object.html_part.body.decoded

    # Add Google analytics tracker info to links in content
    apply_url_tracker :source => "Eblast Generator", :medium => :email

    # Replace the html_part contents
    mail_object.html_part.body = content

    # At this point, mail_object.html_part.body contains the entire
    # HTML string, unencoded. But when I send the email, it gets its
    # entities converted and the email is screwed.

    end

    # Send off email
    mail_object

end

解决方案

Looks like I'm answering my own question again - I'm on a roll this week.

Apparently setting the body directly creates some odd attribute called 'body_raw' instead of replacing the raw_contents of the html_part. So basically I ended up having a duplicate part embedded in the mail object (I don't know why it does this). Creating a separate Mail::Part and assigning it to html_part just added another part instead of replacing html_part! WTF?!

New Edit: Scratch my last remark about String.replace. It looked like it was working but when I went to another computer and tested it, the same problem of duplication occurred.

Another Edit: Finally?

Before I executed the apply_url_tracker method I had reset the content of the email (for the purposes of changing all the links in the rendered view). I don't have any idea why that screws with the Mail object considering the message should already have been rendered but changing my methodology to the following has fixed the duplication of email parts and their subsequent 'reencoding'. I no longer change the content attribute, I only change the html_part:

def prep_for_sending(message)

    if message.html_part
    # Replace the html raw_source
    message.html_part.body.raw_source.replace apply_url_tracker(message.html_part.body.decoded, :source => "Eblast Generator", :medium => :email)
    end

    message

end

Clarification: Even though the call to mail() produces a Mail object with fully rendered HTML/Text parts (i.e., fully rendered views), changing the attribute that is USED by those views (in my case, the 'content' attribute) screws up the final send. Don't modify your model before sending, JUST MODIFY THE MAIL PART DIRECTLY.

这篇关于发送前如何修改actionmailer email html_part的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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