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