Sending HTML emails with attachments in Rails

Here’s something you might have missed in Rails.

From ActionMailer’s Documentation:

Implicit template rendering is not performed if any attachments or parts have been added to the email. This means that you‘ll have to manually add each part to the email and set the content type of the email to multipart/alternative

If you want to have pretty html templates and have an attached file, you can’t just set the content_type to “text/html” and call attachment.  You need to do it separately, like so:


class SomeMailer < ActionMailer::Base

  # assumes that there's a file named pretty_template.html.erb in app/views/some_mailer
  def html_email_with_attachment(email_address, msg)
    recipients [email_address]
    from "hello@world.com"
    subject "This is an html email with an attachment"

    # have to set content_type explicitly
    content_type "multipart/alternative"

    # then set the html part.  Note that there is no call to body(), but 
    # the second argument is a hash containing what you'd put into body() normally.
    part(:content_type => "text/html",
         :body => render_message("pretty_template", :message => msg)

    attachment(:content_type => "text/csv",
               :filename => "attachment.csv",
               body => CSVGenerator.generate_csv_string)

    # as a note, all attachment is doing is adding transfer_encoding and disposition
    # part(:content_type => "text/csv",
    #      :transfer_encoding => "base64",
    #      :disposition => "attachment",
    #      :filename => "attachment.csv",
    #      :body => CSVGenerator.generate_csv_string)
  end

end

http://gist.github.com/170564

There, now you can have html and your attachments too.  Reference: http://am.rubyonrails.org/classes/ActionMailer/PartContainer.html#M000006

 

ALL ART BURNS » fear of sentient robots

The thing is, we’ve had unstoppable, zombie-like, intelligent actors capable of taking out a single human for at least a century. They have legal status, can own property, can file lawsuits, own weapons, have security forces, and they self-replicate based on available resources.

They’re called “corporations”.

I remember watching Bicentennial Man, and the part where the rights of the robot was under question in the movie struck me.

Reading this, I wondered if it was possible to build a robot, and to give it rights under the present legal system, simply create a corporation assigned to the robot?