I have been having troubles sending e-mails using ActionMailer on windows. The simple code-sample:

my_filename="my_image.jpeg"
attachment "image/jpeg" do |a|
  a.body = File.read(my_filename)
  a.filename = File.basename(my_filename)
end

did not work. I did find an attachment inside my e-mail, but it was corrupt. It took me a while to figure out it could be something related to my development platform, which unfortunately still is windows (not for long).

On windows reading binary files is less obvious, apparently, so you need to write:

my_filename="my_image.jpeg"
attachment "image/jpeg" do |a|
  a.body = File.open(my_filename,'rb') { |f| f.read}
  a.filename = File.basename(my_filename)
end

… and this will work! :)

I you have to combine normal e-mail content and one or more attachments, this then becomes something like:

  # we send a mail to respond to a mention
  def mention_response(given_subject, addressee, pdf_attachment = nil)
    from APP_CONFIG["mail"]["from"]
    recipients addressee.to_a
    subject given_subject

    content_type   "multipart/mixed"

    part "text/html" do |p|
      p.body = render_message("mention_response", :thr_name => thr_name)
    end

    attachment :content_type => "application/doc" do |a|
      a.filename File.basename(pdf_attachment)
      a.body File.open(pdf_attachment, "rb") { |f| f.read }
    end unless pdf_attachment.nil?

  end

Nice :)