Trouble sending email image attachment with rails

Hi,

I’m trying to use ActionMailer to send an image as an attachment to an
email. Here’s my code:

class ContactMailer < ActionMailer::Base
def message(sent_at = Time.now)
from ‘[email protected]
recipients ‘[email protected]
subject ‘New message’
sent_on sent_at
body {}
attachment :content_type => “image/jpeg”, :body =>
File.read(“public/images/rails.png”)
end
end

This works, in so far as the email is delivered okay (obviously with
different addresses), but the image attached to the email seems to be
corrupted.

It doesn’t display in my mail client and when I download it and try to
open it with a suitable program I get the message that the file is
either too large or corrupted.

I tried replacing the image with a pdf as follows:
attachment :content_type => “application/pdf”, :body =>
File.read(“public/images/test.pdf”)
and this works fine.

Can anybody give me a clue as to what I’m doing wrong?

Thanks in advance.

On Jan 21, 2011, at 2:22 PM, Jim B. wrote:

sent_on sent_at
body {}
attachment :content_type => “image/jpeg”, :body =>
File.read(“public/images/rails.png”)

Content type is JPEG. File you’re reading is PNG.

Just an update:

I’ve checked through the documentation for ActionMailer.
Reusing the example they list in their documentation I now have:

def message(sent_at = Time.now)
from ‘[email protected]
recipients ‘[email protected]
subject ‘New message’
sent_on sent_at
attachment “image/jpg” do |a|
a.body = File.read(“public/images/test.jpg”)
a.filename = “hello.jpg”
end
end

test.jpg exists in the folder public/images and is definitely a jpg.

In my previous post I had configured my app to use the mail transfer
agent on my production server (presumably sendmail).

To eliminate this as being the problematic link in the chain I also
configured my rails app to use my gmail account to send the email.

Again the mail generated in my rails app gets delivered, but with the
image attachment I get exactly the same result (jpg doesn’t display
inline and when I download it and try to open it I get the message "File
corrupt).

Could anyone give me a clue as to what I am doing wrong?

I’m using rails 2.3.8

Thanks very much in advance.

Content type is JPEG. File you’re reading is PNG.

Yeah, I noticed this too, but according to the book I’m reading this
should work.

I tried sending a jpg as an attachment (instead of a png), but with the
same result. I also tried:
attachment :content_type => “image/png”, :body =>
File.read(“public/images/rails.png”)

but again, in the mail the image displays as broken and when I download
it I cannot open it.

Could I be doing anything else wrong?

Thanks very much.