My app sends multipart/alternative html (with embedded images) and
text emails and lets the client choose.

Here's how I set things up. I take no credit for this - it's a combo
of ideas I found while searching for a solution. On the shoulders of
giants and all that.

I'm running 2.3.2.

Install http://code.google.com/p/premailer/

I ended up creating lib/premailer/ and putting the files in there
since they need to be edited a little.

In premailer.rb, change initialize to take an HTML string instead of a
URL.

class UserNotify < ActionMailer::Base
  public
  def portfolio_data(user,portfolios)

     # ... set up your subject, target, etc ...

      content_type "multipart/alternative"

      part "text/plain" do |p|
        p.body = render_message('portfolio_data.text.plain.erb',@body)
        p.transfer_encoding = "base64"   # VERY IMPORTANT
      end

      @parts << UserNotify.create_portfolio_html_data(@body)
   end

 # embed images in the email.
 # convert HTML with stylesheets to HTML with style attributes
 def portfolio_html_data(body)
    content_type "multipart/related"

    yahoo_cid = Time.now.sec.to_s + "_yahoo"
    google_cid = Time.now.sec.to_s + "_google"

    part "text/html" do |p|
      pm = Premailer.new(render_message
('portfolio_data.text.html.erb',body))
      m = pm.to_inline_css
      pm.warnings.each do |warning|
        clients = (warning[:clients] && (" (" << warning[:clients] <<
")")) || ""
        RAILS_DEFAULT_LOGGER.warn("Premailer warning: #{warning
[:level]}: #{warning[:message]}#{clients}")
      end
      # RAILS_DEFAULT_LOGGER.debug("\n\n\nm:\n#{m}\n\n")
      m.gsub!(/src="#{PortfolioHelper::YAHOO_IMG}[^"]*"/,"src=\"cid:#
{yahoo_cid}\"")
      m.gsub!(/src="#{PortfolioHelper::GOOGLE_IMG}[^"]*"/,"src=\"cid:#
{google_cid}\"")
      p.body = m
      p.transfer_encoding = "base64"   # VERY IMPORTANT
    end

    f = File.open("#{RAILS_ROOT}/public#
{PortfolioHelper::YAHOO_IMG}","rb")
    inline_attachment :content_type => "image/gif",
    :body => f.read,
    :filename => "yahoo.gif",
    :cid => "<#{yahoo_cid}>"
    f.close

    f = File.open("#{RAILS_ROOT}/public#
{PortfolioHelper::GOOGLE_IMG}","rb")
    inline_attachment :content_type => "image/gif",
    :body => f.read,
    :filename => "google.gif",
    :cid => "<#{google_cid}>"
    f.close
  end

end


portfolio_data.text.html.erb looks roughly like this:

<html>
  <head>
    <style>
  <%=File.read("#{RAILS_ROOT}/public/stylesheets/porb.css")-%>
   </style>
  </head>
...
</html>


This works well for me. Images embed, css gets inlined, client gets to
choose whether it wants text or html. I ought to do the next step and
use the html_to_text stuff in premailer but I already had the text
template written.

If there's a better way to pull all this stuff together I'm all ears.

-Chris

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to