在我即将发送电子邮件的地方,我拥有一切,但我需要修改所有链接以包含 Google Analytics 属性。问题是,如果我尝试读/写电子邮件的 html_part.body,整个 html 字符串会以某种方式被编码并且不能正确显示电子邮件(即<html>
变成<html>
)。我已经在记录器中记录了 html_part.body.raw_source 并且它显示为正确的未编码 HTML,只有在实际发送电子邮件时才会发生编码。
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
电子邮件.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